You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by vi...@apache.org on 2011/12/15 20:01:23 UTC

svn commit: r1214912 - in /openejb/trunk/openejb/examples: ./ simple-cdi-interceptor/ simple-cdi-interceptor/src/ simple-cdi-interceptor/src/main/ simple-cdi-interceptor/src/main/java/ simple-cdi-interceptor/src/main/java/org/ simple-cdi-interceptor/sr...

Author: vishwanathk
Date: Thu Dec 15 19:01:22 2011
New Revision: 1214912

URL: http://svn.apache.org/viewvc?rev=1214912&view=rev
Log:
OpenEJB-1705  Split cdi-interceptors to simpler examples

Added:
    openejb/trunk/openejb/examples/simple-cdi-interceptor/   (with props)
    openejb/trunk/openejb/examples/simple-cdi-interceptor/README.md   (with props)
    openejb/trunk/openejb/examples/simple-cdi-interceptor/pom.xml   (with props)
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/beans/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/beans/BookShow.java   (with props)
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/interceptorbinding/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/interceptorbinding/Log.java   (with props)
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/interceptors/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/interceptors/LoggingInterceptor.java   (with props)
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/resources/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/resources/META-INF/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/resources/META-INF/beans.xml   (with props)
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/test/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/test/java/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/test/java/org/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/test/java/org/superbiz/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/test/java/org/superbiz/cdi/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/test/java/org/superbiz/cdi/bookshow/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/test/java/org/superbiz/cdi/bookshow/interceptors/
    openejb/trunk/openejb/examples/simple-cdi-interceptor/src/test/java/org/superbiz/cdi/bookshow/interceptors/BookShowTest.java   (with props)
Modified:
    openejb/trunk/openejb/examples/pom.xml

Modified: openejb/trunk/openejb/examples/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/pom.xml?rev=1214912&r1=1214911&r2=1214912&view=diff
==============================================================================
--- openejb/trunk/openejb/examples/pom.xml (original)
+++ openejb/trunk/openejb/examples/pom.xml Thu Dec 15 19:01:22 2011
@@ -94,6 +94,7 @@
     <module>cdi-events</module>
     <module>dynamic-proxy-to-access-mbean</module>
     <module>reload-persistence-unit-properties</module>
+    <module>simple-cdi-interceptor</module>
   </modules>
   <profiles>
     <profile>

Propchange: openejb/trunk/openejb/examples/simple-cdi-interceptor/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Dec 15 19:01:22 2011
@@ -0,0 +1,2 @@
+.classpath
+.project

