[jboss-cvs] JBossAS SVN: r99862 - in projects/ejb-book/trunk: chxx-interceptors and 17 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sun Jan 24 18:33:36 EST 2010


Author: ALRubinger
Date: 2010-01-24 18:33:36 -0500 (Sun, 24 Jan 2010)
New Revision: 99862

Added:
   projects/ejb-book/trunk/chxx-interceptors/
   projects/ejb-book/trunk/chxx-interceptors/pom.xml
   projects/ejb-book/trunk/chxx-interceptors/src/
   projects/ejb-book/trunk/chxx-interceptors/src/main/
   projects/ejb-book/trunk/chxx-interceptors/src/main/java/
   projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/
   projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/
   projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/
   projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/
   projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/
   projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/
   projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/CachingAuditor.java
   projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/EchoBean.java
   projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/EchoLocalBusiness.java
   projects/ejb-book/trunk/chxx-interceptors/src/main/resources/
   projects/ejb-book/trunk/chxx-interceptors/src/test/
   projects/ejb-book/trunk/chxx-interceptors/src/test/java/
   projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/
   projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/
   projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/
   projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/
   projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/
   projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/
   projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/CachingInterceptorUnitTest.java
   projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/InterceptionIntegrationTest.java
   projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/SecurityActions.java
   projects/ejb-book/trunk/chxx-interceptors/src/test/resources/
Log:
[EJBBOOK-16] Create initial Interceptors examples


Property changes on: projects/ejb-book/trunk/chxx-interceptors
___________________________________________________________________
Name: svn:ignore
   + target
eclipse-target
target-eclipse
.project
.settings
.classpath


