bloritsch 02/01/28 13:09:25
Modified: src/scratchpad/org/apache/avalon/excalibur/system/handler
PerThreadComponentHandler.java
Log:
fix PerThreadComponentHandler code
Revision Changes Path
1.2 +32 -31
jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/PerThreadComponentHandler.java
Index: PerThreadComponentHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/PerThreadComponentHandler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- PerThreadComponentHandler.java 28 Jan 2002 20:54:01 -0000 1.1
+++ PerThreadComponentHandler.java 28 Jan 2002 21:09:25 -0000 1.2
@@ -22,11 +22,11 @@
* and destroyed correctly.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
- * @version CVS $Revision: 1.1 $ $Date: 2002/01/28 20:54:01 $
+ * @version CVS $Revision: 1.2 $ $Date: 2002/01/28 21:09:25 $
* @since 4.0
*/
public class PerThreadComponentHandler implements ComponentHandler {
- private ThreadLocal m_instance;
+ private ThreadLocalComponent m_instance;
private final ComponentFactory m_factory;
private boolean m_initialized = false;
private boolean m_disposed = false;
@@ -46,6 +46,7 @@
throws Exception
{
m_factory = new ComponentFactory( componentClass, config, manager,
context, roles, logkit );
+ m_instance = new ThreadLocalComponent( m_factory );
m_logger = logkit.getLoggerForCategory("system.handler.perthread");
}
@@ -60,12 +61,6 @@
return;
}
- if (m_instance == null)
- {
- m_instance = new ThreadLocal();
- m_instance.set( this.m_factory.newInstance() );
- }
-
if (m_logger.isDebugEnabled())
{
if (this.m_factory != null)
@@ -97,13 +92,7 @@
throw new IllegalStateException( "You cannot get a component
from a disposed holder" );
}
- if (m_instance == null)
- {
- m_instance = new ThreadLocal();
- m_instance.set( this.m_factory.newInstance() );
- }
-
- return (Component) m_instance.get();
+ return m_instance.getComponent();
}
/**
@@ -123,22 +112,7 @@
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_factory.decommission( m_instance.getComponent() );
m_instance = null;
}
@@ -152,5 +126,32 @@
}
m_disposed = true;
+ }
+
+ private final static class ThreadLocalComponent extends ThreadLocal
+ {
+ private final ComponentFactory m_factory;
+
+ protected ThreadLocalComponent(ComponentFactory factory)
+ {
+ m_factory = factory;
+ }
+
+ protected Object initialValue()
+ {
+ try
+ {
+ return m_factory.newInstance();
+ }
+ catch (Exception e)
+ {
+ return null;
+ }
+ }
+
+ public Component getComponent()
+ {
+ return (Component) this.get();
+ }
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>