[jboss-cvs] JBossAS SVN: r99865 - in projects/ejb-book/trunk/chxx-interceptors/src: test/java/org/jboss/ejb3/examples/chxx/echo and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sun Jan 24 20:39:34 EST 2010


Author: ALRubinger
Date: 2010-01-24 20:39:34 -0500 (Sun, 24 Jan 2010)
New Revision: 99865

Added:
   projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/Channel2AccessPolicy.java
   projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/Channel2ClosedException.java
   projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/Channel2Restrictor.java
   projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/TunerBean.java
   projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/TunerLocalBusiness.java
   projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/CachingInterceptorUnitTestCase.java
   projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/Channel2RestrictorUnitTestCase.java
   projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/MockInvocationContext.java
Removed:
   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/test/java/org/jboss/ejb3/examples/chxx/echo/CachingInterceptorUnitTest.java
Modified:
   projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/InterceptionIntegrationTest.java
Log:
[EJBBOOK-16] Give a less generic, more interesting domain to the interceptors example

Added: projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/Channel2AccessPolicy.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/Channel2AccessPolicy.java	                        (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/Channel2AccessPolicy.java	2010-01-25 01:39:34 UTC (rev 99865)
@@ -0,0 +1,74 @@
+/*
+ * 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;
+
+/**
+ * Defines the authoritative policy governing whether or not
+ * Channel 2 should be currently accessible
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class Channel2AccessPolicy
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Flag dictating whether or not Channel 2 should be shown
+    */
+   private static boolean channel2Permitted = false;
+
+   //-------------------------------------------------------------------------------------||
+   // Constructor ------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * No instantiation
+    */
+   private Channel2AccessPolicy()
+   {
+      throw new UnsupportedOperationException("No instances permitted");
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Functional Methods -----------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Returns whether or not requests to view Channel 2 will be honored
+    */
+   public static boolean isChannel2Permitted()
+   {
+      return channel2Permitted;
+   }
+
+   /**
+    * Returns whether or not requests to view Channel 2 will be honored
+    */
+   public static void setChannel2Permitted(final boolean channel2Permitted)
+   {
+      Channel2AccessPolicy.channel2Permitted = channel2Permitted;
+   }
+}

Added: projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/Channel2ClosedException.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/Channel2ClosedException.java	                        (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/Channel2ClosedException.java	2010-01-25 01:39:34 UTC (rev 99865)
@@ -0,0 +1,71 @@
+/*
+ * 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.ApplicationException;
+
+/**
+ * Denotes that Channel 2 is not currently available for viewing
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at ApplicationException
+// Denotes that this exception type should be returned to the client as-is, not wrapped
+public class Channel2ClosedException extends Exception
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * serialVersionUID
+    */
+   private static final long serialVersionUID = 1L;
+
+   /**
+    * The sole instance, this type has no state
+    */
+   public static final Channel2ClosedException INSTANCE;
+   static
+   {
+      INSTANCE = new Channel2ClosedException();
+   }
+
+   /**
+    * Message for all incoming Exceptions
+    */
+   private static final String MSG = "Channel 2 is not currently available for viewing";
+
+   //-------------------------------------------------------------------------------------||
+   // Constructor ------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Constructs a new instance
+    */
+   private Channel2ClosedException()
+   {
+      super(MSG);
+   }
+}

Added: projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/Channel2Restrictor.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/Channel2Restrictor.java	                        (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/Channel2Restrictor.java	2010-01-25 01:39:34 UTC (rev 99865)
@@ -0,0 +1,120 @@
+/*
+ * 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.lang.reflect.Method;
+import java.util.logging.Logger;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+
+/**
+ * Aspect which restricts access to Channel 2 unless
+ * the network has allowed broadcasting.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class Channel2Restrictor
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(Channel2Restrictor.class.getName());
+
+   /**
+    * Name of the method to request channel content
+    */
+   private static final String METHOD_NAME_GET_CHANNEL;
+   static
+   {
+      METHOD_NAME_GET_CHANNEL = TunerLocalBusiness.class.getMethods()[0].getName();
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Required Implementations -----------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Examines the specified request to determine if the caller is attempting 
+    * to obtain content for Channel 2.  If so, will block the request with 
+    * {@link Channel2ClosedException}
+    */
+   @AroundInvoke
+   public Object checkAccessibility(final InvocationContext context) throws Exception
+   {
+      // Precondition checks
+      assert context != null : "Context was not specified";
+
+      // See if we're requesting Channel 2
+      if (isRequestForChannel2(context))
+      {
+         // See if Channel 2 is open
+         if (!Channel2AccessPolicy.isChannel2Permitted())
+         {
+            // Block access
+            throw Channel2ClosedException.INSTANCE;
+         }
+      }
+
+      // Otherwise carry on
+      return context.proceed();
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Functional Methods -----------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Determines whether or not the specified context represents a request for Channel 2
+    */
+   private static boolean isRequestForChannel2(final InvocationContext context)
+   {
+      // Precondition check
+      assert context != null : "Context was not specified";
+
+      // Get the target method
+      final Method targetMethod = context.getMethod();
+
+      // If we're requesting a new channel
+      final String targetMethodName = targetMethod.getName();
+      if (targetMethodName.equals(METHOD_NAME_GET_CHANNEL))
+      {
+         log.info("This is a request for channel content: " + context);
+         // Get the requested channel
+         final int channel = ((Integer) context.getParameters()[0]).intValue();
+         if (channel == 2)
+         {
+            // Yep, they want channel 2
+            return true;
+         }
+      }
+
+      // Return
+      return false;
+   }
+}

Deleted: 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	2010-01-24 23:37:55 UTC (rev 99864)
+++ projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/EchoBean.java	2010-01-25 01:39:34 UTC (rev 99865)
@@ -1,56 +0,0 @@
-/*
- * 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;
-   }
-}

Deleted: 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	2010-01-24 23:37:55 UTC (rev 99864)
+++ projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/EchoLocalBusiness.java	2010-01-25 01:39:34 UTC (rev 99865)
@@ -1,54 +0,0 @@
-/*
- * 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;
-}

Copied: projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/TunerBean.java (from rev 99862, 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/TunerBean.java	                        (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/TunerBean.java	2010-01-25 01:39:34 UTC (rev 99865)
@@ -0,0 +1,107 @@
+/*
+ * 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.IOException;
+import java.io.InputStream;
+import java.util.logging.Logger;
+
+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
+// Class-level interceptors will be run upon requests to every method of this EJB
+ at Interceptors(CachingAuditor.class)
+ at Local(TunerLocalBusiness.class)
+public class TunerBean implements TunerLocalBusiness
+{
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(TunerBean.class.getName());
+
+   //-------------------------------------------------------------------------------------||
+   // Required Implementations -----------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * {@inheritDoc}
+    * @see org.jboss.ejb3.examples.chxx.echo.TunerLocalBusiness#getChannel(int)
+    */
+   // Here we declare method-level interceptors, which will only take place on this method
+   @Interceptors(Channel2Restrictor.class)
+   @Override
+   public InputStream getChannel(final int channel) throws IllegalArgumentException
+   {
+      // Declare the stream we'll use
+      final InputStream stream;
+      switch (channel)
+      {
+         // We want channel 1
+         case 1 :
+            stream = new InputStream()
+            {
+
+               @Override
+               public int read() throws IOException
+               {
+                  return 1;
+               }
+            };
+            break;
+         // We want channel 2
+         case 2 :
+            stream = new InputStream()
+            {
+
+               @Override
+               public int read() throws IOException
+               {
+                  return 2;
+               }
+            };
+            break;
+         // We've requested an improper channel
+         default :
+            throw new IllegalArgumentException("Not a valid channel: " + channel);
+      }
+
+      // Return
+      log.info("Returning stream for Channel " + channel + ": " + stream);
+      return stream;
+   }
+}

Copied: projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/TunerLocalBusiness.java (from rev 99862, 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/TunerLocalBusiness.java	                        (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/main/java/org/jboss/ejb3/examples/chxx/echo/TunerLocalBusiness.java	2010-01-25 01:39:34 UTC (rev 99865)
@@ -0,0 +1,60 @@
+/*
+ * 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.InputStream;
+
+/**
+ * Local business interface of an EJB which 
+ * provides access to television streams
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public interface TunerLocalBusiness
+{
+   //-------------------------------------------------------------------------------------||
+   // Contracts --------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * @deprecated Use Global JNDI Bindings EJBBOOK-2 
+    */
+   //TODO
+   @Deprecated
+   String JNDI_NAME = "TunerBean/local";
+
+   //-------------------------------------------------------------------------------------||
+   // Constants --------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Obtains the stream containing viewable content 
+    * for the specified television channel.  Supported channels are 1 and 2.
+    * 
+    * @param channel
+    * @return
+    * @throws IllegalArgumentException If the channel is not valid
+    */
+   InputStream getChannel(int channel) throws IllegalArgumentException;
+
+}

Deleted: 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	2010-01-24 23:37:55 UTC (rev 99864)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/CachingInterceptorUnitTest.java	2010-01-25 01:39:34 UTC (rev 99865)
@@ -1,153 +0,0 @@
-/*
- * 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);
-      }
-
-   }
-}

Copied: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/CachingInterceptorUnitTestCase.java (from rev 99862, 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/CachingInterceptorUnitTestCase.java	                        (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/CachingInterceptorUnitTestCase.java	2010-01-25 01:39:34 UTC (rev 99865)
@@ -0,0 +1,100 @@
+/*
+ * 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.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 CachingInterceptorUnitTestCase
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(CachingInterceptorUnitTestCase.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(TunerLocalBusiness.class.getMethods()[0],
+            new Object[]
+            {1});
+      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));
+   }
+
+}

Added: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/Channel2RestrictorUnitTestCase.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/Channel2RestrictorUnitTestCase.java	                        (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/Channel2RestrictorUnitTestCase.java	2010-01-25 01:39:34 UTC (rev 99865)
@@ -0,0 +1,137 @@
+/*
+ * 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.logging.Logger;
+
+import javax.interceptor.InvocationContext;
+
+import junit.framework.TestCase;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests to ensure that the {@link Channel2Restrictor}
+ * 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 Channel2RestrictorUnitTestCase
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(Channel2RestrictorUnitTestCase.class.getName());
+
+   /**
+    * Method to get channel content
+    */
+   private static final Method METHOD_GET_CHANNEL = TunerLocalBusiness.class.getMethods()[0];
+
+   //-------------------------------------------------------------------------------------||
+   // Instance Members -------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * The interceptor instance to test
+    */
+   private Channel2Restrictor interceptor;
+
+   //-------------------------------------------------------------------------------------||
+   // Lifecycle --------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Creates the interceptor instance to be used in testing
+    */
+   @Before
+   public void createInterceptor()
+   {
+      interceptor = new Channel2Restrictor();
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Tests ------------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Ensures requests for channel 2 are blocked when the channel's access is closed
+    */
+   @Test(expected = Channel2ClosedException.class)
+   public void requestsToChannel2Blocked() throws Exception
+   {
+      // Set the access policy to block
+      Channel2AccessPolicy.setChannel2Permitted(false);
+
+      // Invoke
+      final InvocationContext invocation = new MockInvocationContext(METHOD_GET_CHANNEL, new Object[]
+      {2});
+      interceptor.checkAccessibility(invocation);
+   }
+
+   /**
+    * Ensures requests for channel 2 are not blocked when the channel's access is open
+    */
+   @Test
+   public void requestsToChannel2NotBlocked() throws Exception
+   {
+      // Set the access policy to block
+      Channel2AccessPolicy.setChannel2Permitted(true);
+
+      // Invoke
+      final InvocationContext invocation = new MockInvocationContext(METHOD_GET_CHANNEL, new Object[]
+      {2});
+      try
+      {
+         interceptor.checkAccessibility(invocation);
+      }
+      catch (final Channel2ClosedException e)
+      {
+         TestCase.fail("Should not have been blocked with: " + e);
+      }
+   }
+
+   /**
+    * Ensures requests for channel 1 are not blocked channel 2's access is closed
+    */
+   @Test
+   public void requestsToChannel1NeverBlocked() throws Exception
+   {
+      // Set the access policy to block
+      Channel2AccessPolicy.setChannel2Permitted(false);
+
+      // Invoke
+      final InvocationContext invocation = new MockInvocationContext(METHOD_GET_CHANNEL, new Object[]
+      {1});
+      interceptor.checkAccessibility(invocation);
+   }
+
+}

Modified: 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	2010-01-24 23:37:55 UTC (rev 99864)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/InterceptionIntegrationTest.java	2010-01-25 01:39:34 UTC (rev 99865)
@@ -22,6 +22,9 @@
 package org.jboss.ejb3.examples.chxx.echo;
 
 import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.UndeclaredThrowableException;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.HashSet;
@@ -92,8 +95,16 @@
    // Instance Members -------------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
 
+   /**
+    * Archive representing the deployment 
+    */
    private JavaArchive deployment;
 
+   /**
+    * The bean to invoke upon
+    */
+   private TunerLocalBusiness bean;
+
    //-------------------------------------------------------------------------------------||
    // Lifecycle --------------------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||
@@ -168,19 +179,22 @@
    }
 
    /**
-    * Deploys the EJB into the server
+    * Deploys the EJB into the server and looks up an invokable reference
     * @throws Exception
     */
    @Before
-   public void deploy() throws Exception
+   public void deployAndGetBean() throws Exception
    {
 
       // Create the archive
-      deployment = Archives.create("echo.jar", JavaArchive.class).addClasses(EchoLocalBusiness.class, EchoBean.class,
-            CachingAuditor.class);
+      deployment = Archives.create("echo.jar", JavaArchive.class).addClasses(TunerLocalBusiness.class, TunerBean.class,
+            CachingAuditor.class, Channel2Restrictor.class);
 
       // Deploy
       server.deploy(deployment);
+
+      // Lookup 
+      bean = (TunerLocalBusiness) NAMING_CONTEXT.lookup(TunerLocalBusiness.JNDI_NAME);
    }
 
    /**
@@ -204,33 +218,68 @@
    //-------------------------------------------------------------------------------------||
 
    /**
-    * Ensures that invocation upon an EJB with {@link Interceptors} declared
+    * Ensures that invocation upon an EJB with {@link CachingAuditor} declared
     * results in the interception of targeted methods
     */
    @Test
-   public void testInterception() throws NamingException
+   public void testCachingInterception() throws NamingException, IOException
    {
-      // 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);
+      final int channel = 1;
+      final InputStream content = bean.getChannel(channel);
 
       // 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);
+      TestCase.assertEquals("Did not obtain expected response", channel, content.read());
 
       // Test the invocation was intercepted 
       TestCase.assertEquals("The invocation should have been intercepted", 1, CachingAuditor.getInvocations().size());
    }
 
+   /**
+    * Ensures that requests to obtain Channel 2 while restricted are blocked with {@link Channel2ClosedException}
+    */
+   @Test(expected = Channel2ClosedException.class)
+   public void testChannel2Restricted() throws Throwable
+   {
+      // Set the policy to block channel 2
+      Channel2AccessPolicy.setChannel2Permitted(false);
+
+      // Invoke
+      try
+      {
+         bean.getChannel(2);
+      }
+      // Expected
+      catch (final UndeclaredThrowableException ute)
+      {
+         throw ute.getCause();
+      }
+
+      // Fail if we reach here
+      TestCase.fail("Request should have been blocked");
+   }
+
+   /**
+    * Ensures that requests to obtain Channel 2 while open succeed
+    */
+   @Test
+   public void testChannel2Allowed() throws NamingException, IOException
+   {
+      // Set the policy to block channel 2
+      Channel2AccessPolicy.setChannel2Permitted(true);
+
+      // Invoke
+      final int channel = 2;
+      final InputStream stream = bean.getChannel(channel);
+
+      // Test
+      TestCase.assertEquals("Unexpected content obtained from channel " + channel, channel, stream.read());
+   }
+
    //-------------------------------------------------------------------------------------||
    // Internal Helper Methods ------------------------------------------------------------||
    //-------------------------------------------------------------------------------------||

Added: projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/MockInvocationContext.java
===================================================================
--- projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/MockInvocationContext.java	                        (rev 0)
+++ projects/ejb-book/trunk/chxx-interceptors/src/test/java/org/jboss/ejb3/examples/chxx/echo/MockInvocationContext.java	2010-01-25 01:39:34 UTC (rev 99865)
@@ -0,0 +1,120 @@
+/*
+ * 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 javax.interceptor.InvocationContext;
+
+/**
+ * {@link InvocationContext} implementation which throws {@link UnsupportedOperationException}
+ * for all required methods except {@link InvocationContext#proceed()}, which will always return null,
+ * {@link InvocationContext#getMethod()}, and {@link InvocationContext#getParameters()}.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+class MockInvocationContext implements InvocationContext
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Message used to denote that the operation is not supported 
+    */
+   private static final String MSG_UNSUPPORTED = "Not supported in mock implementation";
+
+   //-------------------------------------------------------------------------------------||
+   // Instance Members -------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Method invoked
+    */
+   private final Method method;
+
+   /**
+    * Parameters in the request
+    */
+   private final Object[] params;
+
+   //-------------------------------------------------------------------------------------||
+   // Constructor ------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Constructs a new instance with the specified required arguments
+    * @param method
+    * @param params
+    */
+   MockInvocationContext(final Method method, final Object[] params)
+   {
+
+      assert method != null : "method must be specified";
+      assert params != null : "params must be specified";
+      this.method = method;
+      this.params = params;
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Required Implementations -----------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   @Override
+   public Map<String, Object> getContextData()
+   {
+      throw new UnsupportedOperationException(MSG_UNSUPPORTED);
+   }
+
+   @Override
+   public Method getMethod()
+   {
+      return method;
+   }
+
+   @Override
+   public Object[] getParameters()
+   {
+      return params;
+   }
+
+   @Override
+   public Object getTarget()
+   {
+      throw new UnsupportedOperationException(MSG_UNSUPPORTED);
+   }
+
+   @Override
+   public Object proceed() throws Exception
+   {
+      return null;
+   }
+
+   @Override
+   public void setParameters(final Object[] arg0)
+   {
+      throw new UnsupportedOperationException(MSG_UNSUPPORTED);
+   }
+}




More information about the jboss-cvs-commits mailing list