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

Gavin King gavin.king at jboss.com
Wed Feb 14 00:46:29 EST 2007


  User: gavin   
  Date: 07/02/14 00:46:29

  Modified:    src/main/org/jboss/seam    Component.java
  Added:       src/main/org/jboss/seam    Entity.java Model.java
  Log:
  introduced meta-model for non-component objects
  
  Revision  Changes    Path
  1.237     +32 -59    jboss-seam/src/main/org/jboss/seam/Component.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Component.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/Component.java,v
  retrieving revision 1.236
  retrieving revision 1.237
  diff -u -b -r1.236 -r1.237
  --- Component.java	11 Feb 2007 20:23:03 -0000	1.236
  +++ Component.java	14 Feb 2007 05:46:29 -0000	1.237
  @@ -40,9 +40,7 @@
   import java.util.Collection;
   import java.util.HashMap;
   import java.util.HashSet;
  -import java.util.Hashtable;
   import java.util.List;
  -import java.util.Locale;
   import java.util.Map;
   import java.util.Set;
   import java.util.StringTokenizer;
  @@ -54,7 +52,6 @@
   import net.sf.cglib.proxy.Factory;
   import net.sf.cglib.proxy.MethodInterceptor;
   
  -import org.hibernate.validator.ClassValidator;
   import org.jboss.seam.annotations.Asynchronous;
   import org.jboss.seam.annotations.Begin;
   import org.jboss.seam.annotations.BeginTask;
  @@ -88,7 +85,6 @@
   import org.jboss.seam.core.Expressions;
   import org.jboss.seam.core.Init;
   import org.jboss.seam.core.Mutable;
  -import org.jboss.seam.core.ResourceBundle;
   import org.jboss.seam.core.Expressions.MethodBinding;
   import org.jboss.seam.core.Expressions.ValueBinding;
   import org.jboss.seam.databinding.DataBinder;
  @@ -123,16 +119,17 @@
   import org.jboss.seam.util.Conversions.PropertyValue;
   
   /**
  - * A Seam component is any POJO managed by Seam.
  - * A POJO is recognized as a Seam component if it has a @Name annotation
  + * Metamodel class for component classes.
    *
  - * @author <a href="mailto:theute at jboss.org">Thomas Heute</a>
  + * A Seam component is any class with a @Name annotation.
  + *
  + * @author Thomas Heute
    * @author Gavin King
  - * @version $Revision: 1.236 $
  + * 
    */
   @Scope(ScopeType.APPLICATION)
   @SuppressWarnings("deprecation")
  -public class Component
  +public class Component extends Model
   {
      public static final String PROPERTIES = "org.jboss.seam.properties";
   
  @@ -141,7 +138,6 @@
      private ComponentType type;
      private String name;
      private ScopeType scope;
  -   private Class<?> beanClass;
      private String jndiName;
      private InterceptionType interceptionType;
      private boolean startup;
  @@ -178,8 +174,6 @@
      private List<Field> logFields = new ArrayList<Field>();
      private List<org.jboss.seam.log.Log> logInstances = new ArrayList<org.jboss.seam.log.Log>();
   
  -   private Hashtable<Locale, ClassValidator> validators = new Hashtable<Locale, ClassValidator>();
  -
      private Class<Factory> factory;
   
      //only used for tests
  @@ -205,13 +199,14 @@
         this(clazz, componentName, componentScope, jndiName, Contexts.getApplicationContext());
      }
   
  -   private Component(Class<?> clazz, String componentName, ScopeType componentScope, String componentJndiName, Context applicationContext)
  +   private Component(Class<?> beanClass, String componentName, ScopeType componentScope, String componentJndiName, Context applicationContext)
      {
  -      beanClass = clazz;
  +      super(beanClass);
  +      
         name = componentName;
         scope = componentScope;
  -      type = Seam.getComponentType(beanClass);
  -      interceptionType = Seam.getInterceptionType(beanClass);
  +      type = Seam.getComponentType(getBeanClass());
  +      interceptionType = Seam.getInterceptionType(getBeanClass());
   
         initNamespaces(componentName, applicationContext);
   
  @@ -220,7 +215,7 @@
         jndiName = componentJndiName == null ?
               getJndiName(applicationContext) : componentJndiName;
   
  -      startup = beanClass.isAnnotationPresent(Startup.class);
  +      startup = getBeanClass().isAnnotationPresent(Startup.class);
         if (startup)
         {
            if (scope!=SESSION && scope!=APPLICATION)
  @@ -231,15 +226,15 @@
         }
   
         synchronize = ( scope==SESSION /*&& ! beanClass.isAnnotationPresent(ReadOnly.class)*/ ) ||
  -            beanClass.isAnnotationPresent(Synchronized.class);
  +            getBeanClass().isAnnotationPresent(Synchronized.class);
         if (synchronize)
         {
            if (scope==STATELESS)
            {
               throw new IllegalArgumentException("@Synchronized not meaningful for stateless components: " + name);
            }
  -         timeout = beanClass.isAnnotationPresent(Synchronized.class) ?
  -               beanClass.getAnnotation(Synchronized.class).timeout() :
  +         timeout = getBeanClass().isAnnotationPresent(Synchronized.class) ?
  +               getBeanClass().getAnnotation(Synchronized.class).timeout() :
                  Synchronized.DEFAULT_TIMEOUT;
         }
   
  @@ -247,14 +242,14 @@
               "Component: " + getName() +
               ", scope: " + getScope() +
               ", type: " + getType() +
  -            ", class: " + beanClass.getName() +
  +            ", class: " + getBeanClass().getName() +
               ( jndiName==null ? "" : ", JNDI: " + jndiName )
            );
   
  -      initMembers(clazz, applicationContext);
  +      initMembers(beanClass, applicationContext);
         checkDestroyMethod();
   
  -      businessInterfaces = getBusinessInterfaces(beanClass);
  +      businessInterfaces = getBusinessInterfaces(getBeanClass());
   
         if ( interceptionType!=InterceptionType.NEVER)
         {
  @@ -310,7 +305,7 @@
   
         boolean serializableScope = scope==PAGE || scope==SESSION || scope==CONVERSATION;
         boolean serializableType = type==JAVA_BEAN || type==ENTITY_BEAN;
  -      if ( serializableType && serializableScope && !Serializable.class.isAssignableFrom(beanClass) )
  +      if ( serializableType && serializableScope && !Serializable.class.isAssignableFrom(getBeanClass()) )
         {
            log.warn("Component class should be serializable: " + name);
         }
  @@ -326,9 +321,9 @@
   
      private String getJndiName(Context applicationContext)
      {
  -      if ( beanClass.isAnnotationPresent(JndiName.class) )
  +      if ( getBeanClass().isAnnotationPresent(JndiName.class) )
         {
  -         return beanClass.getAnnotation(JndiName.class).value();
  +         return getBeanClass().getAnnotation(JndiName.class).value();
         }
         else
         {
  @@ -343,7 +338,7 @@
                  {
                     throw new IllegalArgumentException("You must specify org.jboss.seam.core.init.jndiPattern or use @JndiName: " + name);
                  }
  -               return jndiPattern.replace( "#{ejbName}", Seam.getEjbName(beanClass) );
  +               return jndiPattern.replace( "#{ejbName}", Seam.getEjbName(getBeanClass()) );
            }
         }
      }
  @@ -370,7 +365,7 @@
               }*/
   
               String propertyName = key.substring( name.length()+1, key.length() );
  -            Method setterMethod = Reflections.getSetterMethod(beanClass, propertyName);
  +            Method setterMethod = Reflections.getSetterMethod(getBeanClass(), propertyName);
               if (setterMethod!=null)
               {
                  if ( !setterMethod.isAccessible() ) setterMethod.setAccessible(true);
  @@ -380,7 +375,7 @@
               }
               else
               {
  -               Field field = Reflections.getField(beanClass, propertyName);
  +               Field field = Reflections.getField(getBeanClass(), propertyName);
                  if ( !field.isAccessible() ) field.setAccessible(true);
                  initializerFields.put( field, getInitialValue(propertyValue, field.getType(), field.getGenericType()) );
               }
  @@ -429,10 +424,10 @@
         Map<Field, Annotation> selectionFields = new HashMap<Field, Annotation>();
         Set<String> dataModelNames = new HashSet<String>();
   
  -      for (;clazz!=Object.class; clazz = clazz.getSuperclass())
  +      for ( ; clazz!=Object.class; clazz = clazz.getSuperclass() )
         {
   
  -         for (Method method: clazz.getDeclaredMethods())
  +         for ( Method method: clazz.getDeclaredMethods() )
            {
               if ( method.isAnnotationPresent(IfInvalid.class) )
               {
  @@ -571,7 +566,7 @@
   
            }
   
  -         for (Field field: clazz.getDeclaredFields())
  +         for ( Field field: clazz.getDeclaredFields() )
            {
   
               if ( !field.isAccessible() )
  @@ -607,7 +602,7 @@
                  org.jboss.seam.log.Log logInstance;
                  if ( "".equals( category ) )
                  {
  -                  logInstance = org.jboss.seam.log.Logging.getLog(beanClass);
  +                  logInstance = org.jboss.seam.log.Logging.getLog(getBeanClass());
                  }
                  else
                  {
  @@ -714,7 +709,7 @@
      {
         initDefaultInterceptors();
   
  -      for ( Annotation annotation: beanClass.getAnnotations() )
  +      for ( Annotation annotation: getBeanClass().getAnnotations() )
         {
            if ( annotation.annotationType().isAnnotationPresent(INTERCEPTORS) )
            {
  @@ -889,11 +884,6 @@
         return false;
      }
   
  -   public Class<?> getBeanClass()
  -   {
  -      return beanClass;
  -   }
  -
      public String getName()
      {
         return name;
  @@ -909,23 +899,6 @@
         return scope;
      }
   
  -   public ClassValidator getValidator()
  -   {
  -      java.util.ResourceBundle bundle = Contexts.isApplicationContextActive() ? //yew, just for testing!
  -            ResourceBundle.instance() : null;
  -      Locale locale = bundle==null ?
  -            new Locale("DUMMY") : bundle.getLocale();
  -      ClassValidator validator = validators.get(locale);
  -      if (validator==null)
  -      {
  -         validator = bundle==null ?
  -               new ClassValidator(beanClass) :
  -               new ClassValidator(beanClass, bundle);
  -         validators.put(locale, validator);
  -      }
  -      return validator;
  -   }
  -
      public List<Interceptor> getInterceptors(InterceptorType type)
      {
         switch(type)
  @@ -1096,14 +1069,14 @@
   
      protected Object instantiateEntityBean() throws Exception
      {
  -      Object bean = beanClass.newInstance();
  +      Object bean = getBeanClass().newInstance();
         initialize(bean);
         return bean;
      }
   
      protected Object instantiateJavaBean() throws Exception
      {
  -      Object bean = beanClass.newInstance();
  +      Object bean = getBeanClass().newInstance();
         if (interceptionType==InterceptionType.NEVER)
         {
            initialize(bean);
  @@ -1428,7 +1401,7 @@
         {
            case JAVA_BEAN:
            case ENTITY_BEAN:
  -            return beanClass.isInstance(bean);
  +            return getBeanClass().isInstance(bean);
            default:
               Class clazz = bean.getClass();
               for ( Class businessInterface: businessInterfaces )
  
  
  
  1.1      date: 2007/02/14 05:46:29;  author: gavin;  state: Exp;jboss-seam/src/main/org/jboss/seam/Entity.java
  
  Index: Entity.java
  ===================================================================
  package org.jboss.seam;
  
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  
  import javax.persistence.Id;
  import javax.persistence.PostLoad;
  import javax.persistence.PrePersist;
  import javax.persistence.PreRemove;
  import javax.persistence.PreUpdate;
  
  import org.jboss.seam.util.Reflections;
  
  /**
   * Metamodel class for entity classes.
   * 
   * A class will be identified as an entity class
   * if it has an @Entity annotation.
   * 
   * @author Gavin King
   *
   */
  public class Entity extends Model
  {
     
     private Method preRemoveMethod;
     private Method prePersistMethod;
     private Method preUpdateMethod;
     private Method postLoadMethod;
     private Method identifierGetter;
     private Field identifierField;
  
     public Entity(Class<?> beanClass)
     {
        super(beanClass);
        
        for ( Class<?> clazz=beanClass; clazz!=Object.class; clazz = clazz.getSuperclass() )
        {
  
           for ( Method method: getBeanClass().getDeclaredMethods() )
           {
              //TODO: does the spec allow multiple lifecycle method
              //      in the entity class heirarchy?
              if ( method.isAnnotationPresent(PreRemove.class) )
              {
                 preRemoveMethod = method;
              }
              if ( method.isAnnotationPresent(PrePersist.class) )
              {
                 prePersistMethod = method;
              }
              if ( method.isAnnotationPresent(PreUpdate.class) )
              {
                 preUpdateMethod = method;
              }
              if ( method.isAnnotationPresent(PostLoad.class) )
              {
                 postLoadMethod = method;
              }
              if ( method.isAnnotationPresent(Id.class) )
              {
                 identifierGetter = method;
              }
           }
           
           if (identifierGetter==null)
           {
              for ( Field field: getBeanClass().getDeclaredFields() )
              {
                 if ( field.isAnnotationPresent(Id.class) )
                 {
                    identifierField = field;
                 }
              }
           }
           
        }
        
     }
  
     public Method getPostLoadMethod()
     {
        return postLoadMethod;
     }
  
     public Method getPrePersistMethod()
     {
        return prePersistMethod;
     }
  
     public Method getPreRemoveMethod()
     {
        return preRemoveMethod;
     }
  
     public Method getPreUpdateMethod()
     {
        return preUpdateMethod;
     }
  
     public Field getIdentifierField()
     {
        return identifierField;
     }
  
     public Method getIdentifierGetter()
     {
        return identifierGetter;
     }
     
     public Object getIdentifier(Object entity)
     {
        if (identifierGetter!=null)
        {
           return Reflections.invokeAndWrap(identifierGetter, entity);
        }
        else if (identifierField!=null)
        {
           return Reflections.getAndWrap(identifierField, entity);
        }
        else
        {
           throw new IllegalStateException("@Id attribute not found for entity class: " + getBeanClass().getName());
        }
     }
  
  }
  
  
  
  1.1      date: 2007/02/14 05:46:29;  author: gavin;  state: Exp;jboss-seam/src/main/org/jboss/seam/Model.java
  
  Index: Model.java
  ===================================================================
  package org.jboss.seam;
  
  import java.util.Hashtable;
  import java.util.Locale;
  
  import org.hibernate.validator.ClassValidator;
  import org.jboss.seam.contexts.Contexts;
  import org.jboss.seam.core.ResourceBundle;
  
  /**
   * Base class of metamodels. For a class which
   * is neither an entity nor a Seam component,
   * the concrete type of the metamodel object
   * will be Model. For components or entities
   * it is a subclass of Model.
   * 
   * @author Gavin King
   *
   */
  public class Model
  {
     private Class<?> beanClass;
     private Hashtable<Locale, ClassValidator> validators = new Hashtable<Locale, ClassValidator>();
  
     public Model(Class<?> beanClass)
     {
        this.beanClass = beanClass;
     }
     
     public final Class<?> getBeanClass()
     {
        return beanClass;
     }
  
     public ClassValidator getValidator()
     {
        java.util.ResourceBundle bundle = Contexts.isApplicationContextActive() ? //yew, just for testing!
              ResourceBundle.instance() : null;
        Locale locale = bundle==null ?
              new Locale("DUMMY") : bundle.getLocale();
        ClassValidator validator = validators.get(locale);
        if (validator==null)
        {
           validator = bundle==null ?
                 new ClassValidator(beanClass) :
                 new ClassValidator(beanClass, bundle);
           validators.put(locale, validator);
        }
        return validator;
     }
     
     public static Model forClass(Class clazz)
     {
        if ( !Contexts.isApplicationContextActive() )
        {
           throw new IllegalStateException("No application context active");
        }
        
        String componentName = Seam.getComponentName(clazz);
        if (componentName!=null)
        {
           return Component.forName(componentName);
        }
        else
        {
           String name = clazz.getName() + ".model";
           Model model = (Model) Contexts.getApplicationContext().get(name);
           if ( model==null )
           {
              model = clazz.isAnnotationPresent(javax.persistence.Entity.class) ? 
                       new Entity(clazz) : new Model(clazz);
              Contexts.getApplicationContext().set(name, model);
           }
           return model;
        }
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list