avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcconn...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/service AbstractManager.java DefaultComponentManager.java PooledProvider.java DefaultServiceManager.java ServiceFactory.java ServiceLoader.java UnitInfo.java package.html
Date Sun, 03 Mar 2002 23:07:49 GMT
mcconnell    02/03/03 15:07:49

  Modified:    src/scratchpad/org/apache/avalon/excalibur/service
                        DefaultServiceManager.java ServiceFactory.java
                        ServiceLoader.java UnitInfo.java package.html
  Added:       src/scratchpad/org/apache/avalon/excalibur/service
                        AbstractManager.java DefaultComponentManager.java
                        PooledProvider.java
  Log:
  addition of support for mpool Pool interface and backward compatabily
  for Composable targets
  
  Revision  Changes    Path
  1.2       +25 -88    jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/service/DefaultServiceManager.java
  
  Index: DefaultServiceManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/service/DefaultServiceManager.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DefaultServiceManager.java	3 Mar 2002 15:51:12 -0000	1.1
  +++ DefaultServiceManager.java	3 Mar 2002 23:07:49 -0000	1.2
  @@ -14,110 +14,47 @@
   
   
   /**
  - * Internal helper class the implements the service manager interface and 
  - * is supplied to dynamically crerated componets during service lifecyle
  - * pipeline processing.
  + * Internal helper class the implements the <code>ServiceManager</code> interface
and 
  + * is supplied to dynamically created componets during lifecyle pipeline processing.
    */
  -class DefaultServiceManager implements ServiceManager
  +class DefaultServiceManager extends AbstractManager implements ServiceManager
   {
  -
  -   /**
  -    * Hashtable containing service providers keyed by role name.  
  -    * The manager use the providers in this table to aquire services
  -    * in response to <code>lookup</code> invocations.  Provider 
  -    * types fall into one of the following three catagories:
  -    *
  -    * <table>
  -    * <tr><td><b>Policy</b></td><td><b>Description</b></td><tr>
  -    * <tr><td>SINGLETON_LIFETIME_POLICY</td><td>
  -    * Service of the type singleton are distinguished by the fact 
  -    * that they do not inherit from Pool or Transient.  The singleton
  -    * provider object is a reference to the singleton service and is 
  -    * return directly by the implemetation on invocation of lookup.
  -    * </td>
  -    * <tr><td>POOLED_LIFETIME_POLICY</td><td>
  -    * Pooled services implement the Pool interface.  The service
  -    * resolves lookup aquires the pooled service by invoking 
  -    * <code>checkout</code> on the pool implementation. Clients 
  -    * using pooled services are required to release services using
  -    * the manager <code>release</code> method.  The implemetation will
  -    * attempt to locate the issuing pool and release the object on 
  -    * behalf of the client. 
  -    * </td>
  -    * <tr><td>TRANSIENT_LIFETIME_POLICY</td><td>
  -    * A transient provider is factory from which new instances are 
  -    * created and pipelined following a invocation of <code>lookup</code>.
  -    * The invocing client is totally responsible for service disposal.
  -    * </td>
  -    */
  -    private Hashtable m_providers = new Hashtable();
  -
  +    /**
  +     * Construct ServiceManager.
  +     */
       public DefaultServiceManager( Hashtable providers ) throws Exception
       {
  -        m_providers = providers;
  +        super( providers );
       }
   
  +   /**
  +    * Returns true if a provider exists for the supplied role.
  +    * @param role the service identifier
  +    * @return boolean TRUE if the service is available else FALSE
  +    */
       public boolean hasService( String role )
       {
  -        return (m_providers.get( role ) != null );
  +        return super.has( role );
       }
   
  +    /**
  +     * Retrieve Object by role from ServiceManager.
  +     * @param role the role
  +     * @return the Object
  +     * @exception ServiceException if an error occurs
  +     */
       public Object lookup( String role ) throws ServiceException
       {
  -        Object provider = m_providers.get( role );
  -        if( provider == null ) throw new ServiceException(
  -            "Could not locate a provider for the role: " + role );
  -
  -        if( provider instanceof TransientProvider )
  -        {
  -            //
  -            // return a transient instance
  -            //
  -
  -            return ((TransientProvider)provider).create( );
  -        }
  -/*
  -        else if( provider instanceof PooledProvider )
  -        {
  -            //
  -            // return a pooled service
  -            //
  -
  -            return ((PooledProvider)provider).checkout( );
  -        }
  -*/
  -        else
  -        {
  -            //
  -            // return a singleton service
  -            //
  -
  -            return ((SingletonProvider)provider).provide( );
  -        }
  +        return super.resolve( role );
       }
   
  +   /**
  +    * Release a pooled object.
  +    * @param object a pooled object
  +    */
       public void release( Object object )
       {
  -        //
  -        // release a pooled service
  -        //
  -/*
  -        Class c = object.getClass();
  -        Enumeration providers = m_providers.elements();
  -        while( providers.hasMoreElements()) 
  -        {
  -            Object provider = providers.nextElement();
  -            if( provider instanceof PooledProvider )
  -            {
  -                PooledProvider pool = (PooledProvider) provider;
  -                if( pool.getBaseClass().isAssignableFrom( c ) )
  -                {
  -                    pool.release( object );
  -                    break;
  -                }
  -            }
  -        }
  -*/
  +        super.disgard( object );
       }
   }
   
  
  
  
  1.2       +15 -2     jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/service/ServiceFactory.java
  
  Index: ServiceFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/service/ServiceFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ServiceFactory.java	3 Mar 2002 15:51:12 -0000	1.1
  +++ ServiceFactory.java	3 Mar 2002 23:07:49 -0000	1.2
  @@ -46,6 +46,7 @@
   import org.apache.avalon.framework.activity.Disposable;
   
   import org.apache.avalon.excalibur.configuration.CascadingConfiguration;
  +import org.apache.avalon.excalibur.mpool.Pool;
   
   /**
    * 
  @@ -164,8 +165,20 @@
   
               case UnitInfo.POOLED_LIFETIME_POLICY :
   
  -              final String error = "Cannot provide a default provider for a pool at this
time.";
  -              throw new RuntimeException( error );
  +              provider = m_pools.get( provider_class );
  +              if( provider == null )
  +              {
  +                  if( m_verbose ) if( getLogger().isDebugEnabled() ) getLogger().debug(

  +                    "Creating pooled provider for :" + provider_class.getName());
  +
  +                  // create and pipeline the singleton instance and 
  +                  // add it to the list of singletons
  +
  +                  Object object = pipeline( block_info, info.getRole() );
  +                  provider = new PooledProvider( (Pool) object, info.getRole() );
  +                  m_pools.put( provider_class, provider );
  +              }
  +              return provider;
   
               default :
   
  
  
  
  1.2       +5 -5      jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/service/ServiceLoader.java
  
  Index: ServiceLoader.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/service/ServiceLoader.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ServiceLoader.java	3 Mar 2002 15:51:12 -0000	1.1
  +++ ServiceLoader.java	3 Mar 2002 23:07:49 -0000	1.2
  @@ -74,11 +74,11 @@
    * <pre>
    * &lt;config&gt;
    *
  - *   &lt;block class="com.magic.DirectoryBlock"&gt;
  + *   &lt;block class="org.apache.DirectoryBlock"&gt;
    *     &lt;-- block specific content --&gt;
    *   &lt;/block&gt;
    *
  - *   &lt;block class="com.magic.ActivatorBlock"&gt;
  + *   &lt;block class="org.apache.ActivatorBlock"&gt;
    *     &lt;-- block specific content --&gt;
    *   &lt;/block&gt;
    *
  @@ -208,8 +208,8 @@
       *   statement.</p>
       *   <p>An example command line is shown below:</p>
       *   <pre>
  -    * $ java -classpath <strong>merlin.jar;avalon-framework.jar;logkit.jar</strong>

  -    *     org.apache.avalon.excalibur.service.ServiceLoader
  +    * $ java -classpath merlin.jar;avalon-framework.jar;logkit.jar 
  +    *     <strong>org.apache.avalon.excalibur.service.ServiceLoader</strong>
       *      &lt;supporting-jar-files&gt; -target &lt;class-name&gt; 
       *   </pre>
       * </ul>
  @@ -239,7 +239,7 @@
       * <td>
       * <p>A value of <code>true</code> will force debug level logging
of the actual pipeline
       * processor.  A value of <code>false</code> will disable pipeline debug
priority logging.
  -    * Visibility of logging inffomration is dependent on the level supplied under the 
  +    * Visibility of logging infomration is dependent on the level supplied under the 
       * <code>priority</code parameter.</p>
       * </td></tr>
       * <tr><td width="20%" valign="top"><code>-priority &lt;priority&gt;</code></td>
  
  
  
  1.2       +12 -4     jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/service/UnitInfo.java
  
  Index: UnitInfo.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/service/UnitInfo.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- UnitInfo.java	3 Mar 2002 15:51:12 -0000	1.1
  +++ UnitInfo.java	3 Mar 2002 23:07:49 -0000	1.2
  @@ -10,6 +10,7 @@
   import java.util.Vector;
   import org.apache.avalon.framework.CascadingException;
   import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.excalibur.mpool.Pool;
   
   /**
    * Meta information about a <code>Serviceable</code> component.  
  @@ -100,18 +101,25 @@
               throw new CascadingException( "Could not construct dependency information.",
e );
           }
   
  -        String policy = xinfo.getChild("implementation").getAttribute("policy","TRANSIENT");
  +        String policy = xinfo.getChild("implementation").getAttribute("policy","OTHER");
           if( policy.equalsIgnoreCase( "SINGLETON" ) )
           {
               m_policy = SINGLETON_LIFETIME_POLICY;
           }
  -        else if( policy.equalsIgnoreCase( "POOLED" ) )
  +        else if( policy.equalsIgnoreCase( "TRANSIENT" ) )
           {
  -            m_policy = POOLED_LIFETIME_POLICY;
  +            m_policy = TRANSIENT_LIFETIME_POLICY;
           }
           else
           {
  -            m_policy = TRANSIENT_LIFETIME_POLICY;
  +            if( Pool.class.isAssignableFrom( block ) )
  +            {
  +                m_policy = POOLED_LIFETIME_POLICY;
  +            }
  +            else
  +            {
  +                m_policy = TRANSIENT_LIFETIME_POLICY;
  +            }
           }
       }
   
  
  
  
  1.3       +11 -7     jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/service/package.html
  
  Index: package.html
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/service/package.html,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- package.html	3 Mar 2002 16:45:15 -0000	1.2
  +++ package.html	3 Mar 2002 23:07:49 -0000	1.3
  @@ -73,7 +73,7 @@
   
   <p>For each component declared in a manifest, the implementation establishes the
available services
   and dependencies based on information contained in an <code>.xinfo</code> file.
 Given a component
  -implementation class <strong><code>org/apache/DirectoryBlock.class</code></strong>
the <code>Pipeline</code> processor will attempt to locate a <strong><code>org/apache/DirectoryBlock.xinfo</code></strong>

  +implementation class <strong><code>org/apache/ReferralBlock.class</code></strong>
the <code>ServiceLoader</code>  will attempt to locate a <strong><code>org/apache/ReferralBlock.xinfo</code></strong>

   resource in the jar file.</P>
   
   <pre>
  @@ -122,7 +122,7 @@
             establish and provide the dependencies via the component Serviceable 
             implementation.  The role name corresponds to the identifying string
             that the component implementation will use to lookup a service from
  -          a service manger during the serviceable lifecycle phase.
  +          a service manager during the serviceable lifecycle phase.
             --&gt;</i></font>
   
             &lt;dependency&gt;
  @@ -133,10 +133,14 @@
         &lt;/dependencies&gt;
   
         <font color="blue"><i>&lt;!--
  -      Component implementation policy may one of the following:
  +      Component implementation policy may be one of the following:
         (a) SINGLETON, service is available for the lifetime of the manager
  -      (b) POOLED, the implementation implements Pool (implementation pending)
  -      (c) TRANSIENT, manager is a factory of transient service instances
  +      (b) TRANSIENT, manager is a factory of transient service instances 
  +      (c) OTHER, (default) The container will check if the class implements 
  +      the org.apache.excalibur.mpool.Pool interface.  If true, <code>lookup</code>

  +      and <code>release</code> invocations will be redirected to the pools
<code>aquire</code> and 
  +      <code>release</code> methods - otherwise, the class will be registered
under the 
  +      TRANSIENT policy.
         --&gt;</i></font>
       
         &lt;implementation policy="SINGLETON" /&gt;
  @@ -149,8 +153,8 @@
   <p>The pipeline processor provides support for the automated retrieval of default
   configurations for a component.  During lifecycle processing, the pipeline processor
   will attempt to locate a configuration resource with the same path and name as 
  -the component implementation class.  For example, for the component <strong><code>org/apache/DirectoryBlock.class</code></strong>,
the implementation will look for 
  -a default configuration under the resource path <strong><code>org/apache/DirectoryBlock.conf</code></strong>.
  +the component implementation class.  For example, for the component <strong><code>org/apache/RefferalBlock.class</code></strong>,
the implementation will look for 
  +a default configuration under the resource path <strong><code>org/apache/RefferalBlock.conf</code></strong>.
   During the configuration stage, the pipeline processor will supply the component with 
   a <code>CascadingConfiguration</code> where the primary configuration is configuration

   derived from the pipeline processor configuration, and a default configuration corresponding

  
  
  
  1.1                  jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/service/AbstractManager.java
  
  Index: AbstractManager.java
  ===================================================================
  /*
   * File: DefaultServiceManager.java
   * License: etc/LICENSE.TXT
   * Copyright: Copyright (C) The Apache Software Foundation. All rights reserved.
   * Copyright: OSM SARL 2002, All Rights Reserved.
   */
  
  package org.apache.avalon.excalibur.service;
  
  import java.util.Enumeration;
  import java.util.Hashtable;
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.avalon.framework.service.ServiceException;
  
  
  /**
   * Internal helper class the handles the functional requirements of 
   * both ComponetManager and ServiceManager.
   */
  class AbstractManager 
  {
  
     /**
      * Hashtable containing service providers keyed by role name.  
      * The manager use the providers in this table to aquire services
      * in response to <code>lookup</code> invocations.  Provider 
      * types fall into one of the following three catagories:
      *
      * <table>
      * <tr><td><b>Policy</b></td><td><b>Description</b></td><tr>
      * <tr><td>SINGLETON_LIFETIME_POLICY</td><td>
      * Service of the type singleton are distinguished by the fact 
      * that they do not inherit from Pool or Transient.  The singleton
      * provider object is a reference to the singleton service and is 
      * return directly by the implemetation on invocation of lookup.
      * </td>
      * <tr><td>POOLED_LIFETIME_POLICY</td><td>
      * Pooled services implement the Pool interface.  The service
      * resolves lookup aquires the pooled service by invoking 
      * <code>checkout</code> on the pool implementation. Clients 
      * using pooled services are required to release services using
      * the manager <code>release</code> method.  The implementation will
      * attempt to locate the issuing pool and release the object on 
      * behalf of the client. 
      * </td>
      * <tr><td>TRANSIENT_LIFETIME_POLICY</td><td>
      * A transient provider is factory from which new instances are 
      * created and pipelined following a invocation of <code>lookup</code>.
      * The invocing client is totally responsible for service disposal.
      * </td>
      */
      private Hashtable m_providers = new Hashtable();
  
     /**
      * Internal table that maintains a mapping betyween pooled objects and
      * the issuing pool.  The object is used as the key to lookup the pool
      * when handling object release.
      */
      private Hashtable m_pooled_table = new Hashtable();
  
      public AbstractManager( Hashtable providers ) throws Exception
      {
          m_providers = providers;
      }
  
      public boolean has( String role )
      {
          return (m_providers.get( role ) != null );
      }
  
      public Object resolve( String role ) throws ServiceException
      {
          Object provider = m_providers.get( role );
          if( provider == null ) throw new ServiceException(
              "Could not locate a provider for the role: " + role );
  
          if( provider instanceof TransientProvider )
          {
              //
              // return a transient instance
              //
  
              return ((TransientProvider)provider).create( );
          }
          else if( provider instanceof PooledProvider )
          {
              //
              // return a pooled service after registering the usage
              //
  
              Object object = null;
              try
              {
                  object = ((PooledProvider)provider).acquire( );
              }
              catch( Throwable e )
              {
                  final String error = "Pool implementation error.";
                  throw new ServiceException( error, e );
              }
              finally
              {
                  // it is invalid for a pool to provide the same object without
                  // it being released beforehand
  
                  if( m_pooled_table.get( object ) != null ) 
                  {
                      final String error = 
                        "Manager has an existing reference to an aquired object from '" 
                        + role + "'.";
                      throw new ServiceException( error );
                  }
                  m_pooled_table.put( object, provider );
                  return object;
              }
          }
          else
          {
              //
              // return a singleton service
              //
  
              return ((SingletonProvider)provider).provide( );
          }
      }
  
     /**
      * Release a pooled object.
      * @param object a pooled object
      */
      public void disgard( Object object )
      {
          //
          // release a pooled service
          //
  
          PooledProvider provider = (PooledProvider) m_pooled_table.get( object );
          if( provider != null ) provider.release( object );
      }
  }
  
  
  
  
  1.1                  jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/service/DefaultComponentManager.java
  
  Index: DefaultComponentManager.java
  ===================================================================
  /*
   * File: DefaultServiceManager.java
   * License: etc/LICENSE.TXT
   * Copyright: Copyright (C) The Apache Software Foundation. All rights reserved.
   * Copyright: OSM SARL 2002, All Rights Reserved.
   */
  
  package org.apache.avalon.excalibur.service;
  
  import java.util.Enumeration;
  import java.util.Hashtable;
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.component.ComponentException;
  
  
  /**
   * Internal helper class the implements the <code>ComponentManager</code> interface
and 
   * is supplied to dynamically created componets during lifecycle pipeline processing.
   */
  class DefaultComponentManager extends AbstractManager implements ComponentManager
  {
  
      public DefaultComponentManager( Hashtable providers ) throws Exception
      {
          super( providers );
      }
  
      public boolean hasComponent( String role )
      {
          return super.has( role );
      }
  
      public Component lookup( String role ) throws ComponentException
      {
          Object object = null;
          try
          {
              object = super.resolve( role );
          }
          catch( Throwable e )
          {
              final String error = "Provider related error during service resolution.";
              throw new ComponentException( error, e );
          }
          finally
          {
              if( object instanceof Component ) return (Component) object;
              throw new ComponentException( "Service provider returned a non-Component." );
          }
      }
  
     /**
      * Release a pooled object.
      * @param object a pooled object
      */
      public void release( Component component )
      {
          super.disgard( component );
      }
  }
  
  
  
  
  1.1                  jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/service/PooledProvider.java
  
  Index: PooledProvider.java
  ===================================================================
  /*
   * File: SingletonProvider.java
   * License: etc/LICENSE.TXT
   * Copyright: Copyright (C) The Apache Software Foundation. All rights reserved.
   * Copyright: OSM SARL 2002, All Rights Reserved.
   */
  
  package org.apache.avalon.excalibur.service;
  
  import org.apache.avalon.framework.activity.Disposable;
  import org.apache.avalon.excalibur.mpool.Pool;
  
  /**
   * A <code>PooledProvider</code> is a provider of pooled services. Invocations
   * of <code>lookup</code> directed towards the manager will be redirected to

   * the pooled provider based on the supplied role name.  The pooled provider 
   * handles the invocation of <code>aquired</code> and <code>release</code>
against
   * the actual <code>Pool</code> instance.
   */
  class PooledProvider extends ServiceProvider
  {
  
      private Pool m_pool;
  
     /**
      * Creation of a new <code>SingletonProvider</code>.
      * @param object the singleton object to provide.
      */
      public PooledProvider( Pool pool, String role )
      {
          super( role );
          m_pool = pool;
      }
  
      /**
       * Acquire an instance of the pooled object.
       * @return the pooled Object instance
       */
      public Object acquire() throws Exception
      {
          return m_pool.acquire();
      }
  
      /**
       * Release the instance of the pooled object.
       * @param pooledObject  The pooled object to release to the pool.
       */
      void release( Object object )
      {
          m_pool.release( object );
      }
  
     /**
      * Disposal of the provider and release of related resources.
      */
      public void dispose()
      {
          if(( m_pool != null ) && ( m_pool instanceof Disposable )) try
          { 
              ((Disposable)m_pool).dispose();
          }
          catch( Throwable anything )
          {
              // ignore it
          }
          finally
          {
              m_pool = null;
              super.dispose();
          }
      }
  }
  
  
  
  

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