avalon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anton Tagunov <atagu...@mail.cnt.ru>
Subject [Patch] #11.3
Date Wed, 28 May 2003 10:36:33 GMT
Uff.. More nuts -- more taste :)

1. ===============

I just couldn't resist this refactoring of
DefaultContainerManager/ContextManager.

I beleive that this patch as compared to #11-bis-2
improves code simplicity. The difference is the following:

  loggerManager
  queue
  metaInfoManager
  poolManager
  instrumentManager

etc. -- all that AbstractContainer fetches from
its ServiceManager -- are now stuffed into the
serviceManager directly by ContextManager.

This has required the introduction of

  m_loggerManager
  m_queue
  m_metaInfoManager
  m_poolManager
  m_instrumentManager

variables into ContextManager, but this is all the
overhead we get.

In return DefaultContainerManager
has become two methods shorter and we do not need
to fetch loggerManager and queue back from
m_containerManagerContext when we need them again in
ContextManager/initialize*.

As an additional bonus this makes the ServiceManager
hierarchy one level shorter in the common usage scenario.

2. ===============

On my way I have found that a change would be desirable
to event//CommandManager, we were calling
    Sink getCommandSink()
on it and after that coerced the result to Queue (as
really a queue was returned)

This patch adds a getCommandQueue() method to CommandManager.

3. ==============

Naturally this patch includes all that is included into
#11
#11-bis-2
so the comments in the respective comments apply.

Sorry about scattering them among severl mails :-/

WBR, Anton


diff -ru avalon-excalibur.0/event/src/java/org/apache/excalibur/event/command/CommandManager.java
avalon-excalibur/event/src/java/org/apache/excalibur/event/command/CommandManager.java
--- avalon-excalibur.0/event/src/java/org/apache/excalibur/event/command/CommandManager.java
   2003-03-22 15:46:28.000000000 +0300
+++ avalon-excalibur/event/src/java/org/apache/excalibur/event/command/CommandManager.java
     2003-05-28 14:09:50.000000000 +0400
