You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by cr...@apache.org on 2002/06/05 19:06:48 UTC
cvs commit: jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/handler AbstractComponentHandler.java FactoryComponentHandler.java PerThreadComponentHandler.java PoolableComponentHandler.java ThreadSafeComponentHandler.java
crafterm 2002/06/05 10:06:48
Modified: fortress/src/java/org/apache/excalibur/fortress/handler
FactoryComponentHandler.java
PerThreadComponentHandler.java
PoolableComponentHandler.java
ThreadSafeComponentHandler.java
Added: fortress/src/java/org/apache/excalibur/fortress/handler
AbstractComponentHandler.java
Log:
Refactored ComponentHandler classes. Moved common code between all handler
classes into an abstract base class, from which all handlers derive.
Revision Changes Path
1.15 +8 -94 jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/handler/FactoryComponentHandler.java
Index: FactoryComponentHandler.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/handler/FactoryComponentHandler.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- FactoryComponentHandler.java 13 May 2002 12:17:39 -0000 1.14
+++ FactoryComponentHandler.java 5 Jun 2002 17:06:48 -0000 1.15
@@ -7,43 +7,24 @@
*/
package org.apache.excalibur.fortress.handler;
-import org.apache.avalon.excalibur.instrument.Instrument;
-import org.apache.avalon.excalibur.instrument.Instrumentable;
import org.apache.avalon.excalibur.logger.LoggerManager;
-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.Logger;
-import org.apache.excalibur.fortress.Container;
-import org.apache.excalibur.fortress.util.RoleManager;
/**
* The FactoryComponentHandler to make sure components are initialized
* and destroyed correctly.
*
* @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
- * @version CVS $Revision: 1.14 $ $Date: 2002/05/13 12:17:39 $
+ * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
+ * @version CVS $Revision: 1.15 $ $Date: 2002/06/05 17:06:48 $
* @since 4.0
*/
-public final class FactoryComponentHandler
- implements ComponentHandler, Instrumentable
+public final class FactoryComponentHandler extends AbstractComponentHandler
{
- /** The instance of the ComponentFactory that creates and disposes of the Component */
- private final ComponentFactory m_factory;
-
- private String m_name = "FactoryComponentHandler";
-
- /** 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;
-
- /** Logger for factory */
- private final Logger m_logger;
-
/**
* Create a ComponentHandler that takes care of hiding the details of
* whether a Component is ThreadSafe, Poolable, or SingleThreaded.
@@ -55,16 +36,9 @@
final Context context )
throws Exception
{
- RoleManager roles = (RoleManager)context.get( Container.ROLE_MANAGER );
- LoggerManager logkit = (LoggerManager)context.get( Container.LOGGER_MANAGER );
-
- m_factory = new ComponentFactory( componentClass, config, manager, context, roles, logkit );
- m_logger = logkit.getLoggerForCategory( "system.handler.factory" );
- }
-
- public boolean isInitialized()
- {
- return m_initialized;
+ super( componentClass, config, manager, context );
+ m_logger = m_logkit.getLoggerForCategory( "system.handler.factory" );
+ m_name = "FactoryComponentHandler";
}
/**
@@ -90,15 +64,7 @@
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" );
- }
+ super.get();
return (Component)m_factory.newInstance();
}
@@ -108,10 +74,7 @@
*/
public void put( final Component component )
{
- if( !m_initialized )
- {
- throw new IllegalStateException( "You cannot put a component in an uninitialized holder." );
- }
+ super.put( component );
try
{
@@ -126,53 +89,4 @@
}
}
}
-
- /**
- * 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( m_logger.isWarnEnabled() )
- {
- m_logger.warn( "Error decommissioning component: " +
- m_factory.getCreatedClass().getName(), e );
- }
- }
-
- m_disposed = true;
- }
-
- public final void setInstrumentableName( String name )
- {
- m_name = name;
- }
-
- public final String getInstrumentableName()
- {
- return m_name;
- }
-
- public final Instrument[] getInstruments()
- {
- return Instrumentable.EMPTY_INSTRUMENT_ARRAY;
- }
-
- public final Instrumentable[] getChildInstrumentables()
- {
- return new Instrumentable[]{
- m_factory
- };
- }
-
}
1.16 +10 -72 jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/handler/PerThreadComponentHandler.java
Index: PerThreadComponentHandler.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/handler/PerThreadComponentHandler.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- PerThreadComponentHandler.java 13 May 2002 12:17:39 -0000 1.15
+++ PerThreadComponentHandler.java 5 Jun 2002 17:06:48 -0000 1.16
@@ -7,33 +7,25 @@
*/
package org.apache.excalibur.fortress.handler;
-import org.apache.avalon.excalibur.instrument.Instrument;
-import org.apache.avalon.excalibur.instrument.Instrumentable;
import org.apache.avalon.excalibur.logger.LoggerManager;
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.Logger;
-import org.apache.excalibur.fortress.Container;
-import org.apache.excalibur.fortress.util.RoleManager;
/**
* 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.15 $ $Date: 2002/05/13 12:17:39 $
+ * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
+ * @version CVS $Revision: 1.16 $ $Date: 2002/06/05 17:06:48 $
* @since 4.0
*/
-public final class PerThreadComponentHandler implements ComponentHandler, Instrumentable
+public final class PerThreadComponentHandler extends AbstractComponentHandler
{
private ThreadLocalComponent m_instance;
- private final ComponentFactory m_factory;
- private boolean m_initialized = false;
- private boolean m_disposed = false;
- private final Logger m_logger;
- private String m_name = "PerThreadComponentHandler";
/**
* Create a ComponentHandler that takes care of hiding the details of
@@ -46,17 +38,10 @@
final Context context )
throws Exception
{
- RoleManager roles = (RoleManager)context.get( Container.ROLE_MANAGER );
- LoggerManager logkit = (LoggerManager)context.get( Container.LOGGER_MANAGER );
-
- m_factory = new ComponentFactory( componentClass, config, manager, context, roles, logkit );
+ super( componentClass, config, manager, context );
m_instance = new ThreadLocalComponent( m_factory );
- m_logger = logkit.getLoggerForCategory( "system.handler.perthread" );
- }
-
- public boolean isInitialized()
- {
- return m_initialized;
+ m_logger = m_logkit.getLoggerForCategory( "system.handler.perthread" );
+ m_name = "PerThreadComponentHandler";
}
/**
@@ -91,15 +76,7 @@
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" );
- }
+ super.get();
return m_instance.getComponent();
}
@@ -109,10 +86,7 @@
*/
public void put( final Component component )
{
- if( !m_initialized )
- {
- throw new IllegalStateException( "You cannot put a component in an uninitialized holder." );
- }
+ super.put( component );
}
/**
@@ -120,22 +94,9 @@
*/
public void dispose()
{
- try
- {
- m_factory.dispose( m_instance.getComponent() );
+ super.dispose();
- m_instance = null;
- }
- catch( final Exception e )
- {
- if( m_logger.isWarnEnabled() )
- {
- m_logger.warn( "Error decommissioning component: " +
- m_factory.getCreatedClass().getName(), e );
- }
- }
-
- m_disposed = true;
+ m_instance = null;
}
private static final class ThreadLocalComponent extends ThreadLocal
@@ -164,27 +125,4 @@
return (Component)this.get();
}
}
-
- public final void setInstrumentableName( String name )
- {
- m_name = name;
- }
-
- public final String getInstrumentableName()
- {
- return m_name;
- }
-
- public final Instrument[] getInstruments()
- {
- return EMPTY_INSTRUMENT_ARRAY;
- }
-
- public final Instrumentable[] getChildInstrumentables()
- {
- return new Instrumentable[]{
- m_factory
- };
- }
-
}
1.19 +12 -72 jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/handler/PoolableComponentHandler.java
Index: PoolableComponentHandler.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/handler/PoolableComponentHandler.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- PoolableComponentHandler.java 13 May 2002 12:17:39 -0000 1.18
+++ PoolableComponentHandler.java 5 Jun 2002 17:06:48 -0000 1.19
@@ -7,8 +7,6 @@
*/
package org.apache.excalibur.fortress.handler;
-import org.apache.avalon.excalibur.instrument.Instrument;
-import org.apache.avalon.excalibur.instrument.Instrumentable;
import org.apache.avalon.excalibur.logger.LoggerManager;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.component.Component;
@@ -26,31 +24,21 @@
* and destroyed correctly.
*
* @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
- * @version CVS $Revision: 1.18 $ $Date: 2002/05/13 12:17:39 $
+ * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
+ * @version CVS $Revision: 1.19 $ $Date: 2002/06/05 17:06:48 $
* @since 4.0
*/
-public final class PoolableComponentHandler implements ComponentHandler, Instrumentable
+public final class PoolableComponentHandler extends AbstractComponentHandler
{
- /** The instance of the ComponentFactory that creates and disposes of the Component */
- private final ComponentFactory m_factory;
-
/** The instance of the PoolManager to create the Pool for the Handler */
private final PoolManager m_poolManager;
/** The pool of components for <code>Poolable</code> Components */
private Pool m_pool;
- /** State management boolean stating whether the Handler is disposed or not */
- private boolean m_disposed = false;
-
- /** Logger for PoolableComponentHandler */
- private final Logger m_logger;
-
/** The Config element for the poolable */
private final int m_poolMin;
- private String m_name = "PoolableComponentHandler";
-
/**
* Create a ComponentHandler that takes care of hiding the details of
* whether a Component is ThreadSafe, Poolable, or SingleThreaded.
@@ -62,19 +50,11 @@
final Context context )
throws Exception
{
+ super( componentClass, config, manager, context );
m_poolMin = config.getAttributeAsInteger( "pool-min", 10 );
- RoleManager roles = (RoleManager)context.get( Container.ROLE_MANAGER );
- LoggerManager logkit = (LoggerManager)context.get( Container.LOGGER_MANAGER );
- m_factory =
- new ComponentFactory( componentClass, config, manager, context, roles, logkit );
-
- m_logger = logkit.getLoggerForCategory( "system.handler.poolable" );
+ m_logger = m_logkit.getLoggerForCategory( "system.handler.poolable" );
m_poolManager = (PoolManager)context.get( Container.POOL_MANAGER );
- }
-
- public boolean isInitialized()
- {
- return null != m_pool;
+ m_name = "PoolableComponentHandler";
}
/**
@@ -83,7 +63,7 @@
public void initialize()
throws Exception
{
- if( null != m_pool )
+ if( m_initialized )
{
return;
}
@@ -95,6 +75,7 @@
m_logger.debug( "ComponentHandler initialized for: " +
m_factory.getCreatedClass().getName() );
}
+ m_initialized = true;
}
/**
@@ -103,17 +84,7 @@
public Component get()
throws Exception
{
- if( null == m_pool )
- {
- 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" );
- }
+ super.get();
return (Component)m_pool.acquire();
}
@@ -123,11 +94,7 @@
*/
public void put( final Component component )
{
- if( null == m_pool )
- {
- throw new IllegalStateException( "You cannot put a component in " +
- "an uninitialized holder." );
- }
+ super.put( component );
m_pool.release( component );
}
@@ -137,34 +104,7 @@
*/
public void dispose()
{
- if( m_factory instanceof Disposable )
- {
- ( (Disposable)m_factory ).dispose();
- }
-
- m_disposed = true;
- }
-
- public final void setInstrumentableName( String name )
- {
- m_name = name;
- }
-
- public final String getInstrumentableName()
- {
- return m_name;
+ super.dispose();
+ // REVISIT: release m_pool ? m_pool = null;
}
-
- public final Instrument[] getInstruments()
- {
- return EMPTY_INSTRUMENT_ARRAY;
- }
-
- public final Instrumentable[] getChildInstrumentables()
- {
- return new Instrumentable[]{
- m_factory
- };
- }
-
}
1.14 +8 -57 jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/handler/ThreadSafeComponentHandler.java
Index: ThreadSafeComponentHandler.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/handler/ThreadSafeComponentHandler.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- ThreadSafeComponentHandler.java 13 May 2002 12:17:39 -0000 1.13
+++ ThreadSafeComponentHandler.java 5 Jun 2002 17:06:48 -0000 1.14
@@ -7,8 +7,6 @@
*/
package org.apache.excalibur.fortress.handler;
-import org.apache.avalon.excalibur.instrument.Instrument;
-import org.apache.avalon.excalibur.instrument.Instrumentable;
import org.apache.avalon.excalibur.logger.LoggerManager;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.activity.Startable;
@@ -17,25 +15,19 @@
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.logger.Logger;
-import org.apache.excalibur.fortress.Container;
-import org.apache.excalibur.fortress.util.RoleManager;
/**
* 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.13 $ $Date: 2002/05/13 12:17:39 $
+ * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
+ * @version CVS $Revision: 1.14 $ $Date: 2002/06/05 17:06:48 $
* @since 4.0
*/
-public final class ThreadSafeComponentHandler implements ComponentHandler, Instrumentable
+public final class ThreadSafeComponentHandler extends AbstractComponentHandler
{
private Component m_instance;
- private final ComponentFactory m_factory;
- private boolean m_initialized = false;
- private boolean m_disposed = false;
- private final Logger m_logger;
- private String m_name = "ThreadSafeComponentHandler";
/**
* Create a ComponentHandler that takes care of hiding the details of
@@ -48,16 +40,9 @@
final Context context )
throws Exception
{
- RoleManager roles = (RoleManager)context.get( Container.ROLE_MANAGER );
- LoggerManager logkit = (LoggerManager)context.get( Container.LOGGER_MANAGER );
-
- m_factory = new ComponentFactory( componentClass, config, manager, context, roles, logkit );
- m_logger = logkit.getLoggerForCategory( "system.handler.threadsafe" );
- }
-
- public boolean isInitialized()
- {
- return m_initialized;
+ super( componentClass, config, manager, context );
+ m_logger = m_logkit.getLoggerForCategory( "system.handler.threadsafe" );
+ m_name = "ThreadSafeComponentHandler";
}
/**
@@ -97,15 +82,7 @@
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" );
- }
+ super.get();
return m_instance;
}
@@ -115,10 +92,7 @@
*/
public void put( final Component component )
{
- if( !m_initialized )
- {
- throw new IllegalStateException( "You cannot put a component in an uninitialized holder." );
- }
+ super.put( component );
}
/**
@@ -158,27 +132,4 @@
m_disposed = true;
}
-
- public final void setInstrumentableName( String name )
- {
- m_name = name;
- }
-
- public final String getInstrumentableName()
- {
- return m_name;
- }
-
- public final Instrument[] getInstruments()
- {
- return EMPTY_INSTRUMENT_ARRAY;
- }
-
- public final Instrumentable[] getChildInstrumentables()
- {
- return new Instrumentable[]{
- m_factory
- };
- }
-
}
1.1 jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/handler/AbstractComponentHandler.java
Index: AbstractComponentHandler.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.excalibur.fortress.handler;
import org.apache.avalon.excalibur.instrument.Instrument;
import org.apache.avalon.excalibur.instrument.Instrumentable;
import org.apache.avalon.excalibur.logger.LoggerManager;
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.Logger;
import org.apache.excalibur.fortress.Container;
import org.apache.excalibur.fortress.util.RoleManager;
/**
* AbstractComponentHandler class, ensures components are initialized
* and destroyed correctly.
*
* @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
* @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/06/05 17:06:48 $
* @since 4.0
*/
public abstract class AbstractComponentHandler
implements ComponentHandler, Disposable, Instrumentable
{
/** The instance of the ComponentFactory that creates and disposes of the Component */
protected final ComponentFactory m_factory;
protected String m_name;
/** State management boolean stating whether the Handler is initialized or not */
protected boolean m_initialized = false;
/** State management boolean stating whether the Handler is disposed or not */
protected boolean m_disposed = false;
/** Logger for factory */
protected Logger m_logger;
/** Logger Manager */
protected LoggerManager m_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.
*/
public AbstractComponentHandler( final Class componentClass,
final Configuration config,
final ComponentManager manager,
final Context context )
throws Exception
{
RoleManager roles = (RoleManager)context.get( Container.ROLE_MANAGER );
m_logkit = (LoggerManager)context.get( Container.LOGGER_MANAGER );
m_factory = new ComponentFactory( componentClass, config, manager, context, roles, m_logkit );
}
public boolean isInitialized()
{
return m_initialized;
}
/**
* Initialize the ComponentHandler.
*/
public abstract void initialize()
throws Exception;
/**
* 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 null; // value not used, satisfies compiler
}
/**
* 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( m_factory instanceof Disposable )
{
( (Disposable)m_factory ).dispose();
}
}
catch( final Exception e )
{
if( m_logger.isWarnEnabled() )
{
m_logger.warn( "Error decommissioning component: " +
m_factory.getCreatedClass().getName(), e );
}
}
m_disposed = true;
}
public final void setInstrumentableName( String name )
{
m_name = name;
}
public final String getInstrumentableName()
{
return m_name;
}
public final Instrument[] getInstruments()
{
return Instrumentable.EMPTY_INSTRUMENT_ARRAY;
}
public final Instrumentable[] getChildInstrumentables()
{
return new Instrumentable[]{
m_factory
};
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>