avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcconn...@apache.org
Subject cvs commit: avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/appliance/impl DefaultAppliance.java DefaultApplianceFactory.java
Date Sun, 22 Jun 2003 00:30:55 GMT
mcconnell    2003/06/21 17:30:55

  Modified:    merlin/assembly/src/java/org/apache/avalon/assembly/appliance/impl
                        DefaultAppliance.java DefaultApplianceFactory.java
  Log:
  Refactored the appliance implementation such that the lifestyle creation operations are
not intergral with the implementation (i.e. a lifestyle service is not longer provided to
the appliance).
  
  Revision  Changes    Path
  1.15      +155 -36   avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/appliance/impl/DefaultAppliance.java
  
  Index: DefaultAppliance.java
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/appliance/impl/DefaultAppliance.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- DefaultAppliance.java	20 Jun 2003 19:02:17 -0000	1.14
  +++ DefaultAppliance.java	22 Jun 2003 00:30:55 -0000	1.15
  @@ -56,30 +56,47 @@
   import java.util.Hashtable;
   import java.util.Map;
   
  -import org.apache.avalon.assembly.appliance.*;
  +import org.apache.avalon.assembly.appliance.Appliance;
  +import org.apache.avalon.assembly.appliance.ApplianceContext;
  +import org.apache.avalon.assembly.appliance.ApplianceException;
  +import org.apache.avalon.assembly.appliance.ApplianceRepository;
  +import org.apache.avalon.assembly.appliance.ApplianceRuntimeException;
  +import org.apache.avalon.assembly.appliance.DependencyGraph;
  +import org.apache.avalon.assembly.appliance.UnknownServiceException;
   import org.apache.avalon.assembly.engine.RepositoryManager;
   import org.apache.avalon.assembly.engine.impl.EngineClassLoader;
   import org.apache.avalon.assembly.engine.service.ServiceRepository;
  +import org.apache.avalon.assembly.lifecycle.DeploymentService;
  +import org.apache.avalon.assembly.lifecycle.impl.DefaultDeploymentService;
   import org.apache.avalon.assembly.lifestyle.LifestyleHandler;
  -import org.apache.avalon.assembly.lifestyle.LifestyleService;
  +import org.apache.avalon.assembly.lifestyle.impl.SingletonLifestyleHandler;
  +import org.apache.avalon.assembly.lifestyle.impl.ThreadLocalLifestyleHandler;
  +import org.apache.avalon.assembly.lifestyle.impl.PooledLifestyleHandler;
  +import org.apache.avalon.assembly.lifestyle.impl.TransientLifestyleHandler;
   import org.apache.avalon.assembly.locator.Contextualizable;
   import org.apache.avalon.assembly.locator.Locator;
   import org.apache.avalon.assembly.locator.LocatorException;
  +import org.apache.avalon.assembly.locator.Resolvable;
  +import org.apache.avalon.assembly.locator.impl.DefaultLocator;
  +import org.apache.avalon.assembly.logging.LoggingManager;
   import org.apache.avalon.framework.activity.Initializable;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.context.Context;
   import org.apache.avalon.framework.context.ContextException;
   import org.apache.avalon.framework.context.DefaultContext;
  +import org.apache.avalon.framework.logger.LogEnabled;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.apache.avalon.framework.parameters.Parameters;
   import org.apache.avalon.meta.info.ContextDescriptor;
   import org.apache.avalon.meta.info.DependencyDescriptor;
  +import org.apache.avalon.meta.info.InfoDescriptor;
   import org.apache.avalon.meta.info.ReferenceDescriptor;
   import org.apache.avalon.meta.info.ServiceDescriptor;
   import org.apache.avalon.meta.info.StageDescriptor;
   import org.apache.avalon.meta.info.Type;
   import org.apache.avalon.meta.model.ContextDirective;
   import org.apache.avalon.meta.model.LoggingDirective;
  +import org.apache.excalibur.mpool.PoolManager;
   
   
   /**
  @@ -133,11 +150,11 @@
    *    appliance.release( object, this );
    *
    * </pre>
  - * @see DefaultApplianceContext
    * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
    * @version $Revision$ $Date$
    */
  -public class DefaultAppliance extends AbstractLogEnabled implements Appliance, Contextualizable,