@@ -130,6 +130,16 @@
     }
 
     /**
+     * Get the Command Queue so that you can enqueue and dequeue commands.
+     *
+     * @return the Sink that feeds the CommandManager
+     */
+    public final Queue getCommandQueue()
+    {
+        return m_queue;
+    }
+
+    /**
      * Register a Signal with an EventHandler.  The Signal is a special object
      * that implements the {@link Signal} interface.  When CommandManager recieves
      * events that match the Signal, it will send a copy of it to all the
diff -ru avalon-excalibur.0/fortress/src/java/org/apache/avalon/fortress/impl/DefaultContainerManager.java
avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/impl/DefaultContainerManager.java
--- avalon-excalibur.0/fortress/src/java/org/apache/avalon/fortress/impl/DefaultContainerManager.java
  2003-05-28 10:20:10.000000000 +0400
+++ avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/impl/DefaultContainerManager.java
    2003-05-28 14:00:12.000000000 +0400
@@ -205,7 +205,8 @@
             ContainerUtil.enableLogging( instance, m_logger );
             ContainerUtil.contextualize( instance, implContext );
 
-            final ServiceManager serviceManager = createServiceManager( implContext );
+            final ServiceManager serviceManager = 
+                (ServiceManager) getContextEntry( managerContext, SERVICE_MANAGER );
 
             ContainerUtil.service( instance, serviceManager );
 
@@ -230,38 +231,6 @@
         }
     }
 
-    private ServiceManager createServiceManager( final Context managerContext )
-    {
-        final ServiceManager smanager =
-            (ServiceManager) getContextEntry( managerContext, SERVICE_MANAGER );
-        final DefaultServiceManager serviceManager = new DefaultServiceManager( smanager
);
-
-        addService( Queue.ROLE, managerContext, serviceManager );
-        addService( LoggerManager.ROLE, managerContext, serviceManager );
-        addService( PoolManager.ROLE, managerContext, serviceManager );
-        addService( InstrumentManager.ROLE, managerContext, serviceManager );
-        addService( MetaInfoManager.ROLE, managerContext, serviceManager );
-        addService( RoleManager.ROLE, managerContext, serviceManager );
-        addService( LifecycleExtensionManager.ROLE, managerContext, serviceManager );
-        serviceManager.makeReadOnly();
-
-        return serviceManager;
-    }
-
-    private void addService( final String role,
-                             final Context context,
-                             final DefaultServiceManager serviceManager )
-    {
-        try
-        {
-            final Object object = context.get( role );
-            serviceManager.put( role, object );
-        }
-        catch ( ContextException e )
-        {
-        }
-    }
-
     /**
      * Retrieve an entry from context if it exists, else return null.
      *
diff -ru avalon-excalibur.0/fortress/src/java/org/apache/avalon/fortress/util/ContextManager.java
avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/util/ContextManager.java
--- avalon-excalibur.0/fortress/src/java/org/apache/avalon/fortress/util/ContextManager.java
   2003-05-28 10:20:26.000000000 +0400
+++ avalon-excalibur/fortress/src/java/org/apache/avalon/fortress/util/ContextManager.java
     2003-05-28 14:34:54.000000000 +0400
@@ -74,7 +74,6 @@
 import org.apache.avalon.framework.service.DefaultServiceSelector;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.excalibur.event.Queue;
-import org.apache.excalibur.event.Sink;
 import org.apache.excalibur.event.command.CommandManager;
 import org.apache.excalibur.event.command.TPCThreadManager;
 import org.apache.excalibur.event.command.ThreadManager;
@@ -103,20 +102,16 @@
  * get an instance of the Context, it is made read-only and returned.  No
  * further operations will be possible on it.</p>
  *
- * <p>You can get two different contexts from the ContextManager: the child
- * context and the impl manager context. The former contains all
- * managers, such as the pool manager etc. necessary for a child impl to
- * create additional child containers. The impl manager context contains
- * all of the child context, but also initialization parameters for the
- * impl, such as a Configuration object, a ComponentLocator, etc., that
- * the impl wants, but does not want to pass on to its children.</p>
+ * <p>You can get two different contexts from the ContextManager: 
+ * the container context (m_childContext) 
+ * and the container manager context (m_contaimerManagerContext).
  *
- * <p>You would typically use the impl manager context to initialize
- * the impl manager, and let it pass the child context on to the
- * impl.</p>
+ * <p>The container manager context is used to provide the container manager
+ * with all the data needed to initialize the container.</p>
+ * <p>The container context is passed directly to the container.</p>
  *
- * <p>The ContextManager will sometimes create new components, such as a
- * component manager, a pool manager, etc. It will manage these components
+ * <p>The ContextManager will sometimes create new components, such as
+ * a service manager, a pool manager, etc. It will manage these components
  * and dispose of them properly when it itself is disposed .</p>
  *
  * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
@@ -139,21 +134,21 @@
     /**
      * The root context.
      */
-    private final Context m_rootContext;
+    protected final Context m_rootContext;
 
     /**
      * The context of the new impl. This context has the rootContext
      * as its parent. Put everything here that you want the new impl
      * to have in its own context.
      */
-    private final DefaultContext m_childContext;
+    protected final DefaultContext m_childContext;
 
     /**
      * Container manager's context. This context has the child context
      * as parent. Put things here that you want the impl manager
      * to see, but do not wish to expose to the impl.
      */
-    private final DefaultContext m_containerManagerContext;
+    protected final DefaultContext m_containerManagerContext;
 
     /**
      * New context passed in, maybe from a FortressConfig.
@@ -161,9 +156,45 @@
      * other pointers. The ContextManager inspects it for
      * elements it can use to create component managers etc.
      */
-    private Logger m_logger;
-    private final Logger m_primordialLogger;
-    private ServiceManager m_manager;
+    protected Logger m_logger;
+    protected final Logger m_primordialLogger;
+
+    /**
+     * Source resolver used to read-in the configurations.
+     * and provided as a default source resolver if the
+     * user has not supplied a ServiceManager.
+     */
+    protected SourceResolver m_defaultSourceResolver;
+
+    /**
+     * The logger manager in use. 
+     * Either supplied via rootContext, or created locally.
+     */ 
+    protected LoggerManager m_loggerManager;
+
+    /**
+     * The Queue in use. 
+     * Either supplied via rootContext or created locally.
+     */
+    protected Queue m_queue;
+
+    /**
+     * The MetaInfoManager to be used by the container.
+     * Either supplied via rootContext or created locally.
+     */
+    protected MetaInfoManager m_metaInfoManager;
+
+    /**
+     * The PoolManager to be used by the container.
+     * Either supplied via rootContext or created locally.
+     */
+    protected PoolManager m_poolManager;
+
+    /**
+     * The InstrumentManager to be used by the container.
+     * Either supplied via rootContext or created locally.
+     */
+    protected InstrumentManager m_instrumentManager;
 
     /**
      * The components that are "owned" by this context and should
@@ -219,7 +250,7 @@
      *
      * @throws IllegalArgumentException if the object is null.
      */