Added: openejb/trunk/openejb/examples/simple-cdi-interceptor/README.md
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/simple-cdi-interceptor/README.md?rev=1214912&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/simple-cdi-interceptor/README.md (added)
+++ openejb/trunk/openejb/examples/simple-cdi-interceptor/README.md Thu Dec 15 19:01:22 2011
@@ -0,0 +1,120 @@
+#Simple CDI Interceptor
+
+Let's write a simple application that would allow us to book tickets for a movie show. As with all applications, logging is one cross-cutting concern that we have. 
+
+(Relevant snippets are inlined but you can check out the complete code, from the links provided)
+
+How do we mark which methods are to be intercepted ? Wouldn't it be handy to annotate a method like 
+
+<pre><code>
+@Log
+public void aMethod(){...} 
+</code></pre>
+
+Let's create an  annotation that would "mark" a method for interception. 
+
+<pre><code>
+@InterceptorBinding
+@Target({ TYPE, METHOD })
+@Retention(RUNTIME)
+public @interface Log {
+}
+</pre></code>
+
+Sure, you haven't missed the @InterceptorBinding annotation above ! Now that our custom annotation is created, lets attach it (or to use a better term for it, "bind it" )
+to an interceptor. 
+
+So here's our logging interceptor. An @AroundInvoke method and we are almost done.
+<pre><code>
+@Interceptor
+@Log  //binding the interceptor here. now any method annotated with @Log would be intercepted by logMethodEntry
+public class LoggingInterceptor {
+    @AroundInvoke
+    public Object logMethodEntry(InvocationContext ctx) throws Exception {
+        System.out.println("Entering method: " + ctx.getMethod().getName());
+        //or logger.info statement 
+        return ctx.proceed();
+    }
+}
+</pre></code>
+
+Now the @Log annotation we created is bound to this interceptor.
+
+That done, let's annotate at class-level or method-level and have fun intercepting ! 
+
+<pre><code>
+@Log
+@Stateful
+public class BookForAShowOneInterceptorApplied implements Serializable {
+    private static final long serialVersionUID = 6350400892234496909L;
+    public List<String> getMoviesList() {
+        List<String> moviesAvailable = new ArrayList<String>();
+        moviesAvailable.add("12 Angry Men");
+        moviesAvailable.add("Kings speech");
+        return moviesAvailable;
+    }
+    public Integer getDiscountedPrice(int ticketPrice) {
+        return ticketPrice - 50;
+    }
+    // assume more methods are present
+}
+</code></pre>
+
+The `@Log` annotation applied at class level denotes that all the methods should be intercepted with `LoggingInterceptor`.
+
+Before we say "all done" there's one last thing we are left with ! To enable the interceptors ! 
+
+Lets quickly put up a [beans.xml file]
+<pre><code>
+&lt;beans&gt;<br/>  &lt;interceptors&gt;<br/>    &lt;class&gt;org.superbiz.cdi.bookshow.interceptors.LoggingInterceptor<br/>    &lt;/class&gt;<br/>  &lt;/interceptors&gt;<br/>&lt;/beans&gt;
+</code></pre>
+
+ in META-INF
+
+
+Those lines in beans.xml not only "enable" the interceptors, but also define the "order of execution" of the interceptors.
+But we'll see that in another example on multiple-cdi-interceptors.
+
+Fire up the test, and we should see a 'Entering method: getMoviesList' printed in the console.
+
+
+
+#Code
+
+#### Java files
+
+${javas}
+
+##### Resource files
+
+${resources} 
+
+#Download
+
+[Download as zip](${zip}) 
+
+<pre><code>
+#Tests
+Apache OpenEJB 4.0.0-beta-2-SNAPSHOT    build: 20111103-01:00
+http://openejb.apache.org/
+INFO - openejb.home = /media/fthree/Workspace/open4/openejb/examples/cdi-simple-interceptors
+INFO - openejb.base = /media/fthree/Workspace/open4/openejb/examples/cdi-simple-interceptors
+INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
+INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
+INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
+INFO - Found EjbModule in classpath: /media/fthree/Workspace/open4/openejb/examples/cdi-simple-interceptors/target/classes
+INFO - Beginning load: /media/fthree/Workspace/open4/openejb/examples/cdi-simple-interceptors/target/classes
+INFO - Configuring enterprise application: /media/fthree/Workspace/open4/openejb/examples/cdi-simple-interceptors
+INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container)
+INFO - Auto-creating a container for bean cdi-simple-interceptors.Comp: Container(type=MANAGED, id=Default Managed Container)
+INFO - Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container)
+INFO - Auto-creating a container for bean BookShow: Container(type=STATEFUL, id=Default Stateful Container)
+INFO - Enterprise application "/media/fthree/Workspace/open4/openejb/examples/cdi-simple-interceptors" loaded.
+INFO - Assembling app: /media/fthree/Workspace/open4/openejb/examples/cdi-simple-interceptors
+INFO - Jndi(name="java:global/cdi-simple-interceptors/BookShow!org.superbiz.cdi.bookshow.beans.BookShow")
+INFO - Jndi(name="java:global/cdi-simple-interceptors/BookShow")
+INFO - Created Ejb(deployment-id=BookShow, ejb-name=BookShow, container=Default Stateful Container)
+INFO - Started Ejb(deployment-id=BookShow, ejb-name=BookShow, container=Default Stateful Container)
+INFO - Deployed Application(path=/media/fthree/Workspace/open4/openejb/examples/cdi-simple-interceptors)
+Entering method: getMoviesList
+</code></pre>

Propchange: openejb/trunk/openejb/examples/simple-cdi-interceptor/README.md
------------------------------------------------------------------------------
    svn:executable = *

Added: openejb/trunk/openejb/examples/simple-cdi-interceptor/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/simple-cdi-interceptor/pom.xml?rev=1214912&view=auto
==============================================================================
Binary file - no diff available.

Propchange: openejb/trunk/openejb/examples/simple-cdi-interceptor/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/beans/BookShow.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/beans/BookShow.java?rev=1214912&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/beans/BookShow.java (added)
+++ openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/beans/BookShow.java Thu Dec 15 19:01:22 2011
@@ -0,0 +1,42 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.superbiz.cdi.bookshow.beans;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.ejb.Stateful;
+
+import org.superbiz.cdi.bookshow.interceptorbinding.Log;
+
+@Log
+@Stateful
+public class BookShow implements Serializable {
+	private static final long serialVersionUID = 6350400892234496909L;
+
+	public List<String> getMoviesList() {
+		List<String> moviesAvailable = new ArrayList<String>();
+		moviesAvailable.add("12 Angry Men");
+		moviesAvailable.add("Kings speech");
+		return moviesAvailable;
+	}
+	public Integer getDiscountedPrice(int ticketPrice) {
+		return ticketPrice - 50;
+	}
+	// assume more methods are present
+}

