Return-Path: Delivered-To: apmail-jakarta-avalon-dev-archive@jakarta.apache.org Received: (qmail 92546 invoked by uid 500); 11 Apr 2001 17:27:08 -0000 Mailing-List: contact avalon-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Reply-To: "Avalon Development" Delivered-To: mailing list avalon-dev@jakarta.apache.org Received: (qmail 92534 invoked by uid 500); 11 Apr 2001 17:27:08 -0000 Delivered-To: apmail-jakarta-avalon-cvs@apache.org Date: 11 Apr 2001 17:27:07 -0000 Message-ID: <20010411172707.92530.qmail@apache.org> From: bloritsch@apache.org To: jakarta-avalon-cvs@apache.org Subject: cvs commit: jakarta-avalon/src/java/org/apache/avalon/component DefaultComponentSelector.java bloritsch 01/04/11 10:27:07 Modified: src/java/org/apache/avalon/component DefaultComponentSelector.java Log: Fixed ComponentSelector error Revision Changes Path 1.5 +42 -90 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.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- DefaultComponentSelector.java 2001/04/11 04:18:10 1.4 +++ DefaultComponentSelector.java 2001/04/11 17:27:07 1.5 @@ -34,10 +34,10 @@ * * @author Berin Loritsch * @author Paul Russell - * @version CVS $Revision: 1.4 $ $Date: 2001/04/11 04:18:10 $ + * @version CVS $Revision: 1.5 $ $Date: 2001/04/11 17:27:07 $ */ -public class DefaultComponentSelector - extends AbstractLoggable +public class DefaultComponentSelector + extends AbstractLoggable implements Contextualizable, ComponentSelector, Composer, Configurable, ThreadSafe, Disposable { private static final String DEFAULT_NAME = "UnnamedSelector"; @@ -80,7 +80,7 @@ /** Construct a new default component manager. */ - public DefaultComponentSelector() + public DefaultComponentSelector() { // Setup the maps. m_componentHandlers = Collections.synchronizedMap( new HashMap() ); @@ -89,9 +89,9 @@ /** Provide the application Context. */ - public void contextualize( final Context context ) + public void contextualize( final Context context ) { - if( null == m_context ) + if( null == m_context ) { m_context = context; } @@ -112,7 +112,7 @@ /** * Properly dispose of all the ComponentHandlers. */ - public synchronized void dispose() + public synchronized void dispose() { m_disposed = true; @@ -122,7 +122,7 @@ while( keys.hasNext() ) { Object key = keys.next(); - DefaultComponentHandler handler = + DefaultComponentHandler handler = (DefaultComponentHandler)m_componentHandlers.get( key ); handler.dispose(); @@ -131,11 +131,11 @@ keys = keyList.iterator(); - while( keys.hasNext() ) + while( keys.hasNext() ) { m_componentHandlers.remove( keys.next() ); } - + keyList.clear(); } @@ -148,13 +148,13 @@ { if( m_disposed ) { - throw new IllegalStateException( "You cannot select a Component " + + throw new IllegalStateException( "You cannot select a Component " + "from a disposed ComponentSelector" ); } - + if( null == hint ) { - final String message = + final String message = getName() + ": ComponentSelector Attempted to retrieve component with null hint."; getLogger().error( message ); throw new ComponentException( message ); @@ -172,10 +172,10 @@ Component component = null; - try + try { component = handler.get(); - } + } catch( final Exception e ) { final String message = @@ -197,89 +197,41 @@ /** * Default Configuration handler for ComponentSelector. */ - public void configure( final Configuration configuration ) + public void configure( final Configuration configuration ) throws ConfigurationException { m_configuration = configuration; - getLogger().debug( "ComponentSelector setting up with root element: " + + getLogger().debug( "ComponentSelector setting up with root element: " + m_configuration.getName() ); final String name = configuration.getName(); - if( name.equals( "component" ) ) + 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(); + Configuration[] instances = m_configuration.getChildren(); - 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" ); - 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(); + final String className; + + if("component-instance".equals(instances[i].getName())) { + className = (String)instances[i].getAttribute( "class" ).trim(); + } else { + className = m_roles.getDefaultClassNameForHint(m_rolename, instances[i].getName()); + } - try + try { final Class clazz = getClass().getClassLoader().loadClass( className ); addComponent( hint, clazz, instances[i]); - } + } catch( final Exception e ) { final String message = @@ -295,7 +247,7 @@ */ public void setRoleManager( final RoleManager roles ) { - if( null == m_roles ) + if( null == m_roles ) { m_roles = roles; } @@ -308,7 +260,7 @@ { if( null == component ) return; - final DefaultComponentHandler handler = + final DefaultComponentHandler handler = (DefaultComponentHandler)m_componentMapping.get( component ); if( null == handler ) return; @@ -323,17 +275,17 @@ * @param component the class of this component. * @param Configuration the configuration for this component. */ - public void addComponent( final Object hint, - final Class component, + public void addComponent( final Object hint, + final Class component, final Configuration configuration ) throws ComponentException { try { - final DefaultComponentHandler handler = - new DefaultComponentHandler( component, - configuration, - m_componentManager, + final DefaultComponentHandler handler = + new DefaultComponentHandler( component, + configuration, + m_componentManager, m_context, m_roles ); @@ -341,7 +293,7 @@ handler.init(); m_componentHandlers.put( hint, handler ); getLogger().debug( "Adding " + component.getName() + " for " + hint.toString() ); - } + } catch( final Exception e ) { final String message = @@ -359,13 +311,13 @@ { try { - final DefaultComponentHandler handler = + final DefaultComponentHandler handler = new DefaultComponentHandler( (Component)instance ); handler.setLogger( getLogger() ); handler.init(); 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 ); @@ -378,9 +330,9 @@ * component instance has been added through * addComponentInstance with no associated configuration */ - private String getName() + private String getName() { - if( null != m_configuration && + if( null != m_configuration && !m_configuration.getName().equals( "" ) ) { return m_configuration.getName(); --------------------------------------------------------------------- To unsubscribe, e-mail: avalon-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: avalon-dev-help@jakarta.apache.org