[jboss-cvs] jboss-seam/src/main/org/jboss/seam/interceptors ...

Gavin King gavin.king at jboss.com
Tue Sep 26 23:53:49 EDT 2006


  User: gavin   
  Date: 06/09/26 23:53:49

  Modified:    src/main/org/jboss/seam/interceptors    Interceptor.java
                        ManagedEntityIdentityInterceptor.java
                        SeamInvocationContext.java
  Log:
  stateful seam interceptors
  page parameters
  crud framework prototype
  bugfixes to PC passivation stuff
  
  Revision  Changes    Path
  1.15      +79 -28    jboss-seam/src/main/org/jboss/seam/interceptors/Interceptor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Interceptor.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/interceptors/Interceptor.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -b -r1.14 -r1.15
  --- Interceptor.java	26 Sep 2006 00:29:53 -0000	1.14
  +++ Interceptor.java	27 Sep 2006 03:53:49 -0000	1.15
  @@ -1,4 +1,4 @@
  -//$Id: Interceptor.java,v 1.14 2006/09/26 00:29:53 gavin Exp $
  +//$Id: Interceptor.java,v 1.15 2006/09/27 03:53:49 gavin Exp $
   package org.jboss.seam.interceptors;
   
   import java.lang.annotation.Annotation;
  @@ -23,18 +23,57 @@
    */
   public final class Interceptor extends Reflections
   {
  -   private final Object userInterceptor;
  +   private Class<?> userInterceptorClass;
  +   private Object statelessUserInterceptorInstance;
      private Method aroundInvokeMethod;
      private Method postConstructMethod;
      private Method preDestroyMethod;
      private Method postActivateMethod;
      private Method prePassivateMethod;
  +   private Method componentInjectorMethod;
  +   private Method annotationInjectorMethod;
      private InterceptorType type;
  +   private Annotation annotation;
  +   private Component component;
      
  -   public Object getUserInterceptor()
  +   private boolean isStateless()
      {
  +      return userInterceptorClass.isAnnotationPresent(org.jboss.seam.annotations.Interceptor.class) &&
  +            userInterceptorClass.getAnnotation(org.jboss.seam.annotations.Interceptor.class).stateless();
  +   }
  +   
  +   public Object createUserInterceptor()
  +   {
  +      if ( isStateless() )
  +      {
  +         return statelessUserInterceptorInstance;
  +      }
  +      else
  +      {
  +         try
  +         {
  +            Object userInterceptor = userInterceptorClass.newInstance();
  +            if (componentInjectorMethod!=null)
  +            {
  +               Reflections.invokeAndWrap(componentInjectorMethod, userInterceptor, component);
  +            }
  +            if (annotationInjectorMethod!=null) 
  +            {
  +               Reflections.invokeAndWrap(annotationInjectorMethod, userInterceptor, annotation);
  +            }
         return userInterceptor;
      }
  +         catch (Exception e)
  +         {
  +            throw new RuntimeException(e);
  +         }
  +      }
  +   }
  +
  +   public Class getUserInterceptorClass()
  +   {
  +      return userInterceptorClass;
  +   }
      
      public InterceptorType getType()
      {
  @@ -43,42 +82,48 @@
      
      public String toString()
      {
  -      return "Interceptor(" + userInterceptor.getClass().getName() + ")";
  +      return "Interceptor(" + userInterceptorClass.getName() + ")";
      }
      
      public Interceptor(Object interceptor, Component component)
      {
  -      userInterceptor = interceptor;
  -      init( null, component, interceptor.getClass() );
  +      userInterceptorClass = interceptor.getClass();
  +      statelessUserInterceptorInstance = interceptor;
  +      this.component = component;
  +      init();
      }
      
      public Interceptor(Annotation annotation, Component component) 
      {
         Interceptors interceptorAnnotation = annotation.annotationType()
               .getAnnotation(Interceptors.class);
  -      Class interceptorClass;
  -      try
  -      {
            Class[] classes = interceptorAnnotation.value();
            if (classes.length!=1)
            {
               //TODO: remove this silly restriction!
               throw new IllegalArgumentException("Must be exactly one interceptor when used as a meta-annotation");
            }
  -         interceptorClass = classes[0];
  -         userInterceptor = interceptorClass.newInstance();
  +      userInterceptorClass = classes[0];
  +      
  +      try
  +      {
  +         statelessUserInterceptorInstance = userInterceptorClass.newInstance();
         }
         catch (Exception e)
         {
            throw new IllegalArgumentException("could not instantiate interceptor", e);
         }
  -      init(annotation, component, interceptorClass);
  +      
  +      this.annotation = annotation;
  +      this.component = component;
  +      
  +      init();
      }
      
   
  -   private void init(Annotation annotation, Component component, Class<?> interceptorClass)
  +   private void init()
      {
  -      for (Method method : userInterceptor.getClass().getMethods())
  +      for (Method method : userInterceptorClass.getMethods())
         {
            if ( !method.isAccessible() ) method.setAccessible(true);
            if ( method.isAnnotationPresent(AroundInvoke.class) )
  @@ -106,45 +151,51 @@
            //if there is a method that takes the annotation, call it, to pass initialization info
            if ( annotation!=null && params.length==1 && params[0]==annotation.annotationType() )
            {
  -            Reflections.invokeAndWrap(method, userInterceptor, annotation);
  +            annotationInjectorMethod = method;
  +            Reflections.invokeAndWrap(method, userInterceptorClass, annotation);
            }
            //if there is a method that takes the component, call it
            if ( params.length==1 && params[0]==Component.class )
            {
  -            Reflections.invokeAndWrap(method, userInterceptor, component);
  +            componentInjectorMethod = method;
  +            Reflections.invokeAndWrap(method, statelessUserInterceptorInstance, component);
            }
         }
   
  -      type = interceptorClass.isAnnotationPresent(org.jboss.seam.annotations.Interceptor.class) ?
  -            interceptorClass.getAnnotation(org.jboss.seam.annotations.Interceptor.class).type() :
  +      type = userInterceptorClass.isAnnotationPresent(org.jboss.seam.annotations.Interceptor.class) ?
  +            userInterceptorClass.getAnnotation(org.jboss.seam.annotations.Interceptor.class).type() :
               InterceptorType.SERVER;
      }
      
  -   public Object aroundInvoke(InvocationContext invocation) throws Exception
  +   public Object aroundInvoke(InvocationContext invocation, Object userInterceptor) throws Exception
      {
         return aroundInvokeMethod==null ?
               invocation.proceed() :
               Reflections.invoke( aroundInvokeMethod, userInterceptor, invocation );
      }
  -   public Object postConstruct(InvocationContext invocation) throws Exception
  +   
  +   public Object postConstruct(InvocationContext invocation, Object userInterceptor) throws Exception
      {
         return postConstructMethod==null ?
               invocation.proceed() :
               Reflections.invoke( postConstructMethod, userInterceptor, invocation );
      }
  -   public Object preDestroy(InvocationContext invocation) throws Exception
  +   
  +   public Object preDestroy(InvocationContext invocation, Object userInterceptor) throws Exception
      {
         return preDestroyMethod==null ?
               invocation.proceed() :
               Reflections.invoke( preDestroyMethod, userInterceptor, invocation );
      }
  -   public Object prePassivate(InvocationContext invocation) throws Exception
  +   
  +   public Object prePassivate(InvocationContext invocation, Object userInterceptor) throws Exception
      {
         return prePassivateMethod==null ?
               invocation.proceed() :
               Reflections.invoke( prePassivateMethod, userInterceptor, invocation );
      }
  -   public Object postActivate(InvocationContext invocation) throws Exception
  +   
  +   public Object postActivate(InvocationContext invocation, Object userInterceptor) throws Exception
      {
         return postActivateMethod==null ?
               invocation.proceed() :
  
  
  
  1.3       +10 -8     jboss-seam/src/main/org/jboss/seam/interceptors/ManagedEntityIdentityInterceptor.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ManagedEntityIdentityInterceptor.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/interceptors/ManagedEntityIdentityInterceptor.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- ManagedEntityIdentityInterceptor.java	26 Sep 2006 14:47:58 -0000	1.2
  +++ ManagedEntityIdentityInterceptor.java	27 Sep 2006 03:53:49 -0000	1.3
  @@ -83,7 +83,7 @@
         if ( pcs!=null && pcs.size()>0 )
         {
            Object bean = ctx.getTarget();
  -         Class beanClass = Seam.getBeanClass( bean.getClass() );
  +         Class beanClass = bean.getClass();
            for (; beanClass!=Object.class; beanClass=beanClass.getSuperclass())
            {
               Field[] fields = beanClass.getDeclaredFields();
  @@ -101,7 +101,7 @@
                           for (String persistenceContextName: pcs)
                           {
                              Object persistenceContext = Component.getInstance(persistenceContextName);
  -                           boolean managed = false;
  +                           boolean managed;
                              if (persistenceContext instanceof EntityManager)
                              {
                                 EntityManager em = (EntityManager) persistenceContext;
  @@ -133,7 +133,7 @@
         if ( list.size()>0 )
         {
            Object bean = ctx.getTarget();
  -         Class beanClass = Seam.getBeanClass( bean.getClass() );
  +         Class beanClass = bean.getClass();
            for (PassivatedEntity pe: list)
            {
               Object persistenceContext = Component.getInstance( pe.getPersistenceContext() );
  @@ -164,26 +164,28 @@
         }
      }
   
  -   private static Object getId(Object bean, Class beanClass) throws Exception
  +   private static Object getId(Object bean, Class entityClass) throws Exception
      {
  -      for (; beanClass!=Object.class; beanClass=beanClass.getSuperclass() )
  +      for (Class beanClass=entityClass; beanClass!=Object.class; beanClass=beanClass.getSuperclass() )
         {
  -         for (Field field: beanClass.getFields()) //TODO: superclasses
  +         for (Field field: beanClass.getDeclaredFields()) //TODO: superclasses
            {
               if ( field.isAnnotationPresent(Id.class) )
               {
  +               if ( !field.isAccessible() ) field.setAccessible(true);
                  return Reflections.get(field, bean);
               }
            }
  -         for (Method method: beanClass.getMethods())
  +         for (Method method: beanClass.getDeclaredMethods())
            {
               if ( method.isAnnotationPresent(Id.class) )
               {
  +               if ( !method.isAccessible() ) method.setAccessible(true);
                  return Reflections.invoke(method, bean);
               }
            }
         }
  -      throw new IllegalArgumentException("no id property found for entity class: " + beanClass.getName());
  +      throw new IllegalArgumentException("no id property found for entity class: " + entityClass.getName());
      }
      
   }
  
  
  
  1.9       +12 -8     jboss-seam/src/main/org/jboss/seam/interceptors/SeamInvocationContext.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: SeamInvocationContext.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/interceptors/SeamInvocationContext.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -b -r1.8 -r1.9
  --- SeamInvocationContext.java	26 Jul 2006 23:33:39 -0000	1.8
  +++ SeamInvocationContext.java	27 Sep 2006 03:53:49 -0000	1.9
  @@ -1,4 +1,4 @@
  -//$Id: SeamInvocationContext.java,v 1.8 2006/07/26 23:33:39 gavin Exp $
  +//$Id: SeamInvocationContext.java,v 1.9 2006/09/27 03:53:49 gavin Exp $
   package org.jboss.seam.interceptors;
   
   import java.lang.reflect.Method;
  @@ -18,12 +18,14 @@
      private final EventType eventType;
      private final InvocationContext ejbInvocationContext;
      private final List<Interceptor> interceptors;
  +   private final List<Object> userInterceptors;
      int location = 0;
   
  -   public SeamInvocationContext(InvocationContext ejbInvocationContext, EventType type, List<Interceptor> interceptors)
  +   public SeamInvocationContext(InvocationContext ejbInvocationContext, EventType type, List<Object> userInterceptors, List<Interceptor> interceptors)
      {
         this.ejbInvocationContext = ejbInvocationContext;
         this.interceptors = interceptors;
  +      this.userInterceptors = userInterceptors;
         this.eventType = type;
      }
      
  @@ -55,14 +57,16 @@
         }
         else
         {
  -         Interceptor interceptor = interceptors.get(location++);
  +         Object userInterceptor = userInterceptors.get(location);
  +         Interceptor interceptor = interceptors.get(location);
  +         location++;
            switch(eventType)
            {
  -            case AROUND_INVOKE: return interceptor.aroundInvoke(this);
  -            case POST_CONSTRUCT: return interceptor.postConstruct(this);
  -            case PRE_DESTORY: return interceptor.preDestroy(this);
  -            case PRE_PASSIVATE: return interceptor.prePassivate(this);
  -            case POST_ACTIVATE: return interceptor.postActivate(this);
  +            case AROUND_INVOKE: return interceptor.aroundInvoke(this, userInterceptor);
  +            case POST_CONSTRUCT: return interceptor.postConstruct(this, userInterceptor);
  +            case PRE_DESTORY: return interceptor.preDestroy(this, userInterceptor);
  +            case PRE_PASSIVATE: return interceptor.prePassivate(this, userInterceptor);
  +            case POST_ACTIVATE: return interceptor.postActivate(this, userInterceptor);
               default: throw new IllegalArgumentException("no InvocationType");
            }
         }
  
  
  



More information about the jboss-cvs-commits mailing list