You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by bl...@apache.org on 2001/03/16 21:01:56 UTC

cvs commit: xml-cocoon/src/org/apache/cocoon/components CocoonComponentSelector.java

bloritsch    01/03/16 12:01:55

  Modified:    src/org/apache/cocoon/components Tag: xml-cocoon2
                        CocoonComponentSelector.java
  Log:
  Major rearchitecting of ComponentManagement infrastructure.  It is not only more maintainable,
  but it is also faster.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.2   +59 -223   xml-cocoon/src/org/apache/cocoon/components/Attic/CocoonComponentSelector.java
  
  Index: CocoonComponentSelector.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon/src/org/apache/cocoon/components/Attic/CocoonComponentSelector.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- CocoonComponentSelector.java	2001/03/12 05:18:05	1.1.2.1
  +++ CocoonComponentSelector.java	2001/03/16 20:01:50	1.1.2.2
  @@ -17,21 +17,18 @@
   import org.apache.avalon.ComponentSelector;
   import org.apache.avalon.Component;
   import org.apache.avalon.ComponentManagerException;
  -import org.apache.avalon.ComponentNotFoundException;
  -import org.apache.avalon.ComponentNotAccessibleException;
  -import org.apache.avalon.Recyclable;
   import org.apache.avalon.Context;
   import org.apache.avalon.Contextualizable;
   import org.apache.avalon.configuration.Configurable;
   import org.apache.avalon.configuration.Configuration;
  +import org.apache.avalon.Composer;
   import org.apache.avalon.configuration.ConfigurationException;
  -import org.apache.avalon.SingleThreaded;
  +import org.apache.avalon.configuration.DefaultConfiguration;
   import org.apache.avalon.ThreadSafe;
  -import org.apache.avalon.Poolable;
  -import org.apache.avalon.Disposable;
  -import org.apache.avalon.Composer;
   
   import org.apache.cocoon.util.ClassUtils;
  +import org.apache.cocoon.util.RoleUtils;
  +import org.apache.cocoon.Roles;
   
   import org.apache.log.Logger;
   import org.apache.avalon.Loggable;
  @@ -39,39 +36,34 @@
   /** Default component manager for Cocoon's non sitemap components.
    * @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.2.1 $ $Date: 2001/03/12 05:18:05 $
  + * @version CVS $Revision: 1.1.2.2 $ $Date: 2001/03/16 20:01:50 $
    */
   public class CocoonComponentSelector implements Contextualizable, ComponentSelector, Composer, Configurable, ThreadSafe, Loggable {
  +
       protected Logger log;
  -    /** Hashmap of all components which this ComponentManager knows about.
  -     */
  -    protected Map components;
   
  -    /** The app Context */
  +    /** The application context for components
  +     */
       protected Context context;
   
  -    /** Static component instances.
  +    /** The application context for components
        */
  -    private Map instances;
  +    private ComponentManager manager;
   
  -    /** Configurations for components.
  +    /** Static component handlers.
        */
  -    private Map configurations;
  -
  -    /** Component pools. */
  -    private Map pools;
  +    private Map componentMapping;
   
  -    /** Parent Component Manager */
  -    private ComponentManager manager;
  +    /** Static component handlers.
  +     */
  +    private Map componentHandlers;
   
       /** Construct a new default component manager.
        */
       public CocoonComponentSelector() {
           // Setup the maps.
  -        components = Collections.synchronizedMap(new HashMap());
  -        configurations = Collections.synchronizedMap(new HashMap());
  -        pools = Collections.synchronizedMap(new HashMap());
  -        instances = Collections.synchronizedMap(new HashMap());
  +        componentHandlers = Collections.synchronizedMap(new HashMap());
  +        componentMapping = Collections.synchronizedMap(new HashMap());
       }
   
       public void setLogger(Logger logger) {
  @@ -80,19 +72,16 @@
           }
       }
   
  -    /** Implement Composer interface
  -     */
  -    public void compose(ComponentManager manager)
  -    throws ComponentManagerException {
  -        if (this.manager == null) {
  -            this.manager = manager;
  +    public void contextualize(Context context) {
  +        if (this.context == null) {
  +            this.context = context;
           }
       }
   
  -    public void contextualize(Context context) {
  -      if (this.context == null) {
  -          this.context = context;
  -      }
  +    public void compose(ComponentManager manager) throws ComponentManagerException {
  +        if (this.manager == null) {
  +            this.manager = manager;
  +        }
       }
   
       /** Return an instance of a component.
  @@ -100,98 +89,30 @@
       public Component select( Object hint )
       throws ComponentManagerException {
   
  -        Component component;
  +        CocoonComponentHandler handler = null;
  +        Component component = null;
   
           if ( hint == null ) {
  -            log.error("CocoonComponentSelector Attempted to retrieve component with null hint.");
  -            throw new ComponentNotFoundException("Attempted to retrieve component with null hint.");
  +            log.error("CocoonComponentManager Attempted to retrieve component with null hint.");
  +            throw new ComponentManagerException("Attempted to retrieve component with null hint.");
           }
   
  +        handler = (CocoonComponentHandler) this.componentHandlers.get(hint);
           // Retrieve the instance of the requested component
  -        component = (Component) this.instances.get(hint);
  -
  -        if ( component != null ) {
  -            return component;
  -        }
  -
  -        // Retrieve the class of the requested component.
  -        Class componentClass = (Class)this.components.get(hint);
  -        if (componentClass == null) {
  -            log.error("CocoonComponentSelector Could not find component for hint '" + hint.toString() + "'.");
  -            throw new ComponentNotFoundException("Could not find component for hint '" + hint.toString() + "'.");
  -        }
  -
  -        if ( !Component.class.isAssignableFrom(componentClass) ) {
  -            log.error("CocoonComponentSelector Component with hint '" + hint.toString() + "' (" + componentClass.getName() + ")does not implement Component.");
  -            throw new ComponentNotAccessibleException(
  -                "Component with hint '" + hint.toString() + "' (" + componentClass.getName() + ")does not implement Component.",
  -                null
  -            );
  -        }
  -
  -        // Work out what class of component we're dealing with.
  -        if ( ThreadSafe.class.isAssignableFrom(componentClass)) {
  -            component = getThreadsafeComponent(hint, componentClass);
  -        } else if ( Poolable.class.isAssignableFrom(componentClass) ) {
  -            component = getPooledComponent(hint, componentClass);
  -        } else if ( SingleThreaded.class.isAssignableFrom(componentClass) ) {
  -            try {
  -                component = (Component)componentClass.newInstance();
  -            } catch ( InstantiationException e ) {
  -                log.error("CocoonComponentSelector Could not access class " + componentClass.getName(), e);
  -                throw new ComponentNotAccessibleException(
  -                    "Could not instantiate component " + componentClass.getName() + ": " + e.getMessage(),
  -                    e
  -                );
  -            } catch ( IllegalAccessException e ) {
  -                log.error("CocoonComponentSelector Could not access class " + componentClass.getName(), e);
  -                throw new ComponentNotAccessibleException(
  -                    "Could not access class " + componentClass.getName() + ": " + e.getMessage(),
  -                    e
  -                );
  -            }
  -            setupComponent(hint, component);
  -        } else {
  -            /* The component doesn't implement any of the Avalon marker
  -             * classes, treat as normal.
  -             */
  +        if ( handler != null ) {
               try {
  -                component = (Component)componentClass.newInstance();
  -            } catch ( InstantiationException e ) {
  -                log.error("CocoonComponentSelector Could not instantiate component " + componentClass.getName(), e);
  -                throw new ComponentNotAccessibleException(
  -                    "Could not instantiate component " + componentClass.getName() + ": " + e.getMessage(),
  -                    e
  -                );
  -            } catch ( IllegalAccessException e ) {
  -                log.error("CocoonComponentSelector Could not access class " + componentClass.getName(), e);
  -                throw new ComponentNotAccessibleException(
  -                    "Could not access class " + componentClass.getName() + ": " + e.getMessage(),
  -                    e
  -                );
  +                component = handler.get();
  +            } catch (Exception e) {
  +                throw new ComponentManagerException("Could not access the Component for you", e);
               }
  -            setupComponent(hint, component);
           }
  -
  -        return component;
  -    }
  -
  -    private Component getThreadsafeComponent(Object hint, Class component)
  -    throws ComponentManagerException {
  -
  -        Component retVal;
  -
  -        try {
  -            retVal = (Component) component.newInstance();
   
  -            this.setupComponent(hint, retVal);
  -            this.instances.put(hint, retVal);
  -        } catch (Exception e) {
  -            log.error("Could not set up the Component for hint: " + String.valueOf(hint), e);
  -            throw new ComponentNotAccessibleException("Could not set up the Component for hint: " + String.valueOf(hint), e);
  +        if (component != null) {
  +            this.componentMapping.put(component, handler);
  +            return component;
           }
   
  -        return retVal;
  +        throw new ComponentManagerException("Could not find the component for hint: " + hint);
       }
   
       public void configure(Configuration conf) throws ConfigurationException {
  @@ -211,118 +132,26 @@
           }
       }
   
  -    /** Return an instance of a component from its associated pool.
  -     * @param componentClass the class of the component of which we need an instance.
  -     */
  -    private Component getPooledComponent(Object hint, Class componentClass)
  -    throws ComponentManagerException {
  -        ComponentPool pool = (ComponentPool)pools.get(componentClass);
  -
  -        if ( pool == null ) {
  -            try {
  -                log.debug("Creating new pool for:" + componentClass);
  -                ComponentFactory cf = new ComponentFactory(componentClass, (Configuration)configurations.get(hint), this.manager, this.context);
  -                cf.setLogger(this.log);
  -
  -                pool = new ComponentPool(cf);
  -                pool.setLogger(this.log);
  -                pool.init();
  -            } catch (Exception e) {
  -                log.error("Could not create pool for component " + componentClass.getName(), e);
  -                throw new ComponentNotAccessibleException(
  -                    "Could not create pool for component " + componentClass.getName() + ": " + e.getMessage(),
  -                    e
  -                );
  -            }
  -
  -            pools.put(componentClass, pool);
  -        }
  -
  -        Component component;
  -        try {
  -            component = (Component)pool.get();
  -        } catch ( Exception e ) {
  -            log.error("Could not retrieve component ", e);
  -            throw new ComponentNotAccessibleException(
  -                "Could not retrieve component " + componentClass.getName() + " due to a " +
  -                e.getClass().getName() + ": " + e.getMessage(),
  -                e
  -            );
  -        }
  -
  -        return component;
  -    }
  -
       public void release(Component component) {
  -        if (
  -            component instanceof Disposable
  -            && ! ( component instanceof Poolable )
  -            && ! ( component instanceof ThreadSafe)
  -        ) {
  -            try {
  -                ((Disposable) component).dispose();
  -            } catch (Exception e) {
  -                this.log.warn(
  -                    "Could not dispose of instance of component " + component.getClass().getName() + ".",
  -                    e
  -                );
  -            }
  -        }
  -        
  -        if (component instanceof Poolable) {
  -            ComponentPool pool = (ComponentPool) pools.get(component.getClass());
  -
  -            if (pool != null) {
  -                pool.put((Poolable) component);
  -            } else {
  -                log.debug("Could not find pool for:" + component.getClass());
  -            }
  -        }
  -
  +        CocoonComponentHandler handler = (CocoonComponentHandler) this.componentMapping.get(component);
  +        handler.put(component);
  +        this.componentMapping.remove(component);
       }
   
  -    /** Configure a new component.
  -     * @param c the component to configure.
  -     */
  -    private void setupComponent(Object hint, Component c)
  -    throws ComponentManagerException {
  -
  -        if ( c instanceof Contextualizable ) {
  -            ((Contextualizable)c).contextualize(this.context);
  -        }
  -
  -        if ( c instanceof Loggable ) {
  -            ((Loggable)c).setLogger(this.log);
  -        }
  -
  -        if ( c instanceof Composer ) {
  -            ((Composer)c).compose(this.manager);
  -        }
  -
  -        if ( c instanceof Configurable ) {
  -            try {
  -                ((Configurable)c).configure(
  -                    (Configuration)this.configurations.get(hint)
  -                );
  -            } catch (ConfigurationException e) {
  -                log.error("CocoonComponentSelector Could not configure component " + c.getClass().getName(), e);
  -                throw new ComponentNotAccessibleException(
  -                    "Could not configure component " + c.getClass().getName() + ".",
  -                    e
  -                );
  -            }
  -        }
  -    }
  -
       /** Add a new component to the manager.
  -     * @param hint the hint for the new component.
  +     * @param hint the hint name for the new component.
        * @param component the class of this component.
        * @param Configuration the configuration for this component.
        */
  -    public void addComponent(Object hint, Class component, Configuration config) {
  -        this.components.put(hint,component);
  -        if ( config != null ) {
  -            this.configurations.put(hint, config);
  +    public void addComponent(Object hint, Class component, Configuration config)
  +    throws ComponentManagerException {
  +        try {
  +            CocoonComponentHandler handler = new CocoonComponentHandler(component, config, this.manager, this.context);
  +            handler.setLogger(this.log);
  +            handler.init();
  +            this.componentHandlers.put(hint, handler);
  +        } catch (Exception e) {
  +            throw new ComponentManagerException ("Could not set up Component for hint: " + hint, e);
           }
       }
   
  @@ -330,7 +159,14 @@
        * @param hint the hint name for the component.
        * @param instance the instance of the component.
        */
  -    public void addComponentInstance(Object hint, Component instance) {
  -        this.instances.put(hint,instance);
  +    public void addComponentInstance(String hint, Object instance) {
  +        try {
  +            CocoonComponentHandler handler = new CocoonComponentHandler((Component) instance);
  +            handler.setLogger(this.log);
  +            handler.init();
  +            this.componentHandlers.put(hint, handler);
  +        } catch (Exception e) {
  +            this.log.warn("Could not set up Component for hint: " + hint, e);
  +        }
       }
   }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org