Propchange: openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/beans/BookShow.java
------------------------------------------------------------------------------
    svn:executable = *

Added: openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/interceptorbinding/Log.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/interceptorbinding/Log.java?rev=1214912&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/interceptorbinding/Log.java (added)
+++ openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/interceptorbinding/Log.java Thu Dec 15 19:01:22 2011
@@ -0,0 +1,30 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.superbiz.cdi.bookshow.interceptorbinding;
+
+import static java.lang.annotation.ElementType.*;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.*;
+import java.lang.annotation.Target;
+
+import javax.interceptor.InterceptorBinding;
+
+@InterceptorBinding
+@Target({ TYPE, METHOD })
+@Retention(RUNTIME)
+public @interface Log {
+}

Propchange: openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/interceptorbinding/Log.java
------------------------------------------------------------------------------
    svn:executable = *

Added: openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/interceptors/LoggingInterceptor.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/interceptors/LoggingInterceptor.java?rev=1214912&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/interceptors/LoggingInterceptor.java (added)
+++ openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/interceptors/LoggingInterceptor.java Thu Dec 15 19:01:22 2011
@@ -0,0 +1,50 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.superbiz.cdi.bookshow.interceptors;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
+import javax.interceptor.InvocationContext;
+
+import org.superbiz.cdi.bookshow.interceptorbinding.Log;
+
+@Interceptor
+@Log
+public class LoggingInterceptor {
+	private static final long serialVersionUID = 8139854519874743530L;
+	@AroundInvoke
+	public Object logMethodEntry(InvocationContext ctx) throws Exception {
+		System.out.println("Entering method: " + ctx.getMethod().getName());
+		return ctx.proceed();
+	}
+}

Propchange: openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/java/org/superbiz/cdi/bookshow/interceptors/LoggingInterceptor.java
------------------------------------------------------------------------------
    svn:executable = *

Added: openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/resources/META-INF/beans.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/resources/META-INF/beans.xml?rev=1214912&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/resources/META-INF/beans.xml (added)
+++ openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/resources/META-INF/beans.xml Thu Dec 15 19:01:22 2011
@@ -0,0 +1,31 @@
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<beans>
+  <!-- By default, a bean archive has no enabled interceptors bound via interceptor 
+    bindings. An interceptor must be explicitly enabled by listing its class 
+    under the element of the beans.xml file of the bean archive. The order of 
+    the interceptor declarations determines the interceptor ordering. Interceptors 
+    which occur earlier in the list are called first. If the same class is listed 
+    twice under the interceptors element, the container automatically detects 
+    the problem and treats it as a deployment problem. -->
+
+  <interceptors>
+    <class>org.superbiz.cdi.bookshow.interceptors.LoggingInterceptor
+    </class>
+  </interceptors>
+</beans>

Propchange: openejb/trunk/openejb/examples/simple-cdi-interceptor/src/main/resources/META-INF/beans.xml
------------------------------------------------------------------------------
    svn:executable = *

Added: openejb/trunk/openejb/examples/simple-cdi-interceptor/src/test/java/org/superbiz/cdi/bookshow/interceptors/BookShowTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/simple-cdi-interceptor/src/test/java/org/superbiz/cdi/bookshow/interceptors/BookShowTest.java?rev=1214912&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/simple-cdi-interceptor/src/test/java/org/superbiz/cdi/bookshow/interceptors/BookShowTest.java (added)
+++ openejb/trunk/openejb/examples/simple-cdi-interceptor/src/test/java/org/superbiz/cdi/bookshow/interceptors/BookShowTest.java Thu Dec 15 19:01:22 2011
@@ -0,0 +1,49 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.superbiz.cdi.bookshow.interceptors;
+
+import javax.ejb.EJB;
+import javax.ejb.embeddable.EJBContainer;
+
+import junit.framework.TestCase;
+
+import org.superbiz.cdi.bookshow.beans.BookShow;
+
+public class BookShowTest extends TestCase {
+	@EJB
+	private BookShow bookForAShowBean;
+	EJBContainer ejbContainer;
+	/**
+	 * Bootstrap the Embedded EJB Container
+	 * 
+	 * @throws Exception
+	 */
+	@Override
+	protected void setUp() throws Exception {
+		ejbContainer = EJBContainer.createEJBContainer();
+		ejbContainer.getContext().bind("inject", this);
+	}
+	/**
+	 * Test basic interception
+	 */
+	public void testMethodShouldBeIntercepted() {
+
+		bookForAShowBean.getMoviesList();
+
+	}
+
+}

Propchange: openejb/trunk/openejb/examples/simple-cdi-interceptor/src/test/java/org/superbiz/cdi/bookshow/interceptors/BookShowTest.java
------------------------------------------------------------------------------
    svn:executable = *