[jboss-cvs] JBossAS SVN: r73653 - in projects/ejb3/trunk/proxy/src: main/java/org/jboss/ejb3/proxy/handler/session/stateful and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat May 24 14:00:05 EDT 2008


Author: ALRubinger
Date: 2008-05-24 14:00:05 -0400 (Sat, 24 May 2008)
New Revision: 73653

Modified:
   projects/ejb3/trunk/proxy/src/main/java/org/jboss/ejb3/proxy/handler/ProxyInvocationHandlerBase.java
   projects/ejb3/trunk/proxy/src/main/java/org/jboss/ejb3/proxy/handler/session/stateful/StatefulProxyInvocationHandler.java
   projects/ejb3/trunk/proxy/src/main/java/org/jboss/ejb3/proxy/handler/session/stateless/StatelessProxyInvocationHandler.java
   projects/ejb3/trunk/proxy/src/test/java/org/jboss/ejb3/test/proxy/spec_3_4_5/ProxyEqualityTestCaseBase.java
   projects/ejb3/trunk/proxy/src/test/java/org/jboss/ejb3/test/proxy/spec_3_4_5/unit/SfsbProxyEqualityTestCase.java
   projects/ejb3/trunk/proxy/src/test/java/org/jboss/ejb3/test/proxy/spec_3_4_5/unit/SlsbProxyEqualityTestCase.java
Log:
[EJBTHREE-1345] Spec 3.4.5 Implementation Completed

Modified: projects/ejb3/trunk/proxy/src/main/java/org/jboss/ejb3/proxy/handler/ProxyInvocationHandlerBase.java
===================================================================
--- projects/ejb3/trunk/proxy/src/main/java/org/jboss/ejb3/proxy/handler/ProxyInvocationHandlerBase.java	2008-05-24 17:49:50 UTC (rev 73652)
+++ projects/ejb3/trunk/proxy/src/main/java/org/jboss/ejb3/proxy/handler/ProxyInvocationHandlerBase.java	2008-05-24 18:00:05 UTC (rev 73653)
@@ -21,8 +21,6 @@
  */
 package org.jboss.ejb3.proxy.handler;
 
-import java.lang.reflect.Proxy;
-
 import org.jboss.ejb3.proxy.lang.SerializableMethod;
 
 /**
@@ -129,7 +127,7 @@
          assert args.length == 1 : "Invocation for 'equals' should have exactly one argument, instead was: "
                + invokedMethod;
          Object argument = args[0];
-         return new Boolean(this.invokeEquals(proxy, argument));
+         return this.invokeEquals(proxy, argument);
       }
       // toString
       if (invokedMethod.equals(ProxyInvocationHandlerBase.METHOD_TO_STRING))
@@ -139,7 +137,7 @@
       // hashCode
       if (invokedMethod.equals(ProxyInvocationHandlerBase.METHOD_HASH_CODE))
       {
-         return new Integer(Proxy.getInvocationHandler(proxy).hashCode());
+         return this.invokeHashCode(proxy);
       }
 
       // If no eligible methods were invoked
@@ -160,6 +158,14 @@
     */
    protected abstract boolean invokeEquals(Object proxy, Object argument);
 
+   /**
+    * Handles invocation of "hashCode()" upon the proxy
+    * 
+    * @param proxy
+    * @return
+    */
+   protected abstract int invokeHashCode(Object proxy);
+
    // ------------------------------------------------------------------------------||
    // Accessors / Mutators ---------------------------------------------------------||
    // ------------------------------------------------------------------------------||

