You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by bl...@apache.org on 2002/01/25 21:48:34 UTC
cvs commit: jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler ComponentHandler.java DefaultComponentFactory.java DefaultComponentHandler.java DefaultComponentPool.java DefaultComponentPoolController.java PoolableComponentHandler.java ThreadSafeComponentHandler.java
bloritsch 02/01/25 12:48:34
Modified: src/scratchpad/org/apache/avalon/excalibur/system
ContainerManager.java
Added: src/scratchpad/org/apache/avalon/excalibur/system/handler
ComponentHandler.java DefaultComponentFactory.java
DefaultComponentHandler.java
DefaultComponentPool.java
DefaultComponentPoolController.java
PoolableComponentHandler.java
ThreadSafeComponentHandler.java
Log:
Carry over handler code and make LogEnabled clean. I will refactor completely later
Revision Changes Path
1.7 +21 -14 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/ContainerManager.java
Index: ContainerManager.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/ContainerManager.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ContainerManager.java 25 Jan 2002 20:15:11 -0000 1.6
+++ ContainerManager.java 25 Jan 2002 20:48:34 -0000 1.7
@@ -136,7 +136,7 @@
* </table>
*
* @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
- * @version CVS $Revision: 1.6 $ $Date: 2002/01/25 20:15:11 $
+ * @version CVS $Revision: 1.7 $ $Date: 2002/01/25 20:48:34 $
*/
public class ContainerManager
{
@@ -163,6 +163,8 @@
private Logger m_defaultLogger;
private Configuration m_containerConfig;
private Configuration m_LogKitConfig;
+ private Configuration m_roleConfig;
+ private Context m_containerContext;
private Container m_containerInstance;
private ComponentStateValidator m_validator;
private RoleManager m_roleManager;
@@ -355,20 +357,25 @@
*/
protected Context setupContext()
{
- DefaultContext context = new DefaultContext();
- context.put( CONTEXT_DIRECTORY, m_contextDirectory );
- context.put( WORK_DIRECTORY, m_workDirectory );
- context.put( CPU_COUNT,
- new Integer(m_initialParameters.getParameterAsInteger( CPU_COUNT, 1 ) )
- );
- context.put( LOG_CATEGORY,
- m_initialParameters.getParameter(LOG_CATEGORY, null)
- );
- context.put( Container.CONTEXT_CLASSLOADER, m_contextClassLoader );
- context.put( Container.ROLE_MANAGER, setupRoleManager() );
+ if ( null == m_containerContext )
+ {
+ DefaultContext context = new DefaultContext();
+ context.put( CONTEXT_DIRECTORY, m_contextDirectory );
+ context.put( WORK_DIRECTORY, m_workDirectory );
+ context.put( CPU_COUNT,
+ new Integer(m_initialParameters.getParameterAsInteger( CPU_COUNT, 1 ) )
+ );
+ context.put( LOG_CATEGORY,
+ m_initialParameters.getParameter(LOG_CATEGORY, null)
+ );
+ context.put( Container.CONTEXT_CLASSLOADER, m_contextClassLoader );
+ context.put( Container.ROLE_MANAGER, setupRoleManager() );
+
+ context.makeReadOnly();
+ m_containerContext = context;
+ }
- context.makeReadOnly();
- return context;
+ return m_containerContext;
}
/**
1.1 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/ComponentHandler.java
Index: ComponentHandler.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.txt file.
*/
package org.apache.avalon.excalibur.system.handler;
import org.apache.avalon.excalibur.system.RoleManager;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.thread.SingleThreaded;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.avalon.excalibur.pool.Poolable;
import org.apache.avalon.excalibur.logger.LoggerManager;
/**
* This class acts like a Factory to instantiate the correct version
* of the ComponentHandler that you need.
*
* @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/01/25 20:48:34 $
* @since 4.0
*/
public abstract class ComponentHandler extends AbstractLogEnabled
implements Initializable, Disposable {
public static ComponentHandler getComponentHandler(
final Class componentClass,
final Configuration config,
final ComponentManager manager,
final Context context,
final RoleManager roles,
final LoggerManager logkit )
throws Exception
{
int numInterfaces = 0;
if (SingleThreaded.class.isAssignableFrom(componentClass))
{
numInterfaces++;
}
if (ThreadSafe.class.isAssignableFrom(componentClass))
{
numInterfaces++;
}
if (Poolable.class.isAssignableFrom(componentClass))
{
numInterfaces++;
}
if (numInterfaces > 1)
{
throw new Exception("[CONFLICT] lifestyle interfaces: " + componentClass.getName());
}
if (Poolable.class.isAssignableFrom(componentClass))
{
return new PoolableComponentHandler(componentClass,
config,
manager,
context,
roles,
logkit);
}
else if (ThreadSafe.class.isAssignableFrom(componentClass))
{
return new ThreadSafeComponentHandler(componentClass,
config,
manager,
context,
roles,
logkit);
}
else // This is a SingleThreaded component
{
return new DefaultComponentHandler(componentClass,
config,
manager,
context,
roles,
logkit);
}
}
public static ComponentHandler getComponentHandler(
final Component componentInstance )
throws Exception
{
int numInterfaces = 0;
if (SingleThreaded.class.isAssignableFrom(componentInstance.getClass()))
{
numInterfaces++;
}
if (ThreadSafe.class.isAssignableFrom(componentInstance.getClass()))
{
numInterfaces++;
}
if (Poolable.class.isAssignableFrom(componentInstance.getClass()))
{
numInterfaces++;
}
if (numInterfaces > 1)
{
throw new Exception("[CONFLICT] lifestyle interfaces: " + componentInstance.getClass().getName());
}
return new ThreadSafeComponentHandler(componentInstance);
}
public abstract Component get() throws Exception;
public abstract void put(Component component) throws Exception;
}
1.1 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/DefaultComponentFactory.java
Index: DefaultComponentFactory.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.txt file.
*/
package org.apache.avalon.excalibur.system.handler;
import org.apache.avalon.excalibur.system.RoleManager;
import org.apache.avalon.framework.activity.Startable;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.parameters.Parameterizable;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.logger.LogEnabled;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.avalon.excalibur.pool.ObjectFactory;
import org.apache.avalon.excalibur.logger.LoggerManager;
/**
* Factory for Avalon components.
*
* @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
* @author <a href="mailto:paul@luminas.co.uk">Paul Russell</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/01/25 20:48:34 $
* @since 4.0
*/
public class DefaultComponentFactory
extends AbstractLogEnabled
implements ObjectFactory, ThreadSafe
{
/** The class which this <code>ComponentFactory</code>
* should create.
*/
private Class m_componentClass;
/** The Context for the component
*/
private Context m_context;
/** The component manager for this component.
*/
private ComponentManager m_componentManager;
/** The configuration for this component.
*/
private Configuration m_configuration;
/** The RoleManager for child ComponentSelectors
*/
private RoleManager m_roles;
/** The LogKitManager for child ComponentSelectors
*/
private LoggerManager m_logManager;
/** The org.apache.avalon.framework.logger.Logger instance.
*/
private org.apache.avalon.framework.logger.Logger m_logEnabledLogger;
/**
* Construct a new component factory for the specified component.
*
* @param componentClass the class to instantiate (must have a default constructor).
* @param configuration the <code>Configuration</code> object to pass to new instances.
* @param componentManager the component manager to pass to <code>Composable</code>s.
* @param context the <code>Context</code> to pass to <code>Contexutalizable</code>s.
* @param roles the <code>RoleManager</code> to pass to <code>DefaultComponentSelector</code>s.
*/
public DefaultComponentFactory( final Class componentClass,
final Configuration configuration,
final ComponentManager componentManager,
final Context context,
final RoleManager roles,
final LoggerManager logkit )
{
m_componentClass = componentClass;
m_configuration = configuration;
m_componentManager = componentManager;
m_context = context;
m_roles = roles;
m_logManager = logkit;
}
public Object newInstance()
throws Exception
{
final Object component = m_componentClass.newInstance();
if (getLogger().isDebugEnabled())
{
getLogger().debug( "ComponentFactory creating new instance of " +
m_componentClass.getName() + "." );
}
if( component instanceof LogEnabled )
{
if( null == m_logManager || null == m_configuration )
{
((LogEnabled)component).enableLogging( getLogger() );
}
else
{
final String logger = m_configuration.getAttribute( "logger", null );
if( null == logger )
{
getLogger().debug( "no logger attribute available, using standard logger" );
((LogEnabled)component).enableLogging( getLogger() );
}
else
{
getLogger().debug( "logger attribute is " + logger );
((LogEnabled)component).enableLogging( m_logManager.getLoggerForCategory( logger ) );
}
}
}
if( component instanceof Contextualizable )
{
((Contextualizable)component).contextualize( m_context );
}
if( component instanceof Composable )
{
((Composable)component).compose( m_componentManager );
}
if( component instanceof Configurable )
{
((Configurable)component).configure( m_configuration );
}
if( component instanceof Parameterizable )
{
((Parameterizable)component).
parameterize( Parameters.fromConfiguration( m_configuration ) );
}
if( component instanceof Initializable )
{
((Initializable)component).initialize();
}
if( component instanceof Startable )
{
((Startable)component).start();
}
return component;
}
public final Class getCreatedClass()
{
return m_componentClass;
}
public final void decommission( final Object component )
throws Exception
{
if (getLogger().isDebugEnabled())
{
getLogger().debug( "ComponentFactory decommissioning instance of " +
m_componentClass.getName() + "." );
}
if( component instanceof Startable )
{
((Startable)component).stop();
}
if( component instanceof Disposable )
{
((Disposable)component).dispose();
}
}
}
1.1 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/DefaultComponentHandler.java
Index: DefaultComponentHandler.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.txt file.
*/
package org.apache.avalon.excalibur.system.handler;
import org.apache.avalon.excalibur.system.RoleManager;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.excalibur.logger.LoggerManager;
/**
* The DefaultComponentHandler to make sure components are initialized
* and destroyed correctly.
*
* @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/01/25 20:48:34 $
* @since 4.0
*/
class DefaultComponentHandler
extends ComponentHandler
{
/** The instance of the ComponentFactory that creates and disposes of the Component */
private final DefaultComponentFactory m_factory;
/** State management boolean stating whether the Handler is initialized or not */
private boolean m_initialized = false;
/** State management boolean stating whether the Handler is disposed or not */
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.
*/
protected DefaultComponentHandler( final Class componentClass,
final Configuration config,
final ComponentManager manager,
final Context context,
final RoleManager roles,
final LoggerManager logkit )
throws Exception
{
m_factory = new DefaultComponentFactory( componentClass, config, manager, context, roles, logkit );
}
/**
* Sets the logger that the ComponentHandler will use.
*/
public void enableLogging( final Logger logger )
{
m_factory.enableLogging( logger );
super.enableLogging( logger );
}
/**
* Initialize the ComponentHandler.
*/
public void initialize()
{
if( m_initialized )
{
return;
}
if (getLogger().isDebugEnabled())
{
getLogger().debug("ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName());
}
m_initialized = true;
}
/**
* Get a reference of the desired Component
*/
public Component get()
throws Exception
{
if( ! m_initialized )
{
throw new IllegalStateException( "You cannot get a component from an uninitialized holder." );
}
if( m_disposed )
{
throw new IllegalStateException( "You cannot get a component from a disposed holder" );
}
return (Component)m_factory.newInstance();
}
/**
* Return a reference of the desired Component
*/
public void put( final Component component )
{
if( !m_initialized )
{
throw new IllegalStateException( "You cannot put a component in an uninitialized holder." );
}
try
{
m_factory.decommission( component );
}
catch( final Exception e )
{
if (getLogger().isWarnEnabled())
{
getLogger().warn( "Error decommissioning component: " +
m_factory.getCreatedClass().getName(), e);
}
}
}
/**
* Dispose of the ComponentHandler and any associated Pools and Factories.
*/
public void dispose()
{
try
{
// do nothing here
if( m_factory instanceof Disposable )
{
((Disposable)m_factory).dispose();
}
}
catch( final Exception e )
{
if (getLogger().isWarnEnabled())
{
getLogger().warn( "Error decommissioning component: " +
m_factory.getCreatedClass().getName(), e );
}
}
m_disposed = true;
}
}
1.1 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/DefaultComponentPool.java
Index: DefaultComponentPool.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.txt file.
*/
package org.apache.avalon.excalibur.system.handler;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.excalibur.pool.ObjectFactory;
import org.apache.avalon.excalibur.pool.SoftResourceLimitingPool;
import org.apache.avalon.excalibur.pool.AbstractPool;
import org.apache.avalon.excalibur.pool.PoolController;
/**
* This is the implementation of <code>Pool</code> for Avalon
* Components that is thread safe. For Component Management, we need
* soft resource limiting due to the possibility of spikes in demand.
* This pool will destroy all unnecessary Components when they are
* no longer needed.
*
* @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
* @author <a href="mailto:Giacomo.Pati@pwr.ch">Giacomo Pati</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/01/25 20:48:34 $
* @since 4.0
*/
public class DefaultComponentPool
extends SoftResourceLimitingPool
implements Initializable
{
/**
* Initialize the <code>Pool</code> with an
* <code>ObjectFactory</code>.
*/
public DefaultComponentPool (ObjectFactory factory) throws Exception {
this(factory,
new DefaultComponentPoolController(
AbstractPool.DEFAULT_POOL_SIZE / 4),
AbstractPool.DEFAULT_POOL_SIZE / 4,
AbstractPool.DEFAULT_POOL_SIZE);
}
/**
* Initialized the <code>Pool</code> with an alternative management
* infrastructure.
*/
public DefaultComponentPool (ObjectFactory factory,
PoolController controller,
int minimumPoolSize,
int maximumPoolSIze)
throws Exception {
super(factory, controller, minimumPoolSize, maximumPoolSIze);
}
}
1.1 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/DefaultComponentPoolController.java
Index: DefaultComponentPoolController.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.txt file.
*/
package org.apache.avalon.excalibur.system.handler;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.avalon.excalibur.pool.PoolController;
/**
* This is the <code>PoolController</code> for the Avalon Excalibur
* Component Management Framework.
*
* @author <a href="mailto:Giacomo.Pati@pwr.ch">Giacomo Pati</a>
* @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/01/25 20:48:34 $
* @since 4.0
*/
public class DefaultComponentPoolController
implements PoolController, ThreadSafe
{
/** Default increase/decrease amount */
public static final int DEFAULT_AMOUNT = 8;
/** Used increase/decrease amount */
protected final int m_amount;
/**
* The default constructor. It initializes the used increase/
* decrease amount to the default.
*/
public DefaultComponentPoolController()
{
m_amount = DefaultComponentPoolController.DEFAULT_AMOUNT;
}
/**
* The alternate constructor. It initializes the used increase/
* decrease amount to the specified number only if it is greater
* than 0. Otherwise it uses the default amount.
*
* @param amount The amount to grow and shrink a pool by.
*/
public DefaultComponentPoolController( final int amount )
{
if ( amount > 0 )
{
m_amount = amount;
}
else
{
m_amount = DefaultComponentPoolController.DEFAULT_AMOUNT;
}
}
/**
* Called when a Pool reaches it's minimum.
* Return the number of elements to increase pool by.
*
* @return the element increase
*/
public int grow()
{
return m_amount;
}
/**
* Called when a pool reaches it's maximum.
* Returns the number of elements to decrease pool by.
*
* @return the element decrease
*/
public int shrink()
{
return m_amount;
}
}
1.1 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/PoolableComponentHandler.java
Index: PoolableComponentHandler.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.txt file.
*/
package org.apache.avalon.excalibur.system.handler;
import org.apache.avalon.excalibur.system.RoleManager;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.excalibur.pool.Poolable;
import org.apache.avalon.excalibur.logger.LoggerManager;
import org.apache.avalon.framework.logger.Logger;
import org.apache.log.Hierarchy;
/**
* The PoolableComponentHandler to make sure components are initialized
* and destroyed correctly.
*
* @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/01/25 20:48:34 $
* @since 4.0
*/
public class PoolableComponentHandler extends ComponentHandler {
/** The instance of the ComponentFactory that creates and disposes of the Component */
private final DefaultComponentFactory m_factory;
/** The pool of components for <code>Poolable</code> Components */
private final DefaultComponentPool m_pool;
/** State management boolean stating whether the Handler is initialized or not */
private boolean m_initialized = false;
/** State management boolean stating whether the Handler is disposed or not */
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.
*/
protected PoolableComponentHandler( final Class componentClass,
final Configuration config,
final ComponentManager manager,
final Context context,
final RoleManager roles,
final LoggerManager logkit )
throws Exception
{
m_factory =
new DefaultComponentFactory( componentClass, config, manager, context, roles, logkit );
int min = config.getAttributeAsInteger("pool-min", DefaultComponentPool.DEFAULT_POOL_SIZE/4);
int max = config.getAttributeAsInteger("pool-max", DefaultComponentPool.DEFAULT_POOL_SIZE);
int grow = config.getAttributeAsInteger("pool-grow", min);
DefaultComponentPoolController controller =
new DefaultComponentPoolController(grow);
m_pool = new DefaultComponentPool( m_factory, controller, min, max );
}
/**
* Sets the logger that the ComponentHandler will use.
*/
public void enableLogging( final Logger logger )
{
m_factory.enableLogging( logger );
m_pool.setLogger( (new Hierarchy()).getLoggerFor("") ); // temporary hack
super.enableLogging( logger );
}
/**
* Initialize the ComponentHandler.
*/
public void initialize()
{
if( m_initialized )
{
return;
}
try
{
m_pool.initialize();
}
catch( Exception e )
{
if (getLogger().isErrorEnabled())
{
getLogger().error( "Cannot use component: " +
m_factory.getCreatedClass().getName(), e );
}
}
if (getLogger().isDebugEnabled())
{
getLogger().debug( "ComponentHandler initialized for: " +
m_factory.getCreatedClass().getName() );
}
m_initialized = true;
}
/**
* Get a reference of the desired Component
*/
public Component get()
throws Exception
{
if( ! m_initialized )
{
throw new IllegalStateException( "You cannot get a component from an " +
"uninitialized holder." );
}
if( m_disposed )
{
throw new IllegalStateException( "You cannot get a component from " +
"a disposed holder" );
}
return (Component)m_pool.get();
}
/**
* Return a reference of the desired Component
*/
public void put( final Component component )
{
if( !m_initialized )
{
throw new IllegalStateException( "You cannot put a component in " +
"an uninitialized holder." );
}
m_pool.put( (Poolable)component );
}
/**
* Dispose of the ComponentHandler and any associated Pools and Factories.
*/
public void dispose()
{
m_pool.dispose();
if( m_factory instanceof Disposable )
{
((Disposable)m_factory).dispose();
}
m_disposed = true;
}
}
1.1 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/ThreadSafeComponentHandler.java
Index: ThreadSafeComponentHandler.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.txt file.
*/
package org.apache.avalon.excalibur.system.handler;
import org.apache.avalon.excalibur.system.RoleManager;
import org.apache.avalon.framework.activity.Startable;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.excalibur.logger.LoggerManager;
import org.apache.avalon.framework.logger.Logger;
/**
* The ThreadSafeComponentHandler to make sure components are initialized
* and destroyed correctly.
*
* @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/01/25 20:48:34 $
* @since 4.0
*/
public class ThreadSafeComponentHandler extends ComponentHandler {
private Component m_instance;
private final DefaultComponentFactory m_factory;
private boolean m_initialized = 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.
*/
protected ThreadSafeComponentHandler( final Class componentClass,
final Configuration config,
final ComponentManager manager,
final Context context,
final RoleManager roles,
final LoggerManager logkit )
throws Exception
{
m_factory = new DefaultComponentFactory( componentClass, config, manager, context, roles, logkit );
}
/**
* 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.
*/
protected ThreadSafeComponentHandler( final Component component )
throws Exception
{
m_instance = component;
m_factory = null;
}
public void enableLogging(Logger log)
{
if ( this.m_factory != null )
{
m_factory.enableLogging(log);
}
super.enableLogging(log);
}
/**
* Initialize the ComponentHandler.
*/
public void initialize()
throws Exception
{
if( m_initialized )
{
return;
}
if (m_instance == null)
{
m_instance = (Component) this.m_factory.newInstance();
}
if (getLogger().isDebugEnabled())
{
if (this.m_factory != null)
{
getLogger().debug("ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName());
}
else
{
getLogger().debug("ComponentHandler initialized for: " + this.m_instance.getClass().getName());
}
}
m_initialized = true;
}
/**
* Get a reference of the desired Component
*/
public final Component get()
throws Exception
{
if( ! m_initialized )
{
throw new IllegalStateException( "You cannot get a component from an uninitialized holder." );
}
if( m_disposed )
{
throw new IllegalStateException( "You cannot get a component from a disposed holder" );
}
return m_instance;
}
/**
* Return a reference of the desired Component
*/
public void put( final Component component )
{
if( !m_initialized )
{
throw new IllegalStateException( "You cannot put a component in an uninitialized holder." );
}
}
/**
* Dispose of the ComponentHandler and any associated Pools and Factories.
*/
public void dispose()
{
try {
if( null != m_factory )
{
m_factory.decommission( m_instance );
}
else
{
if( m_instance instanceof Startable )
{
((Startable)m_instance).stop();
}
if( m_instance instanceof Disposable )
{
((Disposable)m_instance).dispose();
}
}
m_instance = null;
}
catch( final Exception e )
{
if (getLogger().isWarnEnabled())
{
getLogger().warn( "Error decommissioning component: " +
m_factory.getCreatedClass().getName(), e );
}
}
m_disposed = true;
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>