[jboss-cvs] JBossAS SVN: r68807 - in projects/ejb3/trunk/interceptors/src/main/java/org/jboss/ejb3/interceptors: direct and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Jan 10 04:28:34 EST 2008


Author: wolfc
Date: 2008-01-10 04:28:34 -0500 (Thu, 10 Jan 2008)
New Revision: 68807

Modified:
   projects/ejb3/trunk/interceptors/src/main/java/org/jboss/ejb3/interceptors/container/AbstractContainer.java
   projects/ejb3/trunk/interceptors/src/main/java/org/jboss/ejb3/interceptors/direct/AbstractDirectContainer.java
   projects/ejb3/trunk/interceptors/src/main/java/org/jboss/ejb3/interceptors/proxy/ProxyContainer.java
Log:
Merged construction of instances

Modified: projects/ejb3/trunk/interceptors/src/main/java/org/jboss/ejb3/interceptors/container/AbstractContainer.java
===================================================================
--- projects/ejb3/trunk/interceptors/src/main/java/org/jboss/ejb3/interceptors/container/AbstractContainer.java	2008-01-10 09:27:21 UTC (rev 68806)
+++ projects/ejb3/trunk/interceptors/src/main/java/org/jboss/ejb3/interceptors/container/AbstractContainer.java	2008-01-10 09:28:34 UTC (rev 68807)
@@ -21,6 +21,7 @@
  */
 package org.jboss.ejb3.interceptors.container;
 
+import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 
 import org.jboss.aop.AspectManager;
@@ -28,6 +29,8 @@
 import org.jboss.aop.Domain;
 import org.jboss.aop.DomainDefinition;
 import org.jboss.aop.MethodInfo;
+import org.jboss.aop.advice.Interceptor;
+import org.jboss.aop.joinpoint.ConstructionInvocation;
 import org.jboss.aop.joinpoint.MethodInvocation;
 import org.jboss.aop.util.MethodHashing;
 import org.jboss.ejb3.interceptors.annotation.AnnotationAdvisor;
@@ -36,7 +39,10 @@
 import org.jboss.logging.Logger;
 
 /**
- * Comment
+ * The base of all containers. Provides functions to allow for object
+ * construction and invocation with interception.
+ * 
+ * Note that it's up to the actual implementation to expose any methods.
  *
  * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
  * @version $Revision: $
@@ -57,16 +63,41 @@
       
    }
    
-   public AbstractContainer(String name, Domain domain, Class<? extends T> beanClass)
+   protected AbstractContainer(String name, Domain domain, Class<? extends T> beanClass)
    {
       initializeAdvisor(name, domain, beanClass);
    }
    
-   public AbstractContainer(String name, String domainName, Class<? extends T> beanClass)
+   protected AbstractContainer(String name, String domainName, Class<? extends T> beanClass)
    {
       this(name, getDomain(domainName), beanClass);
    }
    
+   protected T construct(Constructor<? extends T> constructor, Object ... initargs)
+   {
+      int idx = advisor.getConstructorIndex(constructor);
+      assert idx != -1 : "can't find constructor in the advisor";
+      try
+      {
+         T targetObject = (T) advisor.invokeNew(initargs, idx);
+         
+         Interceptor interceptors[] = advisor.getConstructionInfos()[idx].getInterceptors();
+         ConstructionInvocation invocation = new ConstructionInvocation(interceptors, constructor, initargs);
+         invocation.setAdvisor(advisor);
+         invocation.setTargetObject(targetObject);
+         invocation.invokeNext();
+         
+         return targetObject;
+      }
+      catch(Throwable t)
+      {
+         // TODO: disect
+         if(t instanceof RuntimeException)
+            throw (RuntimeException) t;
+         throw new RuntimeException(t);
+      }
+   }
+   
    /**
     * Finalize construction of the abstract container by setting the advisor.
     * 
@@ -94,6 +125,12 @@
       return advisor;
    }
    
+   @SuppressWarnings("unchecked")
+   protected Class<? extends T> getBeanClass()
+   {
+      return getAdvisor().getClazz();
+   }
+   
    /*
     * TODO: this should not be here, it's an AspectManager helper function.
     */