Modified: projects/ejb3/trunk/proxy/src/main/java/org/jboss/ejb3/proxy/handler/session/stateful/StatefulProxyInvocationHandler.java
===================================================================
--- projects/ejb3/trunk/proxy/src/main/java/org/jboss/ejb3/proxy/handler/session/stateful/StatefulProxyInvocationHandler.java	2008-05-24 17:49:50 UTC (rev 73652)
+++ projects/ejb3/trunk/proxy/src/main/java/org/jboss/ejb3/proxy/handler/session/stateful/StatefulProxyInvocationHandler.java	2008-05-24 18:00:05 UTC (rev 73653)
@@ -175,6 +175,20 @@
    @Override
    protected boolean invokeEquals(Object proxy, Object argument)
    {
+      /*
+       * EJB 3.0 Core Specification 3.4.5.1: 
+       * 
+       * A stateful session object has a unique identity that is assigned by the 
+       * container at the time the object is created. A client of the stateful 
+       * session bean business interface can determine if two business interface 
+       * references refer to the same session object by use of the equals method.
+       * 
+       * All stateful session bean references to the same business interface for 
+       * the same stateful session bean instance will be equal. Stateful session 
+       * bean references to different interface types or to different session bean 
+       * instances will not have the same identity.
+       */
+
       // If these objects are not of the same type
       if (!argument.getClass().equals(proxy.getClass()))
       {
@@ -186,16 +200,28 @@
          return false;
       }
 
-      // Ensure InvocationHandler is of expected type
-      InvocationHandler handler = Proxy.getInvocationHandler(proxy);
-      assert handler instanceof StatefulSessionProxy : InvocationHandler.class.getSimpleName() + " " + handler
-            + " must be of type " + StatefulSessionProxy.class.getName() + " but instead was: "
-            + handler.getClass().getInterfaces();
+      // Get Invocation Handlers
+      InvocationHandler proxyHandler = this.getInvocationHandler(proxy);
+      InvocationHandler argumentHandler = Proxy.getInvocationHandler(argument);
 
+      // If argument handler is not SLSB Handler
+      if (!(argumentHandler instanceof StatefulProxyInvocationHandler))
+      {
+         return false;
+      }
+
       // Cast
-      StatefulSessionProxy sHandler = (StatefulSessionProxy) handler;
-      StatefulSessionProxy sArgument = (StatefulSessionProxy) Proxy.getInvocationHandler(argument);
+      StatefulProxyInvocationHandler sHandler = (StatefulProxyInvocationHandler) proxyHandler;
+      StatefulProxyInvocationHandler sArgument = (StatefulProxyInvocationHandler) argumentHandler;
 
+      // Ensure target containers are equal
+      String proxyContainerName = sHandler.getContainerName();
+      assert proxyContainerName != null : "Container Name for " + sHandler + " was not set and is required";
+      if (!proxyContainerName.equals(sArgument.getContainerName()))
+      {
+         return false;
+      }
+
       // Equal if Session IDs are equal
       Object sessionId = sHandler.getSessionId();
       assert sessionId != null : "Required Session ID is not present in " + proxy;
@@ -207,7 +233,38 @@
       return equal;
    }
 
+   /**
+    * Handles invocation of "hashCode()" upon the proxy
+    * 
+    * @param proxy
+    * @return
+    */
+   protected int invokeHashCode(Object proxy)
+   {
+      // Get the InvocationHandler
+      StatefulProxyInvocationHandler handler = this.getInvocationHandler(proxy);
+
+      // Generate unique String by value according to rules in "invokeEquals"; 
+      // Destination Container, Session ID, and Business Interface
+      String unique = handler.getContainerName() + handler.getBusinessInterfaceType() + handler.getSessionId();
+
+      // Hash the String
+      return unique.hashCode();
+   }
+
    // ------------------------------------------------------------------------------||
+   // Internal Helper Methods ------------------------------------------------------||
+   // ------------------------------------------------------------------------------||
+
+   protected StatefulProxyInvocationHandler getInvocationHandler(Object proxy)
+   {
+      InvocationHandler handler = Proxy.getInvocationHandler(proxy);
+      assert handler instanceof StatefulProxyInvocationHandler : "Expected " + InvocationHandler.class.getSimpleName()
+            + " of type " + StatefulProxyInvocationHandler.class.getName() + ", but instead was " + handler;
+      return (StatefulProxyInvocationHandler) handler;
+   }
+
+   // ------------------------------------------------------------------------------||
    // TO BE IMPLEMENTED ------------------------------------------------------------||
    // ------------------------------------------------------------------------------||
 

