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

Gavin King gavin.king at jboss.com
Thu Nov 9 02:24:52 EST 2006


  User: gavin   
  Date: 06/11/09 02:24:52

  Modified:    src/main/org/jboss/seam/init  Initialization.java
  Log:
  support fine-grained component.xml files
  
  Revision  Changes    Path
  1.106     +129 -75   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.105
  retrieving revision 1.106
  diff -u -b -r1.105 -r1.106
  --- Initialization.java	8 Nov 2006 06:01:29 -0000	1.105
  +++ Initialization.java	9 Nov 2006 07:24:52 -0000	1.106
  @@ -22,6 +22,7 @@
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   import org.dom4j.Document;
  +import org.dom4j.DocumentException;
   import org.dom4j.Element;
   import org.dom4j.io.SAXReader;
   import org.jboss.seam.Component;
  @@ -106,7 +107,7 @@
   /**
    * @author Gavin King
    * @author <a href="mailto:theute at jboss.org">Thomas Heute</a>
  - * @version $Revision: 1.105 $
  + * @version $Revision: 1.106 $
    */
   public class Initialization
   {
  @@ -125,13 +126,13 @@
      public Initialization(ServletContext servletContext)
      {
         this.servletContext = servletContext;
  -      initPropertiesFromXml();
  +      initComponentsFromXmlDocument();
         initPropertiesFromServletContext();
         initPropertiesFromResource();
         initJndiProperties();
      }
   
  -   private void initPropertiesFromXml()
  +   private void initComponentsFromXmlDocument()
      {
         InputStream stream = Resources.getResourceAsStream("/WEB-INF/components.xml", servletContext);
         if (stream==null)
  @@ -146,11 +147,17 @@
               Properties replacements = new Properties();
               InputStream replaceStream = Resources.getResourceAsStream("components.properties");
               if (replaceStream!=null) replacements.load( replaceStream );
  +            installComponentsFromXmlElements( getDocument(stream), replacements );
  +         }
  +         catch (Exception e)
  +         {
  +            throw new RuntimeException("error while reading components.xml", e);
  +         }
  +      }
  +   }
   
  -            SAXReader saxReader = new SAXReader();
  -            saxReader.setMergeAdjacentText(true);
  -            Document doc = saxReader.read(stream);
  -
  +   private void installComponentsFromXmlElements(Document doc, Properties replacements) throws DocumentException, ClassNotFoundException
  +   {
               List<Element> importElements = doc.getRootElement().elements("import-java-package");
               for (Element importElement: importElements)
               {
  @@ -163,13 +170,19 @@
                  String installed = component.attributeValue("installed");
                  if (installed==null || "true".equals( replace(installed, replacements) ) )
                  {
  -                  installComponent(component, replacements);
  +            installComponentFromXmlElement(component, component.attributeValue("class"), replacements);
                  }
               }
   
               List<Element> factoryElements = doc.getRootElement().elements("factory");
               for (Element factory: factoryElements)
               {
  +         installFactoryFromXmlElement(factory);
  +      }
  +   }
  +
  +   private void installFactoryFromXmlElement(Element factory)
  +   {
                  String scopeName = factory.attributeValue("scope");
                  String name = factory.attributeValue("name");
                  if (name==null)
  @@ -188,12 +201,12 @@
                  factoryDescriptors.add( new FactoryDescriptor(name, scope, method, value) );
               }
   
  -         }
  -         catch (Exception e)
  +   private Document getDocument(InputStream stream) throws DocumentException
            {
  -            throw new RuntimeException("error while reading components.xml", e);
  -         }
  -      }
  +      SAXReader saxReader = new SAXReader();
  +      saxReader.setMergeAdjacentText(true);
  +      Document doc = saxReader.read(stream);
  +      return doc;
      }
   
      private String replace(String value, Properties replacements)
  @@ -205,10 +218,9 @@
         return value;
      }
   
  -   private void installComponent(Element component, Properties replacements) throws ClassNotFoundException
  +   private void installComponentFromXmlElement(Element component, String className, Properties replacements) throws ClassNotFoundException
      {
         String name = component.attributeValue("name");
  -      String className = component.attributeValue("class");
         String scopeName = component.attributeValue("scope");
         ScopeType scope = scopeName==null ? null : ScopeType.valueOf(scopeName.toUpperCase());
         if (className!=null)
  @@ -317,6 +329,7 @@
      public Initialization init()
      {
         log.info("initializing Seam");
  +      installScannedComponents();
         Lifecycle.beginInitialization(servletContext);
         Contexts.getApplicationContext().set(Component.PROPERTIES, properties);
         addComponents();
  @@ -325,6 +338,74 @@
         return this;
      }
   
  +   private void installScannedComponents()
  +   {
  +      if ( isScannerEnabled )
  +      {
  +         for ( Class<Object> scannedClass: new Scanner().getClasses() )
  +         {
  +            installScannedComponentAndRoles(scannedClass);
  +            installComponentsFromDescriptor( descriptorFilename(scannedClass), scannedClass.getName() );
  +         }
  +      }
  +   }
  +
  +   private static String descriptorFilename(Class<Object> scannedClass)
  +   {
  +      return scannedClass.getName().replace('.', '/') + ".component.xml";
  +   }
  +
  +   private void installComponentsFromDescriptor(String fileName, String className)
  +   {
  +      InputStream stream = Resources.getResourceAsStream(fileName);
  +      if (stream!=null)
  +      {
  +         try
  +         {
  +            Properties replacements = new Properties(); //TODO: use components.properties
  +            Document doc = getDocument(stream);
  +            if ( doc.getRootElement().getName().equals("component") )
  +            {
  +               installComponentFromXmlElement( doc.getRootElement(), className, replacements );
  +            }
  +            else
  +            {
  +               installComponentsFromXmlElements(doc, replacements);
  +            }
  +         }
  +         catch (Exception e)
  +         {
  +            throw new RuntimeException(e);
  +         }
  +      }
  +   }
  +
  +   private void installScannedComponentAndRoles(Class<Object> scannedClass)
  +   {
  +      if ( scannedClass.isAnnotationPresent(Name.class) )
  +      {
  +         componentDescriptors.add( new ComponentDescriptor(scannedClass) );
  +      }
  +      if ( scannedClass.isAnnotationPresent(Role.class) )
  +      {
  +         installRole( scannedClass, scannedClass.getAnnotation(Role.class) );
  +      }
  +      if ( scannedClass.isAnnotationPresent(Roles.class) )
  +      {
  +         Role[] roles = scannedClass.getAnnotation(Roles.class).value();
  +         for (Role role: roles)
  +         {
  +            installRole(scannedClass, role);
  +         }
  +      }
  +   }
  +
  +   private void installRole(Class<Object> scannedClass, Role role)
  +   {
  +      ScopeType scope = Seam.getComponentRoleScope(scannedClass, role);
  +      componentDescriptors.add( new ComponentDescriptor( role.name(), scannedClass, scope) );
  +   }
  +
      private void initPropertiesFromServletContext()
      {
         Enumeration params = servletContext.getInitParameterNames();
  @@ -482,16 +563,7 @@
   
         for ( ComponentDescriptor componentDescriptor: componentDescriptors )
         {
  -         addComponent( componentDescriptor, context );
  -      }
  -
  -      if (isScannerEnabled)
  -      {
  -         for ( Class<Object> clazz: new Scanner().getClasses() )
  -         {
  -            addComponent(clazz, context);
  -            addComponentRoles(context, clazz);
  -         }
  +         addComponent(componentDescriptor, context);
         }
   
         for (FactoryDescriptor factoryDescriptor: factoryDescriptors)
  @@ -508,24 +580,6 @@
   
      }
   
  -   private void addComponentRoles(Context context, Class<Object> componentClass) {
  -      if ( componentClass.isAnnotationPresent(Role.class) )
  -      {
  -         Role role = componentClass.getAnnotation(Role.class);
  -         ScopeType scope = Seam.getComponentRoleScope(componentClass, role);
  -         addComponent( new ComponentDescriptor( role.name(), componentClass, scope), context );
  -      }
  -      if ( componentClass.isAnnotationPresent(Roles.class) )
  -      {
  -         Role[] roles =componentClass.getAnnotation(Roles.class).value();
  -         for (Role role: roles)
  -         {
  -            ScopeType scope = Seam.getComponentRoleScope(componentClass, role);
  -            addComponent( new ComponentDescriptor( role.name(), componentClass, scope), context );
  -         }
  -      }
  -   }
  -
      protected void addComponent(ComponentDescriptor descriptor, Context context)
      {
         String name = descriptor.getName();
  
  
  



More information about the jboss-cvs-commits mailing list