@@ -116,7 +153,7 @@
     * @return           return value of the method
     * @throws Throwable if anything goes wrong
     */
-   public Object invoke(Object target, Method method, Object arguments[]) throws Throwable
+   protected Object invoke(T target, Method method, Object arguments[]) throws Throwable
    {
       long methodHash = MethodHashing.calculateHash(method);
       MethodInfo info = getAdvisor().getMethodInfo(methodHash);
@@ -141,7 +178,7 @@
     * @throws Throwable if anything goes wrong
     */
    @SuppressWarnings("unchecked")
-   public <R> R invoke(Object target, String methodName, Object ... args) throws Throwable
+   protected <R> R invoke(T target, String methodName, Object ... args) throws Throwable
    {
       Method method = ClassHelper.getMethod(target.getClass(), methodName);
       return (R) invoke(target, method, args);

Modified: projects/ejb3/trunk/interceptors/src/main/java/org/jboss/ejb3/interceptors/direct/AbstractDirectContainer.java
===================================================================
--- projects/ejb3/trunk/interceptors/src/main/java/org/jboss/ejb3/interceptors/direct/AbstractDirectContainer.java	2008-01-10 09:27:21 UTC (rev 68806)
+++ projects/ejb3/trunk/interceptors/src/main/java/org/jboss/ejb3/interceptors/direct/AbstractDirectContainer.java	2008-01-10 09:28:34 UTC (rev 68807)
@@ -27,11 +27,10 @@
 import org.jboss.aop.ClassAdvisor;
 import org.jboss.aop.Domain;
 import org.jboss.aop.MethodInfo;
-import org.jboss.aop.advice.Interceptor;
-import org.jboss.aop.joinpoint.ConstructionInvocation;
 import org.jboss.aop.joinpoint.MethodInvocation;
 import org.jboss.aop.util.MethodHashing;
 import org.jboss.ejb3.interceptors.container.AbstractContainer;
+import org.jboss.ejb3.interceptors.lang.ClassHelper;
 import org.jboss.logging.Logger;
 
 /**
@@ -64,7 +63,7 @@
    
    public T construct() throws SecurityException, NoSuchMethodException
    {
-      return construct(null, null);
+      return construct((Object[]) null, null);
    }
    
    @SuppressWarnings("unchecked")
@@ -72,41 +71,53 @@
    {
       ClassAdvisor advisor = getAdvisor();
       Constructor<T> constructor = advisor.getClazz().getConstructor(parameterTypes);
-      int idx = advisor.getConstructorIndex(constructor);
-      assert idx != -1 : "can't find constructor in the advisor";
-      try
-      {
-         T targetObject = (T) advisor.invokeNew(initargs, idx);
-         
-         Interceptor interceptors[] = advisor.getConstructionInfos()[idx].getInterceptors();
-         ConstructionInvocation invocation = new ConstructionInvocation(interceptors, constructor, initargs);
-         invocation.setAdvisor(advisor);
-         invocation.setTargetObject(targetObject);
-         invocation.invokeNext();
-         
-         if(targetObject instanceof IndirectContainer)
-            ((IndirectContainer<T, C>) targetObject).setDirectContainer(this);
-         
-         return targetObject;
-      }
-      catch(Throwable t)
-      {
-         // TODO: disect
-         if(t instanceof RuntimeException)
-            throw (RuntimeException) t;
-         throw new RuntimeException(t);
-      }
+      T targetObject = construct(constructor, initargs);
+      
+      if(targetObject instanceof IndirectContainer)
+         ((IndirectContainer<T, C>) targetObject).setDirectContainer((C) this);
+      
+      return targetObject;
    }
    
    /**
     * Do not call, for use in indirect container implementations.
     * @return
     */
-   public Class<?> getBeanClass()
+   public Class<? extends T> getBeanClass()
    {
-      return getAdvisor().getClazz();
+      return super.getBeanClass();
    }
    
+   // expose the invoke method
+   @Override
+   public Object invoke(T target, Method method, Object[] arguments) throws Throwable
+   {
+      return super.invoke(target, method, arguments);
+   }
+   
+   // the compiler won't allow me to expose the super method
+   /**
+    * A convenient, but unchecked and slow method to call a method upon a target.
+    * 
+    * (Slow method)
+    * 
+    * @param <R>        the return type
+    * @param target     the target to invoke upon
+    * @param methodName the method name to invoke
+    * @param args       the arguments to the method
+    * @return           the return value
+    * @throws Throwable if anything goes wrong
+    */
+   @SuppressWarnings("unchecked")
+   public <R> R invoke(T target, String methodName, Object ... args) throws Throwable
+   {
+      Method method = ClassHelper.getMethod(target.getClass(), methodName);
+      return (R) invoke(target, method, args);
+   }
+   
+   /**
+    * Do not call, for use in indirect container implementations.
+    */
    public Object invokeIndirect(Object target, Method method, Object arguments[]) throws Throwable
    {
       long methodHash = MethodHashing.calculateHash(method);

Modified: projects/ejb3/trunk/interceptors/src/main/java/org/jboss/ejb3/interceptors/proxy/ProxyContainer.java
===================================================================
--- projects/ejb3/trunk/interceptors/src/main/java/org/jboss/ejb3/interceptors/proxy/ProxyContainer.java	2008-01-10 09:27:21 UTC (rev 68806)
+++ projects/ejb3/trunk/interceptors/src/main/java/org/jboss/ejb3/interceptors/proxy/ProxyContainer.java	2008-01-10 09:28:34 UTC (rev 68807)
@@ -25,13 +25,8 @@
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
-import java.util.Arrays;
 
-import org.jboss.aop.ClassAdvisor;
-import org.jboss.aop.ConstructionInfo;
 import org.jboss.aop.Domain;
-import org.jboss.aop.advice.Interceptor;
-import org.jboss.aop.joinpoint.ConstructionInvocation;
 import org.jboss.ejb3.interceptors.container.AbstractContainer;
 import org.jboss.logging.Logger;
 
@@ -50,9 +45,9 @@
    
    private class ProxyInvocationHandler implements InvocationHandler
    {
-      private Object target;
+      private T target;
       
-      public ProxyInvocationHandler(Object target)
+      public ProxyInvocationHandler(T target)
       {
          assert target != null : "target is null";
          
@@ -78,26 +73,9 @@
    @SuppressWarnings("unchecked")
    public <I> I constructProxy(Class<?> interfaces[]) throws Throwable
    {
-      // assert interfaces contains I
-      Object args[] = null;
-      int idx = 0; // TODO: find default constructor
-      ClassAdvisor advisor = getAdvisor();
-      // ClassAdvisor
-      ConstructionInfo constructionInfo = advisor.getConstructionInfos()[idx];
-      Interceptor[] cInterceptors = constructionInfo.getInterceptors();
-      if (cInterceptors == null) cInterceptors = new Interceptor[0];
-      log.debug("constructor interceptors " + Arrays.toString(cInterceptors));
-      Constructor<?> constructor = advisor.getConstructors()[idx];
-      ConstructionInvocation invocation = new ConstructionInvocation(cInterceptors, constructor);
+      Constructor<? extends T> constructor = getBeanClass().getConstructor();
+      T instance = construct(constructor);
       
-      invocation.setAdvisor(advisor);
-      invocation.setArguments(args);
-      // First we create the instance
-      Object instance = constructor.newInstance();
-      invocation.setTargetObject(instance);
-      // then we do (construction) interception
-      invocation.invokeNext();
-      
       ClassLoader loader = Thread.currentThread().getContextClassLoader();
       //Class<?> interfaces[] = { intf };
       Object proxy = Proxy.newProxyInstance(loader, interfaces, new ProxyInvocationHandler(instance));




More information about the jboss-cvs-commits mailing list