Initializable
  +public class DefaultAppliance extends AbstractLogEnabled 
  +  implements Appliance, Contextualizable, Initializable
   {
       //=====================================================================
       // static
  @@ -198,12 +215,13 @@
       private boolean m_enabled = true;
   
       /**
  -     * The context provider.
  +     * The appliance instance assigned as the contextualization handler.
        */
       private Appliance m_contextProvider;
   
       /**
  -     * The dependencies providers keyed by role name.
  +     * A map of appliance instances associated and keyed by the role
  +     * the the appliance plays relative to this appliance.
        */
       private final Hashtable m_providers = new Hashtable();
   
  @@ -213,6 +231,11 @@
       private final Hashtable m_managers = new Hashtable();
   
       /**
  +     * The appliance name.
  +     */
  +    private String m_name;
  +
  +    /**
        * The base path for the appliance.
        */
       private String m_path;
  @@ -223,11 +246,6 @@
       private URL m_url;
   
       /**
  -     * The appliance name.
  -     */
  -    private String m_name;
  -
  -    /**
        * The assembled state of the appliance.
        */
       private boolean m_assembled;
  @@ -237,8 +255,6 @@
        */
       private Class m_class;
   
  -    private LifestyleService m_lifestyle;
  -
       private DependencyGraph m_graph;
   
       private ArrayList m_visited = new ArrayList();
  @@ -279,9 +295,11 @@
        */
       public void contextualize( Locator context ) throws ContextException
       {
  -       
  +        if( getLogger().isDebugEnabled() )
  +        {
  +            getLogger().debug( "contextualization" );
  +        }
           m_applianceContext = (ApplianceContext) context.get( ApplianceContext.KEY );
  -        m_lifestyle = (LifestyleService) context.get( LifestyleService.KEY );
           m_engine = (EngineClassLoader) context.get( APPLIANCE_ENGINE_KEY );
           m_system = (Locator) context.get( APPLIANCE_SYSTEM_KEY );
           m_repository = (ApplianceRepository) context.get( APPLIANCE_REPOSITORY_KEY );
  @@ -299,7 +317,17 @@
               throw new IllegalStateException( "contextualization" );
           }
   
  +        if( getLogger().isDebugEnabled() )
  +        {
  +            getLogger().debug( "initialization" );
  +        }
  +
           String classname = getType().getInfo().getClassname();
  +        if( getLogger().isDebugEnabled() )
  +        {
  +            getLogger().debug( "classname: " + classname );
  +        }
  +
           try
           {
               m_class = m_engine.loadClass( classname );
  @@ -312,7 +340,6 @@
               throw new ApplianceException( error, e );
           }
   
  -
           m_name = m_applianceContext.getName();
           m_path = m_applianceContext.getPartitionName() + m_name;
           m_initialized = true;
  @@ -321,24 +348,59 @@
           // finally, create the lifestyle handler
           //
   
  -        try
  +        if( getLogger().isDebugEnabled() )
           {
  -            m_handler = m_lifestyle.createHandler( this, m_engine, m_system );
  +            getLogger().debug( "lifestyle: " + getType().getInfo().getLifestyle() );
  +        }
  +
  +        DefaultLocator context = new DefaultLocator();
  +        context.put( Appliance.KEY, new ApplianceHolder( this ) );
  +        context.put( "urn:assembly:lifecycle.deployment", 
  +          createDeploymentService() );
  +        context.put( "urn:avalon:classloader", m_engine );
  +
  +        LifestyleHandler lifestyle = null;
  +        String policy = getType().getInfo().getLifestyle();
  +        if( policy.equals( InfoDescriptor.SINGLETON ) )
  +        {
  +            lifestyle = new SingletonLifestyleHandler();
           } 
  +        else if( policy.equals( InfoDescriptor.TRANSIENT ) )
  +        {
  +            lifestyle = new TransientLifestyleHandler();
  +        }
  +        else if( policy.equals( InfoDescriptor.THREAD ) )
  +        {
  +            lifestyle = new ThreadLocalLifestyleHandler();
  +        } 
  +        else if( policy.equals( InfoDescriptor.POOLED ) )
  +        {
  +            lifestyle = new PooledLifestyleHandler();
  +            context.put( 
  +              PoolManager.ROLE, 
  +              m_system.get( PoolManager.ROLE ) );
  +        }
  +
  +        context.makeReadOnly();
  +        try
  +        {
  +            ((LogEnabled)lifestyle).enableLogging( getLogger() );
  +            ((Contextualizable)lifestyle).contextualize( context );
  +            ((Initializable)lifestyle).initialize();
  +            m_handler = lifestyle;
  +        }
           catch( Throwable e )
           {
               final String error =
  -              "Unexpected lifestyle handler establishment failure in appliance: " 
  -              + this;
  +              "Unexpected lifestyle handler establishment failure.";
               throw new ApplianceException( error, e );
           }
   
           if( getLogger().isDebugEnabled() )
           {
  -            final String message = "created appliance: " + this + ", for: " + classname;
  -            getLogger().debug( message );
  +            getLogger().debug( "identity: " + System.identityHashCode( this ) );
  +            getLogger().debug( "initialization complete" );
           }
  -
       }
   
       //=====================================================================
  @@ -402,9 +464,8 @@
           } 
           catch( Throwable e )
           {
  -            final String error =
  -                    "Internal lifestyle handler error in appliance: "
  -                    + this;
  +            final String error = 
  +              "Resolution failure in appliance: " + this;
               throw new LocatorException( ref, error, e );
           }
       }
  @@ -424,7 +485,7 @@
           catch( Throwable e )
           {
               final String error =
  -                    "Lifestyle handler raised a release error in appliance: " + this;
  +              "Lifestyle handler raised a release error in appliance: " + this;
               throw new ApplianceRuntimeException( error, e );
           }
       }
  @@ -697,16 +758,15 @@
           {
               Appliance[] providers = m_graph.getProviderGraph( this );
               StringBuffer buffer =
  -                    new StringBuffer(
  -                            "assembled: " + this.toString()
  -                    + " providers: (" + providers.length + ") " );
  +              new StringBuffer( " providers: (" + providers.length + ") " );
   
               for( int k = 0; k < providers.length; k++ )
               {
                   if( k > 0 )
                   {
                       buffer.append( ", " + providers[k] );
  -                } else
  +                } 
  +                else
                   {
                       buffer.append( providers[k].toString() );
                   }
  @@ -727,7 +787,7 @@
   
           if( getLogger().isDebugEnabled() )
           {
  -            getLogger().debug( "disassemble: " + this );
  +            getLogger().debug( "disassembly" );
           }
   
           Appliance[] consumers = getDependencyGraph().getConsumerGraph( this );
  @@ -968,11 +1028,17 @@
               return;
           }
   
  +        if( getLogger().isDebugEnabled() )
  +        {
  +            getLogger().debug( "deployment" );
  +        }
  +
           if( !m_assembled )
           {
               final String message =
                 "invoking self assembly in appliance: " + this;
               getLogger().debug( message );
  +
               try
               {
                   assemble();
  @@ -1002,7 +1068,7 @@
   
           if( getLogger().isDebugEnabled() )
           {
  -            getLogger().debug( "decommissioning: " + this );
  +            getLogger().debug( "decommissioning" );
           }
   
           try
  @@ -1031,7 +1097,7 @@
   
           if( getLogger().isDebugEnabled() )
           {
  -            getLogger().debug( "termination: " + this );
  +            getLogger().debug( "termination" );
           }
           m_handler.terminate();
           m_initialized = false;
  @@ -1039,7 +1105,7 @@
   
       private void executeAssembly( DependencyGraph graph ) throws ApplianceException
       {
  -        getLogger().debug( "assembly: " + this );
  +        getLogger().debug( "assembly" );
           m_visited.add( this );
   
           //
  @@ -1199,4 +1265,57 @@
           final ReferenceDescriptor reference = new ReferenceDescriptor( ref );
           return null != m_applianceContext.getProfile().getService( reference );
       }
  +
  +   /**
  +    * Utility to create a deployment service.
  +    * @param logging the logging manager
  +    * @param logger the logging channel to assign to the deployment service
  +    * @return the deployment service
  +    */
  +    private DeploymentService createDeploymentService()
  +      throws ApplianceException
  +    {
  +        try
  +        {
  +            DefaultDeploymentService deployment = new DefaultDeploymentService();
  +            deployment.enableLogging( getLogger() );
  +            DefaultLocator locator = new DefaultLocator();
  +            locator.put( LoggingManager.KEY, m_system.get( LoggingManager.KEY ) );
  +            deployment.contextualize( locator );
  +            deployment.initialize();
  +            return deployment;
  +        }
  +        catch( Throwable e )
  +        {
  +            final String error =
  +              "Internal error while attempting to create a deployment service.";
  +            throw new ApplianceException( error, e );
  +        }
  +    }
  +
  +   /**
  +    * Internal class the wraps an appliance.  Used when passing an appliance as
  +    * an argument in a locator so that the invocation of resolve returns the 
  +    * appliance and not the conponent managed by the applaince.
  +    */
  +    private class ApplianceHolder implements Resolvable
  +    {
  +        private Appliance m_appliance;
  +
  +        public ApplianceHolder( Appliance appliance )
  +        {
  +            m_appliance = appliance;
  +        }
  +
  +        public Object resolve( Object source )
  +        {
  +            return resolve( source, null );
  +        }
  +
  +        public Object resolve( Object source, String ref )
  +        {
  +            return m_appliance;
  +        }
  +    }
   }
  +
  
  
  
  1.9       +6 -79     avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/appliance/impl/DefaultApplianceFactory.java
  
  Index: DefaultApplianceFactory.java
  ===================================================================
  RCS file: /home/cvs/avalon-sandbox/merlin/assembly/src/java/org/apache/avalon/assembly/appliance/impl/DefaultApplianceFactory.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- DefaultApplianceFactory.java	18 Jun 2003 11:16:13 -0000	1.8
  +++ DefaultApplianceFactory.java	22 Jun 2003 00:30:55 -0000	1.9
  @@ -63,8 +63,6 @@
   import org.apache.avalon.assembly.engine.impl.EngineClassLoader;
   import org.apache.avalon.assembly.lifecycle.impl.DefaultDeploymentService;
   import org.apache.avalon.assembly.lifecycle.DeploymentService;
  -import org.apache.avalon.assembly.lifestyle.LifestyleService;
  -import org.apache.avalon.assembly.lifestyle.impl.DefaultLifestyleService;
   import org.apache.avalon.assembly.locator.Contextualizable;
   import org.apache.avalon.assembly.locator.Locator;
   import org.apache.avalon.assembly.locator.impl.DefaultLocator;
  @@ -240,34 +238,6 @@
           return factory;
       }
   
  -   /**
  -    * Static utility to create a deployment service.
  -    * @param logging the logging manager
  -    * @param logger the logging channel to assign to the deployment service
  -    * @return the deployment service
  -    */
  -    private static DeploymentService createDeploymentService( 
  -      LoggingManager logging, Logger logger )
  -      throws ApplianceException
  -    {
  -        try
  -        {
  -            DefaultDeploymentService deployment = new DefaultDeploymentService();
  -            deployment.enableLogging( logger );
  -            DefaultLocator locator = new DefaultLocator();
  -            locator.put( LoggingManager.KEY, logging );
  -            deployment.contextualize( locator );
  -            deployment.initialize();
  -            return deployment;
  -        }
  -        catch( Throwable e )
  -        {
  -            final String error =
  -              "Internal error while attempting to create bootstrap deployment service.";
  -            throw new ApplianceException( error, e );
  -        }
  -    }
  -
       //---------------------------------------------------------------------------
       // state
       //---------------------------------------------------------------------------
  @@ -280,8 +250,6 @@
   
       private LoggingManager m_logging;
   
  -    private DeploymentService m_deployment;
  -
       //---------------------------------------------------------------------------
       // contextualization
       //---------------------------------------------------------------------------
  @@ -304,8 +272,6 @@
           {
               throw new IllegalStateException( "context" );
           }
  -        final Logger logger = getLogger().getChildLogger( "deployment" ); 
  -        m_deployment = createDeploymentService( m_logging, logger );
       }
   
       //---------------------------------------------------------------------------
  @@ -337,19 +303,18 @@
               throw new IllegalStateException( error );
           }
   
  +        final String path = context.getPartitionName() + context.getName();
  +        Logger logger = m_logging.getSystemLoggerForCategory( path );
  +
           //
           // create the lifestyle service
           //
   
  -        LifestyleService lifestyle = 
  -          createLifestyleService( engine, m_pool, m_deployment );
  -
           DefaultLocator locator = null;
           try
           {
               locator = new DefaultLocator( context );
               locator.put( PoolManager.ROLE, m_pool );
  -            locator.put( LifestyleService.KEY, lifestyle );
               locator.put( ApplianceRepository.KEY, m_repository );
               locator.put( ApplianceContext.KEY, context );
               locator.put( "urn:assembly:engine", engine );
  @@ -367,15 +332,14 @@
           if( (classname == null)
              || (classname.equals( DefaultAppliance.class.getName() )) )
           {
  -            getLogger().debug( "creating a classic appliance" );
  +            getLogger().debug( "creating a classic appliance: " + path );
   
               //
               // bootstrap the appliance using DefaultAppliance
               //
   
               DefaultAppliance appliance = new DefaultAppliance();
  -            final String path = context.getPartitionName() + context.getName();
  -            appliance.enableLogging( m_logging.getSystemLoggerForCategory( path ) );
  +            appliance.enableLogging( logger );
   
               try
               {
  @@ -413,7 +377,6 @@
                     new DefaultApplianceContext( profile, map );
                   cntx.setName( context.getName() + "$bootstrap" );
                   cntx.setPartitionName( context.getPartitionName() );
  -                cntx.put( LifestyleService.KEY, lifestyle );
                   cntx.put( ApplianceContext.KEY, context );
                   cntx.put( ApplianceRepository.KEY, m_repository );
                   cntx.put( PoolManager.ROLE, m_pool );
  @@ -423,7 +386,6 @@
   
                   Appliance appliance = engine.createAppliance( cntx, false );
                   appliance.assemble( new DependencyGraph() );
  -                appliance.deploy();
                   Object object = appliance.resolve( this );
                   if( object instanceof Appliance )
                   {
  @@ -447,39 +409,4 @@
               }
           }
       }
  -
  -    /**
  -     * Utility to create the default lifestyle service.
  -     * @param engine the engine classloader
  -     * @param pool the pool manager
  -     * @param deployment the deployment service
  -     * @return the lifestyle service
  -     */
  -    private LifestyleService createLifestyleService(
  -      Engine engine, 
  -      PoolManager pool, 
  -      DeploymentService deployment ) 
  -      throws ApplianceException
  -    {
  -        try
  -        {
  -            DefaultLifestyleService lifestyle = new DefaultLifestyleService();
  -            lifestyle.enableLogging( getLogger().getChildLogger( "lifestyle" ) );
  -            DefaultLocator context = new DefaultLocator();
  -            context.put( DeploymentService.KEY, deployment );
  -            context.put( "urn:assembly:pool-manager", pool );
  -            context.put( "urn:assembly:engine.classloader", engine );
  -            context.makeReadOnly();
  -            lifestyle.contextualize( context );
  -            lifestyle.initialize();
  -            return lifestyle;
  -        }
  -        catch( Throwable e )
  -        {
  -            final String error =
  -                    "Unexpected error during bootstrap lifecycle service creation.";
  -            throw new ApplianceException( error, e );
  -        }
  -    }
  -
   }
  
  
  

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


Mime
View raw message