Modified: projects/ejb3/trunk/proxy/src/main/java/org/jboss/ejb3/proxy/handler/session/stateless/StatelessProxyInvocationHandler.java
===================================================================
--- projects/ejb3/trunk/proxy/src/main/java/org/jboss/ejb3/proxy/handler/session/stateless/StatelessProxyInvocationHandler.java	2008-05-24 17:49:50 UTC (rev 73652)
+++ projects/ejb3/trunk/proxy/src/main/java/org/jboss/ejb3/proxy/handler/session/stateless/StatelessProxyInvocationHandler.java	2008-05-24 18:00:05 UTC (rev 73653)
@@ -23,6 +23,7 @@
 
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -136,6 +137,20 @@
     */
    protected boolean invokeEquals(Object proxy, Object argument)
    {
+      /*
+       * EJB 3.0 Specification 3.4.5.2:
+       * 
+       * All business object references of the same interface type for the same 
+       * stateless session bean have the same object identity, which is 
+       * assigned by the container.
+       *
+       * The equals method always returns true when used to compare references 
+       * to the same business interface type of the same session bean.
+       * 
+       * Session bean references to either different business interface types
+       * or different session beans will not be equal."
+       */
+
       // If these are not of the same type
       if (!proxy.getClass().equals(argument.getClass()))
       {
@@ -143,21 +158,87 @@
          return false;
       }
 
-      //TODO
+      // If the argument is not a proxy
+      if (!Proxy.isProxyClass(argument.getClass()))
+      {
+         return false;
+      }
 
-      /*
-       * EJB3 3.4.5.2: "Session bean references to either different business interface types
-       * or different session beans will not be equal."
-       * 
-       * See if we must test these conditions as well for Complicance
-       */
+      // Get the InvocationHandlers
+      InvocationHandler proxyHandler = this.getInvocationHandler(proxy);
+      InvocationHandler argumentHandler = Proxy.getInvocationHandler(argument);
 
-      // Same type, SLSB, so return true
+      // If argument handler is not SLSB Handler
+      if (!(argumentHandler instanceof StatelessProxyInvocationHandler))
+      {
+         return false;
+      }
+
+      // Cast
+      StatelessProxyInvocationHandler proxySHandler = (StatelessProxyInvocationHandler) proxyHandler;
+      StatelessProxyInvocationHandler argumentSHandler = (StatelessProxyInvocationHandler) argumentHandler;
+
+      // Ensure target containers are equal
+      String proxyContainerName = proxySHandler.getContainerName();
+      assert proxyContainerName != null : "Container Name for " + proxySHandler + " was not set and is required";
+      if (!proxyContainerName.equals(argumentSHandler.getContainerName()))
+      {
+         return false;
+      }
+
+      // Obtain target business interfaces
+      String proxyBusinessInterface = proxySHandler.getBusinessInterfaceType();
+      String argumentBusinessInterface = argumentSHandler.getBusinessInterfaceType();
+
+      // If no business interface is specified for the proxy, but is for the argument
+      if (proxyBusinessInterface == null && argumentBusinessInterface != null)
+      {
+         return false;
+      }
+
+      // If the business interface of the proxy does not match that of the argument
+      if (proxyBusinessInterface != null && !proxyBusinessInterface.equals(argumentBusinessInterface))
+      {
+         return false;
+      }
+
+      // All conditions passed, so true
       return true;
 
    }
 
+   /**
+    * Handles invocation of "hashCode()" upon the proxy
+    * 
+    * @param proxy
+    * @return
+    */
+   protected int invokeHashCode(Object proxy)
+   {
+      // Get the InvocationHandler
+      StatelessProxyInvocationHandler handler = this.getInvocationHandler(proxy);
+
+      // Generate unique String by value according to rules in "invokeEquals"; 
+      // Destination Container and Business Interface
+      String unique = handler.getContainerName() + handler.getBusinessInterfaceType();
+
+      // Hash the String
+      return unique.hashCode();
+   }
+
    // ------------------------------------------------------------------------------||
+   // Internal Helper Methods ------------------------------------------------------||
+   // ------------------------------------------------------------------------------||
+
+   protected StatelessProxyInvocationHandler getInvocationHandler(Object proxy)
+   {
+      InvocationHandler handler = Proxy.getInvocationHandler(proxy);
+      assert handler instanceof StatelessProxyInvocationHandler : "Expected " + InvocationHandler.class.getSimpleName()
+            + " of type " + StatelessProxyInvocationHandler.class.getName() + ", but instead was " + handler;
+      return (StatelessProxyInvocationHandler) handler;
+   }
+
+   // ------------------------------------------------------------------------------||
    // TO BE IMPLEMENTED ------------------------------------------------------------||
    // ------------------------------------------------------------------------------||
 

Modified: projects/ejb3/trunk/proxy/src/test/java/org/jboss/ejb3/test/proxy/spec_3_4_5/ProxyEqualityTestCaseBase.java
===================================================================
--- projects/ejb3/trunk/proxy/src/test/java/org/jboss/ejb3/test/proxy/spec_3_4_5/ProxyEqualityTestCaseBase.java	2008-05-24 17:49:50 UTC (rev 73652)
+++ projects/ejb3/trunk/proxy/src/test/java/org/jboss/ejb3/test/proxy/spec_3_4_5/ProxyEqualityTestCaseBase.java	2008-05-24 18:00:05 UTC (rev 73653)
@@ -21,10 +21,13 @@
  */
 package org.jboss.ejb3.test.proxy.spec_3_4_5;
 
+import java.lang.reflect.Proxy;
+
 import junit.framework.TestCase;
 
 import org.jboss.ejb3.proxy.factory.session.SessionProxyFactory;
 import org.jboss.ejb3.proxy.hack.Hack;
