avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From blorit...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler ComponentFactory.java FactoryComponentHandler.java PerThreadComponentHandler.java ComponentHandler.java PoolableComponentHandler.java ThreadSafeComponentHandler.java DefaultComponentFactory.java DefaultComponentHandler.java DefaultComponentPool.java DefaultComponentPoolController.java
Date Mon, 28 Jan 2002 20:54:01 GMT
bloritsch    02/01/28 12:54:01

  Modified:    src/scratchpad/org/apache/avalon/excalibur/system/handler
                        ComponentHandler.java PoolableComponentHandler.java
                        ThreadSafeComponentHandler.java
  Added:       src/scratchpad/org/apache/avalon/excalibur/system/handler
                        ComponentFactory.java FactoryComponentHandler.java
                        PerThreadComponentHandler.java
  Removed:     src/scratchpad/org/apache/avalon/excalibur/system/handler
                        DefaultComponentFactory.java
                        DefaultComponentHandler.java
                        DefaultComponentPool.java
                        DefaultComponentPoolController.java
  Log:
  rearrange handler code
  
  Revision  Changes    Path
  1.2       +7 -109    jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/ComponentHandler.java
  
  Index: ComponentHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/ComponentHandler.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ComponentHandler.java	25 Jan 2002 20:48:34 -0000	1.1
  +++ ComponentHandler.java	28 Jan 2002 20:54:01 -0000	1.2
  @@ -7,120 +7,18 @@
    */
   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.
  + * The ComponentHandler interface marks the ComponentHandler implementations.
  + * The desire for a ComponentHandler is to manage the instances of a Component.
    *
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2002/01/25 20:48:34 $
  + * @version CVS $Revision: 1.2 $ $Date: 2002/01/28 20:54:01 $
    * @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;
  +public interface ComponentHandler
  +{
  +    Component get() throws Exception;
  +    void put(Component component);
   }
  
  
  
  1.2       +17 -41    jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/PoolableComponentHandler.java
  
  Index: PoolableComponentHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/PoolableComponentHandler.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PoolableComponentHandler.java	25 Jan 2002 20:48:34 -0000	1.1
  +++ PoolableComponentHandler.java	28 Jan 2002 20:54:01 -0000	1.2
  @@ -13,7 +13,9 @@
   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.Pool;
   import org.apache.avalon.excalibur.pool.Poolable;
  +import org.apache.avalon.excalibur.pool.PoolManager;
   import org.apache.avalon.excalibur.logger.LoggerManager;
   import org.apache.avalon.framework.logger.Logger;
   import org.apache.log.Hierarchy;
  @@ -23,15 +25,15 @@
    * 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 $
  + * @version CVS $Revision: 1.2 $ $Date: 2002/01/28 20:54:01 $
    * @since 4.0
    */
  -public class PoolableComponentHandler extends ComponentHandler {
  +public class PoolableComponentHandler implements ComponentHandler {
       /** The instance of the ComponentFactory that creates and disposes of the Component
*/
  -    private final DefaultComponentFactory    m_factory;
  +    private final ComponentFactory    m_factory;
   
       /** The pool of components for <code>Poolable</code> Components */
  -    private final DefaultComponentPool                m_pool;
  +    private final Pool                m_pool;
   
       /** State management boolean stating whether the Handler is initialized or not */
       private boolean                    m_initialized   = false;
  @@ -39,6 +41,10 @@
       /** State management boolean stating whether the Handler is disposed or not */
       private boolean                    m_disposed      = false;
   
  +    /** Logger for PoolableComponentHandler */
  +    private final Logger               m_logger;
  +
  +
       /**
        * Create a ComponentHandler that takes care of hiding the details of
        * whether a Component is ThreadSafe, Poolable, or SingleThreaded.
  @@ -49,30 +55,15 @@
                                           final ComponentManager manager,
                                           final Context context,
                                           final RoleManager roles,
  -                                        final LoggerManager logkit )
  +                                        final LoggerManager logkit,
  +                                        final PoolManager poolManager )
           throws Exception
       {
           m_factory =
  -            new DefaultComponentFactory( componentClass, config, manager, context, roles,
logkit );
  +            new ComponentFactory( 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 );
  +        m_pool = poolManager.getManagedPool( m_factory, config.getAttributeAsInteger( "pool-min",
4 ) );
  +        m_logger = logkit.getLoggerForCategory("system.handler.poolable");
       }
   
       /**
  @@ -85,22 +76,9 @@
               return;
           }
   
  -        try
  -        {
  -            m_pool.initialize();
  -        }
  -        catch( Exception e )
  +        if (m_logger.isDebugEnabled())
           {
  -            if (getLogger().isErrorEnabled())
  -            {
  -                getLogger().error( "Cannot use component: " +
  -                                   m_factory.getCreatedClass().getName(), e );
  -            }
  -        }
  -
  -        if (getLogger().isDebugEnabled())
  -        {
  -            getLogger().debug( "ComponentHandler initialized for: " +
  +            m_logger.debug( "ComponentHandler initialized for: " +
                                  m_factory.getCreatedClass().getName() );
           }
   
  @@ -147,8 +125,6 @@
        */
       public void dispose()
       {
  -        m_pool.dispose();
  -
           if( m_factory instanceof Disposable )
           {
               ((Disposable)m_factory).dispose();
  
  
  
  1.2       +11 -31    jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/ThreadSafeComponentHandler.java
  
  Index: ThreadSafeComponentHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/ThreadSafeComponentHandler.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ThreadSafeComponentHandler.java	25 Jan 2002 20:48:34 -0000	1.1
  +++ ThreadSafeComponentHandler.java	28 Jan 2002 20:54:01 -0000	1.2
  @@ -22,14 +22,15 @@
    * 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 $
  + * @version CVS $Revision: 1.2 $ $Date: 2002/01/28 20:54:01 $
    * @since 4.0
    */
  -public class ThreadSafeComponentHandler extends ComponentHandler {
  +public class ThreadSafeComponentHandler implements ComponentHandler {
       private Component m_instance;
  -    private final DefaultComponentFactory m_factory;
  +    private final ComponentFactory m_factory;
       private boolean m_initialized = false;
       private boolean m_disposed = false;
  +    private final Logger m_logger;
   
       /**
        * Create a ComponentHandler that takes care of hiding the details of
  @@ -44,29 +45,8 @@
                                             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);
  +        m_factory = new ComponentFactory( componentClass, config, manager, context, roles,
logkit );
  +        m_logger = logkit.getLoggerForCategory("system.handler.threadsafe");
       }
   
       /**
  @@ -85,15 +65,15 @@
               m_instance = (Component) this.m_factory.newInstance();
           }
   
  -        if (getLogger().isDebugEnabled())
  +        if (m_logger.isDebugEnabled())
           {
               if (this.m_factory != null)
               {
  -                getLogger().debug("ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName());
  +                m_logger.debug("ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName());
               }
               else
               {
  -                getLogger().debug("ComponentHandler initialized for: " + this.m_instance.getClass().getName());
  +                m_logger.debug("ComponentHandler initialized for: " + this.m_instance.getClass().getName());
               }
           }
   
  @@ -157,9 +137,9 @@
           }
           catch( final Exception e )
           {
  -            if (getLogger().isWarnEnabled())
  +            if (m_logger.isWarnEnabled())
               {
  -                getLogger().warn( "Error decommissioning component: " +
  +                m_logger.warn( "Error decommissioning component: " +
                                     m_factory.getCreatedClass().getName(), e );
               }
           }
  
  
  
  1.1                  jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/handler/ComponentFactory.java
  
  Index: ComponentFactory.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.Logger;
  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/28 20:54:01 $
   * @since 4.0
   */
  public class ComponentFactory
      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 logger for the ComponentFactory
       */
      private Logger                  m_logger;
  
      /**
       * 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 ComponentFactory( 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;
          m_logger = m_logManager.getLoggerForCategory("system.factory");
      }
  
      public Object newInstance()
          throws Exception
      {
          final Object component = m_componentClass.newInstance();
  
          if (m_logger.isDebugEnabled())
          {
              m_logger.debug( "ComponentFactory creating new instance of " +
                                 m_componentClass.getName() + "." );
          }
  
          if( component instanceof LogEnabled )
          {
              final String logger = m_configuration.getAttribute( "logger", null );
              if( null == logger )
              {
                  m_logger.debug( "no logger attribute available, using standard logger" );
                  ((LogEnabled)component).enableLogging( m_logManager.getDefaultLogger() );
              }
              else
              {
                  m_logger.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 (m_logger.isDebugEnabled())
          {
              m_logger.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/FactoryComponentHandler.java
  
  Index: FactoryComponentHandler.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/28 20:54:01 $
   * @since 4.0
   */
  class DefaultComponentHandler
      implements ComponentHandler
  {
      /** The instance of the ComponentFactory that creates and disposes of the Component
*/
      private final ComponentFactory     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;
  
      /** 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.
       * 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 ComponentFactory( componentClass, config, manager, context, roles,
logkit );
          m_logger = logkit.getLoggerForCategory("system.handler.factory");
      }
  
      /**
       * Initialize the ComponentHandler.
       */
      public void initialize()
      {
          if( m_initialized )
          {
              return;
          }
  
          if (m_logger.isDebugEnabled())
          {
              m_logger.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 (m_logger.isWarnEnabled())
              {
                  m_logger.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 (m_logger.isWarnEnabled())
              {
                  m_logger.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/PerThreadComponentHandler.java
  
  Index: PerThreadComponentHandler.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/28 20:54:01 $
   * @since 4.0
   */
  public class PerThreadComponentHandler implements ComponentHandler {
      private ThreadLocal m_instance;
      private final ComponentFactory m_factory;
      private boolean m_initialized = false;
      private boolean m_disposed = false;
      private final Logger m_logger;
  
      /**
       * 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 PerThreadComponentHandler ( final Class componentClass,
                                            final Configuration config,
                                            final ComponentManager manager,
                                            final Context context,
                                            final RoleManager roles,
                                            final LoggerManager logkit )
          throws Exception
      {
          m_factory = new ComponentFactory( componentClass, config, manager, context, roles,
logkit );
          m_logger = logkit.getLoggerForCategory("system.handler.perthread");
      }
  
      /**
       * Initialize the ComponentHandler.
       */
      public void initialize()
      throws Exception
      {
          if( m_initialized )
          {
              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)
              {
                  m_logger.debug("ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName());
              }
              else
              {
                 m_logger.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" );
          }
  
          if (m_instance == null)
          {
              m_instance = new ThreadLocal();
              m_instance.set( this.m_factory.newInstance() );
          }
  
          return (Component) m_instance.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." );
          }
      }
  
      /**
       * 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 (m_logger.isWarnEnabled())
              {
                  m_logger.warn( "Error decommissioning component: " +
                                  m_factory.getCreatedClass().getName(), e );
              }
          }
  
          m_disposed = true;
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message