donaldp     2002/11/10 03:42:40

  Modified:    fortress/src/java/org/apache/excalibur/fortress/container
                        AbstractContainer.java
  Log:
  Aquire services by serviceManager rather than via context.
  
  Rework so that handlers also aquire services in a similar way
  
  Revision  Changes    Path
  1.12      +100 -75   
jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/container/AbstractContainer.java
  
  Index: AbstractContainer.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-excalibur/fortress/src/java/org/apache/excalibur/fortress/container/AbstractContainer.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- AbstractContainer.java    10 Nov 2002 02:39:50 -0000      1.11
  +++ AbstractContainer.java    10 Nov 2002 11:42:40 -0000      1.12
  @@ -49,7 +49,6 @@
   */
   package org.apache.excalibur.fortress.container;
   
  -import java.lang.reflect.Constructor;
   import java.util.ArrayList;
   import java.util.Iterator;
   import java.util.List;
  @@ -64,11 +63,13 @@
   import org.apache.avalon.framework.context.ContextException;
   import org.apache.avalon.framework.context.Contextualizable;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
  +import org.apache.avalon.framework.service.DefaultServiceManager;
   import org.apache.avalon.framework.service.ServiceException;
   import org.apache.avalon.framework.service.ServiceManager;
   import org.apache.avalon.framework.service.Serviceable;
   import org.apache.commons.collections.BoundedFifoBuffer;
   import org.apache.commons.collections.StaticBucketMap;
  +import org.apache.excalibur.container.lifecycle.LifecycleExtensionManager;
   import org.apache.excalibur.event.Queue;
   import org.apache.excalibur.fortress.Container;
   import org.apache.excalibur.fortress.ContainerConstants;
  @@ -85,7 +86,6 @@
   import org.apache.excalibur.instrument.Instrumentable;
   import org.apache.excalibur.mpool.ObjectFactory;
   import org.apache.excalibur.mpool.PoolManager;
  -import org.apache.excalibur.container.lifecycle.LifecycleExtensionManager;
   
   /**
    * The Container is an interface used to mark the Containers in your system.
  @@ -103,7 +103,7 @@
   {
       protected Context m_context;
       protected ServiceManager m_serviceManager;
  -    protected LoggerManager m_logManager;
  +    protected LoggerManager m_loggerManager;
       protected PoolManager m_poolManager;
       protected Queue m_commandQueue;
       protected ClassLoader m_classLoader;
  @@ -118,70 +118,105 @@
        * Pull the manager items from the context so we can use them to set up
        * the system.
        */
  -    public void contextualize( Context containerContext )
  +    public void contextualize( final Context context )
           throws ContextException
       {
  -        m_context = containerContext;
  -
  -        m_logManager = (LoggerManager)m_context.get( 
ContainerConstants.LOGGER_MANAGER );
  -        m_poolManager = (PoolManager)m_context.get( 
ContainerConstants.POOL_MANAGER );
  -
  +        m_context = context;
           try
           {
  -            m_extManager = (LifecycleExtensionManager)m_context.get( 
ContainerConstants.EXTENSION_MANAGER );
  +            m_classLoader = (ClassLoader)context.get( 
ContainerConstants.CONTEXT_CLASSLOADER );
           }
           catch( ContextException ce )
           {
  -            m_extManager = new LifecycleExtensionManager();
  -
  -            if( getLogger().isDebugEnabled() )
  -            {
  -                getLogger().debug( "No Container.EXTENSION_MANAGER is given, 
installing default "
  -                                   + "lifecycle extension manager with 0 
extensions" );
  -            }
  +            m_classLoader = Thread.currentThread().getContextClassLoader();
           }
  +    }
   
  -        m_extManager.enableLogging( getLogger() );
  +    /**
  +     * Root ServiceManager.  The Container may choose to have it's
  +     * ServiceManager delegate to the root manager, or it may choose to be
  +     * entirely self contained.
  +     *
  +     * @avalon.service interface="LoggerManager"
  +     * @avalon.service interface="PoolManager"
  +     * @avalon.service interface="InstrumentManager"
  +     * @avalon.service interface="LifecycleExtensionManager" optional="true"
  +     * @avalon.service interface="RoleManager" optional="true"
  +     * @avalon.service interface="Queue" optional="true"
  +     */
  +    public void service( final ServiceManager serviceManager )
  +        throws ServiceException
  +    {
  +        m_loggerManager = (LoggerManager)serviceManager.lookup( 
LoggerManager.ROLE );
  +        m_poolManager = (PoolManager)serviceManager.lookup( PoolManager.ROLE 
);
  +        m_instrumentManager = (InstrumentManager)serviceManager.lookup( 
InstrumentManager.ROLE );
   
  -        try
  +        if( serviceManager.hasService( LifecycleExtensionManager.ROLE ) )
           {
  -            m_classLoader = (ClassLoader)m_context.get( 
ContainerConstants.CONTEXT_CLASSLOADER );
  +            m_extManager =
  +                (LifecycleExtensionManager)serviceManager.lookup( 
LifecycleExtensionManager.ROLE );
           }
  -        catch( ContextException ce )
  +        else
           {
  -            m_classLoader = Thread.currentThread().getContextClassLoader();
  +            m_extManager = new LifecycleExtensionManager();
  +            m_extManager.enableLogging( getLogger() );
  +
  +            if( getLogger().isDebugEnabled() )
  +            {
  +                final String message =
  +                    "No Container.LIFECYCLE_EXTENSION_MANAGER is given, " +
  +                    "installing default lifecycle extension manager with " +
  +                    "0 extensions";
  +                getLogger().debug( message );
  +            }
           }
   
  -        try
  +        if( serviceManager.hasService( Queue.ROLE ) )
           {
  -            m_commandQueue = (Queue)m_context.get( 
ContainerConstants.COMMAND_QUEUE );
  +            m_commandQueue = (Queue)serviceManager.lookup( Queue.ROLE );
           }
  -        catch( ContextException ce )
  +        else
           {
  -            m_commandQueue = null;
  -            getLogger().warn( "No Container.COMMAND_QUEUE is given, all 
management will be "
  -                              + "performed synchronously" );
  +            final String message =
  +                "No Container.COMMAND_QUEUE is given, all " +
  +                "management will be performed synchronously";
  +            getLogger().warn( message );
           }
   
  -        try
  +        if( serviceManager.hasService( RoleManager.ROLE ) )
           {
  -            m_roleManager = (RoleManager)m_context.get( 
ContainerConstants.ROLE_MANAGER );
  +            m_roleManager = (RoleManager)serviceManager.lookup( 
RoleManager.ROLE );
           }
  -        catch( ContextException ce )
  +        else
           {
               try
               {
  -                m_roleManager = new ExcaliburRoleManager( null, 
m_classLoader );
  -                ContainerUtil.enableLogging( m_roleManager, 
getLogger().getChildLogger( "roles" ) );
  -                ContainerUtil.initialize( m_roleManager );
  +                m_roleManager = createDefaultRoleManager();
               }
  -            catch( Exception e )
  +            catch( final Exception e )
               {
  -                throw new ContextException( "Unable to create default role 
manager", e );
  +                final String message = "Unable to create default role 
manager";
  +                throw new ServiceException( message, e );
               }
           }
   
  -        m_instrumentManager = (InstrumentManager)m_context.get( 
ContainerConstants.INSTRUMENT_MANAGER );
  +        m_serviceManager = new FortressServiceManager( this, serviceManager 
);
  +    }
  +
  +    /**
  +     * Create a default RoleManager that can be used to setup system.
  +     *
  +     * @return the default role manager
  +     * @throws Exception if unable to create role manager
  +     */
  +    private ExcaliburRoleManager createDefaultRoleManager()
  +        throws Exception
  +    {
  +        final ExcaliburRoleManager roleManager =
  +            new ExcaliburRoleManager( null, m_classLoader );
  +        ContainerUtil.enableLogging( roleManager, 
getLogger().getChildLogger( "roles" ) );
  +        ContainerUtil.initialize( roleManager );
  +        return roleManager;
       }
   
       /**
  @@ -257,17 +292,21 @@
       {
           String policy = component.getAttribute( "activation", null );
   
  -        boolean policyUnspecified = policy == null;
  -        boolean isLazy = "request".equalsIgnoreCase( policy );
  -        boolean isNonLazy = "startup".equalsIgnoreCase( policy );
  +        final boolean policyUnspecified = policy == null;
  +        final boolean isLazy = "request".equalsIgnoreCase( policy );
  +        final boolean isNonLazy = "startup".equalsIgnoreCase( policy );
   
           if( policyUnspecified || isNonLazy )
  +        {
               return false;
  +        }
           else if( isLazy )
  +        {
               return true;
  +        }
   
           // policy was not null, but didn't match anything above
  -        String klass = component.getAttribute( "class", null );
  +        final String klass = component.getAttribute( "class", null );
   
           final String message =
               "Unknown activation policy for class " + klass + ": " + policy;
  @@ -279,28 +318,28 @@
        * <code>HANDLER_CONSTRUCTOR</code>  for the component class passed in.
        */
       private ComponentHandler getComponentHandler( final String 
handlerClassName,
  -                                                  final String className,
  +                                                  final String classname,
                                                     final Configuration 
configuration,
                                                     final boolean isLazy )
       {
  -        Constructor constructor;
           ComponentHandler handler = null;
  -
           try
           {
  -            Class klass = m_classLoader.loadClass( className );
  -            Class handlerKlass = m_classLoader.loadClass( handlerClassName );
  -            constructor = handlerKlass.getConstructor( 
ComponentHandler.HANDLER_CONSTRUCTOR );
  -
  +            final Class handlerClazz = m_classLoader.loadClass( 
handlerClassName );
               final ObjectFactory factory =
  -                createObjectFactory( klass, configuration );
  -
  -            final Object[] args = new Object[] { factory };
  +                createObjectFactory( classname, configuration );
   
               final ComponentHandler targetHandler =
  -                (ComponentHandler)constructor.newInstance( args );
  +                (ComponentHandler)handlerClazz.newInstance();
   
               ContainerUtil.contextualize( targetHandler, m_context );
  +
  +            final DefaultServiceManager serviceManager =
  +                new DefaultServiceManager( getServiceManager() );
  +            serviceManager.put( ObjectFactory.ROLE, factory );
  +            serviceManager.makeReadOnly();
  +
  +            ContainerUtil.service( targetHandler, serviceManager );
               ContainerUtil.configure( targetHandler, configuration );
               ContainerUtil.initialize( targetHandler );
   
  @@ -319,14 +358,14 @@
               if( getLogger().isDebugEnabled() )
               {
                   getLogger().debug( "Could not create the '" + 
handlerClassName +
  -                                   "' handler for the '" + className +
  +                                   "' handler for the '" + classname +
                                      "' component.", e );
               }
           }
   
           if( getLogger().isDebugEnabled() )
           {
  -            getLogger().debug( "Component " + className +
  +            getLogger().debug( "Component " + classname +
                                  " uses handler " + handlerClassName );
           }
   
  @@ -340,24 +379,21 @@
       /**
        * Create an objectFactory for specified Object configuration.
        *
  -     * @param clazz the class of object
  +     * @param classname the classname of object
        * @param configuration the objests configuration
        * @return the ObjectFactory
        * @throws Exception if unable to create object factory
        */
  -    protected ObjectFactory createObjectFactory( final Class clazz,
  +    protected ObjectFactory createObjectFactory( final String classname,
                                                    final Configuration 
configuration )
           throws Exception
       {
  -        final LoggerManager loggerManager =
  -            (LoggerManager)m_context.get( ContainerConstants.LOGGER_MANAGER 
);
  -        final InstrumentManager instrumentManager =
  -            (InstrumentManager)m_context.get( 
ContainerConstants.INSTRUMENT_MANAGER );
  +        final Class clazz = m_classLoader.loadClass( classname );
           final ComponentFactory componentFactory =
               new ComponentFactory( clazz, configuration,
  -                                  getServiceManager(), m_context,
  -                                  loggerManager, m_extManager,
  -                                  instrumentManager );
  +                                  m_serviceManager, m_context,
  +                                  m_loggerManager, m_extManager,
  +                                  m_instrumentManager );
           return new ProxyObjectFactory( componentFactory );
       }
   
  @@ -449,17 +485,6 @@
           }
   
           return hasComponent;
  -    }
  -
  -    /**
  -     * Root ServiceManager.  The Container may choose to have it's
  -     * ServiceManager delegate to the root manager, or it may choose to be
  -     * entirely self contained.
  -     */
  -    public void service( final ServiceManager parent )
  -        throws ServiceException
  -    {
  -        m_serviceManager = new FortressServiceManager( this, parent );
       }
   
       /**
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to