bloritsch 02/02/08 10:09:41
Modified: src/scratchpad/org/apache/avalon/excalibur/system
AbstractContainer.java
Log:
rewrite AbstractContainer from scratch, it just didn't work
Revision Changes Path
1.18 +174 -142
jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/AbstractContainer.java
Index: AbstractContainer.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/AbstractContainer.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- AbstractContainer.java 7 Feb 2002 22:07:39 -0000 1.17
+++ AbstractContainer.java 8 Feb 2002 18:09:41 -0000 1.18
@@ -19,7 +19,6 @@
import org.apache.avalon.excalibur.logger.LoggerManager;
import org.apache.avalon.excalibur.event.Queue;
import org.apache.avalon.excalibur.pool.PoolManager;
-import org.apache.avalon.excalibur.util.ComponentStateValidator;
import java.util.ArrayList;
import java.util.Collections;
@@ -36,7 +35,7 @@
* Manager can expose that to the instantiating class.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
- * @version CVS $Revision: 1.17 $ $Date: 2002/02/07 22:07:39 $
+ * @version CVS $Revision: 1.18 $ $Date: 2002/02/08 18:09:41 $
*/
public abstract class AbstractContainer
extends AbstractLogEnabled
@@ -51,8 +50,9 @@
protected ClassLoader m_classLoader;
protected RoleManager m_roleManager;
protected Configuration m_configuration;
- protected List m_components = new ArrayList();
- protected Map m_configs = new HashMap();
+ protected Map m_configs = new HashMap(10);
+ protected Map m_mapper = new HashMap(10);
+ protected List m_components = new ArrayList(10);
/**
* Wrap this so that ComponentStateValidator functions properly.
@@ -107,75 +107,49 @@
* Process the configuration and set up the components and their
mappings.
* At this point, all components are prepared and all mappings are made.
* However, nothing is initialized.
+ *
+ * <p>The native Configuration format follows a specific convention. If
you
+ * use a RoleManager to map roles and implementations to more helpful
names,
+ * we will internally rewrite the configuration to match this format.
Please
+ * note: If a configuration element does *not* have a unique id, it will
not
+ * be treated as a Component. That ID is used as the hint when there is
more
+ * than one implementation of a role.</p>
+ *
+ * <pre>
+ * <component
role="org.apache.avalon.excalibur.datasource.DataSourceComponent"
+ * id="default-connection"
+ *
class="org.apache.avalon.excalibur.datasource.JdbcDataSourceComponent"
+ *
handler="org.apache.avalon.excalibur.system.handler.ThreadSafeComponentHandler">
+ *
+ * <!-- Component specific configuration -->
+ *
+ * </component>
+ * </pre>
*/
public void configure( Configuration configElement )
throws ConfigurationException
{
m_configuration = configElement;
- Map managerMap = new HashMap();
- m_childManager = new ContainerComponentManager( managerMap,
m_manager );
- Configuration[] components = configElement.getChildren();
+ Configuration elements[] = configElement.getChildren();
- for ( int i = 0; i < components.length; i++ )
+ for ( int i = 0; i < elements.length; i++ )
{
- final String name = components[ i ].getName();
- String role;
- Class klass;
- Class handlerKlass;
- Configuration config = null;
+ Configuration component = null;
+ Object hint = elements[ i ].getAttribute( "id", null );
- if ( name.equals( "component" ) )
+ if ( null != hint )
{
- config = components[ i ];
- role = config.getAttribute( "role" );
-
- try
- {
- klass = m_classLoader.loadClass( config.getAttribute(
"class" ) );
- }
- catch ( Exception e )
+ if ( "component".equals( elements[ i ].getName() ) )
{
- if ( getLogger().isDebugEnabled() )
- {
- getLogger().debug( "Component class '" +
config.getAttribute( "class" ) +
- "' is not valid.", e );
- }
- continue;
+ component = elements[ i ];
}
-
- try
- {
- handlerKlass = m_classLoader.loadClass(
config.getAttribute( "handler" ) );
- }
- catch ( Exception e )
+ else
{
- if ( getLogger().isDebugEnabled() )
- {
- getLogger().debug( "ComponentHandler class '" +
config.getAttribute( "handler" ) +
- "' is not valid.", e );
- }
- continue;
+ component = rewriteConfiguration( elements[ i ] );
}
- assignHandler( getHandler( handlerKlass, klass, config ),
config, managerMap );
- }
- else
- {
- if ( getLogger().isDebugEnabled() )
- {
- getLogger().debug( "Attempting to process custom
configuration element: " + name );
- }
-
- try
- {
- handleConfiguration( components[ i ], managerMap );
- }
- catch ( Exception e )
- {
- getLogger().debug( "It appears that the configuration
element " +
- components[ i ].getName() + " was not
a Component" );
- }
+ addComponent( component, hint );
}
}
}
@@ -185,8 +159,7 @@
* makes it easier to handle ComponentSelector hierarchies. It is meant
to
* either return a ComponentHandler or a ComponentSelector
*/
- protected void handleConfiguration( final Configuration configItem,
- final Map managerMap )
+ protected Configuration rewriteConfiguration ( final Configuration
configItem )
throws ConfigurationException
{
DefaultConfiguration temp = new DefaultConfiguration( "component",
"AbstractContainer-rewrite" );
@@ -212,66 +185,50 @@
temp.makeReadOnly();
- assignHandler( getHandler( handlerKlass, klass, temp ), temp,
managerMap );
+ return temp;
}
/**
- * Adds the ComponentHandler and Configuration to the system.
+ * Add a Component.
+ *
*/
- protected void assignHandler( ComponentHandler handler, Configuration
config, Map managerMap )
- throws ConfigurationException
+ protected void addComponent( final Configuration component, Object hint )
{
- if ( null == handler )
+ if ( null == hint )
{
- throw new ConfigurationException("The ComponentHandler passed in
was null");
+ throw new IllegalArgumentException("Hint must not be null");
}
- m_components.add( handler );
- m_configs.put( handler, config );
- String role = config.getAttribute( "role" );
+ String role = component.getAttribute( "role", null );
+ String klass = component.getAttribute( "class", null );
+ ComponentHandler handler = getComponentHandler(
+ component.getAttribute( "handler", null ), klass, component
+ );
- Object contents = managerMap.get( role );
- if ( null == contents )
+ if ( null != role && null != klass && null != handler )
{
- managerMap.put( role, handler );
- }
- else
- {
- if ( contents instanceof ComponentHandler )
- {
- Map selectorMap = new HashMap( 3 );
- selectorMap.put( ( (Configuration) m_configs.get(contents) )
- .getAttribute( "id", "1" ), contents );
- selectorMap.put( config.getAttribute( "id", "2" ), contents
);
+ Map hintMap = (Map) m_mapper.get( role );
- assignSelector( role, new ContainerComponentSelector(
selectorMap ), managerMap );
- }
- else if ( contents instanceof ContainerComponentSelector )
+ if ( null == hintMap )
{
- ( (ContainerComponentSelector) contents )
- .addComponentHandler( config.getAttribute( "id", null ),
- handler);
+ hintMap = new HashMap(5);
}
- }
- if ( getLogger().isDebugEnabled() )
- {
- getLogger().debug( "Assigned handler " +
handler.getClass().getName() +
- " to role " + role );
- }
- }
+ hintMap.put( hint, handler );
- /**
- * Adds the ComponentHandler and Configuration to the system.
- */
- protected void assignSelector( String role, ComponentSelector selector,
Map managerMap )
- throws ConfigurationException
- {
- managerMap.put( role, selector );
+ if ( hintMap.containsKey( "default" ) )
+ {
+ if ( ! hintMap.containsKey( "selector" ) )
+ {
+ hintMap.put( "selector", new ContainerComponentSelector(
this, role ) );
+ }
+ }
+ else
+ {
+ hintMap.put( "default", handler );
+ }
- if ( getLogger().isDebugEnabled() )
- {
- getLogger().debug( "Assigned ComponentSelector to role " + role
);
+ m_mapper.put( role, hintMap );
}
}
@@ -279,15 +236,17 @@
* Get a ComponentHandler with the standard
<code>HANDLER_CONSTRUCTOR</code>
* for the component class passed in.
*/
- protected ComponentHandler getHandler( Class handlerKlass,
- Class klass,
- Configuration configuration )
+ private ComponentHandler getComponentHandler( String handlerClassName,
+ String className,
+ Configuration
configuration )
{
Constructor constructor;
ComponentHandler handler = null;
try
{
+ Class klass = m_classLoader.loadClass( className );
+ Class handlerKlass = m_classLoader.loadClass( handlerClassName );
constructor = handlerKlass.getConstructor(
ComponentHandler.HANDLER_CONSTRUCTOR );
handler = (ComponentHandler) constructor.newInstance(new
Object[] {
klass, configuration, m_childManager, m_context
@@ -297,22 +256,92 @@
{
if ( getLogger().isDebugEnabled() )
{
- getLogger().debug( "Could not create the '" +
handlerKlass.getName() +
- "' handler for the '" + klass.getName() +
+ getLogger().debug( "Could not create the '" +
handlerClassName +
+ "' handler for the '" + className +
"' component.", e );
}
}
if ( getLogger().isDebugEnabled() )
{
- getLogger().debug( "Component " + klass.getName() +
- " uses handler " + handlerKlass.getName() );
+ getLogger().debug( "Component " + className +
+ " uses handler " + handlerClassName );
}
+ m_configs.put( handler, configuration );
+ m_components.add( handler );
+
return handler;
}
/**
+ * This is the method that the ContainerComponentManager and Selector
use to gain
+ * access to the ComponentHandlers and ComponentSelectors. The actual
access of
+ * the ComponentHandler is delegated to the Container.
+ *
+ * @param role The role we intend to access a Component for.
+ * @param hint The hint that we use as a qualifier
+ * (note: if null, the default implementation is returned).
+ *
+ * @return Object a reference to the ComponentHandler or
ComponentSelector for the
+ * role/hint combo.
+ */
+ protected Object get( final String role, final Object hint )
+ throws ComponentException
+ {
+ Map hintMap = (Map) m_mapper.get( role );
+ Object value;
+
+ if ( null == hintMap )
+ {
+ throw new ComponentException( "Component does not exist" );
+ }
+
+ if ( null == hint )
+ {
+ value = hintMap.get( "selector" );
+
+ if ( null == value )
+ {
+ value = hintMap.get( "default" );
+ }
+
+ return value;
+ }
+
+ value = hintMap.get( hint );
+
+ if ( null == value )
+ {
+ throw new ComponentException( "Component does not exist" );
+ }
+
+ return value;
+ }
+
+ /**
+ * This is the method that the ContainerComponentManager and Selector
use to gain
+ * access to the ComponentHandlers and ComponentSelectors. The actual
access of
+ * the ComponentHandler is delegated to the Container.
+ *
+ * @param role The role we intend to access a Component for.
+ * @param hint The hint that we use as a qualifier
+ * (note: if null, the default implementation is returned).
+ *
+ * @return true if a reference to the role exists.
+ */
+ protected boolean has( final String role, final Object hint )
+ {
+ if ( null == hint )
+ {
+ return m_mapper.keySet().contains( role );
+ }
+
+ Map hintMap = (Map) m_mapper.get( role );
+ return hintMap.keySet().contains( hint );
+ }
+
+ /**
* Root ComponentManager. The Container may choose to have it's
ComponentManager
* delegate to the root manager, or it may choose to be entirely self
contained.
*/
@@ -328,6 +357,7 @@
public void initialize()
throws Exception
{
+ m_childManager = new ContainerComponentManager( this, m_manager );
Iterator i = m_components.iterator();
FixedSizeBuffer buffer = new FixedSizeBuffer( m_components.size() );
@@ -421,50 +451,55 @@
protected final static class ContainerComponentManager
implements ComponentManager
{
- private final Map m_components;
- private final Map m_used;
- private final ComponentManager m_parent;
+ private final AbstractContainer m_components;
+ private final Map m_used;
+ private final ComponentManager m_parent;
/**
* This constructor is for a ContainerComponentManager with no parent
* ComponentManager
*/
- public ContainerComponentManager( Map componentMap )
+ public ContainerComponentManager( final AbstractContainer container
)
{
- this( componentMap, null );
+ this( container, null );
}
/**
* This constructor is for a ContainerComponentManager with a parent
* ComponentManager
*/
- public ContainerComponentManager( Map componentMap, ComponentManager
parent )
+ public ContainerComponentManager( final AbstractContainer
container, final ComponentManager parent )
{
m_parent = parent;
- m_components = componentMap;
- m_used = new HashMap( m_components.size() );
+ m_components = container;
+ m_used = new HashMap( 10 );
}
public Component lookup( String role )
throws ComponentException
{
- Object temp = m_components.get( role );
+ Object temp = null;
- if ( null == temp )
+ try
{
+ temp = m_components.get( role, null );
+ }
+ catch ( ComponentException ce )
+ {
+ ce.printStackTrace( System.err );
if ( null != m_parent )
{
return m_parent.lookup( role );
}
else
{
- throw new ComponentException( "The role does not exist
in the ComponentManager" );
+ throw ce;
}
}
if ( temp instanceof ComponentSelector )
{
- return (ComponentSelector) temp;
+ return (Component) temp;
}
if ( ! ( temp instanceof ComponentHandler ) )
@@ -500,7 +535,7 @@
public boolean hasComponent( String role )
{
- final boolean hasComponent = m_components.containsKey( role );
+ final boolean hasComponent = m_components.has( role, null );
if ( ! hasComponent && null != m_parent )
{
@@ -537,19 +572,26 @@
protected final static class ContainerComponentSelector
implements ComponentSelector
{
- private final Map m_components;
- private final Map m_used;
+ private final String m_role;
+ private final AbstractContainer m_components;
+ private final Map m_used;
- public ContainerComponentSelector( Map selectorMap )
+ public ContainerComponentSelector( final AbstractContainer
container, final String role )
{
- m_components = selectorMap;
- m_used = new HashMap( m_components.size() );
+ m_role = role;
+ m_components = container;
+ m_used = new HashMap( 5 );
}
- public Component select( Object hint )
+ public Component select( final Object hint )
throws ComponentException
{
- ComponentHandler handler = (ComponentHandler) m_components.get(
hint );
+ if ( null == hint )
+ {
+ throw new IllegalArgumentException("hint cannot be null");
+ }
+
+ ComponentHandler handler = (ComponentHandler) m_components.get(
m_role, hint );
if ( null == handler )
{
@@ -582,7 +624,7 @@
public boolean hasComponent( Object hint )
{
- return m_components.containsKey( hint );
+ return m_components.has( m_role, hint );
}
public void release( Component component )
@@ -595,16 +637,6 @@
}
handler.put( component );
- }
-
- protected void addComponentHandler( Object hint, ComponentHandler
handler )
- {
- if ( null == hint )
- {
- hint = new Integer( m_components.size() ).toString();
- }
-
- m_components.put( hint, handler );
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>