donaldp 01/04/10 21:18:10 Modified: src/java/org/apache/avalon/component DefaultComponentFactory.java DefaultComponentHandler.java DefaultComponentManager.java DefaultComponentPool.java DefaultComponentPoolController.java DefaultComponentSelector.java DefaultRoleManager.java RoleManager.java Added: src/java/org/apache/avalon/component ComponentException.java Log: Updated to Avalon style and substituted in ComponentException rather than ComponentManagerException in preparation for move to 4.0. Revision Changes Path 1.3 +86 -71 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.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- DefaultComponentFactory.java 2001/04/10 16:15:37 1.2 +++ DefaultComponentFactory.java 2001/04/11 04:18:09 1.3 @@ -1,129 +1,144 @@ -/***************************************************************************** - * Copyright (C) The Apache Software Foundation. All rights reserved. * - * ------------------------------------------------------------------------- * - * This software is published under the terms of the Apache Software License * - * version 1.1, a copy of which has been included with this distribution in * - * the LICENSE file. * - *****************************************************************************/ - +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE file. + */ package org.apache.avalon.component; -import org.apache.avalon.util.pool.ObjectFactory; -import org.apache.avalon.util.pool.Pool; - -import org.apache.avalon.configuration.Configuration; +import org.apache.avalon.AbstractLoggable; import org.apache.avalon.ComponentManager; -import org.apache.avalon.configuration.Configurable; import org.apache.avalon.Composer; -import org.apache.avalon.ThreadSafe; -import org.apache.avalon.Contextualizable; import org.apache.avalon.Context; -import org.apache.avalon.Poolable; -import org.apache.avalon.Initializable; +import org.apache.avalon.Contextualizable; import org.apache.avalon.Disposable; -import org.apache.avalon.Stoppable; -import org.apache.avalon.Startable; -import org.apache.avalon.AbstractLoggable; +import org.apache.avalon.Initializable; import org.apache.avalon.Loggable; +import org.apache.avalon.Poolable; +import org.apache.avalon.Startable; +import org.apache.avalon.Stoppable; +import org.apache.avalon.ThreadSafe; +import org.apache.avalon.configuration.Configurable; +import org.apache.avalon.configuration.Configuration; +import org.apache.avalon.util.pool.ObjectFactory; +import org.apache.avalon.util.pool.Pool; /** * Factory for Avalon components. * * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> * @author <a href="mailto:[EMAIL PROTECTED]">Paul Russell</a> - * @version CVS $Revision: 1.2 $ $Date: 2001/04/10 16:15:37 $ + * @version CVS $Revision: 1.3 $ $Date: 2001/04/11 04:18:09 $ */ -public class DefaultComponentFactory extends AbstractLoggable implements ObjectFactory, ThreadSafe { - +public class DefaultComponentFactory + extends AbstractLoggable + implements ObjectFactory, ThreadSafe +{ /** The class which this <code>ComponentFactory</code> * should create. */ - private Class componentClass; + private Class m_componentClass; - /** The configuration for this component. + /** The Context for the component */ - private Configuration conf; + private Context m_context; /** The component manager for this component. */ - private ComponentManager manager; + private ComponentManager m_componentManager; - /** The Context for the component + /** The configuration for this component. */ - private Context context; + private Configuration m_configuration; /** The RoleManager for child ComponentSelectors */ - private RoleManager roles; + private RoleManager m_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. + * @param configuration the <code>Configuration</code> object to pass to new instances. + * @param componentManager the component manager to pass to <code>Composer</code>s. */ - 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 DefaultComponentFactory( final Class componentClass, + final Configuration configuration, + final ComponentManager componentManager, + final Context context, + final RoleManager roles ) + { + m_componentClass = componentClass; + m_configuration = configuration; + m_componentManager = componentManager; + m_context = context; + m_roles = roles; } - public Object newInstance() throws Exception { - Object comp = componentClass.newInstance(); + public Object newInstance() + throws Exception + { + final Object component = m_componentClass.newInstance(); - getLogger().debug("ComponentFactory creating new instance of " - + componentClass.getName() + "." - ); + getLogger().debug( "ComponentFactory creating new instance of " + + m_componentClass.getName() + "." ); - if ( comp instanceof Loggable) { - ((Loggable)comp).setLogger(getLogger()); + if( component instanceof Loggable ) + { + ((Loggable)component).setLogger( getLogger() ); } - if ( comp instanceof Contextualizable ) { - ((Contextualizable)comp).contextualize(this.context); + if( component instanceof Contextualizable ) + { + ((Contextualizable)component).contextualize( m_context ); } - if ( comp instanceof Composer) { - ((Composer)comp).compose(this.manager); + if( component instanceof Composer ) + { + ((Composer)component).compose( m_componentManager ); } - if ( comp instanceof DefaultComponentSelector ) { - ((DefaultComponentSelector)comp).setRoleManager(this.roles); + if ( component instanceof DefaultComponentSelector ) + { + ((DefaultComponentSelector)component).setRoleManager( m_roles ); } - if ( comp instanceof Configurable ) { - ((Configurable)comp).configure(this.conf); + if( component instanceof Configurable ) + { + ((Configurable)component).configure( m_configuration ); } - if ( comp instanceof Initializable ) { - ((Initializable)comp).init(); + if( component instanceof Initializable ) + { + ((Initializable)component).init(); } - if ( comp instanceof Startable ) { - ((Startable)comp).start(); + if( component instanceof Startable ) + { + ((Startable)component).start(); } - return comp; + return component; } - public final Class getCreatedClass() { - return componentClass; + public final Class getCreatedClass() + { + return m_componentClass; } - public final void decommission(Object comp) throws Exception { - getLogger().debug("ComponentFactory decommissioning instance of " - + componentClass.getName() + "." - ); - - if ( comp instanceof Stoppable ) { - ((Stoppable)comp).stop(); + public final void decommission( final Object component ) + throws Exception + { + getLogger().debug( "ComponentFactory decommissioning instance of " + + m_componentClass.getName() + "." ); + + if( component instanceof Stoppable ) + { + ((Stoppable)component).stop(); } - if ( comp instanceof Disposable ) { - ((Disposable)comp).dispose(); + if( component instanceof Disposable ) + { + ((Disposable)component).dispose(); } - - comp = null; } } 1.3 +194 -131 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.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- DefaultComponentHandler.java 2001/04/10 16:15:38 1.2 +++ DefaultComponentHandler.java 2001/04/11 04:18:09 1.3 @@ -1,22 +1,22 @@ -/***************************************************************************** - * Copyright (C) The Apache Software Foundation. All rights reserved. * - * ------------------------------------------------------------------------- * - * This software is published under the terms of the Apache Software License * - * version 1.1, a copy of which has been included with this distribution in * - * the LICENSE file. * - *****************************************************************************/ - +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE file. + */ package org.apache.avalon.component; +import org.apache.avalon.AbstractLoggable; import org.apache.avalon.Component; import org.apache.avalon.ComponentManager; -import org.apache.avalon.configuration.Configuration; import org.apache.avalon.Context; import org.apache.avalon.Disposable; import org.apache.avalon.Initializable; -import org.apache.avalon.AbstractLoggable; import org.apache.avalon.Poolable; import org.apache.avalon.Stoppable; +import org.apache.avalon.ThreadSafe; +import org.apache.avalon.configuration.Configuration; import org.apache.log.Logger; /** @@ -24,51 +24,65 @@ * and destroyed correctly. * * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> - * @version CVS $Revision: 1.2 $ $Date: 2001/04/10 16:15:38 $ + * @version CVS $Revision: 1.3 $ $Date: 2001/04/11 04:18:09 $ */ -class DefaultComponentHandler extends AbstractLoggable implements Initializable, Disposable { +class DefaultComponentHandler + extends AbstractLoggable + implements Initializable, Disposable +{ /** Indicates that the Handler is holding a <code>ThreadSafe</code> Component */ - final static int THREADSAFE = 0; + private final static int THREADSAFE = 0; /** Indicates that the Handler is holding a <code>Poolable</code> Component */ - final static int POOLABLE = 1; + private final static int POOLABLE = 1; /** Indicates that the Handler is holding a <code>SingleThreaded</code> Component */ - final static int SINGLETHREADED = 2; + private final static int SINGLETHREADED = 2; /** The instance of the ComponentFactory that creates and disposes of the Component */ - private DefaultComponentFactory factory; + private DefaultComponentFactory m_factory; /** The pool of components for <code>Poolable</code> Components */ - private DefaultComponentPool pool; + private DefaultComponentPool m_pool; /** The instance of the Component for <code>ThreadSafe</code> Components */ - private Component instance; + private Component m_instance; /** The type of the Component: THREADSAFE, POOLABLE, or SINGLETHREADED */ - private final int type; + private final int m_type; /** State management boolean stating whether the Handler is initialized or not */ - private boolean initialized = false; + private boolean m_initialized = false; /** State management boolean stating whether the Handler is disposed or not */ - private boolean disposed = false; + private boolean m_disposed = false; /** * Create a ComponentHandler that takes care of hiding the details of * 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, 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); - this.type = DefaultComponentHandler.POOLABLE; - } else if (org.apache.avalon.ThreadSafe.class.isAssignableFrom(componentClass)) { - this.type = DefaultComponentHandler.THREADSAFE; - } else { - this.type = DefaultComponentHandler.SINGLETHREADED; + DefaultComponentHandler( final Class componentClass, + final Configuration config, + final ComponentManager manager, + final Context context, + final RoleManager roles ) + throws Exception + { + m_factory = new DefaultComponentFactory( componentClass, config, manager, context, roles ); + + if( Poolable.class.isAssignableFrom( componentClass ) ) + { + m_pool = new DefaultComponentPool( m_factory ); + m_type = POOLABLE; + } + else if( ThreadSafe.class.isAssignableFrom( componentClass ) ) + { + m_type = THREADSAFE; + } + else + { + m_type = SINGLETHREADED; } } @@ -77,155 +91,204 @@ * whether a Component is ThreadSafe, Poolable, or SingleThreaded. * It falls back to SingleThreaded if not specified. */ - DefaultComponentHandler(Component comp) throws Exception { - this.type = DefaultComponentHandler.THREADSAFE; - this.instance = comp; + DefaultComponentHandler( final Component component ) + throws Exception + { + m_type = THREADSAFE; + m_instance = component; } /** * Sets the logger that the ComponentHandler will use. */ - public void setLogger(Logger log) { - if (this.factory != null) { - this.factory.setLogger(log); + public void setLogger( final Logger logger ) + { + if( null != m_factory ) + { + m_factory.setLogger( logger ); } - if (this.pool != null) { - this.pool.setLogger(log); + if( null != m_pool ) + { + m_pool.setLogger( logger ); } - super.setLogger(log); + super.setLogger( logger ); } /** * Initialize the ComponentHandler. */ - public void init() { - if (this.initialized) return; - - switch (this.type) { - case DefaultComponentHandler.THREADSAFE: - try { - if (this.instance == null) { - this.instance = (Component)this.factory.newInstance(); - } - } catch (Exception e) { - getLogger().error("Cannot use component: " + this.factory.getCreatedClass().getName(), e); + public void init() + { + if( m_initialized ) return; + + switch( m_type ) + { + case THREADSAFE: + try + { + if( null == m_instance ) + { + m_instance = (Component)m_factory.newInstance(); } - break; - case DefaultComponentHandler.POOLABLE: - try { - this.pool.init(); - } catch (Exception e) { - getLogger().error("Cannot use component: " + this.factory.getCreatedClass().getName(), e); - } - break; - default: - // Nothing to do for SingleThreaded Components - break; + } + catch( final Exception e ) + { + getLogger().error( "Cannot use component: " + + m_factory.getCreatedClass().getName(), e ); + } + break; + + case POOLABLE: + try + { + m_pool.init(); + } + catch( Exception e ) + { + getLogger().error( "Cannot use component: " + m_factory.getCreatedClass().getName(), e ); + } + break; + + default: + // Nothing to do for SingleThreaded Components + break; } - this.initialized = true; + + m_initialized = true; } /** * Get a reference of the desired Component */ - public Component get() throws Exception { - if (! this.initialized) { - throw new IllegalStateException("You cannot get a component from an uninitialized holder."); + public Component get() + throws Exception + { + if( ! m_initialized ) + { + throw new IllegalStateException( "You cannot get a component from an uninitialized holder." ); } - if (this.disposed) { - throw new IllegalStateException("You cannot get a component from a disposed holder"); + if( m_disposed ) + { + throw new IllegalStateException( "You cannot get a component from a disposed holder" ); } - Component comp = null; + Component component = null; - switch (this.type) { - case DefaultComponentHandler.THREADSAFE: - comp = this.instance; - break; - case DefaultComponentHandler.POOLABLE: - comp = (Component)this.pool.get(); - break; - default: - comp = (Component)this.factory.newInstance(); - break; + switch( m_type ) + { + case THREADSAFE: + component = m_instance; + break; + + case POOLABLE: + component = (Component)m_pool.get(); + break; + + default: + component = (Component)m_factory.newInstance(); + break; } - return comp; + return component; } /** * Return a reference of the desired Component */ - public void put(Component comp) { - if (! this.initialized) { - throw new IllegalStateException("You cannot put a component in an uninitialized holder."); + public void put( final Component component ) + { + if( !m_initialized ) + { + throw new IllegalStateException( "You cannot put a component in an uninitialized holder." ); } - if (this.disposed) { - throw new IllegalStateException("You cannot put a component in a disposed holder"); + if( m_disposed ) + { + throw new IllegalStateException( "You cannot put a component in a disposed holder" ); } - switch (this.type) { - case DefaultComponentHandler.THREADSAFE: - // Nothing to do for ThreadSafe Components - break; - case DefaultComponentHandler.POOLABLE: - this.pool.put((Poolable) comp); - break; - default: - try { - this.factory.decommission(comp); - } catch (Exception e) { - getLogger().warn("Error decommissioning component: " + this.factory.getCreatedClass().getName(), e); - } - break; + switch( m_type ) + { + case THREADSAFE: + // Nothing to do for ThreadSafe Components + break; + + case POOLABLE: + m_pool.put( (Poolable)component ); + break; + + default: + try + { + m_factory.decommission( component ); + } + catch( final Exception e ) + { + getLogger().warn( "Error decommissioning component: " + + m_factory.getCreatedClass().getName(), e); + } + break; } } /** * Dispose of the ComponentHandler and any associated Pools and Factories. */ - public void dispose() { - this.disposed = true; - - try { - switch (this.type) { - case DefaultComponentHandler.THREADSAFE: - if (this.factory != null) { - this.factory.decommission(this.instance); - } else { - if ( this.instance instanceof Stoppable ) { - ((Stoppable) this.instance).stop(); - } - - if ( this.instance instanceof Disposable ) { - ((Disposable) this.instance).dispose(); - } + public void dispose() + { + m_disposed = true; + + try + { + switch( m_type ) + { + case THREADSAFE: + if( null != m_factory ) + { + m_factory.decommission( m_instance ); + } + else + { + if( m_instance instanceof Stoppable ) + { + ((Stoppable)m_instance).stop(); } - this.instance = null; - break; - case DefaultComponentHandler.POOLABLE: - if (this.pool instanceof Disposable) { - ((Disposable) this.pool).dispose(); + + if( m_instance instanceof Disposable ) + { + ((Disposable)m_instance).dispose(); } + } + m_instance = null; + break; - this.pool = null; - break; - default: - // do nothing here - break; - } + case POOLABLE: + if( m_pool instanceof Disposable ) + { + ((Disposable)m_pool).dispose(); + } - if (this.factory instanceof Disposable) { - ((Disposable) this.factory).dispose(); + m_pool = null; + break; + + default: + // do nothing here + break; } - this.factory = null; - } catch (Exception e) { - getLogger().warn("Error decommissioning component: " + this.factory.getCreatedClass().getName(), e); + if( m_factory instanceof Disposable ) + { + ((Disposable)m_factory).dispose(); + } + m_factory = null; + } + catch( final Exception e ) + { + getLogger().warn( "Error decommissioning component: " + + m_factory.getCreatedClass().getName(), e ); } } } 1.5 +205 -124 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.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- DefaultComponentManager.java 2001/04/10 16:15:38 1.4 +++ DefaultComponentManager.java 2001/04/11 04:18:10 1.5 @@ -1,100 +1,104 @@ -/***************************************************************************** - * Copyright (C) The Apache Software Foundation. All rights reserved. * - * ------------------------------------------------------------------------- * - * This software is published under the terms of the Apache Software License * - * version 1.1, a copy of which has been included with this distribution in * - * the LICENSE file. * - *****************************************************************************/ - +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE file. + */ package org.apache.avalon.component; -import java.util.HashMap; -import java.util.Map; +import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; -import java.util.ArrayList; import java.util.List; - -import org.apache.avalon.ComponentManager; +import java.util.Map; +import org.apache.avalon.AbstractLoggable; import org.apache.avalon.Component; +import org.apache.avalon.ComponentManager; import org.apache.avalon.ComponentManagerException; +import org.apache.avalon.Composer; import org.apache.avalon.Context; import org.apache.avalon.Contextualizable; +import org.apache.avalon.Disposable; +import org.apache.avalon.Initializable; 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.configuration.DefaultConfiguration; -import org.apache.avalon.Disposable; -import org.apache.avalon.Initializable; -import org.apache.avalon.AbstractLoggable; /** * Default component manager for Avalon's components. * * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> * @author <a href="mailto:[EMAIL PROTECTED]">Paul Russell</a> - * @version CVS $Revision: 1.4 $ $Date: 2001/04/10 16:15:38 $ + * @version CVS $Revision: 1.5 $ $Date: 2001/04/11 04:18:10 $ */ -public class DefaultComponentManager extends AbstractLoggable - implements ComponentManager, Configurable, Contextualizable, Disposable { - +public class DefaultComponentManager + extends AbstractLoggable + implements ComponentManager, Configurable, Contextualizable, Disposable +{ /** The application context for components */ - private Context context; + private Context m_context; /** Static component mapping handlers. */ - private Map componentMapping; + private Map m_componentMapping; /** Static component handlers. */ - private Map componentHandlers; + private Map m_componentHandlers; /** RoleInfos. */ - private RoleManager roles; + private RoleManager m_roles; /** Is the Manager disposed or not? */ - private boolean disposed = false; + private boolean m_disposed; - /** Construct a new default component manager. - */ - public DefaultComponentManager() { + public DefaultComponentManager() + { // Setup the maps. - componentHandlers = Collections.synchronizedMap(new HashMap()); - componentMapping = Collections.synchronizedMap(new HashMap()); + m_componentHandlers = Collections.synchronizedMap( new HashMap() ); + m_componentMapping = Collections.synchronizedMap( new HashMap() ); } /** Set up the Component's Context. */ - public void contextualize(Context context) { - if (this.context == null) { - this.context = context; + public void contextualize( final Context context ) + { + //HACK: Is this really needed ??? (Isn't a symtom of fault elsewhere in system) + if( null == m_context ) + { + m_context = context; } } /** Properly dispose of the Child handlers. */ - public synchronized void dispose() { - this.disposed = true; - - Iterator keys = this.componentHandlers.keySet().iterator(); - List keyList = new ArrayList(); - - while (keys.hasNext()) { - Object key = keys.next(); - DefaultComponentHandler handler = (DefaultComponentHandler) - this.componentHandlers.get(key); + public synchronized void dispose( ) + { + m_disposed = true; + + Iterator keys = m_componentHandlers.keySet().iterator(); + final List keyList = new ArrayList(); + + while( keys.hasNext() ) + { + final Object key = keys.next(); + final DefaultComponentHandler handler = + (DefaultComponentHandler)m_componentHandlers.get( key ); handler.dispose(); - keyList.add(key); + keyList.add( key ); } keys = keyList.iterator(); - while (keys.hasNext()) { - this.componentHandlers.remove(keys.next()); + while( keys.hasNext() ) + { + m_componentHandlers.remove( keys.next() ); } keyList.clear(); @@ -105,94 +109,150 @@ * Fully Qualified Name(FQN)--unless there are multiple Components for the same Role. In that * case, the Role's FQN is appended with "Selector", and we return a ComponentSelector. */ - public Component lookup( String role ) - throws ComponentManagerException { + public Component lookup( final String role ) + throws ComponentException + { + + if( m_disposed ) + { + throw new IllegalStateException( "You cannot lookup components " + + "on a disposed ComponentManager" ); + } - if (disposed) throw new IllegalStateException("You cannot lookup components on a disposed ComponentManager"); + if( null == role ) + { + final String message = + "ComponentManager Attempted to retrieve component with null role."; + getLogger().error( message ); + throw new ComponentException( message ); + } - DefaultComponentHandler handler = null; - Component component = null; + DefaultComponentHandler handler = (DefaultComponentHandler)m_componentHandlers.get( role ); - handler = (DefaultComponentHandler) this.componentHandlers.get(role); // Retrieve the instance of the requested component - if ( handler == null ) { - getLogger().debug("Could not find ComponentHandler, attempting to create one for role: " + role); - Class componentClass = null; - Configuration config = new DefaultConfiguration("", "-"); - - try { - componentClass = this.getClass().getClassLoader().loadClass(this.roles.getDefaultClassNameForRole(role)); + if( null == handler ) + { + getLogger().debug( "Could not find ComponentHandler, " + + "attempting to create one for role: " + role ); + + try + { + final String className = m_roles.getDefaultClassNameForRole( role ); + final Class componentClass = + getClass().getClassLoader().loadClass( className ); + + final Configuration configuration = new DefaultConfiguration( "", "-" ); + + handler = + new DefaultComponentHandler( componentClass, + configuration, + this, + m_context, + m_roles ); - handler = new DefaultComponentHandler(componentClass, config, this, this.context, this.roles); - handler.setLogger(getLogger()); + handler.setLogger( getLogger() ); handler.init(); - } catch (Exception e) { - getLogger().error("ComponentManager Could not find component for role: " + role, e); - throw new ComponentManagerException("Could not find component for role: " + role, e); + } + catch( final Exception e ) + { + final String message = + "ComponentManager Could not find component for role: " + role; + getLogger().error( message, e ); + throw new ComponentException( message, e ); } - this.componentHandlers.put(role, handler); + m_componentHandlers.put( role, handler ); } - try { + Component component = null; + + try + { component = handler.get(); - if (component instanceof DefaultComponentSelector) { - ((DefaultComponentSelector) component).setRoleManager(this.roles); + if( component instanceof DefaultComponentSelector ) + { + ((DefaultComponentSelector)component).setRoleManager( m_roles ); } - } catch (IllegalStateException ise) { + } + catch( final IllegalStateException ise ) + { handler.init(); - try { + try + { component = handler.get(); - } catch (Exception ee) { - throw new ComponentManagerException("Could not access the Component for role: " + role, ee); + } + catch( final Exception e ) + { + final String message = "Could not access the Component for role: " + role; + throw new ComponentException( message, e ); } - } catch (Exception e) { - throw new ComponentManagerException("Could not access the Component for role: " + role, e); + } + catch( final Exception e ) + { + final String message = "Could not access the Component for role: " + role; + throw new ComponentException( message, e ); } - this.componentMapping.put(component, handler); + m_componentMapping.put(component, handler); return component; } /** * Configure the ComponentManager. */ - public void configure(Configuration conf) throws ConfigurationException { - if (this.roles == null) { + public void configure( final Configuration configuration ) + throws ConfigurationException + { + if( null != m_roles ) + { DefaultRoleManager role_info = new DefaultRoleManager(); - role_info.setLogger(getLogger()); - role_info.configure(conf); - this.roles = role_info; + role_info.setLogger( getLogger() ); + role_info.configure( configuration ); + m_roles = role_info; } // Set components - Configuration[] e = conf.getChildren(); - for (int i = 0; i < e.length; i++) { - String type = e[i].getName(); // types are already trimmed - - if (("role".equals(type) == false)) { - String role = e[i].getAttribute("role", ""); - String className = e[i].getAttribute("class", ""); + final Configuration[] configurations = configuration.getChildren(); - if ("".equals(role)) { - role = this.roles.getRoleForName(type); + for( int i = 0; i < configurations.length; i++ ) + { + String type = configurations[i].getName(); // types are already trimmed + + if( !type.equals( "role" ) ) + { + String role = configurations[ i ].getAttribute( "role", "" ); + String className = configurations[ i ].getAttribute( "class", "" ); + + if( role.equals( "" ) ) + { + role = m_roles.getRoleForName( type ); } - if (role != null && ("".equals(role) == false)) { - if ("".equals(className)) { - className = this.roles.getDefaultClassNameForRole(role); + if( null != role && !role.equals( "" ) ) + { + if( className.equals( "" ) ) + { + className = m_roles.getDefaultClassNameForRole( role ); } - try { - getLogger().debug("Adding component (" + role + " = " + className + ")"); - this.addComponent(role, this.getClass().getClassLoader().loadClass(className), e[i]); - } catch ( Exception ex ) { - getLogger().error("Could not load class " + className, ex); - throw new ConfigurationException("Could not get class " + className - + " for role " + role + " on configuration element " + e[i].getName(), ex); + try + { + getLogger().debug( "Adding component (" + role + " = " + className + ")" ); + final Class clazz = + getClass().getClassLoader().loadClass( className ); + addComponent( role, clazz, configurations[ i ] ); + } + catch( final Exception e ) + { + final String message = + "Could not get class " + className + " for role " + role + + " on configuration element " + configurations[ i ].getName(); + + getLogger().error( message, e ); + throw new ConfigurationException( message, e ); } } } @@ -202,9 +262,12 @@ /** * Configure the RoleManager */ - public void setRoleManager(RoleManager roles) { - if (this.roles == null) { - this.roles = roles; + public void setRoleManager( final RoleManager roles ) + { + //HACK: Is this really necessary??? + if( null == m_roles ) + { + m_roles = roles; } } @@ -212,12 +275,18 @@ * 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. */ - public void release(Component component) { - if (component == null) return; - DefaultComponentHandler handler = (DefaultComponentHandler) this.componentMapping.get(component); - if (handler == null) return; - handler.put(component); - this.componentMapping.remove(component); + public void release( final Component component ) + { + if( null == component ) return; + + final DefaultComponentHandler handler = + (DefaultComponentHandler)m_componentMapping.get( component ); + + if( null != handler ) + { + handler.put( component ); + m_componentMapping.remove( component ); + } } /** Add a new component to the manager. @@ -225,14 +294,22 @@ * @param component the class of this component. * @param Configuration the configuration for this component. */ - public void addComponent(String role, Class component, Configuration config) - throws ComponentManagerException { - try { - DefaultComponentHandler handler = new DefaultComponentHandler(component, config, this, this.context, this.roles); - handler.setLogger(getLogger()); - this.componentHandlers.put(role, handler); - } catch (Exception e) { - throw new ComponentManagerException ("Could not set up Component for role: " + role, e); + public void addComponent( final String role, + final Class component, + final Configuration configuration ) + throws ComponentException + { + try + { + final DefaultComponentHandler handler = + new DefaultComponentHandler( component, configuration, this, m_context, m_roles ); + + handler.setLogger( getLogger() ); + m_componentHandlers.put( role, handler ); + } + catch( final Exception e ) + { + throw new ComponentException( "Could not set up Component for role: " + role, e ); } } @@ -240,13 +317,17 @@ * @param role the role name for the component. * @param instance the instance of the component. */ - public void addComponentInstance(String role, Object instance) { - try { - DefaultComponentHandler handler = new DefaultComponentHandler((Component) instance); - handler.setLogger(getLogger()); - this.componentHandlers.put(role, handler); - } catch (Exception e) { - getLogger().warn("Could not set up Component for role: " + role, e); + public void addComponentInstance( final String role, final Object instance ) + { + try + { + DefaultComponentHandler handler = new DefaultComponentHandler( (Component)instance ); + handler.setLogger( getLogger() ); + m_componentHandlers.put( role, handler ); + } + catch( final Exception e ) + { + getLogger().warn( "Could not set up Component for role: " + role, e ); } } } 1.3 +181 -120 jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentPool.java Index: DefaultComponentPool.java =================================================================== RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentPool.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- DefaultComponentPool.java 2001/04/06 18:12:10 1.2 +++ DefaultComponentPool.java 2001/04/11 04:18:10 1.3 @@ -7,18 +7,17 @@ */ package org.apache.avalon.component; -import java.util.List; import java.util.ArrayList; - +import java.util.List; +import org.apache.avalon.AbstractLoggable; +import org.apache.avalon.Disposable; +import org.apache.avalon.Initializable; import org.apache.avalon.Poolable; +import org.apache.avalon.Recyclable; import org.apache.avalon.ThreadSafe; -import org.apache.avalon.Initializable; -import org.apache.avalon.Disposable; -import org.apache.avalon.util.pool.Pool; -import org.apache.avalon.util.pool.ObjectFactory; import org.apache.avalon.util.Lock; -import org.apache.avalon.Recyclable; -import org.apache.avalon.AbstractLoggable; +import org.apache.avalon.util.pool.ObjectFactory; +import org.apache.avalon.util.pool.Pool; /** * This is a implementation of <code>Pool</code> for SitemapComponents @@ -27,90 +26,117 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> * @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a> */ -public class DefaultComponentPool extends AbstractLoggable implements Pool, Initializable, Disposable, Runnable, ThreadSafe { - - public final static int DEFAULT_POOL_SIZE = 8; +public class DefaultComponentPool + extends AbstractLoggable + implements Pool, Initializable, Disposable, Runnable, ThreadSafe +{ + public final static int DEFAULT_POOL_SIZE = 8; /** The resources that are currently free */ - protected List availableResources = new ArrayList(); + protected List m_availableResources = new ArrayList(); /** Resources that have been allocated out of the pool */ - protected List usedResources = new ArrayList(); + protected List m_usedResources = new ArrayList(); - private boolean initialized = false; - private boolean disposed = false; + private Lock m_mutex = new Lock(); - private Lock mutex = new Lock(); - private Thread initializationThread; + private boolean m_initialized; + private boolean m_disposed; + private Thread m_initializationThread; + protected ObjectFactory m_factory; + protected int m_initial = DEFAULT_POOL_SIZE/2; + protected int m_maximum = DEFAULT_POOL_SIZE; - protected ObjectFactory factory = null; - - protected int initial = DEFAULT_POOL_SIZE/2; - - protected int maximum = DEFAULT_POOL_SIZE; - - public DefaultComponentPool(final ObjectFactory factory) throws Exception { - init(factory, DEFAULT_POOL_SIZE/2, DEFAULT_POOL_SIZE); + public DefaultComponentPool( final ObjectFactory factory ) + throws Exception + { + init( factory, DEFAULT_POOL_SIZE/2, DEFAULT_POOL_SIZE ); } - public DefaultComponentPool(final ObjectFactory factory, - final int initial) throws Exception { - init(factory, initial, initial); + public DefaultComponentPool( final ObjectFactory factory, + final int initial ) + throws Exception + { + init( factory, initial, initial ); } - public DefaultComponentPool(final ObjectFactory factory, - final int initial, - final int maximum) throws Exception { - init(factory, initial, maximum); + public DefaultComponentPool( final ObjectFactory factory, + final int initial, + final int maximum ) + throws Exception + { + init( factory, initial, maximum ); } - private void init(final ObjectFactory factory, - final int initial, - final int maximum) throws Exception { - this.factory = factory; - this.initial = initial; - this.maximum = maximum; + private void init( final ObjectFactory factory, + final int initial, + final int maximum ) + throws Exception + { + m_factory = factory; + m_initial = initial; + m_maximum = maximum; } - public void init() throws Exception { - this.initializationThread = new Thread(this); - this.initializationThread.start(); + public void init() + throws Exception + { + m_initializationThread = new Thread( this ); + m_initializationThread.start(); } - public void run() { - try { - this.mutex.lock(); - - for( int i = 0; i < this.initial; i++ ) { - try { - this.availableResources.add(this.factory.newInstance()); - } catch (Exception e) { - getLogger().warn("Could not create poolable resource", e); + public void run() + { + try + { + m_mutex.lock(); + + for( int i = 0; i < m_initial; i++ ) + { + try + { + m_availableResources.add( m_factory.newInstance() ); + } + catch( final Exception e ) + { + getLogger().warn( "Could not create poolable resource", e ); } } - if (this.availableResources.size() > 0) { - this.initialized = true; + if( m_availableResources.size() > 0 ) + { + m_initialized = true; } - } catch (Exception e) { - getLogger().debug("ComponentPool.run()", e); - } finally { - this.mutex.unlock(); + } + catch( final Exception e ) + { + getLogger().debug( "ComponentPool.run()", e ); + } + finally + { + m_mutex.unlock(); } } - public void dispose() { - try { - this.mutex.lock(); - this.disposed = true; - - while ( ! this.availableResources.isEmpty() ) { - this.availableResources.remove(0); + public void dispose() + { + try + { + m_mutex.lock(); + m_disposed = true; + + while( !m_availableResources.isEmpty() ) + { + m_availableResources.remove( 0 ); } - } catch (Exception e) { - getLogger().debug("ComponentPool.dispose()", e); - } finally { - this.mutex.unlock(); + } + catch( final Exception e ) + { + getLogger().debug( "ComponentPool.dispose()", e ); + } + finally + { + m_mutex.unlock(); } } @@ -122,12 +148,14 @@ * * @return A new resource */ - protected Poolable getOverflowResource() throws Exception { - Poolable poolable = (Poolable) this.factory.newInstance(); - getLogger().debug("Component Pool - creating Overflow Resource:" - + " Resource=" + poolable - + " Available=" + availableResources.size() - + " Used=" + usedResources.size() ); + protected Poolable getOverflowResource() + throws Exception + { + final Poolable poolable = (Poolable)m_factory.newInstance(); + getLogger().debug( "Component Pool - creating Overflow Resource:" + + " Resource=" + poolable + + " Available=" + m_availableResources.size() + + " Used=" + m_usedResources.size() ); return poolable; } @@ -135,45 +163,63 @@ * No extra information is associated with the allocated resource. * @return The allocated resource */ - public Poolable get() throws Exception { - if (! this.initialized) { - if (this.initializationThread == null) { - throw new IllegalStateException("You cannot get a resource before the pool is initialized"); - } else { - this.initializationThread.join(); - this.initializationThread = null; + public Poolable get() + throws Exception + { + if( !m_initialized ) + { + if( null == m_initializationThread ) + { + throw new IllegalStateException( "You cannot get a resource before " + + "the pool is initialized" ); + } + else + { + m_initializationThread.join(); + m_initializationThread = null; } } - if (this.disposed) { + if( m_disposed ) + { throw new IllegalStateException("You cannot get a resource after the pool is disposed"); } Poolable resource = null; - try { - this.mutex.lock(); + try + { + m_mutex.lock(); // See if there is a resource in the pool already - - if (this.availableResources.size() > 0) { - resource = (Poolable)this.availableResources.remove(0); - - this.usedResources.add(resource); - } else { - resource = this.getOverflowResource(); - if (resource != null) { - this.usedResources.add(resource); + if( m_availableResources.size() > 0 ) + { + resource = (Poolable)m_availableResources.remove( 0 ); + + m_usedResources.add( resource ); + } + else + { + resource = getOverflowResource(); + + if( null != resource ) + { + m_usedResources.add( resource ); } } - } catch (Exception e) { - getLogger().debug("ComponentPool.get()", e); - } finally { - this.mutex.unlock(); + } + catch( final Exception e ) + { + getLogger().debug( "ComponentPool.get()", e ); + } + finally + { + m_mutex.unlock(); } - if (resource == null) { - throw new RuntimeException("Could not get the component from the pool"); + if( null == resource ) + { + throw new RuntimeException( "Could not get the component from the pool" ); } return resource; @@ -182,46 +228,61 @@ /** Releases a resource back to the pool of available resources * @param resource The resource to be returned to the pool */ - public void put(Poolable resource) + public void put( Poolable resource ) { int pos = -1; - try { - this.mutex.lock(); + try + { + m_mutex.lock(); // Make sure the resource is in the used list - pos = usedResources.indexOf(resource); + pos = m_usedResources.indexOf( resource ); - if (resource instanceof Recyclable) { + if( resource instanceof Recyclable ) + { ((Recyclable)resource).recycle(); } // If the resource was in the used list, remove it from the used list and // add it back to the free list - if (pos >= 0) { - this.usedResources.remove(pos); + if( pos >= 0 ) + { + m_usedResources.remove( pos ); - if (this.availableResources.size() < this.maximum) { + if( m_availableResources.size() < m_maximum ) + { // If the available resources are below the maximum add this back. - this.availableResources.add(resource); - } else { + m_availableResources.add( resource ); + } + else + { // If the available are above the maximum destroy this resource. - try { - this.factory.decommission(resource); - getLogger().debug("Component Pool - decommissioning Overflow Resource:" - + " Resource=" + resource - + " Available=" + availableResources.size() - + " Used=" + usedResources.size() ); + try + { + m_factory.decommission( resource ); + + getLogger().debug( "Component Pool - decommissioning Overflow Resource:" + + " Resource=" + resource + + " Available=" + m_availableResources.size() + + " Used=" + m_usedResources.size() ); resource = null; - } catch (Exception e) { - throw new RuntimeException("caught exception decommissioning resource: " + resource); + } + catch( final Exception e ) + { + throw new RuntimeException( "caught exception decommissioning " + + "resource: " + resource); } } } - } catch (Exception e) { - getLogger().debug("ComponentPool.put()", e); - } finally { - this.mutex.unlock(); + } + catch( final Exception e ) + { + getLogger().debug( "ComponentPool.put()", e ); + } + finally + { + m_mutex.unlock(); } } } 1.2 +32 -35 jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentPoolController.java Index: DefaultComponentPoolController.java =================================================================== RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/component/DefaultComponentPoolController.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- DefaultComponentPoolController.java 2001/04/05 19:40:45 1.1 +++ DefaultComponentPoolController.java 2001/04/11 04:18:10 1.2 @@ -1,19 +1,18 @@ -/***************************************************************************** - * Copyright (C) The Apache Software Foundation. All rights reserved. * - * ------------------------------------------------------------------------- * - * This software is published under the terms of the Apache Software License * - * version 1.1, a copy of which has been included with this distribution in * - * the LICENSE file. * - *****************************************************************************/ - +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE file. + */ package org.apache.avalon.component; import org.apache.avalon.Component; import org.apache.avalon.ComponentManager; import org.apache.avalon.Composer; -import org.apache.avalon.configuration.Configuration; -import org.apache.avalon.configuration.Configurable; import org.apache.avalon.ThreadSafe; +import org.apache.avalon.configuration.Configurable; +import org.apache.avalon.configuration.Configuration; import org.apache.avalon.util.pool.PoolController; /** @@ -21,36 +20,33 @@ * a spezial behaviour or treatment. * * @author <a href="mailto:[EMAIL PROTECTED]">Giacomo Pati</a> - * @version CVS $Revision: 1.1 $ $Date: 2001/04/05 19:40:45 $ + * @version CVS $Revision: 1.2 $ $Date: 2001/04/11 04:18:10 $ */ -public class DefaultComponentPoolController implements PoolController, ThreadSafe, Component { - +public class DefaultComponentPoolController + implements PoolController, ThreadSafe, Component +{ /** Initial increase/decrease amount */ - public final static int DEFAULT_AMOUNT = 8; + public final static int DEFAULT_AMOUNT = 8; /** Current increase/decrease amount */ - protected int amount = DEFAULT_AMOUNT; + protected int m_amount = DEFAULT_AMOUNT; /** The last direction to increase/decrease >0 means increase, <0 decrease */ - protected int sizing_direction = 0; - - /** Creates a PoolController */ - public DefaultComponentPoolController() { - super(); - } + protected int m_sizing_direction = 0; /** * Called when a Pool reaches it's minimum. * Return the number of elements to increase minimum and maximum by. * @return the element increase */ - public int grow() { - /* - if (sizing_direction < 0 && amount > 1) - amount /= 2; - sizing_direction = 1; - */ - return amount; + public int grow() + { + /* + if (m_sizing_direction < 0 && m_amount > 1) + m_amount /= 2; + m_sizing_direction = 1; + */ + return m_amount; } /** @@ -58,12 +54,13 @@ * Returns the number of elements to decrease mi and max by. * @return the element decrease */ - public int shrink() { - /* - if (sizing_direction > 0 && amount > 1) - amount /= 2; - sizing_direction = -1; - */ - return amount; + public int shrink() + { + /* + if (m_sizing_direction > 0 && m_amount > 1) + m_amount /= 2; + m_sizing_direction = -1; + */ + return m_amount; } } 1.4 +254 -133 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.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- DefaultComponentSelector.java 2001/04/10 16:15:40 1.3 +++ DefaultComponentSelector.java 2001/04/11 04:18:10 1.4 @@ -1,127 +1,141 @@ -/***************************************************************************** - * Copyright (C) The Apache Software Foundation. All rights reserved. * - * ------------------------------------------------------------------------- * - * This software is published under the terms of the Apache Software License * - * version 1.1, a copy of which has been included with this distribution in * - * the LICENSE file. * - *****************************************************************************/ - +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE file. + */ package org.apache.avalon.component; -import java.util.HashMap; -import java.util.Map; + +import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; -import java.util.ArrayList; import java.util.List; - -import org.apache.avalon.ComponentManager; -import org.apache.avalon.ComponentSelector; +import java.util.Map; +import org.apache.avalon.AbstractLoggable; import org.apache.avalon.Component; +import org.apache.avalon.ComponentManager; import org.apache.avalon.ComponentManagerException; +import org.apache.avalon.ComponentSelector; +import org.apache.avalon.Composer; import org.apache.avalon.Context; import org.apache.avalon.Contextualizable; +import org.apache.avalon.Disposable; +import org.apache.avalon.ThreadSafe; 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.configuration.DefaultConfiguration; -import org.apache.avalon.AbstractLoggable; -import org.apache.avalon.Disposable; -import org.apache.avalon.ThreadSafe; /** * Default component manager for Avalon's components. * * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a> * @author <a href="mailto:[EMAIL PROTECTED]">Paul Russell</a> - * @version CVS $Revision: 1.3 $ $Date: 2001/04/10 16:15:40 $ + * @version CVS $Revision: 1.4 $ $Date: 2001/04/11 04:18:10 $ */ -public class DefaultComponentSelector extends AbstractLoggable implements Contextualizable, ComponentSelector, Composer, Configurable, ThreadSafe, Disposable { +public class DefaultComponentSelector + extends AbstractLoggable + implements Contextualizable, ComponentSelector, Composer, Configurable, ThreadSafe, Disposable +{ + private static final String DEFAULT_NAME = "UnnamedSelector"; + /** The role name for this instance */ - private String rolename = null; + private String m_rolename; /** The application context for components */ - protected Context context; + protected Context m_context; /** The application context for components */ - private ComponentManager manager; + private ComponentManager m_componentManager; /** Dynamic component handlers mapping. */ - private Map componentMapping; + private Map m_componentMapping; /** Static configuraiton object. */ - private Configuration conf = null; + private Configuration m_configuration; /** Static component handlers. */ - private Map componentHandlers; + private Map m_componentHandlers; /** Flag for if this is disposed or not. */ - private boolean disposed = false; + private boolean m_disposed; - /** The RoleManager to get hint shortcuts + /** Shorthand for hints */ - private RoleManager roles; + private Map m_hints; - /** Shorthand for hints + /** The RoleManager to get hint shortcuts */ - private Map hints; + private RoleManager m_roles; /** Construct a new default component manager. */ - public DefaultComponentSelector() { + public DefaultComponentSelector() + { // Setup the maps. - componentHandlers = Collections.synchronizedMap(new HashMap()); - componentMapping = Collections.synchronizedMap(new HashMap()); + m_componentHandlers = Collections.synchronizedMap( new HashMap() ); + m_componentMapping = Collections.synchronizedMap( new HashMap() ); } /** Provide the application Context. */ - public void contextualize(Context context) { - if (this.context == null) { - this.context = context; + public void contextualize( final Context context ) + { + if( null == m_context ) + { + m_context = context; } } /** Compose the ComponentSelector so that we know what the parent ComponentManager is. */ - public void compose(ComponentManager manager) throws ComponentManagerException { - if (this.manager == null) { - this.manager = manager; + public void compose( final ComponentManager componentManager ) + throws ComponentException + { + //HACK: Is this necessary??? + if( null == m_componentManager ) + { + m_componentManager = componentManager; } } /** * Properly dispose of all the ComponentHandlers. */ - public synchronized void dispose() { - this.disposed = true; + public synchronized void dispose() + { + m_disposed = true; - Iterator keys = this.componentHandlers.keySet().iterator(); + Iterator keys = m_componentHandlers.keySet().iterator(); List keyList = new ArrayList(); - while (keys.hasNext()) { + while( keys.hasNext() ) + { Object key = keys.next(); - DefaultComponentHandler handler = (DefaultComponentHandler) - this.componentHandlers.get(key); + DefaultComponentHandler handler = + (DefaultComponentHandler)m_componentHandlers.get( key ); handler.dispose(); - keyList.add(key); + keyList.add( key ); } keys = keyList.iterator(); - while (keys.hasNext()) { - this.componentHandlers.remove(keys.next()); + while( keys.hasNext() ) + { + m_componentHandlers.remove( keys.next() ); } - + keyList.clear(); } @@ -129,70 +143,149 @@ * Return an instance of a component based on a hint. The Composer has already selected the * role, so the only part left it to make sure the Component is handled. */ - public Component select( Object hint ) - throws ComponentManagerException { - - if (disposed) throw new IllegalStateException("You cannot select a Component from a disposed ComponentSelector"); - - DefaultComponentHandler handler = null; - Component component = null; - - if ( hint == null ) { - getLogger().error(this.getName() + ": ComponentSelector Attempted to retrieve component with null hint."); - throw new ComponentManagerException("Attempted to retrieve component with null hint."); + public Component select( final Object hint ) + throws ComponentException + { + if( m_disposed ) + { + throw new IllegalStateException( "You cannot select a Component " + + "from a disposed ComponentSelector" ); + } + + if( null == hint ) + { + final String message = + getName() + ": ComponentSelector Attempted to retrieve component with null hint."; + getLogger().error( message ); + throw new ComponentException( message ); } - handler = (DefaultComponentHandler) this.componentHandlers.get(hint); + DefaultComponentHandler handler = (DefaultComponentHandler)m_componentHandlers.get( hint ); + // Retrieve the instance of the requested component - if ( handler == null ) { - throw new ComponentManagerException(this.getName() + ": ComponentSelector could not find the component for hint: " + hint); + if( null == handler ) + { + final String message = + getName() + ": ComponentSelector could not find the component for hint: " + hint; + throw new ComponentException( message ); } - try { - component = handler.get(); - } catch (Exception e) { - throw new ComponentManagerException(this.getName() + ": ComponentSelector could not access the Component for hint: " + hint, e); - } + Component component = null; - if (component == null) { - throw new ComponentManagerException(this.getName() + ": ComponentSelector could not find the component for hint: " + hint); + try + { + component = handler.get(); + } + catch( final Exception e ) + { + final String message = + getName() + ": ComponentSelector could not access the Component for hint: " + hint; + throw new ComponentException( message, e ); + } + + if( null == component ) + { + final String message = + getName() + ": ComponentSelector could not find the component for hint: " + hint; + throw new ComponentException( message ); } - this.componentMapping.put(component, handler); + m_componentMapping.put( component, handler ); return component; } /** * Default Configuration handler for ComponentSelector. */ - public void configure(Configuration conf) throws ConfigurationException { - this.conf = conf; - getLogger().debug("ComponentSelector setting up with root element: " + this.conf.getName()); - - if ("component".equals(this.conf.getName())) { - this.rolename = this.conf.getAttribute("role"); - } else { - this.rolename = this.roles.getRoleForName(this.conf.getName()); - } - - Configuration[] instances = conf.getChildren(); - - for (int i = 0; i < instances.length; i++) { - Object hint = instances[i].getAttribute("name").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()); + public void configure( final Configuration configuration ) + throws ConfigurationException + { + m_configuration = configuration; + getLogger().debug( "ComponentSelector setting up with root element: " + + m_configuration.getName() ); + + final String name = configuration.getName(); + if( name.equals( "component" ) ) + { + m_rolename = m_configuration.getAttribute( "role" ); + } + else + { + m_rolename = m_roles.getRoleForName( name ); + } + + final Configuration[] hints = m_configuration.getChildren( "hint" ); + final HashMap hintMap = new HashMap(); + + for( int i = 0; i < hints.length; i++ ) + { + final String shortHand = hints[i].getAttribute("short-hand").trim(); + final String className = hints[i].getAttribute("class").trim(); + hintMap.put( shortHand, className ); + } + + m_hints = Collections.unmodifiableMap( hintMap ); + + Iterator shorthand = m_hints.keySet().iterator(); + Configuration[] instances = null; + + while( shorthand.hasNext() ) + { + String type = (String)shorthand.next(); + Class clazz = null; + + try + { + final String className = (String)m_hints.get( type ); + clazz = getClass().getClassLoader().loadClass( className ); + } + catch( final Exception e ) + { + final String message = + "The component instance for '" + type + "' has an invalid class name."; + getLogger().error( message, e ); + throw new ConfigurationException( message, e ); + } + + instances = m_configuration.getChildren( type ); + + for( int i = 0; i < instances.length; i++ ) + { + final Object hint = instances[ i ].getAttribute( "name" ).trim(); + + try + { + addComponent( hint, clazz, instances[i] ); + } + catch( final Exception e ) + { + final String message = + "The component instance for '" + hint + "' has an invalid class name."; + + getLogger().error( message, e ); + throw new ConfigurationException( message, e ); + } } + } + + instances = m_configuration.getChildren( "component-instance" ); - getLogger().debug(this.rolename + ":" + hint + " classname = " + className); - try { - this.addComponent(hint, this.getClass().getClassLoader().loadClass(className), 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); + for( int i = 0; i < instances.length; i++ ) + { + final Object hint = instances[ i ].getAttribute( "name" ).trim(); + final String className = (String)instances[i].getAttribute( "class" ).trim(); + + try + { + final Class clazz = getClass().getClassLoader().loadClass( className ); + addComponent( hint, clazz, instances[i]); + } + catch( final Exception e ) + { + final String message = + "The component instance for '" + hint + "' has an invalid class name."; + getLogger().error( message, e ); + throw new ConfigurationException( message, e ); } } } @@ -200,21 +293,29 @@ /** * Configure the RoleManager */ - public void setRoleManager(RoleManager roles) { - if (this.roles == null) { - this.roles = roles; + public void setRoleManager( final RoleManager roles ) + { + if( null == m_roles ) + { + m_roles = roles; } } /** * Release the Component to the propper ComponentHandler. */ - public void release(Component component) { - if (component == null) return; - DefaultComponentHandler handler = (DefaultComponentHandler) this.componentMapping.get(component); - if (handler == null) return; - handler.put(component); - this.componentMapping.remove(component); + public void release( final Component component ) + { + if( null == component ) return; + + final DefaultComponentHandler handler = + (DefaultComponentHandler)m_componentMapping.get( component ); + + if( null == handler ) return; + + handler.put( component ); + + m_componentMapping.remove( component ); } /** Add a new component to the manager. @@ -222,17 +323,31 @@ * @param component the class of this component. * @param Configuration the configuration for this component. */ - public void addComponent(Object hint, Class component, Configuration config) - throws ComponentManagerException { - try { - DefaultComponentHandler handler = new DefaultComponentHandler(component, config, this.manager, this.context, this.roles); - handler.setLogger(getLogger()); + public void addComponent( final Object hint, + final Class component, + final Configuration configuration ) + throws ComponentException + { + try + { + final DefaultComponentHandler handler = + new DefaultComponentHandler( component, + configuration, + m_componentManager, + m_context, + m_roles ); + + handler.setLogger( getLogger() ); handler.init(); - this.componentHandlers.put(hint, handler); - getLogger().debug("Adding " + component.getName() + " for " + hint.toString()); - } catch (Exception e) { - getLogger().error("Could not set up Component for hint: " + hint, e); - throw new ComponentManagerException ("Could not set up Component for hint: " + hint, e); + m_componentHandlers.put( hint, handler ); + getLogger().debug( "Adding " + component.getName() + " for " + hint.toString() ); + } + catch( final Exception e ) + { + final String message = + "Could not set up Component for hint: " + hint; + getLogger().error( message, e); + throw new ComponentException( message, e ); } } @@ -240,31 +355,37 @@ * @param hint the hint name for the component. * @param instance the instance of the component. */ - public void addComponentInstance(String hint, Object instance) { - try { - DefaultComponentHandler handler = new DefaultComponentHandler((Component) instance); - handler.setLogger(getLogger()); + public void addComponentInstance( final String hint, final Object instance ) + { + try + { + final DefaultComponentHandler handler = + new DefaultComponentHandler( (Component)instance ); + handler.setLogger( getLogger() ); handler.init(); - this.componentHandlers.put(hint, handler); - getLogger().debug("Adding " + instance.getClass().getName() + " for " + hint.toString()); - } catch (Exception e) { - getLogger().error("Could not set up Component for hint: " + hint, e); + m_componentHandlers.put( hint, handler ); + getLogger().debug( "Adding " + instance.getClass().getName() + " for " + hint.toString() ); + } + catch( final Exception e ) + { + getLogger().error( "Could not set up Component for hint: " + hint, e ); } } - private static final String DEFAULT_NAME = "UnnamedSelector"; - /** * Return this selector's configuration name or a default name if no such * configuration was provided. This accounts for the case when a static * component instance has been added through * <code>addComponentInstance</code> with no associated configuration */ - private String getName() { - if (this.conf != null && ("".equals(this.conf.getName()) == false)) { - return this.conf.getName(); - } + private String getName() + { + if( null != m_configuration && + !m_configuration.getName().equals( "" ) ) + { + return m_configuration.getName(); + } - return DEFAULT_NAME; - } + return DEFAULT_NAME; + } } 1.3 +81 -61 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.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- DefaultRoleManager.java 2001/04/10 16:15:41 1.2 +++ DefaultRoleManager.java 2001/04/11 04:18:10 1.3 @@ -1,23 +1,19 @@ -/***************************************************************************** - * Copyright (C) The Apache Software Foundation. All rights reserved. * - * ------------------------------------------------------------------------- * - * This software is published under the terms of the Apache Software License * - * version 1.1, a copy of which has been included with this distribution in * - * the LICENSE file. * - *****************************************************************************/ - +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE file. + */ package org.apache.avalon.component; -import java.util.Map; -import java.util.Iterator; -import java.util.HashMap; import java.util.Collections; - +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import org.apache.avalon.AbstractLoggable; import org.apache.avalon.configuration.Configurable; import org.apache.avalon.configuration.Configuration; - -import org.apache.avalon.AbstractLoggable; - import org.apache.avalon.configuration.ConfigurationException; /** @@ -27,70 +23,94 @@ * @author <a href="mailto:[EMAIL PROTECTED]">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.2 $ $Date: 2001/04/10 16:15:41 $ + * @version CVS $Revision: 1.3 $ $Date: 2001/04/11 04:18:10 $ */ -public class DefaultRoleManager extends AbstractLoggable implements RoleManager, Configurable { - 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)); - return (String) this.shorthands.get(shorthandName); +public class DefaultRoleManager + extends AbstractLoggable + implements RoleManager, Configurable +{ + private Map m_shorthands; + private Map m_classNames; + private Map m_hintClassNames; + + public final String getRoleForName( final String shorthandName ) + { + final String role = (String)m_shorthands.get( shorthandName ); + + getLogger().debug( "looking up shorthand " + shorthandName + + ", returning " + role ); + + return role; } - public final String getDefaultClassNameForRole(String role) { - return (String) this.classNames.get(role); + public final String getDefaultClassNameForRole( final String role ) + { + return (String)m_classNames.get( role ); } + + public final String getDefaultClassNameForHint( final String role, + final String shorthand ) + { + getLogger().debug( "looking up hintmap for role " + role ); - public final String getDefaultClassNameForHint(String role, String shorthand) { - getLogger().debug("looking up hintmap for role " + role); - Map hintMap = (Map) this.hintClassNames.get(role); + final Map hintMap = (Map)m_hintClassNames.get( role ); - if (hintMap == null) { + if( null == hintMap ) + { return ""; } - getLogger().debug("looking up classname for hint " + shorthand); - return (String) hintMap.get(shorthand); + 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); + public final void configure( final Configuration configuration ) + throws ConfigurationException + { + final Map shorts = new HashMap(); + final Map classes = new HashMap(); + final Map hintclasses = new HashMap(); + + final Configuration[] roles = configuration.getChildren( "role" ); + + for( int i = 0; i < roles.length; i++ ) + { + final String name = roles[ i ].getAttribute( "name" ); + final String shorthand = roles[ i ].getAttribute( "shorthand" ); + final String defaultClassName = + roles[ i ].getAttribute( "default-class", null ); + + shorts.put( shorthand, name ); + + if( null != defaultClassName ) + { + classes.put( name, defaultClassName ); } - Configuration[] hints = roles[i].getChildren("hint"); - if (hints.length > 0) { + final 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()); + for( int j = 0; j < hints.length; j++ ) + { + final String shortHand = hints[ j ].getAttribute("shorthand").trim(); + final String className = hints[ j ].getAttribute("class").trim(); + + hintMap.put( shortHand, className ); + getLogger().debug( "Adding hint type " + shortHand + " associated with role " + + name + " and class " + className ); } - - hintclasses.put(name, Collections.unmodifiableMap(hintMap)); + + hintclasses.put( name, Collections.unmodifiableMap( hintMap ) ); } - getLogger().debug("added Role " + name + " with shorthand " + shorthand + " for " + defaultClassName); + getLogger().debug( "added Role " + name + " with shorthand " + + shorthand + " for " + defaultClassName ); } - this.shorthands = Collections.unmodifiableMap(shorts); - this.classNames = Collections.unmodifiableMap(classes); - this.hintClassNames = Collections.unmodifiableMap(hintclasses); + m_shorthands = Collections.unmodifiableMap( shorts ); + m_classNames = Collections.unmodifiableMap( classes ); + m_hintClassNames = Collections.unmodifiableMap( hintclasses ); } -} \ No newline at end of file +} 1.3 +15 -15 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.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- RoleManager.java 2001/04/10 16:15:41 1.2 +++ RoleManager.java 2001/04/11 04:18:10 1.3 @@ -1,11 +1,10 @@ -/***************************************************************************** - * Copyright (C) The Apache Software Foundation. All rights reserved. * - * ------------------------------------------------------------------------- * - * This software is published under the terms of the Apache Software License * - * version 1.1, a copy of which has been included with this distribution in * - * the LICENSE file. * - *****************************************************************************/ - +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE file. + */ package org.apache.avalon.component; import java.util.Iterator; @@ -17,9 +16,10 @@ * @author <a href="mailto:[EMAIL PROTECTED]">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.2 $ $Date: 2001/04/10 16:15:41 $ + * @version CVS $Revision: 1.3 $ $Date: 2001/04/11 04:18:10 $ */ -public interface RoleManager { +public interface RoleManager +{ /** * Find Role name based on shorthand name. Please note that if * this returns <code>null</code> or an empty string, then the @@ -27,16 +27,16 @@ * words, you should not try to instantiate a class from an empty * role. */ - String getRoleForName(String shorthandName); + String getRoleForName( String shorthandName ); /** - * Get the default classname for a given role + * Get the default classname for a given role. */ - 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); -} \ No newline at end of file + String getDefaultClassNameForHint( String hint, String shorthand ); +} 1.1 jakarta-avalon/src/java/org/apache/avalon/component/ComponentException.java Index: ComponentException.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software License * version 1.1, a copy of which has been included with this distribution in * the LICENSE file. */ package org.apache.avalon.component; import org.apache.avalon.ComponentManagerException; /** * The exception thrown by ComponentManager. * * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a> * @author <a href="mailto:[EMAIL PROTECTED]">Pierpaolo Fumagalli</a> * @author <a href="mailto:[EMAIL PROTECTED]">Federico Barbieri</a> * @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a> */ public class ComponentException extends ComponentManagerException { /** * Construct a new <code>ComponentException</code> instance. */ public ComponentException( final String message, final Throwable throwable ) { super( message, throwable ); } /** * Construct a new <code>ComponentException</code> instance. */ public ComponentException( final String message ) { super( message, null ); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]