You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avalon.apache.org by bl...@apache.org on 2001/04/10 18:15:44 UTC

cvs commit: jakarta-avalon/src/java/org/apache/avalon/component DefaultComponentFactory.java DefaultComponentHandler.java DefaultComponentManager.java DefaultComponentSelector.java DefaultRoleManager.java RoleManager.java

bloritsch    01/04/10 09:15:44

  Modified:    src/java/org/apache/avalon/component
                        DefaultComponentFactory.java
                        DefaultComponentHandler.java
                        DefaultComponentManager.java
                        DefaultComponentSelector.java
                        DefaultRoleManager.java RoleManager.java
  Log:
  Updated ComponentManagement Infrastructure to allow separation of roles/hint
  definitions from the configuration file--or share the same file.
  
  Revision  Changes    Path
  1.2       +11 -2     jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentFactory.java
  
  Index: DefaultComponentFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DefaultComponentFactory.java	2001/04/05 19:40:45	1.1
  +++ DefaultComponentFactory.java	2001/04/10 16:15:37	1.2
  @@ -31,7 +31,7 @@
    *
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
    * @author <a href="mailto:paul@luminas.co.uk">Paul Russell</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2001/04/05 19:40:45 $
  + * @version CVS $Revision: 1.2 $ $Date: 2001/04/10 16:15:37 $
    */
   public class DefaultComponentFactory extends AbstractLoggable implements ObjectFactory, ThreadSafe {
   
  @@ -52,16 +52,21 @@
        */
       private Context context;
   
  +    /** The RoleManager for child ComponentSelectors
  +     */
  +    private RoleManager roles;
  +
       /** Construct a new component factory for the specified component.
        * @param componentClass the class to instantiate (must have a default constructor).
        * @param config the <code>Configuration</code> object to pass to new instances.
        * @param manager the component manager to pass to <code>Composer</code>s.
        */
  -    public DefaultComponentFactory(Class componentClass, Configuration config, ComponentManager manager, Context context) {
  +    public DefaultComponentFactory(Class componentClass, Configuration config, ComponentManager manager, Context context, RoleManager roles) {
           this.componentClass = componentClass;
           this.conf = config;
           this.manager = manager;
           this.context = context;
  +        this.roles = roles;
       }
   
       public Object newInstance() throws Exception {
  @@ -81,6 +86,10 @@
   
           if ( comp instanceof Composer) {
               ((Composer)comp).compose(this.manager);
  +        }
  +
  +        if ( comp instanceof DefaultComponentSelector ) {
  +            ((DefaultComponentSelector)comp).setRoleManager(this.roles);
           }
   
           if ( comp instanceof Configurable ) {
  
  
  
  1.2       +3 -3      jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentHandler.java
  
  Index: DefaultComponentHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentHandler.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DefaultComponentHandler.java	2001/04/05 19:40:45	1.1
  +++ DefaultComponentHandler.java	2001/04/10 16:15:38	1.2
  @@ -24,7 +24,7 @@
    * and destroyed correctly.
    *
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2001/04/05 19:40:45 $
  + * @version CVS $Revision: 1.2 $ $Date: 2001/04/10 16:15:38 $
    */
   class DefaultComponentHandler extends AbstractLoggable implements Initializable, Disposable {
       /** Indicates that the Handler is holding a <code>ThreadSafe</code> Component */
  @@ -59,8 +59,8 @@
        * whether a Component is ThreadSafe, Poolable, or SingleThreaded.
        * It falls back to SingleThreaded if not specified.
        */
  -    DefaultComponentHandler(Class componentClass, Configuration config, ComponentManager manager, Context context) throws Exception {
  -        this.factory = new DefaultComponentFactory(componentClass, config, manager, context);
  +    DefaultComponentHandler(Class componentClass, Configuration config, ComponentManager manager, Context context, RoleManager roles) throws Exception {
  +        this.factory = new DefaultComponentFactory(componentClass, config, manager, context, roles);
   
           if (org.apache.avalon.Poolable.class.isAssignableFrom(componentClass)) {
               this.pool = new DefaultComponentPool(this.factory);
  
  
  
  1.4       +25 -15    jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentManager.java
  
  Index: DefaultComponentManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentManager.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DefaultComponentManager.java	2001/04/06 18:12:10	1.3
  +++ DefaultComponentManager.java	2001/04/10 16:15:38	1.4
  @@ -34,7 +34,7 @@
    *
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
    * @author <a href="mailto:paul@luminas.co.uk">Paul Russell</a>
  - * @version CVS $Revision: 1.3 $ $Date: 2001/04/06 18:12:10 $
  + * @version CVS $Revision: 1.4 $ $Date: 2001/04/10 16:15:38 $
    */
   public class DefaultComponentManager extends AbstractLoggable
           implements ComponentManager, Configurable, Contextualizable, Disposable {
  @@ -113,11 +113,6 @@
           DefaultComponentHandler handler = null;
           Component component = null;
   
  -        if ( role == null ) {
  -            getLogger().error("ComponentManager Attempted to retrieve component with null role.");
  -            throw new ComponentManagerException("Attempted to retrieve component with null role.");
  -        }
  -
           handler = (DefaultComponentHandler) this.componentHandlers.get(role);
           // Retrieve the instance of the requested component
           if ( handler == null ) {
  @@ -126,9 +121,9 @@
               Configuration config = new DefaultConfiguration("", "-");
   
               try {
  -                componentClass = this.getClass().getClassLoader().loadClass(roles.getDefaultClassNameForRole(role));
  +                componentClass = this.getClass().getClassLoader().loadClass(this.roles.getDefaultClassNameForRole(role));
   
  -                handler = new DefaultComponentHandler(componentClass, config, this, this.context);
  +                handler = new DefaultComponentHandler(componentClass, config, this, this.context, this.roles);
                   handler.setLogger(getLogger());
                   handler.init();
               } catch (Exception e) {
  @@ -141,6 +136,10 @@
   
           try {
               component = handler.get();
  +
  +            if (component instanceof DefaultComponentSelector) {
  +                ((DefaultComponentSelector) component).setRoleManager(this.roles);
  +            }
           } catch (IllegalStateException ise) {
               handler.init();
   
  @@ -161,10 +160,12 @@
        * Configure the ComponentManager.
        */
       public void configure(Configuration conf) throws ConfigurationException {
  -        DefaultRoleManager role_info = new DefaultRoleManager();
  -        role_info.setLogger(getLogger());
  -        role_info.configure(conf);
  -        roles = role_info;
  +        if (this.roles == null) {
  +            DefaultRoleManager role_info = new DefaultRoleManager();
  +            role_info.setLogger(getLogger());
  +            role_info.configure(conf);
  +            this.roles = role_info;
  +        }
   
           // Set components
   
  @@ -177,12 +178,12 @@
                   String className = e[i].getAttribute("class", "");
   
                   if ("".equals(role)) {
  -                    role = roles.getRoleForName(type);
  +                    role = this.roles.getRoleForName(type);
                   }
   
                   if (role != null && ("".equals(role) == false)) {
                       if ("".equals(className)) {
  -                        className = roles.getDefaultClassNameForRole(role);
  +                        className = this.roles.getDefaultClassNameForRole(role);
                       }
   
                       try {
  @@ -199,6 +200,15 @@
       }
   
       /**
  +     * Configure the RoleManager
  +     */
  +    public void setRoleManager(RoleManager roles) {
  +        if (this.roles == null) {
  +            this.roles = roles;
  +        }
  +    }
  +
  +    /**
        * Release a Component.  This implementation makes sure it has a handle on the propper
        * ComponentHandler, and let's the ComponentHandler take care of the actual work.
        */
  @@ -218,7 +228,7 @@
       public void addComponent(String role, Class component, Configuration config)
       throws ComponentManagerException {
           try {
  -            DefaultComponentHandler handler = new DefaultComponentHandler(component, config, this, this.context);
  +            DefaultComponentHandler handler = new DefaultComponentHandler(component, config, this, this.context, this.roles);
               handler.setLogger(getLogger());
               this.componentHandlers.put(role, handler);
           } catch (Exception e) {
  
  
  
  1.3       +32 -40    jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentSelector.java
  
  Index: DefaultComponentSelector.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentSelector.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DefaultComponentSelector.java	2001/04/06 18:12:10	1.2
  +++ DefaultComponentSelector.java	2001/04/10 16:15:40	1.3
  @@ -35,9 +35,12 @@
    *
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
    * @author <a href="mailto:paul@luminas.co.uk">Paul Russell</a>
  - * @version CVS $Revision: 1.2 $ $Date: 2001/04/06 18:12:10 $
  + * @version CVS $Revision: 1.3 $ $Date: 2001/04/10 16:15:40 $
    */
   public class DefaultComponentSelector extends AbstractLoggable implements Contextualizable, ComponentSelector, Composer, Configurable, ThreadSafe, Disposable {
  +    /** The role name for this instance
  +     */
  +    private String rolename = null;
   
       /** The application context for components
        */
  @@ -63,6 +66,10 @@
        */
       private boolean disposed = false;
   
  +    /** The RoleManager to get hint shortcuts
  +     */
  +    private RoleManager roles;
  +
       /** Shorthand for hints
        */
       private Map hints;
  @@ -160,51 +167,27 @@
        */
       public void configure(Configuration conf) throws ConfigurationException {
           this.conf = conf;
  -        getLogger().debug("ComponentSelector setting up with root element: " + conf.getName());
  +        getLogger().debug("ComponentSelector setting up with root element: " + this.conf.getName());
   
  -        Configuration[] hints = conf.getChildren("hint");
  -        HashMap hintMap = new HashMap();
  -
  -        for (int i = 0; i < hints.length; i++) {
  -            hintMap.put(hints[i].getAttribute("short-hand").trim(), hints[i].getAttribute("class").trim());
  +        if ("component".equals(this.conf.getName())) {
  +            this.rolename = this.conf.getAttribute("role");
  +        } else {
  +            this.rolename = this.roles.getRoleForName(this.conf.getName());
           }
  -
  -        this.hints = Collections.unmodifiableMap(hintMap);
  -
  -        Iterator shorthand = this.hints.keySet().iterator();
  -        Configuration[] instances = null;
  -
  -        while (shorthand.hasNext()) {
  -            String type = (String) shorthand.next();
  -            Class clazz = null;
  -
  -            try {
  -                clazz = this.getClass().getClassLoader().loadClass((String) this.hints.get(type));
  -            } catch (Exception e) {
  -                getLogger().error("ComponentSelector The component instance for \"" + type + "\" has an invalid class name.", e);
  -                throw new ConfigurationException("The component instance for '" + type + "' has an invalid class name.", e);
  -            }
  -
  -            instances = conf.getChildren(type);
   
  -            for (int i = 0; i < instances.length; i++) {
  -                Object hint = instances[i].getAttribute("name").trim();
  +        Configuration[] instances = conf.getChildren();
   
  -                try {
  -                    this.addComponent(hint, clazz, instances[i]);
  -                } catch (Exception e) {
  -                    getLogger().error("ComponentSelector The component instance for \"" + hint + "\" has an invalid class name.", e);
  -                    throw new ConfigurationException("The component instance for '" + hint + "' has an invalid class name.", e);
  -                }
  -            }
  -        }
  -
  -        instances = conf.getChildren("component-instance");
  -
           for (int i = 0; i < instances.length; i++) {
               Object hint = instances[i].getAttribute("name").trim();
  -            String className = (String) instances[i].getAttribute("class").trim();
  +            String className = "";
  +
  +            if ("component-instance".equals(instances[i].getName())) {
  +                className = (String) instances[i].getAttribute("class").trim();
  +            } else {
  +                className = this.roles.getDefaultClassNameForHint(this.rolename, instances[i].getName());
  +            }
   
  +            getLogger().debug(this.rolename + ":" + hint + " classname = " + className);
               try {
                   this.addComponent(hint, this.getClass().getClassLoader().loadClass(className), instances[i]);
               } catch (Exception e) {
  @@ -215,6 +198,15 @@
       }
   
       /**
  +     * Configure the RoleManager
  +     */
  +    public void setRoleManager(RoleManager roles) {
  +        if (this.roles == null) {
  +            this.roles = roles;
  +        }
  +    }
  +
  +    /**
        * Release the Component to the propper ComponentHandler.
        */
       public void release(Component component) {
  @@ -233,7 +225,7 @@
       public void addComponent(Object hint, Class component, Configuration config)
       throws ComponentManagerException {
           try {
  -            DefaultComponentHandler handler = new DefaultComponentHandler(component, config, this.manager, this.context);
  +            DefaultComponentHandler handler = new DefaultComponentHandler(component, config, this.manager, this.context, this.roles);
               handler.setLogger(getLogger());
               handler.init();
               this.componentHandlers.put(hint, handler);
  
  
  
  1.2       +38 -10    jakarta-avalon/src/java/org/apache/avalon/component/DefaultRoleManager.java
  
  Index: DefaultRoleManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/component/DefaultRoleManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DefaultRoleManager.java	2001/04/06 13:58:08	1.1
  +++ DefaultRoleManager.java	2001/04/10 16:15:41	1.2
  @@ -27,11 +27,12 @@
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
    * @author <a href="mailto:ricardo@apache,org">Ricardo Rocha</a>
    * @author <a href="mailto:giacomo@apache,org">Giacomo Pati</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2001/04/06 13:58:08 $
  + * @version CVS $Revision: 1.2 $ $Date: 2001/04/10 16:15:41 $
    */
   public class DefaultRoleManager extends AbstractLoggable implements RoleManager, Configurable {
  -    private Map shorthands = new HashMap();
  -    private Map classNames = new HashMap();
  +    private Map shorthands;
  +    private Map classNames;
  +    private Map hintClassNames;
   
       public final String getRoleForName(String shorthandName) {
           getLogger().debug("looking up role " + shorthandName + ", returning " + (String) this.shorthands.get(shorthandName));
  @@ -42,27 +43,54 @@
           return (String) this.classNames.get(role);
       }
   
  -    protected final void addRole(String name, String shorthand, String defaultClassName) {
  -        this.shorthands.put(shorthand, name);
  +    public final String getDefaultClassNameForHint(String role, String shorthand) {
  +        getLogger().debug("looking up hintmap for role " + role);
  +        Map hintMap = (Map) this.hintClassNames.get(role);
   
  -        if (defaultClassName != null) {
  -            this.classNames.put(name, defaultClassName);
  +        if (hintMap == null) {
  +            return "";
           }
  +
  +        getLogger().debug("looking up classname for hint " + shorthand);
  +        return (String) hintMap.get(shorthand);
       }
   
       public final void configure(Configuration conf) throws ConfigurationException {
  +        Map shorts = new HashMap();
  +        Map classes = new HashMap();
  +        Map hintclasses = new HashMap();
           Configuration[] roles = conf.getChildren("role");
   
           for (int i = 0; i < roles.length; i++) {
               String name = roles[i].getAttribute("name");
               String shorthand = roles[i].getAttribute("shorthand");
               String defaultClassName = roles[i].getAttribute("default-class", null);
  +
  +            shorts.put(shorthand, name);
  +
  +            if (defaultClassName != null) {
  +                classes.put(name, defaultClassName);
  +            }
  +
  +            Configuration[] hints = roles[i].getChildren("hint");
  +            if (hints.length > 0) {
  +                HashMap hintMap = new HashMap();
  +
  +                for (int j = 0; j < hints.length; j++) {
  +                    hintMap.put(hints[j].getAttribute("shorthand").trim(), hints[j].getAttribute("class").trim());
  +                    getLogger().debug("Adding hint type " + hints[j].getAttribute("shorthand").trim()
  +                                      + " associated with role " + name + " and class " +
  +                                      hints[j].getAttribute("class").trim());
  +                }
  +
  +                hintclasses.put(name, Collections.unmodifiableMap(hintMap));
  +            }
   
  -            this.addRole(name, shorthand, defaultClassName);
               getLogger().debug("added Role " + name + " with shorthand " + shorthand + " for " + defaultClassName);
           }
   
  -        this.shorthands = Collections.unmodifiableMap(this.shorthands);
  -        this.classNames = Collections.unmodifiableMap(this.classNames);
  +        this.shorthands = Collections.unmodifiableMap(shorts);
  +        this.classNames = Collections.unmodifiableMap(classes);
  +        this.hintClassNames = Collections.unmodifiableMap(hintclasses);
       }
   }
  
  
  
  1.2       +9 -3      jakarta-avalon/src/java/org/apache/avalon/component/RoleManager.java
  
  Index: RoleManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/component/RoleManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- RoleManager.java	2001/04/06 13:58:09	1.1
  +++ RoleManager.java	2001/04/10 16:15:41	1.2
  @@ -17,7 +17,7 @@
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
    * @author <a href="mailto:ricardo@apache,org">Ricardo Rocha</a>
    * @author <a href="mailto:giacomo@apache,org">Giacomo Pati</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2001/04/06 13:58:09 $
  + * @version CVS $Revision: 1.2 $ $Date: 2001/04/10 16:15:41 $
    */
   public interface RoleManager {
       /**
  @@ -27,10 +27,16 @@
        * words, you should not try to instantiate a class from an empty
        * role.
        */
  -    public String getRoleForName(String shorthandName);
  +    String getRoleForName(String shorthandName);
   
       /**
        * Get the default classname for a given role
        */
  -    public String getDefaultClassNameForRole(String role);
  +    String getDefaultClassNameForRole(String role);
  +
  +    /**
  +     * Get the default classname for a given hint type.  This is only
  +     * used by ComponentSelectors.
  +     */
  +    String getDefaultClassNameForHint(String role, String shorthand);
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: avalon-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: avalon-dev-help@jakarta.apache.org