leosutic    02/03/06 23:03:34

  Modified:    src/scratchpad/org/apache/avalon/excalibur/system/util
                        ContextManager.java
  Added:       src/scratchpad/org/apache/avalon/excalibur/system/util
                        ContextManagerConstants.java ContextBuilder.java
  Log:
  First cut of modifications
  
  Revision  Changes    Path
  1.6       +470 -156  
jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/util/ContextManager.java
  
  Index: ContextManager.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/util/ContextManager.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ContextManager.java       4 Mar 2002 22:37:01 -0000       1.5
  +++ ContextManager.java       7 Mar 2002 07:03:34 -0000       1.6
  @@ -7,16 +7,27 @@
    */
   package org.apache.avalon.excalibur.system.util;
   
  -import org.apache.avalon.excalibur.system.*;
  +import org.apache.avalon.framework.activity.*;
  +import org.apache.avalon.framework.configuration.*;
   import org.apache.avalon.framework.context.*;
   import org.apache.avalon.framework.component.ComponentManager;
  +import org.apache.avalon.framework.component.DefaultComponentManager;
  +import org.apache.avalon.framework.component.DefaultComponentSelector;
   import org.apache.avalon.framework.parameters.*;
  -import org.apache.avalon.framework.configuration.*;
  -import org.apache.avalon.excalibur.logger.LoggerManager;
  +import org.apache.avalon.framework.logger.ConsoleLogger;
  +import org.apache.avalon.framework.logger.Logger;
  +import org.apache.avalon.excalibur.command.CommandManager;
   import org.apache.avalon.excalibur.event.Queue;
  +import org.apache.avalon.excalibur.logger.LoggerManager;
  +import org.apache.avalon.excalibur.logger.LogKitLoggerManager;
  +import org.apache.avalon.excalibur.mpool.DefaultPoolManager;
   import org.apache.avalon.excalibur.mpool.PoolManager;
  +import org.apache.avalon.excalibur.source.*;
  +import org.apache.avalon.excalibur.system.*;
   
   import java.io.File;
  +import java.util.ArrayList;
  +import java.util.Iterator;
   
   
   /**
  @@ -27,193 +38,496 @@
    * of the Context, it is made read-only and returned.  No further operations
    * will be possible on it.
    *
  + * <p>You can get two different contexts from the ContextManager: the child
  + * context and the container manager context. The former contains all 
managers,
  + * such as the pool manager etc. necessary for a child container to create
  + * additional child containers. The container manager context contains all
  + * of the child context, but also initialization parameters for the
  + * container, such as a Configuration object, a ComponentManager, etc., that
  + * the container wants, but does not want to pass on to its children.
  + *
  + * <p>You would typically use the container manager context to initialize
  + * the container manager, and let it pass the child context on to the 
container.
  + *
  + * <p>The ContextManager will sometimes create new components, such as a 
component
  + * manager, a pool manager, etc. It will manage these components and dispose
  + * of them properly when it itself is disposed .
  + *
    * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
  - * @version CVS $Revision: 1.5 $ $Date: 2002/03/04 22:37:01 $
  + * @author <a href="mailto:[EMAIL PROTECTED]">Leo Sutic</a>
  + * @version CVS $Revision: 1.6 $ $Date: 2002/03/07 07:03:34 $
    * @since 4.1
    */
  -public class ContextManager
  +public class ContextManager implements ContextManagerConstants
   {
  -    private final Context        m_rootContext;
  -    private       DefaultContext m_currContext;
  -
  -    public ContextManager()
  -    {
  -        this( defaultParameters() );
  +   
  +    private static final Configuration EMPTY_CONFIG;
  +    
  +    static
  +    {
  +        DefaultConfiguration config = new DefaultConfiguration("", "", "", 
"");
  +        config.makeReadOnly();
  +        EMPTY_CONFIG = config;
  +    }    
  +    
  +    /**
  +     * The root context.
  +     */
  +    private final Context          rootContext;
  +    
  +    /**
  +     * The context of the new container. This context has the rootContext
  +     * as its parent. Put everything here that you want the new container
  +     * to have in its own context.
  +     */
  +    private final DefaultContext   childContext;
  +    
  +    /**
  +     * Container manager's context. This context has the child context
  +     * as parent. Put things here that you want the container manager 
  +     * to see, but do not wish to expose to the container.
  +     */
  +    private final DefaultContext   containerManagerContext;
  +    
  +    /**
  +     * New context passed in, maybe from a ContextBuilder.
  +     * This context may not have objects, but rather URI's or
  +     * other pointers. The ContextManager inspects it for
  +     * elements it can use to create component managers etc.
  +     */
  +    private final Context          overrideContext;
  +    private       Logger           logger;
  +    private final Logger           primordialLogger;
  +    private       ComponentManager manager;
  +    
  +    /**
  +     * The components that are "owned" by this context and should
  +     * be disposed by it. Any manager that is created as a result
  +     * of it not being in the rootContext or overrideContext,
  +     * or having been created by the ContextManager should go in here.
  +     */
  +    private final ArrayList      ownedComponents = new ArrayList ();
  +    
  +    private final DefaultConfigurationBuilder    configBuilder =
  +        new DefaultConfigurationBuilder();
  +    private final DefaultConfigurationSerializer configSerializer =
  +        new DefaultConfigurationSerializer();
  +    
  +    /**
  +     * Create a new ContextManager.
  +     *
  +     * @param rootContext the default values.
  +     * @param overrides   values to be overridden in the root context. This 
parameter is typically
  +     *                    created with a ContextBuilder.
  +     * @param logger      logger to use when creating new components.
  +     */
  +    public ContextManager( Context rootContext, Context overrides, Logger 
logger )
  +    {
  +        this.rootContext = rootContext;
  +        this.childContext = new DefaultContext( rootContext );
  +        this.containerManagerContext = new DefaultContext( childContext );
  +        this.overrideContext = overrides;
  +        this.logger = logger;
  +        
  +        this.primordialLogger = new ConsoleLogger();
       }
  -
  -    public ContextManager( Parameters params )
  +    
  +    public void assumeOwnership( Object o )
       {
  -        this( params, Thread.currentThread().getContextClassLoader() );
  +        if (o == null)
  +        {
  +            throw new IllegalArgumentException( "Can not assume ownership of 
a null!" );
  +        }
  +        ownedComponents.add(o);
       }
  -
  -    public ContextManager( Parameters params, ClassLoader contextClassLoader 
)
  +    
  +    public void initialize() throws Exception
       {
  -        this( defaultContext( params, contextClassLoader ) );
  +        initializeOwnComponentManager();
  +        initializeLoggerManager();
  +        initializeRoleManager();
  +        initializeComponentManager();
  +        initializeCommandQueue();
  +        initializePoolManager();
  +        initializeContext();
  +        initializeConfiguration();
  +                
  +        childContext.makeReadOnly ();
  +        containerManagerContext.makeReadOnly ();
       }
  -
  -    public ContextManager( Context context )
  +    
  +    protected void initializeConfiguration() throws Exception
       {
  -        m_rootContext = context;
  -        m_currContext = new DefaultContext( m_rootContext );
  +        try 
  +        {
  +            containerManagerContext.put( CONFIGURATION, overrideContext.get( 
CONFIGURATION ) );
  +        } 
  +        catch (ContextException ce)
  +        {
  +            Configuration containerConfig = getConfiguration( CONFIGURATION, 
CONFIGURATION_URI );
  +            if (containerConfig == null)
  +            {
  +                // No config.
  +                // Does the parent supply a logger manager?
  +                try 
  +                {
  +                    containerManagerContext.get(CONFIGURATION);
  +                    
  +                    // OK, done.
  +                    return;
  +                }
  +                catch (ContextException cex)
  +                {
  +                    // Guess there is none.
  +                    return;
  +                }
  +            }
  +            else
  +            {
  +                containerManagerContext.put( CONFIGURATION, containerConfig 
);
  +            }
  +        }
       }
   
  -    public void makeReadOnly()
  +    protected void initializeContext() throws Exception
       {
  -        m_currContext.makeReadOnly();
  +        copyEntry( CONTAINER_CLASS );
  +        copyEntry( PARAMETERS );
       }
  -
  -    public Object get( Object key )
  +    
  +    protected void copyEntry( String key ) throws Exception
       {
  -        try
  +        try 
           {
  -            return m_currContext.get( key );
  +            containerManagerContext.put( key, overrideContext.get( key ) );
           }
  -        catch (Exception e)
  +        catch (ContextException ce)
           {
  -            return null;
           }
       }
  -
  -    public Context getContextInstance()
  -        throws ContextException
  -    {
  -        m_currContext.get( ContainerManager.CONTAINER_CLASS );
  -
  -        try
  -        {
  -            m_currContext.get( ContainerManager.LOGKIT_CONFIG );
  -        }
  -        catch (Exception e)
  +    
  +    /**
  +     * Disposes all items that this ContextManager has created.
  +     */
  +    public void dispose()
  +    {
  +        // Dispose owned components
  +        Iterator ownedComponentsIter = ownedComponents.iterator();
  +        while ( ownedComponentsIter.hasNext() )
           {
  -            m_currContext.get( Container.LOGGER_MANAGER );
  +            Object o = ownedComponentsIter.next();
  +            
  +            try 
  +            {
  +                if (o instanceof Startable)
  +                {
  +                    ((Startable) o).stop();
  +                }
  +                
  +                if (o instanceof Disposable)
  +                {
  +                    ((Disposable) o).dispose();
  +                }                
  +            } 
  +            catch (Exception e) 
  +            {
  +                getLogger().warn( "Unable to dispose of owned component " + 
o.getClass().getName(), e);
  +            }
           }
  -        return m_currContext;
  -    }
  -
  -    public void setContainerClass( String className )
  -    {
  -        m_currContext.put( ContainerManager.CONTAINER_CLASS, className );
  -    }
  -
  -    public void setContextDirectory( File file )
  -    {
  -        m_currContext.put( Container.CONTEXT_DIRECTORY, file );
  -    }
  -
  -    public void setWorkDirectory( File file )
  -    {
  -        m_currContext.put( Container.WORK_DIRECTORY, file );
  -    }
  -
  -    public void setContextClassLoader( ClassLoader loader )
  -    {
  -        m_currContext.put( Container.CONTEXT_CLASSLOADER, loader );
  -    }
  -
  -    public void setLogKitLoggerManagerConfiguration( String location )
  -    {
  -        m_currContext.put( ContainerManager.LOGKIT_CONFIG, location );
  -    }
  -
  -    public void setLogKitLoggerManagerConfiguration( Configuration config )
  -    {
  -        m_currContext.put( ContainerManager.ROLE_CONFIG, config );
  -    }
  -
  -    public void setRoleManagerConfiguration( String location )
  -    {
  -        m_currContext.put( ContainerManager.ROLE_CONFIG, location );
  -    }
  -
  -    public void setRoleManagerConfiguration( Configuration config )
  -    {
  -        m_currContext.put( ContainerManager.ROLE_CONFIG, config );
  -    }
  -
  -    public void setContainerConfiguration( String location )
  -    {
  -        m_currContext.put( ContainerManager.CONTAINER_CONFIG, location );
  -    }
  -
  -    public void setContainerConfiguration( Configuration config )
  -    {
  -        m_currContext.put( ContainerManager.CONTAINER_CONFIG, config );
  -    }
  -
  -    public void setLoggerCategory( String category )
  -    {
  -        m_currContext.put( ContainerManager.LOG_CATEGORY, category );
  -    }
  -
  -    public void setNumberOfThreadsPerCPU( int numberOfThreads )
  -    {
  -        m_currContext.put( ContainerManager.THREADS_CPU, new Integer( 
numberOfThreads ) );
  +        ownedComponents.clear ();        
       }
   
  -    public void setThreadTimeout( long timeout )
  +    protected Object get( Context context, String key, Object defaultValue )
       {
  -        m_currContext.put( ContainerManager.THREAD_TIMEOUT, new Long( 
timeout ) );
  -    }
  -
  -    public void setLoggerManager( LoggerManager logManager )
  -    {
  -        m_currContext.put( Container.LOGGER_MANAGER, logManager );
  -    }
  -
  -    public void setCommandQueue( Queue commandQueue )
  -    {
  -        m_currContext.put( Container.COMMAND_QUEUE, commandQueue );
  -    }
  -
  -    public void setPoolManager( PoolManager poolManager )
  -    {
  -        m_currContext.put( Container.POOL_MANAGER, poolManager );
  +        try 
  +        {
  +            return context.get(key);
  +        } 
  +        catch (ContextException ce)
  +        {
  +            return defaultValue;
  +        }
       }
  +    
  +    /**
  +     * Will set up a ComponentManager if none is supplied.
  +     *
  +     * The postcondition is that childContext.get( 
Container.COMPONENT_MANAGER )
  +     * should return a valid logger manager.
  +     */
  +    protected void initializeComponentManager() throws Exception
  +    {
  +        try {
  +            childContext.put( COMPONENT_MANAGER, 
overrideContext.get(COMPONENT_MANAGER) );
  +            return;
  +        } 
  +        catch (ContextException ce) 
  +        {
  +        }
  +        
  +        // See if we can inherit from the parent...
  +        try 
  +        {
  +            childContext.get(COMPONENT_MANAGER);
  +            
  +            // OK, done.
  +            return;
  +        }
  +        catch (ContextException ce)
  +        {
  +            // No ComponentManager available anywhere. (Set one up.)
  +        }
   
  -    public void setRoleManager( RoleManager roleManager )
  -    {
  -        m_currContext.put( Container.ROLE_MANAGER, roleManager );
  +        //
  +        // TODO: Load configuration from COMPONENT_MANAGER_CONFIGURATION or 
COMPONENT_MANAGER_CONFIGURATION_URI
  +        //       and create a proper CM from those (ECM?). Need a container 
CM for that, though. /LS
  +        //
  +        ComponentManager cm = new DefaultComponentManager( 
  +            (ComponentManager) get(overrideContext, 
COMPONENT_MANAGER_PARENT, null) );
  +        assumeOwnership(cm);
  +        containerManagerContext.put( COMPONENT_MANAGER, cm );        
       }
   
  -    public void setComponentManager( ComponentManager componentManager )
  +    protected void initializeCommandQueue() throws Exception
       {
  -        m_currContext.put( ContainerManager.PARENT_COMPONENT_MANAGER, 
componentManager );
  +        try 
  +        {
  +            childContext.put( COMMAND_QUEUE, 
overrideContext.get(COMMAND_QUEUE) );
  +            return;
  +        } 
  +        catch (ContextException ce) 
  +        {
  +        }
  +        
  +        try 
  +        {
  +            childContext.get( COMMAND_QUEUE );
  +            return;
  +        }
  +        catch (ContextException ce) 
  +        {
  +        }
  +        
  +        CommandManager cm = new CommandManager();
  +        assumeOwnership(cm);
  +        childContext.put( COMMAND_QUEUE, cm.getCommandQueue() );
  +    }        
  +    
  +    protected void initializePoolManager() throws Exception
  +    {
  +        try {
  +            childContext.put( POOL_MANAGER, 
overrideContext.get(POOL_MANAGER) );
  +            return;
  +        } 
  +        catch (ContextException ce) 
  +        {
  +        }
  +        
  +        PoolManager pm = new DefaultPoolManager( (Queue) 
childContext.get(COMMAND_QUEUE) );
  +        assumeOwnership(pm);
  +        childContext.put( POOL_MANAGER, pm );
  +    }
  +    
  +    protected void initializeRoleManager() throws Exception
  +    {
  +        try {
  +            childContext.put( ROLE_MANAGER, 
overrideContext.get(ROLE_MANAGER) );
  +            return;
  +        } 
  +        catch (ContextException ce) 
  +        {
  +        }
  +        
  +        // Attempt to load config...
  +        //if (config == null) 
  +        //{
  +        
  +        // See if we can inherit from the parent...
  +        try 
  +        {
  +            childContext.get(ROLE_MANAGER);
  +            
  +            // OK, done.
  +            return;
  +        }
  +        catch (ContextException ce)
  +        {
  +            // No RoleManager available anywhere. (Set one up.)
  +        }
  +        //}
  +        
  +        //
  +        // TODO: Load configuration from ROLE_MANAGER_CONFIGURATION or 
ROLE_MANAGER_CONFIGURATION_URI
  +        //       and create a proper RM from those (ERM?). /LS
  +        //
  +        RoleManager rm = new ExcaliburRoleManager();
  +        assumeOwnership(rm);
  +        childContext.put( ROLE_MANAGER, rm );
  +    }        
  +    
  +    /**
  +     * Get a reference to the initial ComponentManager used by the 
ContainerManager
  +     * to hold the Components used for parsing the config files and setting 
up the
  +     * environment.
  +     */
  +    protected void initializeOwnComponentManager() throws Exception
  +    {
  +        DefaultComponentManager manager = new DefaultComponentManager();
  +        
  +        DefaultComponentSelector selector = new DefaultComponentSelector();
  +        ResourceSourceFactory resource = new ResourceSourceFactory();
  +        resource.enableLogging( getLogger() );
  +        selector.put("resource", resource);
  +        
  +        manager.put( resource.ROLE + "Selector", selector );
  +        
  +        SourceResolverImpl resolver = new SourceResolverImpl();
  +        resolver.enableLogging( getLogger() );
  +        resolver.contextualize( this.childContext );
  +        resolver.compose( manager );
  +        
  +        manager.put( resolver.ROLE, resolver );
  +        
  +        manager.makeReadOnly();
  +        
  +        assumeOwnership( manager );
  +        
  +        this.manager = manager;
  +    } 
  +    
  +    protected Configuration getConfiguration( String configKey, String 
uriKey )
  +    {
  +        Configuration config = null;
  +        SourceResolver resolver = null;
  +        Source src = null;
  +        
  +        try 
  +        {
  +            return (Configuration) overrideContext.get( configKey );
  +        }
  +        catch (ContextException ce)
  +        {
  +        }
  +        
  +        String configUri = null;
  +        try 
  +        {
  +            configUri = (String) overrideContext.get( uriKey );
  +        }
  +        catch (ContextException ce)
  +        {
  +            return null;
  +        }            
  +        
  +        try
  +        {
  +            resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
  +            src = resolver.resolve( configUri );
  +            
  +            return configBuilder.build( src.getInputStream() );
  +        }
  +        catch (Exception e)
  +        {
  +            getLogger().warn("Could not read configuration file: " + 
configUri, e);
  +            return null;
  +        }
  +        finally
  +        {
  +            resolver.release( src );
  +            manager.release( resolver );
  +        }
       }
  -
  -    private static final Parameters defaultParameters()
  +    
  +    /**
  +     * Finalizes and returns the context.
  +     */
  +    public Context getContainerManagerContext()
  +    {
  +        return containerManagerContext;
  +    }
  +    
  +    /**
  +     * Finalizes and returns the context.
  +     */
  +    public Context getChildContext()
  +    {
  +        return childContext;
  +    }
  +        
  +    /**
  +     * Get a reference the Logger.
  +     */
  +    protected Logger getLogger()
       {
  -        Parameters params = new Parameters();
  -        params.setParameter( ContainerManager.CONTEXT_DIRECTORY, "./" );
  -        params.setParameter( ContainerManager.WORK_DIRECTORY, "/tmp/" );
  -        params.setParameter( ContainerManager.LOG_CATEGORY, "test" );
  -        params.setParameter( ContainerManager.THREADS_CPU, "2" );
  -        params.setParameter( ContainerManager.THREAD_TIMEOUT, "1000" );
  -        params.makeReadOnly();
  -
  -        return params;
  +        if ( logger == null )
  +        {
  +            return primordialLogger;
  +        }
  +        else
  +        {
  +            return logger;
  +        }
       }
  -
  -    private static final Context defaultContext( Parameters params,
  -                                          ClassLoader contextClassLoader )
  +    
  +    /**
  +     * Will set up a LogKitLoggerManager if none is supplied.  This can be 
  +     * overridden if you don't want a LogKitLoggerManager.
  +     *
  +     * The postcondition is that childContext.get( Container.LOGGER_MANAGER )
  +     * should return a valid logger manager.
  +     */
  +    protected void initializeLoggerManager() throws Exception
       {
  -        DefaultContext context = new DefaultContext();
  -
  -        String[] keys = params.getNames();
  -        for (int i = 0; i < keys.length; i++)
  +        try 
           {
  -            context.put( keys[i], params.getParameter( keys[i], null ) );
  +            // Try copying an already existing logger manager from the 
override context.
  +            
  +            childContext.put( LOGGER_MANAGER, overrideContext.get( 
LOGGER_MANAGER ) );
  +        } 
  +        catch (ContextException ce)
  +        {
  +            // Should we set one up?
  +            // Try to get a configuration for it...
  +            Configuration loggerManagerConfig = getConfiguration( 
LOGGER_MANAGER_CONFIGURATION, LOGGER_MANAGER_CONFIGURATION_URI );
  +            if (loggerManagerConfig == null)
  +            {
  +                // No config.
  +                // Does the parent supply a logger manager?
  +                try 
  +                {
  +                    childContext.get(LOGGER_MANAGER);
  +                    
  +                    // OK, done.
  +                    return;
  +                }
  +                catch (ContextException cex)
  +                {
  +                }
  +            }
  +            
  +            String logCategory = (String) get( overrideContext, 
LOG_CATEGORY, get( childContext, LOG_CATEGORY, null ) );
  +            
  +            LogKitLoggerManager logManager = new LogKitLoggerManager( 
logCategory );
  +            
  +            logManager.contextualize( rootContext );
  +            logManager.configure( loggerManagerConfig );
  +            
  +            assumeOwnership( logManager );
  +            
  +            childContext.put( LOGGER_MANAGER, logManager );
           }
  -
  -        context.put( ContainerManager.CONTEXT_DIRECTORY,
  -                     new File( params.getParameter( 
ContainerManager.CONTEXT_DIRECTORY, "./" ) ) );
  -        context.put( ContainerManager.WORK_DIRECTORY,
  -                     new File( params.getParameter( 
ContainerManager.CONTEXT_DIRECTORY, "/tmp" ) ));
  -        context.put( ContainerManager.LOG_CATEGORY,
  -                params.getParameter(ContainerManager.LOG_CATEGORY, null)
  -        );
  -        context.put( Container.CONTEXT_CLASSLOADER, contextClassLoader );
  -        context.makeReadOnly();
  -
  -        return context;
  -    }
  +        finally
  +        {
  +            // Since we now have a LoggerManager, we can update the 
this.logger field
  +            // if it is null and start logging to the "right" logger.
  +            
  +            if (this.logger == null)
  +            {
  +                getLogger().info( "Switching to default Logger provided by 
LoggerManager." );
  +                
  +                LoggerManager loggerManager = (LoggerManager) 
childContext.get(LOGGER_MANAGER);
  +                this.logger = loggerManager.getDefaultLogger();
  +            }
  +        }
  +    }    
   }
  
  
  
  1.1                  
jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/util/ContextManagerConstants.java
  
  Index: ContextManagerConstants.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.
   */
  package org.apache.avalon.excalibur.system.util;
  
  import org.apache.avalon.excalibur.system.ContainerManagerConstants;
  
  /**
   * Provides constants used to access the Context object for containers.
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]">Leo Sutic</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/03/07 07:03:34 $
   */
  public interface ContextManagerConstants extends ContainerManagerConstants
  {
      public static final String LOG_CATEGORY                        = 
"container.logcategory";
      
      public static final String LOGGER_MANAGER_CONFIGURATION        = 
"container.logManager.config";
      public static final String LOGGER_MANAGER_CONFIGURATION_URI    = 
"container.logManager.config.uri";
      
      public static final String ROLE_MANAGER_CONFIGURATION          = 
"container.roleManager.config";
      public static final String ROLE_MANAGER_CONFIGURATION_URI      = 
"container.roleManager.config.uri";
      
      public static final String CONFIGURATION_URI                   = 
"container.configuration.uri";
      
      public static final String COMPONENT_MANAGER_CLASS             = 
"container.componentManager.config";
      public static final String COMPONENT_MANAGER_CONFIGURATION     = 
"container.componentManager.config";
      public static final String COMPONENT_MANAGER_PARENT            = 
"container.componentManager.parent";
      public static final String COMPONENT_MANAGER_CONFIGURATION_URI = 
"container.componentManager.config.uri";
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/util/ContextBuilder.java
  
  Index: ContextBuilder.java
  ===================================================================
  package org.apache.avalon.excalibur.system.util;
  
  import java.io.File;
  
  import org.apache.avalon.excalibur.logger.LoggerManager;
  import org.apache.avalon.excalibur.event.Queue;
  import org.apache.avalon.excalibur.mpool.PoolManager;
  import org.apache.avalon.framework.component.*;
  import org.apache.avalon.framework.configuration.*;
  import org.apache.avalon.framework.context.*;
  
  /**
   * Helper class to create a context for the ContextManager.
   */
  public class ContextBuilder implements ContextManagerConstants
  {
      private final DefaultContext context;
      
      /**
       * Creates a context builder and initializes it with default values.
       * The default values are:
       * 
       * <ul>
       * <li>THREADS_CPU =  2
       * <li>THREAD_TIMEOUT = 1000
       * <li>CONTEXT_DIRECTORY = "./"
       * <li>WORK_DIRECTORY = "/tmp"
       * <li>LOG_CATEGORY = "test"
       * <li>CONTEXT_CLASSLOADER = the thread context class loader
       * </ul>
       */
      public ContextBuilder( )
      {
          context = new DefaultContext( createDefaultContext() );
      }
      
      /**
       * Creates a context builder and initializes it with default values.
       *
       * @param parent parent context with default values.
       */
      public ContextBuilder( Context parent )
      {
          context = new DefaultContext( parent );
      }
      
      /**
       * Creates a default context.
       */
      public static final Context createDefaultContext()
      {
          return createDefaultContext( 
Thread.currentThread().getContextClassLoader() );
      }
      
      /**
       * Creates a default context.
       */
      public static final Context createDefaultContext( ClassLoader 
contextClassLoader )
      {
          DefaultContext defaultContext = new DefaultContext();
          
          defaultContext.put( THREADS_CPU, new Integer( 2 ) );
          defaultContext.put( THREAD_TIMEOUT, new Long( 1000 ) );
          defaultContext.put( CONTEXT_DIRECTORY, new File( "./" ) );
          defaultContext.put( WORK_DIRECTORY, new File( "/tmp" ) );
          defaultContext.put( LOG_CATEGORY, "test" );
          defaultContext.put( CONTEXT_CLASSLOADER, contextClassLoader );
          
          defaultContext.makeReadOnly();
          
          return defaultContext;
      }
      
      /**
       * Finalizes the context and returns it.
       */
      public Context getContext()
      {
          context.makeReadOnly();
          return context;
      }
      
      public void setCommandQueue( Queue commandQueue )
      {
          context.put( COMMAND_QUEUE, commandQueue );
      }
      
      public void setComponentManagerParent( ComponentManager componentManager )
      {
          context.put( COMPONENT_MANAGER_PARENT, componentManager );
      }
          
      public void setComponentManager( ComponentManager componentManager )
      {
          context.put( COMPONENT_MANAGER, componentManager );
      }
      
      public void setContainerClass( String containerClass )
          throws ClassNotFoundException 
      {
          ClassLoader classLoader = null;
          try 
          {
              classLoader = (ClassLoader) context.get( CONTEXT_CLASSLOADER );
          }
          catch (ContextException ce)
          {
              classLoader = Thread.currentThread().getContextClassLoader();
          }
          
          context.put( CONTAINER_CLASS, classLoader.loadClass (containerClass) 
);
      }
      
      public void setContainerClass( Class containerClass )
      {
          context.put( CONTAINER_CLASS, containerClass );
      }
      
      public void setContainerConfiguration( Configuration config )
      {
          context.put( CONFIGURATION, config );
      }
      
      public void setContainerConfiguration( String location )
      {
          context.put( CONFIGURATION_URI, location );
      }
      
      public void setContextClassLoader( ClassLoader loader )
      {
          context.put( CONTEXT_CLASSLOADER, loader );
      }
      
      public void setContextDirectory( File file )
      {
          context.put( CONTEXT_DIRECTORY, file );
      }
      
      public void setContextDirectory( String directory )
      {
          context.put( CONTEXT_DIRECTORY, new File(directory) );
      }
      
      public void setLoggerCategory( String category )
      {
          context.put( LOG_CATEGORY, category );
      }
      
      public void setLoggerManager( LoggerManager logManager )
      {
          context.put( LOGGER_MANAGER, logManager );
      }
      
      public void setLoggerManagerConfiguration( Configuration config ) 
      {
          context.put( LOGGER_MANAGER_CONFIGURATION, config );
      }
      
      public void setLoggerManagerConfiguration( String location )
      {
          context.put( LOGGER_MANAGER_CONFIGURATION_URI, location );
      }
      
      public void setNumberOfThreadsPerCPU( int numberOfThreads )
      {
          context.put( THREADS_CPU, new Integer( numberOfThreads ) );
      }
      
      public void setPoolManager( PoolManager poolManager )
      {
          context.put( POOL_MANAGER, poolManager );
      }
      
      public void setRoleManager( RoleManager roleManager )
      {
          context.put( ROLE_MANAGER, roleManager );
      }
      
      public void setRoleManagerConfiguration( Configuration config )
      {
          context.put( ROLE_MANAGER_CONFIGURATION, config );
      }
      
      public void setRoleManagerConfiguration( String location )
      {
          context.put( ROLE_MANAGER_CONFIGURATION_URI, location );
      }
      
      public void setThreadTimeout( long timeout )
      {
          context.put( THREAD_TIMEOUT, new Long( timeout ) );
      }
      
      public void setWorkDirectory( File file )
      {
          context.put( WORK_DIRECTORY, file );
      }
      
      public void setWorkDirectory( String directory )
      {
          setWorkDirectory( new File( directory ) );
      }
  }
  
  
  

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

Reply via email to