bloritsch    01/01/18 13:34:37

  Modified:    src/org/apache/cocoon Tag: xml-cocoon2
                        CocoonComponentSelector.java
                        DefaultComponentManager.java
               webapp/docs/samples/forms Tag: xml-cocoon2 employee.xsp
  Log:
  Fixed many ills in the Component Management scheme
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.12  +38 -53    
xml-cocoon/src/org/apache/cocoon/Attic/CocoonComponentSelector.java
  
  Index: CocoonComponentSelector.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon/src/org/apache/cocoon/Attic/CocoonComponentSelector.java,v
  retrieving revision 1.1.2.11
  retrieving revision 1.1.2.12
  diff -u -r1.1.2.11 -r1.1.2.12
  --- CocoonComponentSelector.java      2001/01/17 18:33:35     1.1.2.11
  +++ CocoonComponentSelector.java      2001/01/18 21:34:28     1.1.2.12
  @@ -40,7 +40,7 @@
   /** Default component manager for Cocoon's non sitemap components.
    * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Paul Russell</a>
  - * @version CVS $Revision: 1.1.2.11 $ $Date: 2001/01/17 18:33:35 $
  + * @version CVS $Revision: 1.1.2.12 $ $Date: 2001/01/18 21:34:28 $
    */
   public class CocoonComponentSelector implements ComponentSelector, Composer, 