+import org.jboss.ejb3.proxy.handler.ProxyInvocationHandler;
 import org.jboss.ejb3.test.proxy.common.EmbeddedTestMcBootstrap;
 import org.jboss.ejb3.test.proxy.common.container.SessionContainer;
 import org.jboss.logging.Logger;
@@ -75,6 +78,9 @@
 
       // Create Proxy
       Object proxy = this.createProxyDefault(factory);
+      
+      // Manually set the target container
+      this.setContainerNameOnProxy(proxy);
 
       // Ensure equal to itself by value
       TestCase
@@ -96,6 +102,9 @@
 
       // Create Proxy
       Object proxy = this.createProxyDefault(factory);
+      
+      // Manually set the target container
+      this.setContainerNameOnProxy(proxy);
 
       // Ensure equal to itself by value
       TestCase
@@ -141,6 +150,18 @@
       return factory.createProxyDefault();
    }
 
+   /**
+    * Sets the Container Name on the specified proxy
+    * 
+    * @param proxy
+    */
+   protected void setContainerNameOnProxy(Object proxy)
+   {
+      // Get the InvocationHander for the Proxy
+      ProxyInvocationHandler handler = (ProxyInvocationHandler) Proxy.getInvocationHandler(proxy);
+      handler.setContainerName(ProxyEqualityTestCaseBase.getContainerName());
+   }
+
    // --------------------------------------------------------------------------------||
    // Specifications -----------------------------------------------------------------||
    // --------------------------------------------------------------------------------||

Modified: projects/ejb3/trunk/proxy/src/test/java/org/jboss/ejb3/test/proxy/spec_3_4_5/unit/SfsbProxyEqualityTestCase.java
===================================================================
--- projects/ejb3/trunk/proxy/src/test/java/org/jboss/ejb3/test/proxy/spec_3_4_5/unit/SfsbProxyEqualityTestCase.java	2008-05-24 17:49:50 UTC (rev 73652)
+++ projects/ejb3/trunk/proxy/src/test/java/org/jboss/ejb3/test/proxy/spec_3_4_5/unit/SfsbProxyEqualityTestCase.java	2008-05-24 18:00:05 UTC (rev 73653)
@@ -79,6 +79,10 @@
       this.setSessionIdOnProxy(proxy1, new Integer(1));
       this.setSessionIdOnProxy(proxy2, new Integer(2));
 
+      // Manually set the target container
+      this.setContainerNameOnProxy(proxy1);
+      this.setContainerNameOnProxy(proxy2);
+
       // Ensure they're not equal to one another
       TestCase
             .assertTrue(
@@ -107,6 +111,10 @@
       this.setSessionIdOnProxy(proxy1, new Integer(1));
       this.setSessionIdOnProxy(proxy2, new Integer(2));
 
+      // Manually set the target container
+      this.setContainerNameOnProxy(proxy1);
+      this.setContainerNameOnProxy(proxy2);
+
       // Ensure they're not equal to one another
       TestCase
             .assertTrue(
@@ -122,6 +130,9 @@
 
    /**
     * Sets the specified ID on the specified proxy
+    * 
+    * @param proxy
+    * @param id
     */
    private void setSessionIdOnProxy(Object proxy, Object id)
    {

Modified: projects/ejb3/trunk/proxy/src/test/java/org/jboss/ejb3/test/proxy/spec_3_4_5/unit/SlsbProxyEqualityTestCase.java
===================================================================
--- projects/ejb3/trunk/proxy/src/test/java/org/jboss/ejb3/test/proxy/spec_3_4_5/unit/SlsbProxyEqualityTestCase.java	2008-05-24 17:49:50 UTC (rev 73652)
+++ projects/ejb3/trunk/proxy/src/test/java/org/jboss/ejb3/test/proxy/spec_3_4_5/unit/SlsbProxyEqualityTestCase.java	2008-05-24 18:00:05 UTC (rev 73653)
@@ -70,6 +70,10 @@
       // Create 2 Proxies
       Object proxy1 = factory.createProxyDefault();
       Object proxy2 = factory.createProxyDefault();
+      
+      // Manually set the target container
+      this.setContainerNameOnProxy(proxy1);
+      this.setContainerNameOnProxy(proxy2);
 
       // Ensure they're equal to one another
       TestCase
@@ -94,6 +98,10 @@
       // Create 2 Proxies
       Object proxy1 = factory.createProxyDefault();
       Object proxy2 = factory.createProxyDefault();
+      
+      // Manually set the target container
+      this.setContainerNameOnProxy(proxy1);
+      this.setContainerNameOnProxy(proxy2);
 
       // Ensure they're equal to one another
       TestCase




More information about the jboss-cvs-commits mailing list