-    private void assumeOwnership( final Object object )
+    protected void assumeOwnership( final Object object )
     {
         if ( object == null )
         {
@@ -237,7 +268,7 @@
      */
     public void initialize() throws Exception
     {
-        initializeServiceManager();
+        initializeDefaultSourceResolver();
         initializeLoggerManager();
         initializeMetaInfoManager();
         initializeCommandQueue();
@@ -245,9 +276,12 @@
         initializeContext();
         initializeInstrumentManager();
         initializeConfiguration();
+        initializeServiceManager();
 
         m_childContext.makeReadOnly();
         m_containerManagerContext.makeReadOnly();
+
+        m_defaultSourceResolver = null;
     }
 
     /**
@@ -295,6 +329,22 @@
             getLogger().debug( "Could not copy Context parameters.  This may be Ok depending
on "
                 + "other configured context values." );
         }
+       
+        // hide from the container implementation what it does not need
+        m_childContext.put( CONFIGURATION, null );
+        m_childContext.put( CONFIGURATION_URI, null );
+        m_childContext.put( RoleManager.ROLE, null );
+        m_childContext.put( ROLE_MANAGER_CONFIGURATION, null );
+        m_childContext.put( ROLE_MANAGER_CONFIGURATION_URI, null );
+        m_childContext.put( LoggerManager.ROLE, null );
+        m_childContext.put( LOGGER_MANAGER_CONFIGURATION, null );
+        m_childContext.put( LOGGER_MANAGER_CONFIGURATION_URI, null );
+        m_childContext.put( InstrumentManager.ROLE, null );
+        m_childContext.put( INSTRUMENT_MANAGER_CONFIGURATION, null );
+        m_childContext.put( INSTRUMENT_MANAGER_CONFIGURATION_URI, null );
+        m_childContext.put( Queue.ROLE, null );
+        m_childContext.put( MetaInfoManager.ROLE, null );
+        m_childContext.put( PoolManager.ROLE, null );
     }
 
     /**
@@ -392,15 +442,13 @@
     {
         try
         {
-            m_rootContext.get( Queue.ROLE );
-            return;
+            m_queue = (Queue) m_rootContext.get( Queue.ROLE );
         }
         catch ( ContextException ce )
         {
+            // No CommandQueue specified, create a default one
+            m_queue = createCommandQueue();
         }
-
-        // No CommandQueue specified, create a default one
-        m_childContext.put( Queue.ROLE, createCommandSink() );
     }
 
     /**
@@ -409,7 +457,7 @@
      * @return a default <code>CommandQueue</code>
      * @throws Exception if an error occurs
      */
-    private Sink createCommandSink() throws Exception
+    private Queue createCommandQueue() throws Exception
     {
         final CommandManager cm = new CommandManager();
         final ThreadManager tm = new TPCThreadManager();
@@ -417,11 +465,8 @@
         assumeOwnership( cm );
         assumeOwnership( tm );
 
-        // Get the context Logger Manager
-        final LoggerManager loggerManager = (LoggerManager) m_childContext.get( LoggerManager.ROLE
);
-
         // Get the logger for the thread manager
-        final Logger tmLogger = loggerManager.getLoggerForCategory( "system.threadmgr" );
+        final Logger tmLogger = m_loggerManager.getLoggerForCategory( "system.threadmgr"
);
 
         ContainerUtil.enableLogging( tm, tmLogger );
         ContainerUtil.parameterize( tm, buildCommandQueueConfig() );
@@ -429,7 +474,7 @@
 
         tm.register( cm );
 
-        return cm.getCommandSink();
+        return cm.getCommandQueue();
     }
 
     /**
@@ -487,16 +532,14 @@
     {
         try
         {
-            m_rootContext.get( PoolManager.ROLE );
-            return;
+            m_poolManager = (PoolManager) m_rootContext.get( PoolManager.ROLE );
         }
         catch ( ContextException ce )
         {
+            final PoolManager pm = new DefaultPoolManager( m_queue );
+            assumeOwnership( pm );
+            m_poolManager = pm;
         }
-
-        final PoolManager pm = new DefaultPoolManager( (Sink) m_childContext.get( Queue.ROLE
) );
-        assumeOwnership( pm );
-        m_childContext.put( PoolManager.ROLE, pm );
     }
 
     /**
@@ -534,14 +577,11 @@
             return null;
         }
 
-        // Get the context Logger Manager
-        final LoggerManager loggerManager = (LoggerManager) m_childContext.get( LoggerManager.ROLE
);
-
         // Lookup the context class loader
         final ClassLoader classLoader = (ClassLoader) m_rootContext.get( ClassLoader.class.getName()
);
 
         // Create a logger for the role manager
-        final Logger rmLogger = loggerManager.getLoggerForCategory(
+        final Logger rmLogger = m_loggerManager.getLoggerForCategory(
             roleConfig.getAttribute( "logger", "system.roles" ) );
 
         // Create a parent role manager with all the default roles
@@ -560,7 +600,17 @@
 
     protected void initializeMetaInfoManager() throws Exception
     {
-        final boolean mmSupplied = entryPresent( m_rootContext, MetaInfoManager.ROLE );
+        boolean mmSupplied = false;
+        try
+        {
+            m_metaInfoManager = (MetaInfoManager) m_rootContext.get( MetaInfoManager.ROLE
);
+            mmSupplied = true;
+        }
+        catch( ContextException ce )
+        {
+            // okay, we will create one
+        }
+
         RoleManager roleManager = obtainRoleManager();
         final boolean rmSupplied = roleManager != null;
 
@@ -575,13 +625,12 @@
         }
         else
         {
-            final LoggerManager loggerManager = (LoggerManager)m_childContext.get( LoggerManager.ROLE
);
             final ClassLoader classLoader = (ClassLoader)m_rootContext.get( ClassLoader.class.getName()
);
 
             if ( ! rmSupplied )
             {
                 final FortressRoleManager newRoleManager = new FortressRoleManager( null,
classLoader );
-                newRoleManager.enableLogging(loggerManager.getLoggerForCategory("system.roles"));
+                newRoleManager.enableLogging(m_loggerManager.getLoggerForCategory("system.roles"));
                 newRoleManager.initialize();
 
                 roleManager = newRoleManager;
@@ -589,24 +638,19 @@
 
             final ServiceMetaManager metaManager = new ServiceMetaManager( new Role2MetaInfoManager(
roleManager ), classLoader );
 
-            metaManager.enableLogging( loggerManager.getLoggerForCategory("system.meta")
);
+            metaManager.enableLogging( m_loggerManager.getLoggerForCategory("system.meta")
);
             metaManager.initialize();
             assumeOwnership( metaManager );
-            m_childContext.put( MetaInfoManager.ROLE, metaManager );
+            m_metaInfoManager = metaManager;
         }
     }
 
     /**
-     * Get a reference to the initial ComponentLocator used by the
-     * ContainerManager to hold the Components used for parsing the config
-     * files and setting up the environment.
-     *
-     * @throws Exception when there is an error.
+     *  Initialize the default SrouceResolver.
      */
-    protected void initializeServiceManager() throws Exception
+    protected void initializeDefaultSourceResolver() throws Exception
     {
-        final ServiceManager parent = (ServiceManager) get( m_rootContext, SERVICE_MANAGER,
null );
-        final DefaultServiceManager manager = new DefaultServiceManager( parent );
+        final DefaultServiceManager manager = new DefaultServiceManager();
         final DefaultServiceSelector selector = new DefaultServiceSelector();
         final URLSourceFactory file = new URLSourceFactory();
         file.enableLogging( getLogger() );
@@ -623,15 +667,41 @@
         ContainerUtil.service( resolver, manager );
         ContainerUtil.parameterize( resolver, new Parameters() );
 
-        manager.put( SourceResolver.ROLE, resolver );
-
-        manager.makeReadOnly();
-
-        assumeOwnership( manager );
+        m_defaultSourceResolver = resolver;
+    }
+    /**
+     * Get a reference to the initial ComponentLocator used by the
+     * ContainerManager to hold the Components used for parsing the config
+     * files and setting up the environment.
+     *
+     * @throws Exception when there is an error.
+     */
+    protected void initializeServiceManager() throws Exception
+    {
+        final ServiceManager parent = (ServiceManager) get( m_rootContext, SERVICE_MANAGER,
null );
+        final DefaultServiceManager manager = new DefaultServiceManager( parent );
 
-        m_manager = manager;
+        /**
+         * We assume that if there is a parent ServiceManager provided,
+         * there is a SourceResolver mounted there. And if there
+         * is none, then it is the true caller's intetion.
+         * However it is hard to imagine how that could be usefull
+         * except for testing purposes.
+         */
+        if ( parent == null )
+        {
+            manager.put( SourceResolver.ROLE, m_defaultSourceResolver );
+        }
+        
+        manager.put( LoggerManager.ROLE, m_loggerManager );
+        manager.put( Queue.ROLE, m_queue );
+        manager.put( MetaInfoManager.ROLE, m_metaInfoManager );
+        manager.put( PoolManager.ROLE, m_poolManager );
+        manager.put( InstrumentManager.ROLE, m_instrumentManager );
 
-        m_childContext.put( ContextManagerConstants.SERVICE_MANAGER, m_manager );
+        manager.makeReadOnly();
+    
+        m_containerManagerContext.put( SERVICE_MANAGER, manager );
     }
 
     /**
@@ -668,8 +738,7 @@
         Source src = null;
         try
         {
-            resolver = (SourceResolver) m_manager.lookup( SourceResolver.ROLE );
-            src = resolver.resolveURI( configUri );
+            src = m_defaultSourceResolver.resolveURI( configUri );
             if ( configBuilder == null )
             {
                 configBuilder = new DefaultConfigurationBuilder();
@@ -685,12 +754,7 @@
         }
         finally
         {
-            if ( null != resolver )
-            {
-                resolver.release( src );
-            }
-
-            m_manager.release( resolver );
+            m_defaultSourceResolver.release( src );
         }
     }
 
@@ -747,7 +811,7 @@
         {
             // Try copying an already existing logger manager from the override context.
 
-            m_childContext.put( LoggerManager.ROLE, m_rootContext.get( LoggerManager.ROLE
) );
+            m_loggerManager = (LoggerManager) m_rootContext.get( LoggerManager.ROLE );
         }
         catch ( ContextException ce )
         {
@@ -788,7 +852,7 @@
 
             assumeOwnership( logManager );
 
-            m_childContext.put( LoggerManager.ROLE, logManager );
+            m_loggerManager = logManager;
         }
 
         // Since we now have a LoggerManager, we can update the this.logger field
@@ -798,8 +862,7 @@
         {
             getLogger().debug( "Switching to default Logger provided by LoggerManager." );
 
-            final LoggerManager loggerManager = (LoggerManager) m_childContext.get( LoggerManager.ROLE
);
-            m_logger = loggerManager.getDefaultLogger();
+            m_logger = m_loggerManager.getDefaultLogger();
         }
     }
 
@@ -819,7 +882,7 @@
         {
             // Try copying an already existing instrument manager from the override context.
 
-            m_rootContext.get( InstrumentManager.ROLE );
+            m_instrumentManager = (InstrumentManager) m_rootContext.get( InstrumentManager.ROLE
);
         }
         catch ( ContextException ce )
         {
@@ -832,11 +895,8 @@
                 instrumentConfig = EMPTY_CONFIG;
             }
 
-            // Get the context Logger Manager
-            final LoggerManager loggerManager = (LoggerManager) m_childContext.get( LoggerManager.ROLE
);
-
             // Get the logger for the instrument manager
-            final Logger imLogger = loggerManager.getLoggerForCategory(
+            final Logger imLogger = m_loggerManager.getLoggerForCategory(
                 instrumentConfig.getAttribute( "logger", "system.instrument" ) );
 
             // Set up the Instrument Manager
@@ -847,7 +907,7 @@
 
             assumeOwnership( instrumentManager );
 
-            m_childContext.put( InstrumentManager.ROLE, instrumentManager );
+            m_instrumentManager = instrumentManager;
         }
     }


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


Mime
View raw message