hlship      2003/09/11 07:24:30

  Modified:    hivemind/src/java/org/apache/commons/hivemind/impl
                        InterceptorStackImpl.java
               hivemind/src/java/org/apache/commons/hivemind
                        InterceptorStack.java
               hivemind/src/java/org/apache/commons/hivemind/service/impl
                        AbstractServiceInterceptorFactory.java
                        LoggingInterceptorFactory.java
  Log:
  Make interceptors invoke methods on the next innermost implementation using the 
actual type, not the service interface, for efficiency.
  
  Revision  Changes    Path
  1.8       +1 -10     
jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/impl/InterceptorStackImpl.java
  
  Index: InterceptorStackImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/impl/InterceptorStackImpl.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- InterceptorStackImpl.java 10 Sep 2003 20:50:24 -0000      1.7
  +++ InterceptorStackImpl.java 11 Sep 2003 14:24:30 -0000      1.8
  @@ -83,15 +83,12 @@
       private ServiceInterceptorContribution _contribution;
       private ServiceExtensionPoint _sep;
       private Class _interfaceClass;
  -    private Object _root;
       private Object _top;
       private Registry _registry;
   
       public InterceptorStackImpl(ServiceExtensionPoint sep, Object root)
       {
           _sep = sep;
  -        _root = root;
  -
           _top = root;
           _interfaceClass = sep.getServiceInterface();
           _registry = sep.getModule().getRegistry();
  @@ -102,7 +99,6 @@
           ToStringBuilder builder = new ToStringBuilder(this);
           builder.append("contribution", _contribution);
           builder.append("interfaceClass", _interfaceClass);
  -        builder.append("root", _root);
           builder.append("top", _top);
   
           return builder.toString();
  @@ -121,11 +117,6 @@
       public Class getServiceInterface()
       {
           return _interfaceClass;
  -    }
  -
  -    public Object getRoot()
  -    {
  -        return _root;
       }
   
       public Object peek()
  
  
  
  1.5       +1 -7      
jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/InterceptorStack.java
  
  Index: InterceptorStack.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/InterceptorStack.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- InterceptorStack.java     30 Aug 2003 14:29:52 -0000      1.4
  +++ InterceptorStack.java     11 Sep 2003 14:24:30 -0000      1.5
  @@ -79,12 +79,6 @@
        public Class getServiceInterface();
        
        /**
  -      * Returns the root object of the stack, the core
  -      * service implementation instance.
  -      */
  -     public Object getRoot();
  -     
  -     /**
         * Returns the current top object on the stack.
         */
        public Object peek();
  
  
  
  1.16      +35 -42    
jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/AbstractServiceInterceptorFactory.java
  
  Index: AbstractServiceInterceptorFactory.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/AbstractServiceInterceptorFactory.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- AbstractServiceInterceptorFactory.java    5 Sep 2003 22:32:38 -0000       1.15
  +++ AbstractServiceInterceptorFactory.java    11 Sep 2003 14:24:30 -0000      1.16
  @@ -99,13 +99,13 @@
           _cachedClasses = Collections.synchronizedMap(new HashMap());
       }
   
  -     /**
  -      * Creates the interceptor. Expects that the parameters list is empty.
  -      * The class that is created is cached; if an interceptor is requested
  -      * for the same extension point, then the previously constructed class
  -      * is reused (this can happen with the threaded service model, for example,
  -      * when a thread-local service implementation is created for different 
threads).
  -      */
  +    /**
  +     * Creates the interceptor. Expects that the parameters list is empty.
  +     * The class that is created is cached; if an interceptor is requested
  +     * for the same extension point, then the previously constructed class
  +     * is reused (this can happen with the threaded service model, for example,
  +     * when a thread-local service implementation is created for different threads).
  +     */
       public void createInterceptor(
           InterceptorStack stack,
           Module contributingModule,
  @@ -116,12 +116,7 @@
   
           try
           {
  -            Object interceptor =
  -                instantiateInterceptor(
  -                    stack,
  -                    serviceInterfaceClass,
  -                    stack.peek(),
  -                    interceptorClass);
  +            Object interceptor = instantiateInterceptor(stack, interceptorClass);
   
               stack.push(interceptor);
           }
  @@ -166,7 +161,7 @@
   
           classFab.addInterface(serviceInterfaceClass);
   
  -        createInfrastructure(serviceInterfaceClass, classFab);
  +        createInfrastructure(stack, classFab);
   
           addServiceMethods(stack, classFab);
   
  @@ -186,27 +181,18 @@
       /**
        * Invoked in subclasses to create any infrastructure. 
        * <p>This implementation adds a field, <code>_inner</code> whose
  -     * type matches the service interface, and
  -     * a constructor to set the field.
  -     * 
  +     * type is the <em>actual</em> type of the 
  +     * [EMAIL PROTECTED] InterceptorStack#peek() top object on the stack} (which, of
  +     * course, implements the service interface).  A constructor is created
  +     * to set the field. 
        */
  -    protected void createInfrastructure(Class serviceInterfaceClass, ClassFab 
classFab)
  +    protected void createInfrastructure(InterceptorStack stack, ClassFab classFab)
       {
  -        classFab.addField("_inner", serviceInterfaceClass);
  -        classFab.addConstructor(new Class[] { serviceInterfaceClass }, null, 
"_inner = $1;");
  -    }
  +        Class topClass = stack.peek().getClass();
   
  -    /**
  -     * Invoked for each method in the service interface to allow the factory to
  -     * construct the corresponding method in the interceptor.
  -     */
  -
  -    protected abstract void addServiceMethodImplementation(
  -        ClassFab classFab,
  -        String methodName,
  -        Class returnType,
  -        Class[] parameterTypes,
  -        Class[] exceptions);
  +        classFab.addField("_inner", topClass);
  +        classFab.addConstructor(new Class[] { topClass }, null, "_inner = $1;");
  +    }
   
       /**
        * Used to instantiate the interceptor.  This implementation passes the top 
object
  @@ -215,26 +201,33 @@
        * will need to override this method as well.
        * 
        * @param stack the interceptor stack on which the returned interceptor will be 
placed.
  -     * @param serviceInterfaceClass the class for the interface
  -     * @param stackTop the top object on the stack (which may be the core 
implementation, or
  -     * another interceptor)
        * @param interceptorClass the generated class for the interceptor.
        * 
        * @throws Exception if there is an error getting or invoking the constructor
        * @see #createInfrastructure(Class, ClassFab)
        */
  -    protected Object instantiateInterceptor(
  -        InterceptorStack stack,
  -        Class serviceInterfaceClass,
  -        Object stackTop,
  -        Class interceptorClass)
  +    protected Object instantiateInterceptor(InterceptorStack stack, Class 
interceptorClass)
           throws Exception
       {
  -        Constructor c = interceptorClass.getConstructor(new Class[] { 
serviceInterfaceClass });
  +        Object stackTop = stack.peek();
  +
  +        Constructor c = interceptorClass.getConstructor(new Class[] { 
stackTop.getClass()});
   
           return c.newInstance(new Object[] { stackTop });
   
       }
  +
  +    /**
  +     * Invoked for each method in the service interface to allow the factory to
  +     * construct the corresponding method in the interceptor.
  +     */
  +
  +    protected abstract void addServiceMethodImplementation(
  +        ClassFab classFab,
  +        String methodName,
  +        Class returnType,
  +        Class[] parameterTypes,
  +        Class[] exceptions);
   
       private void addServiceMethods(InterceptorStack stack, ClassFab fab)
       {
  
  
  
  1.10      +22 -18    
jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/LoggingInterceptorFactory.java
  
  Index: LoggingInterceptorFactory.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/hivemind/src/java/org/apache/commons/hivemind/service/impl/LoggingInterceptorFactory.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- LoggingInterceptorFactory.java    30 Aug 2003 14:29:53 -0000      1.9
  +++ LoggingInterceptorFactory.java    11 Sep 2003 14:24:30 -0000      1.10
  @@ -85,16 +85,34 @@
    */
   public class LoggingInterceptorFactory extends AbstractServiceInterceptorFactory
   {
  -    protected void createInfrastructure(Class serviceInterfaceClass, ClassFab 
classFab)
  +    protected void createInfrastructure(InterceptorStack stack, ClassFab classFab)
       {
  -        classFab.addField("_inner", serviceInterfaceClass);
  +     Class topClass = stack.peek().getClass();
  +     
  +        classFab.addField("_inner", topClass);
   
           classFab.addConstructor(
  -            new Class[] { Log.class, serviceInterfaceClass },
  +            new Class[] { Log.class, topClass },
               null,
               "{ super($1); _inner = $2; }");
       }
   
  +     protected Object instantiateInterceptor(
  +             InterceptorStack stack,
  +             Class interceptorClass)
  +             throws Exception
  +     {
  +             Object stackTop = stack.peek();
  +             Class topClass = stackTop.getClass();
  +     
  +             Log log = 
LogFactory.getLog(stack.getServiceExtensionPoint().getExtensionPointId());
  +
  +             Constructor c =
  +                     interceptorClass.getConstructor(new Class[] { Log.class, 
topClass });
  +
  +             return c.newInstance(new Object[] { log, stackTop });
  +     }
  +     
       protected void addServiceMethodImplementation(
           ClassFab classFab,
           String methodName,
  @@ -180,19 +198,5 @@
           return AbstractLoggingInterceptor.class;
       }
   
  -    protected Object instantiateInterceptor(
  -        InterceptorStack stack,
  -        Class serviceInterfaceClass,
  -        Object stackTop,
  -        Class interceptorClass)
  -        throws Exception
  -    {
  -        Log log = 
LogFactory.getLog(stack.getServiceExtensionPoint().getExtensionPointId());
  -
  -        Constructor c =
  -            interceptorClass.getConstructor(new Class[] { Log.class, 
serviceInterfaceClass });
  -
  -        return c.newInstance(new Object[] { log, stackTop });
  -    }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to