Added: projects/ejb-book/trunk/chxx-interceptors/pom.xml
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/pom.xml	                        (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/pom.xml	2010-01-24 23:33:36 UTC (rev 99862)
@@ -0,0 +1,229 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+  <!-- Parent Information -->
+  <parent>
+    <groupId>org.jboss.ejb3.examples</groupId>
+    <artifactId>jboss-ejb3-examples-build</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <relativePath>../pom.xml</relativePath>
+  </parent>
+
+  <!-- Model Version -->
+  <modelVersion>4.0.0</modelVersion>
+
+  <!-- Artifact Information -->
+  <artifactId>jboss-ejb3-examples-chxx-interceptors</artifactId>
+  <name>JBoss EJB 3.x Examples - Chapter XX: Intercepted EJBs</name>
+  <description>Example to accompany O'Reilly "Enterprise Java Beans 6th Edition" Chapter XX</description>
+
+  <!-- Build -->
+  <build>
+  </build>
+
+
+  <!-- Properties -->
+  <properties>
+
+    <!-- JBOSS_HOME (We'll unpack into here -->
+    <JBOSS_HOME>${project.build.directory}/jboss-${version.org.jboss.jbossas}</JBOSS_HOME>
+
+    <!-- Versioning -->
+
+  </properties>
+
+  <!-- Dependencies -->
+  <dependencies>
+
+    <dependency>
+      <groupId>org.jboss.ejb3</groupId>
+      <artifactId>jboss-ejb3-api</artifactId>
+      <!--
+        Must exclude jboss-metadata to prevent class leaking onto the
+        appCL which breaks booting AS? In an isolated CL context this is
+        not a problem EJBBOOK-12
+      -->
+      <exclusions>
+        <exclusion>
+          <groupId>jboss.jbossws</groupId>
+          <artifactId>jboss-jaxrpc</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+
+    <dependency>
+      <groupId>org.jboss.ejb3</groupId>
+      <artifactId>jboss-ejb3-ext-api</artifactId>
+      <!-- TODO: Resolve this need for extra configuration -->
+      <!--
+        Must exclude jboss-metadata to prevent class leaking onto the
+        appCL which breaks booting AS? In an isolated CL context this is
+        not a problem EJBBOOK-12
+      -->
+      <exclusions>
+        <exclusion>
+          <groupId>org.jboss.metadata</groupId>
+          <artifactId>jboss-metadata</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.jboss.javaee</groupId>
+          <artifactId>jboss-ejb-api</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+
+    <!-- EJBBOOK-12 Need the whole JNP Server for just client stuff? 
+    <dependency>
+      <groupId>org.jboss.naming</groupId>
+      <artifactId>jnpserver</artifactId>
+      <version>5.0.4.GA</version>
+      <scope>test</scope> 
+      <exclusions>
+        <exclusion>
+          <groupId>org.jboss</groupId>
+          <artifactId>jboss-common-core</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency> -->
+
+    <dependency>
+      <groupId>org.jboss.javaee</groupId>
+      <artifactId>jboss-jms-api</artifactId>
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.jboss.logging</groupId>
+      <artifactId>jboss-logging-log4j</artifactId>
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.jboss.logging</groupId>
+      <artifactId>jboss-logging-spi</artifactId>
+    </dependency>
+
+    <!-- 
+    
+    Test Dependencies
+     -->
+
+    <dependency>
+      <groupId>org.jboss.embedded</groupId>
+      <artifactId>jboss-embedded-api</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.jboss.shrinkwrap</groupId>
+      <artifactId>shrinkwrap-impl-base</artifactId>
+    </dependency>
+    
+    <!-- TODO: Remove the dependency on the full AS depchain; it pollutes the CP. EJBBOOK-12 -->
+    <dependency>
+      <groupId>org.jboss.embedded</groupId>
+      <artifactId>jboss-embedded-depchain</artifactId>
+      <scope>test</scope>
+      <version>${version.org.jboss.embedded}</version>
+      <type>pom</type>
+    </dependency>
+    
+  </dependencies>
+
+  <profiles>
+
+    <profile>
+
+      <!-- Declare the "Integration Test" Profile -->
+      <id>it</id>
+
+      <build>
+
+        <plugins>
+
+          <!-- Surefire -->
+          <plugin>
+            <artifactId>maven-surefire-plugin</artifactId>
+            <executions>
+              <execution>
+                <id>integration-test</id>
+                <phase>integration-test</phase>
+                <goals>
+                  <goal>test</goal>
+                </goals>
+                <configuration>
+                  <additionalClasspathElements>
+                    <additionalClasspathElement>${JBOSS_HOME}/bin/run.jar</additionalClasspathElement>
+                  </additionalClasspathElements>
+                  <redirectTestOutputToFile>true</redirectTestOutputToFile>
+                  <trimStackTrace>false</trimStackTrace>
+                  <printSummary>true</printSummary>
+                  <includes>
+                    <include>**/*IntegrationTest.java</include>
+                  </includes>
+                  <forkMode>always</forkMode>
+                  <argLine>-Xmx512m -XX:MaxPermSize=256m -Djava.net.preferIPv4Stack=true -Djava.endorsed.dirs=${JBOSS_HOME}/lib/endorsed -Djboss.home=${JBOSS_HOME}</argLine>
+                </configuration>
+              </execution>
+            </executions>
+          </plugin>
+
+          <plugin>
+            <groupId>org.apache.maven.plugins</groupId>
+            <artifactId>maven-dependency-plugin</artifactId>
+            <executions>
+              <!-- Get AS and put into "target" -->
+              <execution>
+                <id>unpack</id>
+                <phase>pre-integration-test</phase> <!-- So run before testing -->
+                <goals>
+                  <goal>unpack</goal>
+                </goals>
+                <configuration>
+                  <artifactItems>
+                    <artifactItem>
+                      <groupId>org.jboss.jbossas</groupId>
+                      <artifactId>jboss-as-distribution</artifactId>
+                      <version>${version.org.jboss.jbossas}</version>
+                      <type>zip</type>
+                      <overWrite>false</overWrite>
+                      <outputDirectory>${project.build.directory}</outputDirectory>
+                    </artifactItem>
+                  </artifactItems>
+                </configuration>
+              </execution>
+              <!-- Other deps, not part of AS, into "target/deps" -->
+              <execution>
+                <id>copy</id>
+                <phase>pre-integration-test</phase>
+                <goals>
+                  <goal>copy</goal>
+                </goals>
+                <configuration>
+                  <artifactItems>
+                    <artifactItem>
+                      <groupId>org.jboss.embedded</groupId>
+                      <artifactId>jboss-embedded-core</artifactId>
+                      <version>${version.org.jboss.embedded}</version>
+                      <overWrite>false</overWrite>
+                      <outputDirectory>${project.build.directory}/deps</outputDirectory>
+                    </artifactItem>
+                  </artifactItems>
+                </configuration>
+              </execution>
+            </executions>
+          </plugin>
+
+        </plugins>
+
+      </build>
+
+    </profile>
+
+  </profiles>
+
+</project>


Property changes on: projects/ejb-book/trunk/chxx-interceptors/pom.xml
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/CachingAuditor.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/CachingAuditor.java	                        (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/CachingAuditor.java	2010-01-24 23:33:36 UTC (rev 99862)
@@ -0,0 +1,113 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.chxx.echo;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.logging.Logger;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+
+/**
+ * Aspect which keeps a cache of all intercepted
+ * invocations in a globally-accessible cache.
+ * 
+ * Though demonstrative for testing and learning purposes, this is a very
+ * poor example of a real-world auditing mechanism.  In a production environment, 
+ * the copy-on-write nature of the cache will degrade geometrically 
+ * over time, and additionally we export mutable views 
+ * (ie. {@link InvocationContext#setParameters(Object[])}) to callers
+ * of {@link CachingAuditor#getInvocations()}.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class CachingAuditor
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(CachingAuditor.class.getName());
+
+   /**
+    * Cached invocations; must be in a thread-safe implementation because this member
+    * is shared by all interceptor instances, which are linked to bean instances.  Though
+    * each bean instance is guaranteed to be used by only one thread at once, many bean instances
+    * may be executed concurrently.
+    */
+   private static final List<InvocationContext> invocations = new CopyOnWriteArrayList<InvocationContext>();
+
+   //-------------------------------------------------------------------------------------||
+   // Required Implementations -----------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Caches the intercepted {@link InvocationContext} such that
+    * a test may obtain it later
+    */
+   @AroundInvoke
+   public Object audit(final InvocationContext context) throws Exception
+   {
+      // Precondition checks
+      assert context != null : "Context was not specified";
+
+      // Add the invocation to the cache
+      invocations.add(context);
+
+      // Carry out the invocation, noting where we've intercepted before and after the call (around it)
+      try
+      {
+         // Log
+         log.info("Intercepted: " + context);
+
+         // Return
+         return context.proceed();
+      }
+      finally
+      {
+         // Log
+         log.info("Done with: " + context);
+      }
+
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Functional Methods -----------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Returns a read-only view of the {@link InvocationContext}
+    * cached by this interceptor
+    */
+   public static List<InvocationContext> getInvocations()
+   {
+      // Copy on export
+      return Collections.unmodifiableList(invocations);
+   }
+}

Added: projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/EchoBean.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/EchoBean.java	                        (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/EchoBean.java	2010-01-24 23:33:36 UTC (rev 99862)
@@ -0,0 +1,56 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.chxx.echo;
+
+import javax.ejb.Local;
+import javax.ejb.Stateless;
+import javax.interceptor.Interceptors;
+import javax.interceptor.InvocationContext;
+
+/**
+ * Simple EJB which returns references back to the client.  Used to
+ * show configuration of interceptors; here we've configured the 
+ * {@link CachingAuditor} to remember all previous 
+ * {@link InvocationContext}s made upon the EJB.  
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Stateless
+ at Interceptors(CachingAuditor.class)
+ at Local(EchoLocalBusiness.class)
+public class EchoBean implements EchoLocalBusiness
+{
+   //-------------------------------------------------------------------------------------||
+   // Required Implementations -----------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * {@inheritDoc}
+    * @see org.jboss.ejb3.examples.chxx.echo.EchoLocalBusiness#echo(java.lang.String)
+    */
+   @Override
+   public String echo(final String value) throws IllegalArgumentException
+   {
+      return value;
+   }
+}

Added: projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/EchoLocalBusiness.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/EchoLocalBusiness.java	                        (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/EchoLocalBusiness.java	2010-01-24 23:33:36 UTC (rev 99862)
@@ -0,0 +1,54 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.chxx.echo;
+
+/**
+ * Local business interface of an EJB which simply returns a reference
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public interface EchoLocalBusiness
+{
+   //-------------------------------------------------------------------------------------||
+   // Contracts --------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * @deprecated Use Global JNDI Bindings EJBBOOK-2 
+    */
+   //TODO
+   @Deprecated
+   String JNDI_NAME = "EchoBean/local";
+
+   //-------------------------------------------------------------------------------------||
+   // Constants --------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Returns the specified reference
+    * @param value
+    * @return
+    * @throws IllegalArgumentException If the value was not specified
+    */
+   String echo(String value) throws IllegalArgumentException;
+}

Added: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/CachingInterceptorUnitTest.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/CachingInterceptorUnitTest.java	                        (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/CachingInterceptorUnitTest.java	2010-01-24 23:33:36 UTC (rev 99862)
@@ -0,0 +1,153 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.chxx.echo;
+
+import java.lang.reflect.Method;
+import java.util.Map;
+import java.util.logging.Logger;
+
+import javax.interceptor.InvocationContext;
+
+import junit.framework.TestCase;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests to ensure that the {@link CachingAuditor}
+ * interceptor is working as expected outside the context
+ * of a full container.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class CachingInterceptorUnitTest
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(CachingInterceptorUnitTest.class.getName());
+
+   //-------------------------------------------------------------------------------------||
+   // Instance Members -------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * The interceptor instance to test
+    */
+   private CachingAuditor interceptor;
+
+   //-------------------------------------------------------------------------------------||
+   // Lifecycle --------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Creates the interceptor instance to be used in testing
+    */
+   @Before
+   public void createInterceptor()
+   {
+      interceptor = new CachingAuditor();
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Tests ------------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Ensures that contexts passed through the interceptor are cached
+    */
+   @Test
+   public void testCache() throws Exception
+   {
+      // Ensure the cache is empty to start
+      TestCase.assertEquals("Cache should start empty", 0, CachingAuditor.getInvocations().size());
+
+      // Invoke
+      final InvocationContext invocation = new MockInvocationContext();
+      interceptor.audit(invocation);
+
+      // Test our invocation was cached properly
+      TestCase.assertEquals("Cache should have the first invocation", 1, CachingAuditor.getInvocations().size());
+      TestCase.assertEquals("Invocation cached was not the one that was invoked", invocation, CachingAuditor
+            .getInvocations().get(0));
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Inner Classes ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * {@link InvocationContext} implementation which throws {@link UnsupportedOperationException}
+    * for all required methods except {@link InvocationContext#proceed()}, which will always return null.
+    */
+   private static class MockInvocationContext implements InvocationContext
+   {
+
+      /**
+       * Message used to denote that the operation is not supported 
+       */
+      private static final String MSG_UNSUPPORTED = "Not supported in mock implementation";
+
+      @Override
+      public Map<String, Object> getContextData()
+      {
+         throw new UnsupportedOperationException(MSG_UNSUPPORTED);
+      }
+
+      @Override
+      public Method getMethod()
+      {
+         throw new UnsupportedOperationException(MSG_UNSUPPORTED);
+      }
+
+      @Override
+      public Object[] getParameters()
+      {
+         throw new UnsupportedOperationException(MSG_UNSUPPORTED);
+      }
+
+      @Override
+      public Object getTarget()
+      {
+         throw new UnsupportedOperationException(MSG_UNSUPPORTED);
+      }
+
+      @Override
+      public Object proceed() throws Exception
+      {
+         return null;
+      }
+
+      @Override
+      public void setParameters(Object[] arg0)
+      {
+         throw new UnsupportedOperationException(MSG_UNSUPPORTED);
+      }
+
+   }
+}

Added: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/InterceptionIntegrationTest.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/InterceptionIntegrationTest.java	                        (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/InterceptionIntegrationTest.java	2010-01-24 23:33:36 UTC (rev 99862)
@@ -0,0 +1,267 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.chxx.echo;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.logging.Logger;
+
+import javax.interceptor.Interceptors;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import junit.framework.TestCase;
+
+import org.jboss.bootstrap.api.lifecycle.LifecycleState;
+import org.jboss.embedded.api.server.JBossASEmbeddedServer;
+import org.jboss.embedded.api.server.JBossASEmbeddedServerFactory;
+import org.jboss.embedded.api.server.JBossHomeClassLoader;
+import org.jboss.shrinkwrap.api.Archives;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Integration test ensuring that an EJB with {@link Interceptors} 
+ * declared are intercepted when invoked
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class InterceptionIntegrationTest
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(InterceptionIntegrationTest.class.getName());
+
+   /**
+    * The CL of the test as originally loaded
+    */
+   private static ClassLoader originalClassLoader;
+
+   /**
+    * The server instance
+    */
+   private static JBossASEmbeddedServer server;
+
+   /**
+    * Name of the system property for JBOSS_HOME
+    * @deprecated EJBBOOK-14
+    */
+   @Deprecated
+   private static final String NAME_SYSPROP_JBOSS_HOME = "jboss.home";
+
+   /**
+    * The JNDI Context
+    */
+   private static Context NAMING_CONTEXT;
+
+   //-------------------------------------------------------------------------------------||
+   // Instance Members -------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   private JavaArchive deployment;
+
+   //-------------------------------------------------------------------------------------||
+   // Lifecycle --------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Creates and starts a new JBossAS Server Embedded within this JVM
+    */
+   //TODO EJBBOOK-15
+   @BeforeClass
+   public static void createAndStartJBossASAndSetNamingContext() throws Exception
+   {
+      // Get JBOSS_HOME
+      final URL jbossHome = getJBossHome();
+
+      // Get additional binaries which need CL visibility (ie. jboss-embedded-core,
+      // which is placed under "target/deps" by the build).  These
+      // binaries are not presently available under $JBOSS_HOME
+      final Set<URL> additionalUrls = new HashSet<URL>();
+      final URL source = InterceptionIntegrationTest.class.getProtectionDomain().getCodeSource().getLocation();
+      final URL target = new URL(source, "..");
+      final URL additionalDeps = new URL(target, "deps");
+      final File deps = new File(additionalDeps.toURI());
+      TestCase.assertTrue("Dependencies location does not exist: " + deps, deps.exists());
+      TestCase.assertTrue("Dependencies location is not a directory: " + deps, deps.isDirectory());
+      for (final File child : deps.listFiles())
+      {
+         additionalUrls.add(child.toURI().toURL());
+         log.info("Booting with: " + child);
+      }
+
+      // Make the new ClassLoader
+      originalClassLoader = SecurityActions.getThreadContextClassLoader();
+      final ClassLoader jbossHomeClassLoader = JBossHomeClassLoader.newInstance(jbossHome, additionalUrls
+            .toArray(new URL[]
+            {}), originalClassLoader);
+
+      // Make Server
+      server = JBossASEmbeddedServerFactory.createServer(jbossHomeClassLoader);
+      log.info("Created: " + server);
+
+      // Start
+      log.info("Starting Server: " + server);
+
+      // Set TCCL
+      Thread.currentThread().setContextClassLoader(jbossHomeClassLoader);
+
+      // Start the Server
+      server.start();
+
+      // Set Naming Context
+      NAMING_CONTEXT = new InitialContext();
+   }
+
+   /**
+    * Stops the Application Server
+    */
+   @AfterClass
+   public static void stopJBossAS() throws Exception
+   {
+      if (server != null && server.getState().equals(LifecycleState.STARTED))
+      {
+         try
+         {
+            server.shutdown();
+         }
+         finally
+         {
+            // Reset the TCCL 
+            Thread.currentThread().setContextClassLoader(originalClassLoader);
+         }
+      }
+   }
+
+   /**
+    * Deploys the EJB into the server
+    * @throws Exception
+    */
+   @Before
+   public void deploy() throws Exception
+   {
+
+      // Create the archive
+      deployment = Archives.create("echo.jar", JavaArchive.class).addClasses(EchoLocalBusiness.class, EchoBean.class,
+            CachingAuditor.class);
+
+      // Deploy
+      server.deploy(deployment);
+   }
+
+   /**
+    * Undeploys the EJB from the server
+    * @throws Exception
+    */
+   @After
+   public void undeploy() throws Exception
+   {
+      // If we've created the running server and deployed into it
+      if (deployment != null && server != null && server.getState().equals(LifecycleState.STARTED))
+      {
+         // Undeploy
+         server.undeploy(deployment);
+      }
+
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Tests ------------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Ensures that invocation upon an EJB with {@link Interceptors} declared
+    * results in the interception of targeted methods
+    */
+   @Test
+   public void testInterception() throws NamingException
+   {
+      // Lookup
+      final EchoLocalBusiness bean = (EchoLocalBusiness) NAMING_CONTEXT.lookup(EchoLocalBusiness.JNDI_NAME);
+
+      // Ensure no invocations intercepted yet
+      TestCase.assertEquals("No invocations should have yet been intercepted", 0, CachingAuditor.getInvocations()
+            .size());
+
+      // Invoke
+      final String request = "Hey-o!";
+      final String response = bean.echo(request);
+
+      // Test the response is as expected
+      TestCase.assertEquals("Did not obtain expected response", request, response);
+      TestCase.assertTrue(
+            "Invocation returned an object equal by value, but local invocations must be pass-by-reference",
+            request == response);
+
+      // Test the invocation was intercepted 
+      TestCase.assertEquals("The invocation should have been intercepted", 1, CachingAuditor.getInvocations().size());
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Internal Helper Methods ------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Obtains $JBOSS_HOME from the system property
+    * 
+    * @deprecated EJBBOOK-14
+    * @return
+    */
+   @Deprecated
+   private static URL getJBossHome()
+   {
+      final String sysProp = NAME_SYSPROP_JBOSS_HOME;
+      final String jbossHomeString = SecurityActions.getSystemProperty(sysProp);
+      if (jbossHomeString == null)
+      {
+         throw new IllegalStateException("System property \"" + sysProp + "\" must be present in the environment");
+      }
+      final File jbossHomeFile = new File(jbossHomeString);
+      if (!jbossHomeFile.exists())
+      {
+         throw new IllegalStateException("JBOSS_HOME does not exist: " + jbossHomeFile.getAbsolutePath());
+      }
+      try
+      {
+         return jbossHomeFile.toURI().toURL();
+      }
+      catch (final MalformedURLException murle)
+      {
+         throw new RuntimeException("Could not get JBOSS_HOME", murle);
+      }
+   }
+}

Added: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/SecurityActions.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/SecurityActions.java	                        (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/SecurityActions.java	2010-01-24 23:33:36 UTC (rev 99862)
@@ -0,0 +1,208 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.chxx.echo;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+/**
+ * Protected security actions not to leak outside this package
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+class SecurityActions
+{
+
+   //-------------------------------------------------------------------------------||
+   // Constructor ------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------||
+
+   /**
+    * No external instanciation
+    */
+   private SecurityActions()
+   {
+
+   }
+
+   //-------------------------------------------------------------------------------||
+   // Utility Methods --------------------------------------------------------------||
+   //-------------------------------------------------------------------------------||
+
+   /**
+    * Obtains the Thread Context ClassLoader
+    */
+   static ClassLoader getThreadContextClassLoader()
+   {
+      return AccessController.doPrivileged(GetTcclAction.INSTANCE);
+   }
+
+   /**
+    * Sets the specified CL upon the current Thread's Context 
+    * 
+    * @param cl
+    * @throws IllegalArgumentException If the CL was null
+    */
+   static void setThreadContextClassLoader(final ClassLoader cl) throws IllegalArgumentException
+   {
+      if (cl == null)
+      {
+         throw new IllegalArgumentException("ClassLoader was null");
+      }
+
+      AccessController.doPrivileged(new PrivilegedAction<Void>()
+      {
+         public Void run()
+         {
+            Thread.currentThread().setContextClassLoader(cl);
+            return null;
+         };
+      });
+   }
+
+   /**
+    * Obtains the environment variable with the specified name, or null
+    * if not present
+    * @param envVarName
+    * @return
+    * @throws IllegalArgumentException If the environment variable name was not specified
+    */
+   static String getEnvironmentVariable(final String envVarName) throws IllegalArgumentException
+   {
+      // Precondition checks
+      if (envVarName == null || envVarName.length() == 0)
+      {
+         throw new IllegalArgumentException("Environment variable name was not specified");
+      }
+
+      // Return
+      return AccessController.doPrivileged(new GetEnvironmentVariableAction(envVarName));
+   }
+
+   /**
+    * Obtains the system property with the specified key
+    * 
+    * @param key
+    * @return
+    * @throws IllegalArgumentException If the key is null
+    */
+   static String getSystemProperty(final String key) throws IllegalArgumentException
+   {
+      // Precondition check
+      if (key == null)
+      {
+         throw new IllegalArgumentException("key was null");
+      }
+
+      // Get sysprop
+      return AccessController.doPrivileged(new GetSystemPropertyAction(key));
+   }
+
+   //-------------------------------------------------------------------------------||
+   // Inner Classes ----------------------------------------------------------------||
+   //-------------------------------------------------------------------------------||
+
+   /**
+    * {@link PrivilegedAction} action to obtain the TCCL
+    */
+   private enum GetTcclAction implements PrivilegedAction<ClassLoader> {
+      INSTANCE;
+
+      @Override
+      public ClassLoader run()
+      {
+         return Thread.currentThread().getContextClassLoader();
+      }
+   }
+
+   /**
+    * {@link PrivilegedAction} to access an environment variable
+    * 
+    *
+    * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+    * @version $Revision: $
+    */
+   private static class GetEnvironmentVariableAction implements PrivilegedAction<String>
+   {
+
+      /**
+       * Name of the environment variable to get
+       */
+      private String envVarName;
+
+      /**
+       * Creates a new instance capable of obtaining the specified environment variable name
+       * @param envVarName
+       */
+      public GetEnvironmentVariableAction(final String envVarName)
+      {
+         this.envVarName = envVarName;
+      }
+
+      /**
+       * {@inheritDoc}
+       * @see java.security.PrivilegedAction#run()
+       */
+      @Override
+      public String run()
+      {
+         return System.getenv(envVarName);
+      }
+   }
+
+   /**
+    * {@link PrivilegedAction} to access a system property
+    * 
+    *
+    * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+    * @version $Revision: $
+    */
+   private static class GetSystemPropertyAction implements PrivilegedAction<String>
+   {
+
+      /**
+       * Name of the sysprop to get
+       */
+      private String sysPropName;
+
+      /**
+       * Creates a new instance capable of obtaining the specified system property by name
+       * @param sysPropName
+       */
+      public GetSystemPropertyAction(final String sysPropName)
+      {
+         this.sysPropName = sysPropName;
+      }
+
+      /**
+       * {@inheritDoc}
+       * @see java.security.PrivilegedAction#run()
+       */
+      @Override
+      public String run()
+      {
+         return System.getProperty(sysPropName);
+      }
+   }
+
+}




More information about the jboss-cvs-commits mailing list