Configurable, ThreadSafe {
       protected Logger log = LogKit.getLoggerFor("cocoon");
  @@ -48,10 +48,6 @@
        */
       private Map components;
   
  -    /** Thread safe instances.
  -     */
  -    private Map threadSafeInstances;
  -
       /** Static component instances.
        */
       private Map instances;
  @@ -72,7 +68,6 @@
       public CocoonComponentSelector() {
           // Setup the maps.
           components = Collections.synchronizedMap(new HashMap());
  -        threadSafeInstances = Collections.synchronizedMap(new HashMap());
           configurations = Collections.synchronizedMap(new HashMap());
           pools = Collections.synchronizedMap(new HashMap());
           instances = Collections.synchronizedMap(new HashMap());
  @@ -99,18 +94,18 @@
               throw new ComponentNotFoundException("Attempted to retrieve 
component with null 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 ) {
  -            component = (Component)this.instances.get(hint);
  -            if ( component == null ) {
  -                log.error("CocoonComponentSelector Could not find component 
for hint '" + hint.toString() + "'.");
  -                throw new ComponentNotFoundException("Could not find 
component for hint '" + hint.toString() + "'.");
  -            } else {
  -                // we found an individual instance of a component.
  -                return component;
  -            }
  +        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) ) {
  @@ -142,7 +137,7 @@
                       e
                   );
               }
  -            setupComponent(component);
  +            setupComponent(hint, component);
           } else {
               /* The component doesn't implement any of the Avalon marker
                * classes, treat as normal.
  @@ -162,12 +157,32 @@
                       e
                   );
               }
  -            setupComponent(component);
  +            setupComponent(hint, component);
           }
   
           return component;
       }
   
  +    private Component getThreadsafeComponent(Object hint, Class component)
  +    throws ComponentManagerException {
  +
  +        Component retVal = (Component) this.instances.get(hint);
  +
  +        if (retVal == null) {
  +            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);
  +            }
  +        }
  +
  +        return retVal;
  +    }
  +
       public void configure(Configuration conf) throws ConfigurationException {
           log.debug("CocoonComponentSelector setting up with root element: " + 
conf.getName());
           Iterator instances = conf.getChildren("component-instance");
  @@ -186,36 +201,6 @@
           }
       }
   
  -    /** Retrieve an instance of a threadsafe component.
  -     * @param componentClass the class to retrieve an instance of.
  -     * @return and instance of the component.
  -     */
  -    private Component getThreadsafeComponent(Object hint, Class 
componentClass)
  -    throws ComponentManagerException {
  -        Component component = 
(Component)threadSafeInstances.get(componentClass);
  -
  -        if ( component == null ) {
  -            try {
  -                component = (Component)componentClass.newInstance();
  -            } catch ( InstantiationException e ) {
  -                log.error("Could not access component", e);
  -                throw new ComponentNotAccessibleException(
  -                    "Failed to instantiate component " + 
componentClass.getName() + ": " + e.getMessage(),
  -                    e
  -                );
  -            } catch ( IllegalAccessException e ) {
  -                log.error("Could not access component", e);
  -                throw new ComponentNotAccessibleException(
  -                    "Could not access component " + componentClass.getName() 
+ ": " + e.getMessage(),
  -                    e
  -                );
  -            }
  -            setupComponent(component);
  -            threadSafeInstances.put(hint,component);
  -        }
  -        return component;
  -    }
  -
       /** Return an instance of a component from its associated pool.
        * @param componentClass the class of the component of which we need an 
instance.
        */
  @@ -226,7 +211,7 @@
           if ( pool == null ) {
               try {
                   pool = new ComponentPool(
  -                    new ComponentFactory(componentClass, 
(Configuration)configurations.get(componentClass), this.manager),
  +                    new ComponentFactory(componentClass, 
(Configuration)configurations.get(hint), this.manager),
                       new ComponentPoolController()
                       );
               } catch (Exception e) {
  @@ -257,12 +242,12 @@
       /** Configure a new component.
        * @param c the component to configure.
        */
  -    private void setupComponent(Component c)
  +    private void setupComponent(Object hint, Component c)
       throws ComponentManagerException {
           if ( c instanceof Configurable ) {
               try {
                   ((Configurable)c).configure(
  -                    (Configuration)this.configurations.get(c.getClass())
  +                    (Configuration)this.configurations.get(hint)
                   );
               } catch (ConfigurationException e) {
                   log.error("CocoonComponentSelector Could not configure 
component " + c.getClass().getName(), e);
  @@ -286,7 +271,7 @@
       private void addComponent(Object hint, Class component, Configuration 
config) {
           this.components.put(hint,component);
           if ( config != null ) {
  -            this.configurations.put(component,config);
  +            this.configurations.put(hint, config);
           }
       }
   
  @@ -294,7 +279,7 @@
        * @param hint the hint name for the component.
        * @param instance the instance of the component.
        */
  -    public void addComponentInstance(Object hint, Object instance) {
  +    public void addComponentInstance(Object hint, Component instance) {
           this.instances.put(hint,instance);
       }
   }
  
  
  
  1.1.2.11  +41 -70    
xml-cocoon/src/org/apache/cocoon/Attic/DefaultComponentManager.java
  
  Index: DefaultComponentManager.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon/src/org/apache/cocoon/Attic/DefaultComponentManager.java,v
  retrieving revision 1.1.2.10
  retrieving revision 1.1.2.11
  diff -u -r1.1.2.10 -r1.1.2.11
  --- DefaultComponentManager.java      2001/01/17 18:33:35     1.1.2.10
  +++ DefaultComponentManager.java      2001/01/18 21:34:30     1.1.2.11
  @@ -38,7 +38,7 @@
   
   /** Default component manager for Cocoon's non sitemap components.
    * @author <a href="mailto:[EMAIL PROTECTED]">Paul Russell</a>
  - * @version CVS $Revision: 1.1.2.10 $ $Date: 2001/01/17 18:33:35 $
  + * @version CVS $Revision: 1.1.2.11 $ $Date: 2001/01/18 21:34:30 $
    */
   public class DefaultComponentManager implements ComponentManager, 
Configurable {
   
  @@ -48,10 +48,6 @@
        */
       private Map components;
   
  -    /** Thread safe instances.
  -     */
  -    private Map threadSafeInstances;
  -
       /** Static component instances.
        */
       private Map instances;
  @@ -69,7 +65,6 @@
       public DefaultComponentManager() {
           // Setup the maps.
           components = Collections.synchronizedMap(new HashMap());
  -        threadSafeInstances = Collections.synchronizedMap(new HashMap());
           configurations = Collections.synchronizedMap(new HashMap());
           pools = Collections.synchronizedMap(new HashMap());
           instances = Collections.synchronizedMap(new HashMap());
  @@ -77,45 +72,39 @@
   
       /** Return an instance of a component.
        */
  -    public Component lookup( String role ) throws
  -        ComponentManagerException {
  +    public Component lookup( String role )
  +    throws ComponentManagerException {
   
           Component component;
   
           if ( role == null ) {
  -            log.error("Attempted to retrieve a component with a null Role");
  -            throw new ComponentNotFoundException("Attempted to retrieve 
component will null roll.");
  +            log.error("DefaultComponentManager Attempted to retrieve 
component with null role.");
  +            throw new ComponentNotFoundException("Attempted to retrieve 
component with null role.");
  +        }
  +
  +        // Retrieve the instance of the requested component
  +        component = (Component) this.instances.get(role);
  +
  +        if ( component != null ) {
  +            return component;
           }
   
           // Retrieve the class of the requested component.
           Class componentClass = (Class)this.components.get(role);
   
  -        if ( componentClass == null ) {
  -            component = (Component)this.instances.get(role);
  -            if ( component == null ) {
  -                String className = RoleUtils.defaultClass(role);
  -                if (className == null) {
  -                    log.error(role + " could not be found");
  -                    throw new ComponentNotFoundException("Could not find 
component for role '" + role + "'.");
  -                }
  -                try {
  -                    componentClass = ClassUtils.loadClass(className);
  -                } catch (Exception e) {
  -                    throw new ComponentNotAccessibleException("Could not 
load component for role '" + role + "'.", e);
  -                }
  -                this.components.put(role, componentClass);
  -                if (Configurable.class.isAssignableFrom(componentClass)) {
  -                    this.configurations.put(role, new 
DefaultConfiguration("", "-"));
  -                }
  -            } else {
  -                // we found an individual instance of a component.
  -                log.debug("DefaultComponentManager returned instance for 
role " + role + ".");
  -                return component;
  +        if (componentClass == null) {
  +            try {
  +                componentClass = 
ClassUtils.loadClass(RoleUtils.defaultClass(role));
  +            } catch (Exception e) {
  +                log.error("DefaultComponentManager Could not find component 
for role '" + role + "'.", e);
  +                throw new ComponentNotFoundException("Could not find 
component for role '" + role + "'.", e);
               }
  +
  +            this.components.put(role, componentClass);
           }
   
           if ( !Component.class.isAssignableFrom(componentClass) ) {
  -            log.error("The object found is not a Component");
  +            log.error("DefaultComponentManager Component with role '" + role 
+ "' (" + componentClass.getName() + ")does not implement Component.");
               throw new ComponentNotAccessibleException(
                   "Component with role '" + role + "' (" + 
componentClass.getName() + ")does not implement Component.",
                   null
  @@ -123,28 +112,21 @@
           }
   
           // Work out what class of component we're dealing with.
  -        if ( ThreadSafe.class.isAssignableFrom(componentClass) ) {
  -            log.debug("DefaultComponentManager using threadsafe instance of 
" + componentClass.getName() + " for role " + role + ".");
  +        if ( ThreadSafe.class.isAssignableFrom(componentClass)) {
               component = getThreadsafeComponent(role, componentClass);
           } else if ( Poolable.class.isAssignableFrom(componentClass) ) {
  -            log.debug("DefaultComponentManager using poolable instance of "
  -                + componentClass.getName() + " for role " + role + "."
  -            );
  -            component = getPooledComponent(componentClass);
  +            component = getPooledComponent(role, componentClass);
           } else if ( SingleThreaded.class.isAssignableFrom(componentClass) ) {
               try {
  -                log.debug("DefaultComponentManager using new instance of 
single threaded component "
  -                    + componentClass.getName() + "for role " + role + "."
  -                );
                   component = (Component)componentClass.newInstance();
               } catch ( InstantiationException e ) {
  -                log.error("Could not create new instance of SingleThreaded " 
+ role, e);
  +                log.error("DefaultComponentManager Could not access class " 
+ componentClass.getName(), e);
                   throw new ComponentNotAccessibleException(
                       "Could not instantiate component " + 
componentClass.getName() + ": " + e.getMessage(),
                       e
                   );
               } catch ( IllegalAccessException e ) {
  -                log.error("Could not access class " + 
componentClass.getName(), e);
  +                log.error("DefaultComponentManager Could not access class " 
+ componentClass.getName(), e);
                   throw new ComponentNotAccessibleException(
                       "Could not access class " + componentClass.getName() + 
": " + e.getMessage(),
                       e
  @@ -156,18 +138,15 @@
                * classes, treat as normal.
                */
               try {
  -                log.debug("DefaultComponentManager using new instance of 
unmarked component "
  -                    + componentClass.getName() + " for role " + role + "."
  -                );
                   component = (Component)componentClass.newInstance();
               } catch ( InstantiationException e ) {
  -                log.error("Could not create new instance of class " + 
componentClass.getName(), e);
  +                log.error("DefaultComponentManager Could not instantiate 
component " + componentClass.getName(), e);
                   throw new ComponentNotAccessibleException(
                       "Could not instantiate component " + 
componentClass.getName() + ": " + e.getMessage(),
                       e
                   );
               } catch ( IllegalAccessException e ) {
  -                log.error("Could not access class " + 
componentClass.getName(), e);
  +                log.error("DefaultComponentManager Could not access class " 
+ componentClass.getName(), e);
                   throw new ComponentNotAccessibleException(
                       "Could not access class " + componentClass.getName() + 
": " + e.getMessage(),
                       e
  @@ -235,34 +214,26 @@
        */
       private Component getThreadsafeComponent(String role, Class 
componentClass)
       throws ComponentManagerException {
  -        Component component = (Component)threadSafeInstances.get(role);
   
  -        if ( component == null ) {
  -            try {
  -                component = (Component)componentClass.newInstance();
  -            } catch ( InstantiationException e ) {
  -                log.error("Failed to instantiate component " + 
componentClass.getName(), e);
  -                throw new ComponentNotAccessibleException(
  -                    "Failed to instantiate component " + 
componentClass.getName() + ": " + e.getMessage(),
  -                    e
  -                );
  -            } catch ( IllegalAccessException e ) {
  -                log.error("Could not access component " + 
componentClass.getName(), e);
  -                throw new ComponentNotAccessibleException(
  -                    "Could not access component " + componentClass.getName() 
+ ": " + e.getMessage(),
  -                    e
  -                );
  -            }
  -            setupComponent(role, component);
  -            threadSafeInstances.put(role, component);
  +        Component retVal;
  +
  +        try {
  +            retVal = (Component) componentClass.newInstance();
  +
  +            this.setupComponent(role, retVal);
  +            this.instances.put(role, retVal);
  +        } catch (Exception e) {
  +            log.error("Could not set up the Component for role: " + role, e);
  +            throw new ComponentNotAccessibleException("Could not set up the 
Component for role: " + role, e);
           }
  -        return component;
  +
  +        return retVal;
       }
   
       /** 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(Class componentClass)
  +    private Component getPooledComponent(String role, Class componentClass)
       throws ComponentManagerException {
           ComponentPool pool = (ComponentPool)pools.get(componentClass);
   
  @@ -270,7 +241,7 @@
               try {
                   log.debug("Creating new component pool for " + 
componentClass.getName() + ".");
                   pool = new ComponentPool(
  -                    new ComponentFactory(componentClass, 
(Configuration)configurations.get(componentClass), this),
  +                    new ComponentFactory(componentClass, 
(Configuration)configurations.get(role), this),
                       new ComponentPoolController()
                       );
               } catch (Exception e) {
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.7   +2 -1      xml-cocoon/webapp/docs/samples/forms/Attic/employee.xsp
  
  Index: employee.xsp
  ===================================================================
  RCS file: /home/cvs/xml-cocoon/webapp/docs/samples/forms/Attic/employee.xsp,v
  retrieving revision 1.1.2.6
  retrieving revision 1.1.2.7
  diff -u -r1.1.2.6 -r1.1.2.7
  --- employee.xsp      2001/01/16 21:44:20     1.1.2.6
  +++ employee.xsp      2001/01/18 21:34:34     1.1.2.7
  @@ -48,11 +48,12 @@
           <input type="hidden" name="employee"/>
           <para>Employee Name:
          <input name="name" type="text">
  -        <xsp:attribute name="value"><xsp:expr>name</xsp:expr></xsp:attribute>
  +        <xsp:attribute name="value"><xsp:expr>((name != null) ? name : 
"")</xsp:expr></xsp:attribute>
          </input>
        </para>
           <para>Department:
             <select name="department">
  +            <option value="-1">-- Select department --</option>
               <esql:connection>
                 <esql:pool>personnel</esql:pool>
                 <esql:execute-query>
  
  
  

Reply via email to