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/assembly/src/java/org/apache/excalibur/merlin/resource ThreadLocalLifestyleHandler.java
Date Thu, 15 Aug 2002 00:54:29 GMT
bloritsch    2002/08/14 17:54:29

  Added:       assembly/src/java/org/apache/excalibur/merlin/resource
                        ThreadLocalLifestyleHandler.java
  Log:
  Add the "PerThread" or THreadLocal version of component lifestyle
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/assembly/src/java/org/apache/excalibur/merlin/resource/ThreadLocalLifestyleHandler.java
  
  Index: ThreadLocalLifestyleHandler.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.
   *
   * Original contribution by OSM SARL, http://www.osm.net
   */
  package org.apache.excalibur.merlin.resource;
  
  import org.apache.avalon.framework.activity.Disposable;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.context.DefaultContext;
  import org.apache.avalon.framework.context.Context;
  import org.apache.excalibur.merlin.assembly.ContainerManager;
  import org.apache.excalibur.merlin.model.Profile;
  import org.apache.excalibur.merlin.model.Resource;
  import org.apache.excalibur.meta.info.PhaseDescriptor;
  import org.apache.excalibur.meta.info.ExtensionDescriptor;
  
  
  /**
   * Lifestyle implementation that provides suppport for the per thread
   * policy.  Request for a single instance will allways return the
   * same instance for each request.
   *
   * NOTE:  Need to register a listener for threaddeath so I can properly
   *        dispose of the component when the associated thread dies.
   *
   * NOTE 2: This is untested.
   *
   * @author <a href="mailto:mcconnell@osm.net">Stephen McConnell</a>
   */
  public class ThreadLocalLifestyleHandler extends AbstractLifestyleHandler
  {
       private Profile m_profile;
       private ContainerManager m_manager;
  
       private ThreadLocalComponent m_instance;
  
      /**
       * Creation of a new singleton lifecycle manager.
       * @param manager the type manager
       * @param provider the resource provider
       * @param helper the lifecycle helper
       * @param profile the profile defining the object type
       * @param context the establishment context
       */
       public ThreadLocalLifestyleHandler(
                             final ContainerManager manager,
                             final DeploymentHelper deployment,
                             final LifecycleHelper helper,
                             final Profile profile,
                             final Context context )
       {
           super( manager, deployment, helper, profile, context );
           m_profile = profile;
           m_manager = manager;
           m_instance = new ThreadLocalComponent( this );
       }
  
      /**
       * Returns an instance of the object type supported by the
       * manager to the client.
       *
       * @return an instance of the type defined by the profile
       */
       public Object get() throws Exception
       {
           if( m_disposed )
             throw new IllegalStateException("disposed");
  
           getLogger().debug("get");
  
           Object object = m_instance.get();
           super.processAccessStage( object );
  
           return object;
  
       }
  
      /**
       * Invoked by a client to return an instance of the object type previously
       * supported by the manager.  The singleton manager does nothing as the
       * object may be used across an arbitary number of clients.
       *
       * @param object the object to return
       */
       public void put( Object object ) throws Exception
       {
           if( m_disposed )
             throw new IllegalStateException("disposed");
  
           getLogger().debug("put");
           if( object == null )
             return;
  
           if( object.equals( m_instance.get() ) )
           {
               super.processReleaseStage( object );
           }
           else
           {
               final String warning =
                 "Illegal attempt to release an object that was not provided by this handler.";
               getLogger().warn( warning );
           }
       }
  
      /**
       * Request for disposal of the manager.  This operation is normally invoked
       * by the client that initialy instantiated the manager.  The implementation
       * handles the disposal of the singleton resoruce and release of associated
       * references.
       */
       public void dispose()
       {
           handleShutdown( m_instance.get() );
  
           m_instance = null;
           m_profile = null;
           m_manager = null;
  
           super.dispose();
  
  
       }
  
      private static final class ThreadLocalComponent extends ThreadLocal
      {
          private final ThreadLocalLifestyleHandler m_lifestyle;
  
          protected ThreadLocalComponent( ThreadLocalLifestyleHandler handler )
          {
              m_lifestyle = handler;
          }
  
          protected Object initialValue()
          {
              try
              {
                  return m_lifestyle.createInstance();
              }
              catch( Exception e )
              {
                  return null;
              }
          }
      }
  }
  
  
  

--
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