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/28 19:11:21 UTC

cvs commit: jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/handler AbstractThreadSafeComponentHandler.java LazyThreadSafeComponentHandler.java ThreadSafeComponentHandler.java

crafterm    2002/06/28 10:11:21

  Modified:    fortress/src/java/org/apache/excalibur/fortress/handler
                        ThreadSafeComponentHandler.java
  Added:       fortress/src/java/org/apache/excalibur/fortress/handler
                        AbstractThreadSafeComponentHandler.java
                        LazyThreadSafeComponentHandler.java
  Log:
  Created a LazyThreadSafeComponentHandler class, which acts the same as a
  ThreadSafeComponentHandler, except that the instance is not instantiated
  during initialization, rather, at first access when its actually needed.
  
  Refactored common code between ThreadSafeComponentHandler and
  LazyThreadSafeComponentHandler into an abstract base class to avoid
  duplication.
  
  Revision  Changes    Path
  1.17      +7 -70     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.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- ThreadSafeComponentHandler.java	18 Jun 2002 18:45:36 -0000	1.16
  +++ ThreadSafeComponentHandler.java	28 Jun 2002 17:11:20 -0000	1.17
  @@ -7,13 +7,9 @@
    */
   package org.apache.excalibur.fortress.handler;
   
  -import org.apache.avalon.excalibur.logger.LoggerManager;
  -import org.apache.avalon.framework.activity.Disposable;
  -import org.apache.avalon.framework.activity.Startable;
   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.avalon.framework.service.ServiceManager;
   
   /**
  @@ -25,10 +21,9 @@
    * @version CVS $Revision$ $Date$
    * @since 4.0
    */
  -public final class ThreadSafeComponentHandler extends AbstractComponentHandler
  +public final class ThreadSafeComponentHandler
  +    extends AbstractThreadSafeComponentHandler
   {
  -    private Object m_instance;
  -
       /**
        * Create a ComponentHandler that takes care of hiding the details of
        * whether a Component is ThreadSafe, Poolable, or SingleThreaded.
  @@ -57,80 +52,22 @@
               return;
           }
   
  -        if( m_instance == null )
  -        {
  -            m_instance = this.m_factory.newInstance();
  -        }
  +        m_instance = 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_logger.debug( "ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName() );
           }
   
           m_initialized = true;
       }
   
       /**
  -     * Get a reference of the desired Component
  +     * Obtain the instance. 
        */
  -    public final Object get()
  +    protected Object getInstance()
           throws Exception
       {
  -        super.get();
  -
           return m_instance;
  -    }
  -
  -    /**
  -     * Return a reference of the desired Component
  -     */
  -    public void put( final Object component )
  -    {
  -        super.put( component );
  -    }
  -
  -    /**
  -     * Dispose of the ComponentHandler and any associated Pools and Factories.
  -     */
  -    public void dispose()
  -    {
  -        try
  -        {
  -            if( null != m_factory )
  -            {
  -                m_factory.dispose( 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;
       }
   }
  
  
  
  1.1                  jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/handler/AbstractThreadSafeComponentHandler.java
  
  Index: AbstractThreadSafeComponentHandler.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.logger.LoggerManager;
  import org.apache.avalon.framework.activity.Disposable;
  import org.apache.avalon.framework.activity.Startable;
  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.avalon.framework.service.ServiceManager;
  
  /**
   * The ThreadSafeComponentHandler to make sure 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/28 17:11:20 $
   * @since 4.0
   */
  public abstract class AbstractThreadSafeComponentHandler
      extends AbstractComponentHandler
  {
      protected Object m_instance;
  
      /**
       * 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 AbstractThreadSafeComponentHandler( final Class componentClass,
                                                 final Configuration config,
                                                 final ComponentManager manager,
                                                 final ServiceManager service,
                                                 final Context context )
          throws Exception
      {
          super( componentClass, config, manager, service, context );
      }
  
      /**
       * Obtain the instance
       */
      protected abstract Object getInstance() throws Exception;
  
      /**
       * Get a reference of the desired Component
       */
      public final Object get()
          throws Exception
      {
          super.get();
          return getInstance();
      }
  
      /**
       * Return a reference of the desired Component
       */
      public void put( final Object component )
      {
          try
          {
              super.put( component );
          }
          catch ( Exception e )
          {
              if ( m_logger.isWarnEnabled() )
              {
                  m_logger.warn( "Error occured while releasing component", e );
              }
          }
      }
  
      /**
       * Dispose of the ComponentHandler.
       */
      public void dispose()
      {
          if ( m_instance == null)
              return;
  
          try
          {
              if( null != m_factory )
              {
                  m_factory.dispose( 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;
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/handler/LazyThreadSafeComponentHandler.java
  
  Index: LazyThreadSafeComponentHandler.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.framework.component.ComponentManager;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.service.ServiceManager;
  
  /**
   * LazyThreadSafeComponentHandler. This class manages thread safe
   * components exactly like ThreadSafeComponentHandler, except that it
   * does not instantiate the component until it is first requested.
   *
   * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/06/28 17:11:20 $
   * @since 4.0
   */
  public final class LazyThreadSafeComponentHandler
      extends AbstractThreadSafeComponentHandler
  {
      /**
       * 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 LazyThreadSafeComponentHandler( final Class componentClass,
                                             final Configuration config,
                                             final ComponentManager manager,
                                             final ServiceManager service,
                                             final Context context )
          throws Exception
      {
          super( componentClass, config, manager, service, context );
          m_logger = m_logkit.getLoggerForCategory( "system.handler.threadsafe.lazy" );
          m_name = "LazyThreadSafeComponentHandler";
      }
  
      /**
       * Initialize the ComponentHandler.
       */
      public void initialize()
          throws Exception
      {
          if( m_initialized )
          {
              return;
          }
  
          if( m_logger.isDebugEnabled() )
          {
              m_logger.debug( "ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName() );
          }
  
          m_initialized = true;
      }
  
      /**
       * Obtain the instance
       */
      protected Object getInstance()
          throws Exception
      {
          synchronized (this)
          {
              if ( m_instance == null )
              {
                  m_instance = m_factory.newInstance();
  
                  if( m_logger.isDebugEnabled() )
                  {
                      m_logger.debug(
                          "Created instance of " + m_factory.getCreatedClass().getName() );
                  }
              }
          }
  
          return m_instance;
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>