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

Norman Richards norman.richards at jboss.com
Fri Nov 17 19:38:23 EST 2006


  User: nrichards
  Date: 06/11/17 19:38:23

  Modified:    src/main/org/jboss/seam/init  Initialization.java
  Log:
  JBSEAM-484: add @Install annotation, rework initialization code
  
  Revision  Changes    Path
  1.119     +139 -165  jboss-seam/src/main/org/jboss/seam/init/Initialization.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: Initialization.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/init/Initialization.java,v
  retrieving revision 1.118
  retrieving revision 1.119
  diff -u -b -r1.118 -r1.119
  --- Initialization.java	17 Nov 2006 02:35:45 -0000	1.118
  +++ Initialization.java	18 Nov 2006 00:38:23 -0000	1.119
  @@ -29,6 +29,7 @@
   import org.jboss.seam.Component;
   import org.jboss.seam.ScopeType;
   import org.jboss.seam.Seam;
  +import org.jboss.seam.annotations.Install;
   import org.jboss.seam.annotations.Name;
   import org.jboss.seam.annotations.Namespace;
   import org.jboss.seam.annotations.Role;
  @@ -111,7 +112,7 @@
   /**
    * @author Gavin King
    * @author <a href="mailto:theute at jboss.org">Thomas Heute</a>
  - * @version $Revision: 1.118 $
  + * @version $Revision: 1.119 $
    */
   public class Initialization
   {
  @@ -121,7 +122,6 @@
   
      private Map<String, Conversions.PropertyValue> properties = new HashMap<String, Conversions.PropertyValue>();
      private ServletContext servletContext;
  -   private boolean isScannerEnabled = true;
      private List<ComponentDescriptor> componentDescriptors = new ArrayList<ComponentDescriptor>();
      private List<FactoryDescriptor> factoryDescriptors = new ArrayList<FactoryDescriptor>();
      private Set<Class> installedComponents = new HashSet<Class>();
  @@ -220,15 +220,11 @@
         List<Element> componentElements = doc.getRootElement().elements("component");
         for (Element component: componentElements)
         {
  -         String installed = component.attributeValue("installed");
  -         if (installed==null || "true".equals( replace(installed, replacements) ) )
  -         {
               installComponentFromXmlElement(component, 
                                              component.attributeValue("name"), 
                                              component.attributeValue("class"), 
                                              replacements);
            }
  -      }
   
         List<Element> factoryElements = doc.getRootElement().elements("factory");
         for (Element factory: factoryElements)
  @@ -242,8 +238,6 @@
             String ns = elem.getNamespace().getURI();
             NamespaceInfo nsInfo = namespaceMap.get(ns);
             if (nsInfo != null) {
  -              String installed = elem.attributeValue("installed");
  -              if (installed==null || "true".equals(replace(installed, replacements))) {
                     String name = elem.attributeValue("name");
   
                     String className = nsInfo.getPackage().getName() + "." + elem.getName();
  @@ -273,7 +267,7 @@
                                                    name,
                                                    className, 
                                                    replacements);
  -              }
  +//               }
             }
         }
      }
  @@ -326,6 +320,12 @@
                                                  Properties replacements) 
          throws ClassNotFoundException
      {
  +      String installText = component.attributeValue("installed");
  +      boolean installed = false;
  +      if (installText == null || "true".equals(replace(installText, replacements))) {
  +          installed = true;
  +      }
  +
         String scopeName = component.attributeValue("scope");
         String jndiName = component.attributeValue("jndi-name");
         ScopeType scope = scopeName==null ? null : ScopeType.valueOf(scopeName.toUpperCase());
  @@ -355,7 +355,8 @@
            {
               name = clazz.getAnnotation(Name.class).value();
            }
  -         componentDescriptors.add( new ComponentDescriptor(name, clazz, scope, autoCreate, jndiName) );
  +
  +         componentDescriptors.add( new ComponentDescriptor(name, clazz, scope, autoCreate, jndiName, installed) );
            installedComponents.add(clazz);
         }
         else if (name==null)
  @@ -433,7 +434,7 @@
      public Initialization init()
      {
         log.info("initializing Seam");
  -      installScannedComponents();
  +      scanForComponents();
         Lifecycle.beginInitialization(servletContext);
         Contexts.getApplicationContext().set(Component.PROPERTIES, properties);
         addComponents();
  @@ -442,21 +443,16 @@
         return this;
      }
   
  -   private void installScannedComponents()
  +   private void scanForComponents()
      {
         Set<Package> scannedPackages = new HashSet<Package>();
  -      if ( isScannerEnabled )
  -      {
  -         for ( Class<Object> scannedClass: new ComponentScanner("seam.properties").getClasses() )
  -         {
  +      for (Class<Object> scannedClass: new ComponentScanner("seam.properties").getClasses()) {
               installScannedClass(scannedPackages, scannedClass);
            }
  -         for ( Class<Object> scannedClass: new ComponentScanner("META-INF/components.xml").getClasses() )
  -         {
  +      for (Class<Object> scannedClass: new ComponentScanner("META-INF/components.xml").getClasses()) {
               installScannedClass(scannedPackages, scannedClass);
            }
         }
  -   }
   
      private void installScannedClass(Set<Package> scannedPackages, Class<Object> scannedClass)
      {
  @@ -530,7 +526,7 @@
      private void installRole(Class<Object> scannedClass, Role role)
      {
         ScopeType scope = Seam.getComponentRoleScope(scannedClass, role);
  -      componentDescriptors.add( new ComponentDescriptor( role.name(), scannedClass, scope, false, null ) );
  +      componentDescriptors.add( new ComponentDescriptor( role.name(), scannedClass, scope, false, null, null ) );
      }
   
       private void addNamespace(Package pkg) {
  @@ -552,7 +548,6 @@
           addNamespace(org.jboss.seam.remoting.RequestContext.class.getPackage());
           addNamespace(org.jboss.seam.theme.Theme.class.getPackage());
   
  -        if (isScannerEnabled) {
               for (Package pkg: new NamespaceScanner("seam.properties").getPackages()) { 
                   addNamespace(pkg);
               }
  @@ -560,7 +555,6 @@
                   addNamespace(pkg);
               }
           }
  -    }
   
   
      private void initPropertiesFromServletContext()
  @@ -613,119 +607,84 @@
         return props;
      }
   
  +   protected ComponentDescriptor findDescriptor(Class<?> componentClass) {
  +       for (ComponentDescriptor component: componentDescriptors) {
  +           if (component.getComponentClass().equals(componentClass)) {
  +               return component;
  +           }
  +       }
  +       return null;
  +   }
  +
  +
      protected void addComponents()
      {
         Context context = Contexts.getApplicationContext();
   
  -      addComponent( Init.class, context );
  -
         //force instantiation of Init
  +      addComponent(new ComponentDescriptor(Init.class), context);
         Init init = (Init) Component.getInstance(Init.class, ScopeType.APPLICATION);
   
  -      addComponent( Expressions.class, context);
  -      addComponent( Pages.class, context);
  -      addComponent( FacesPage.class, context);
  -      addComponent( Events.class, context);
  -      addComponent( ConversationEntries.class, context );
  -      addComponent( Manager.class, context );
  -      addComponent( Switcher.class, context );
  -      addComponent( Redirect.class, context );
  -      addComponent( HttpError.class, context );
  -      addComponent( UserPrincipal.class, context );
  -      addComponent( IsUserInRole.class, context );
  -      addComponent( Conversation.class, context );
  -      addComponent( ConversationList.class, context );
  -      addComponent( ConversationStack.class, context );
  -      addComponent( FacesContext.class, context );
  -      addComponent( PageContext.class, context );
  -      addComponent( EventContext.class, context );
  -      addComponent( SessionContext.class, context );
  -      addComponent( ApplicationContext.class, context );
  -      addComponent( ConversationContext.class, context );
  -      addComponent( BusinessProcessContext.class, context );
  -      addComponent( Locale.class, context );
  -      addComponent( Messages.class, context );
  -      addComponent( Theme.class, context);
  -      addComponent( ThemeSelector.class, context);
  -      addComponent( Interpolator.class, context );
  -      addComponent( Validation.class, context );
  -      addComponent( FacesMessages.class, context );
  -      addComponent( ResourceBundle.class, context );
  -      addComponent( LocaleSelector.class, context );
  -      addComponent( UiComponent.class, context );
  -      addComponent( SafeActions.class, context );
  -      addComponent( PersistenceContexts.class, context );
  -      addComponent( CurrentDate.class, context );
  -      addComponent( CurrentTime.class, context );
  -      addComponent( CurrentDatetime.class, context );
  -      addComponent( Exceptions.class, context );
  -
  -      //addComponent( Dispatcher.class, context );
  -      
  -      addComponentIfPossible( SeamSecurityManager.class, context );
  -      addComponentIfPossible( RemotingConfig.class, context );
  -      addComponentIfPossible( SubscriptionRegistry.class, context );
  -      addComponentIfPossible( PojoCache.class, context );
  -      
  -      if ( installedComponents.contains(ManagedPersistenceContext.class) )
  -      {
  -         try
  -         {
  -            Reflections.classForName("org.hibernate.Session");
  -            addComponent( HibernatePersistenceProvider.class, context );
  -         }
  -         catch (ClassNotFoundException cnfe)
  -         {
  -            addComponent( PersistenceProvider.class, context );
  -         }
  +      ComponentDescriptor desc = findDescriptor(Jbpm.class);
  +      if (desc != null && desc.isInstalled()) {
  +          init.setJbpmInstalled(true);
         }
   
  -      if ( installedComponents.contains(Jbpm.class) )
  -      {
  -         init.setJbpmInstalled(true);
  +//       addComponentIfPossible( SeamSecurityManager.class, context );
  +//       addComponentIfPossible( RemotingConfig.class, context );
  +//       addComponentIfPossible( SubscriptionRegistry.class, context );
  +
  +      try {
  +          Reflections.classForName("org.jboss.cache.aop.PojoCache");
  +          componentDescriptors.add(new ComponentDescriptor(PojoCache.class, true));
  +      } catch (ClassNotFoundException e) {
         }
   
  -      if ( init.isDebug() )
  -      {
  -         addComponent( Introspector.class, context );
  -         addComponent( org.jboss.seam.debug.Contexts.class, context );
  +      if (installedComponents.contains(ManagedPersistenceContext.class)) {
  +          try {
  +              Reflections.classForName("org.hibernate.Session");
  +              componentDescriptors.add(new ComponentDescriptor(HibernatePersistenceProvider.class, true));
  +          } catch (ClassNotFoundException cnfe) {
  +              componentDescriptors.add(new ComponentDescriptor(PersistenceProvider.class, true));
  +          }
         }
   
  -      if ( init.isJbpmInstalled() )
  -      {
  -         addComponent( Actor.class, context);
  -         addComponent( BusinessProcess.class, context );
  -         addComponent( Pageflow.class, context );
  -         addComponent( Transition.class, context);
  -         addComponent( PooledTask.class, context );
  -         addComponent( TaskInstance.class, context );
  -         addComponent( ProcessInstance.class, context );
  -         addComponent( TaskInstanceList.class, context );
  -         addComponent( PooledTaskInstanceList.class, context );
  -         addComponent( TaskInstanceListForType.class, context );
  -         addComponent( ManagedJbpmContext.class, context );
  +      if (init.isDebug()) {
  +          componentDescriptors.add(new ComponentDescriptor(Introspector.class, true));
  +          componentDescriptors.add(new ComponentDescriptor(org.jboss.seam.debug.Contexts.class, true));
         }
   
  -      if ( installedComponents.contains(ManagedTopicPublisher.class) )
  -      {
  -         addComponent( TopicConnection.class, context );
  -         addComponent( TopicSession.class, context );
  +      if (installedComponents.contains(ManagedTopicPublisher.class)) {
  +          componentDescriptors.add(new ComponentDescriptor(TopicConnection.class, true));
  +          componentDescriptors.add(new ComponentDescriptor(TopicSession.class, true));
         }
   
  -      if ( installedComponents.contains(ManagedQueueSender.class) )
  -      {
  -         addComponent( QueueConnection.class, context );
  -         addComponent( QueueSession.class, context );
  +      if (installedComponents.contains(ManagedQueueSender.class)) {
  +          componentDescriptors.add(new ComponentDescriptor(QueueConnection.class, true));
  +          componentDescriptors.add(new ComponentDescriptor(QueueSession.class, true));
         }
   
  -      for ( ComponentDescriptor componentDescriptor: componentDescriptors )
  +
  +      boolean installedSomething = false;
  +      do {
  +          installedSomething = false;
  +          log.info("Instantiating components...");
  +          for (ComponentDescriptor componentDescriptor: componentDescriptors) {
  +              String compName = componentDescriptor.getName() + COMPONENT_SUFFIX;
  +              if (!context.isSet(compName)) {
  +                  if (componentDescriptor.isInstalled() &&
  +                      dependenciesMet(context, componentDescriptor.getDependencies())) 
         {
            addComponent(componentDescriptor, context);
  -         if ( componentDescriptor.isAutoCreate() )
  -         {
  -            init.addAutocreateVariable( componentDescriptor.getName() );
  +                      if (componentDescriptor.isAutoCreate()) {
  +                          init.addAutocreateVariable(componentDescriptor.getName());
            }
  +                      installedSomething = true;
         }
  +              }
  +          }
  +
  +      } while (installedSomething);
   
         for (FactoryDescriptor factoryDescriptor: factoryDescriptors)
         {
  @@ -742,9 +701,26 @@
               init.addAutocreateVariable(factoryDescriptor.getName());
            }
         }
  +   }
   
  +   protected boolean dependenciesMet(Context context, String[] dependencies) {
  +       if (dependencies == null) {
  +           return true;
      }
   
  +       for (String dependency: dependencies) {
  +           if (!context.isSet(dependency + COMPONENT_SUFFIX)) {
  +               return false;
  +           }
  +       }
  +
  +       return true;
  +   }
  +
  +    /** 
  +     * This actually creates a propert component and should only be called
  +     * when we want to install a component
  +     */
      protected void addComponent(ComponentDescriptor descriptor, Context context)
      {
         String name = descriptor.getName();
  @@ -762,40 +738,6 @@
               descriptor.getJndiName()
            );
         context.set(componentName, component);
  -
  -   }
  -
  -   protected void addComponentIfPossible(Class<?> clazz, Context context)
  -   {
  -      try
  -      {
  -         addComponent(clazz, context);
  -      }
  -      catch (NoClassDefFoundError ncdfe)
  -      {
  -         log.info(
  -               "could not install component: " + 
  -               clazz.getAnnotation(Name.class).value() +
  -               " due to missing class: " +
  -               ncdfe.getMessage()
  -            );
  -      }
  -   }
  -
  -   protected void addComponent(Class clazz, Context context)
  -   {
  -      addComponent( new ComponentDescriptor(clazz), context );
  -   }
  -
  -   public boolean isScannerEnabled()
  -   {
  -      return isScannerEnabled;
  -   }
  -
  -   public Initialization setScannerEnabled(boolean isScannerEnabled)
  -   {
  -      this.isScannerEnabled = isScannerEnabled;
  -      return this;
      }
   
      private static class FactoryDescriptor
  @@ -873,23 +815,36 @@
      private static class ComponentDescriptor
      {
         private String name;
  -      private Class componentClass;
  +      private Class<?> componentClass;
         private ScopeType scope;
         private String jndiName;
  +      private Boolean installed;
         private boolean autoCreate;
   
  -      public ComponentDescriptor(String name, Class componentClass, ScopeType scope, boolean autoCreate, String jndiName)
  +      public ComponentDescriptor(String name, 
  +                                 Class<?> componentClass, 
  +                                 ScopeType scope, 
  +                                 boolean autoCreate,
  +                                 String jndiName, 
  +                                 Boolean installed)
         {
            this.name = name;
            this.componentClass = componentClass;
            this.scope = scope;
            this.jndiName = jndiName;
  +         this.installed = installed;
            this.autoCreate = autoCreate;
         }
         public ComponentDescriptor(Class componentClass)
         {
            this.componentClass = componentClass;
         }
  +      public ComponentDescriptor(Class componentClass, Boolean installed)
  +      {
  +          this.componentClass = componentClass;
  +          this.installed = installed;
  +          
  +      }
   
         public String getName()
         {
  @@ -912,10 +867,29 @@
            return autoCreate;
         }
   
  +      public String[] getDependencies() {
  +          Install anno = componentClass.getAnnotation(Install.class);
  +          if (anno == null) {
  +              return null;
  +          }
  +          return anno.depends();
  +      }
  +
  +      public boolean isInstalled() {
  +          if (installed != null) {
  +              return installed;
  +          }
  +          Install anno = componentClass.getAnnotation(Install.class);
  +          if (anno == null) {
  +              return true;
  +          }
  +          return anno.value();
  +      }
  +
         @Override
         public String toString()
         {
  -         return "ComponentDescriptor(" + name + ')';
  +         return "ComponentDescriptor(" + getName() + ')';
         }
      }
   
  
  
  



More information about the jboss-cvs-commits mailing list