leosutic 02/03/06 23:03:15
Modified: src/scratchpad/org/apache/avalon/excalibur/system
DefaultContainerManager.java ContainerManager.java
Container.java
Added: src/scratchpad/org/apache/avalon/excalibur/system
ContainerManagerConstants.java
ContainerConstants.java
Log:
First cut of modifications
Revision Changes Path
1.3 +246 -220
jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/DefaultContainerManager.java
Index: DefaultContainerManager.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/DefaultContainerManager.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DefaultContainerManager.java 4 Mar 2002 22:37:01 -0000 1.2
+++ DefaultContainerManager.java 7 Mar 2002 07:03:15 -0000 1.3
@@ -1,220 +1,246 @@
-/*
- * 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;
-
-import org.apache.avalon.framework.component.*;
-import org.apache.avalon.framework.context.Context;
-import org.apache.avalon.framework.configuration.*;
-import org.apache.avalon.framework.logger.*;
-import org.apache.avalon.excalibur.logger.*;
-import org.apache.avalon.excalibur.source.*;
-import org.apache.avalon.excalibur.system.util.*;
-import org.apache.avalon.excalibur.util.ComponentStateValidator;
-import org.apache.avalon.excalibur.util.SystemUtil;
-
-import java.io.File;
-
-/**
- * The ContainerManager is a single point of contact to manage your Container
- * resources. It takes care of creating the other managers that a Container
- * needs to use, as well as initializing the Container. It is designed to be
- * directly instantiated by whatever class needs to initialize your system.
- *
- * <p>
- * The ContainerManager provides some constants used in the initial
- * <code>Parameters</code> passed into the ContainerManager. The
ContainerManager
- * uses these values to create all the pieces necessary for the Container.
- * Below is a table that describes what those options are.
- * </p>
- *
- * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
- * @version CVS $Revision: 1.2 $ $Date: 2002/03/04 22:37:01 $
- */
-public class DefaultContainerManager implements ContainerManager
-{
- private static final Configuration EMPTY_CONFIG;
-
- static
- {
- DefaultConfiguration config = new DefaultConfiguration("", "", "",
"");
- config.makeReadOnly();
- EMPTY_CONFIG = config;
- }
-
- private final DefaultConfigurationBuilder m_configBuilder =
- new
DefaultConfigurationBuilder();
- private final DefaultConfigurationSerializer m_configSerializer =
- new
DefaultConfigurationSerializer();
-
- private final ContextManager m_contextManager;
- private Logger m_logger;
- private Object m_containerInstance;
-
- public DefaultContainerManager()
- {
- this( new ContextManager(), new ConsoleLogger() );
- }
-
- public DefaultContainerManager( ContextManager externalManager )
- {
- this( externalManager, new ConsoleLogger() );
- }
-
- public DefaultContainerManager( ContextManager externalManager, Logger
defaultLogger )
- {
- m_contextManager = (null == externalManager) ? new ContextManager()
: externalManager;
-
- if ( null == defaultLogger ||
- defaultLogger instanceof ConsoleLogger ||
- defaultLogger instanceof NullLogger )
- {
- Logger testLogger = defaultLogger;
-
- try
- {
- LoggerManager manager = (LoggerManager)
m_contextManager.get( Container.LOGGER_MANAGER );
-
- testLogger = manager.getDefaultLogger();
- }
- catch (Exception e)
- {
- if ( null == testLogger )
- {
- testLogger = new ConsoleLogger();
- }
- }
-
- m_logger = testLogger;
- }
-
- setupContext();
- }
-
- /**
- * Initialize the ContainerManager
- */
- public void initialize()
- {
- }
-
- /**
- * Dispose of the ContainerManager and managed Container
- */
- public void dispose()
- {
- }
-
- /**
- * Get a reference to your Container. Typically, you would cast this to
- * whatever interface you will use to interact with it.
- */
- public Object getContainer()
- {
- return m_containerInstance;
- }
-
- /**
- * Get a reference the default Logger.
- */
- public Logger getLogger()
- {
- return m_logger;
- }
-
- /**
- * Get a reference to the ContextManager. The ContextManager is used to
- * set up the initial properties for the system.
- */
- public ContextManager getContextManager()
- {
- return m_contextManager;
- }
-
- protected void setupContext()
- {
- }
-
- /**
- * 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 ComponentManager getComponentManager()
- {
- ComponentManager parentManager = (ComponentManager)
- m_contextManager.get( ContainerManager.PARENT_COMPONENT_MANAGER
);
-
- if ( null == parentManager )
- {
- DefaultComponentManager manager = new DefaultComponentManager();
-
- try
- {
- 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(
m_contextManager.getContextInstance() );
- resolver.compose( manager );
- manager.put( resolver.ROLE, resolver );
- }
- catch ( Exception e )
- {
- getLogger().warn("Could not set up the initial components",
e);
- }
-
- manager.makeReadOnly();
- m_contextManager.setComponentManager( manager );
- parentManager = manager;
- }
-
- return parentManager;
- }
-
- protected Configuration getConfiguration( String key )
- {
- Object value = m_contextManager.get( key );
-
- if ( value instanceof Configuration )
- {
- return (Configuration) value;
- }
-
- Configuration config = null;
- SourceResolver resolver = null;
- try
- {
- resolver = (SourceResolver)
getComponentManager().lookup(SourceResolver.ROLE);
- Source src = resolver.resolve( key );
-
- config = m_configBuilder.build( src.getInputStream() );
-
- resolver.release( src );
- }
- catch (Exception e)
- {
- config = EMPTY_CONFIG;
-
- if ( getLogger().isWarnEnabled() )
- {
- getLogger().warn("Could not read configuration file: " +
key, e);
- }
- }
- finally
- {
- getComponentManager().release(resolver);
- }
-
- return config;
- }
-}
+/*
+ * 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;
+
+import org.apache.avalon.framework.activity.*;
+import org.apache.avalon.framework.component.*;
+import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.Contextualizable;
+import org.apache.avalon.framework.configuration.*;
+import org.apache.avalon.framework.logger.*;
+import org.apache.avalon.framework.parameters.*;
+import org.apache.avalon.excalibur.logger.*;
+import org.apache.avalon.excalibur.source.*;
+import org.apache.avalon.excalibur.system.util.*;
+import org.apache.avalon.excalibur.util.ComponentStateValidator;
+import org.apache.avalon.excalibur.util.SystemUtil;
+
+import java.io.File;
+
+/**
+ * The ContainerManager is a single point of contact to manage your Container
+ * resources. It takes care of creating the other managers that a Container
+ * needs to use, as well as initializing the Container. It is designed to be
+ * directly instantiated by whatever class needs to initialize your system.
+ *
+ * <p>
+ * The ContainerManager provides some constants used in the initial
+ * <code>Parameters</code> passed into the ContainerManager. The
ContainerManager
+ * uses these values to create all the pieces necessary for the Container.
+ * Below is a table that describes what those options are.
+ * </p>
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
+ * @version CVS $Revision: 1.3 $ $Date: 2002/03/07 07:03:15 $
+ */
+public class DefaultContainerManager implements ContainerManager,
ContainerManagerConstants
+{
+ private final Context initParameters;
+ private final Context containerContext;
+ private Logger logger;
+ private Object containerInstance;
+ private ComponentStateValidator validator;
+
+ public DefaultContainerManager( ContextManager contextManager )
+ {
+ this( contextManager, null );
+ }
+
+ public DefaultContainerManager( ContextManager contextManager, Logger
logger )
+ {
+ this( contextManager.getContainerManagerContext() ,
contextManager.getChildContext(), logger );
+ }
+
+ public DefaultContainerManager( Context initParameters )
+ {
+ this( initParameters, null, null );
+ }
+
+ public DefaultContainerManager( Context initParameters, Logger logger )
+ {
+ this( initParameters, null, logger );
+ }
+
+
+ public DefaultContainerManager( Context initParameters, Context
containerContext )
+ {
+ this( initParameters, containerContext, null );
+ }
+
+ public DefaultContainerManager( Context initParameters, Context
containerContext, Logger logger )
+ {
+ this.initParameters = initParameters;
+ this.containerContext = containerContext != null ? containerContext
: initParameters;
+ this.logger = logger == null ? createLoggerFromContext(
initParameters ) : logger;
+ }
+
+ protected Logger createLoggerFromContext( Context initParameters )
+ {
+ try
+ {
+ return ((LoggerManager) initParameters.get( LOGGER_MANAGER
)).getDefaultLogger();
+ }
+ catch (ContextException ce)
+ {
+ Logger consoleLogger = new ConsoleLogger();
+ consoleLogger.error( "Could not obtain logger manager from init
parameters (this should not happen). Using console instead." );
+ return consoleLogger;
+ }
+ }
+
+ /**
+ * Initialize the ContainerManager
+ */
+ public void initialize()
+ {
+ initializeContainer();
+ }
+
+ protected void initializeContainer()
+ {
+ if ( null == containerInstance )
+ {
+ Object instance = null;
+ try
+ {
+ instance = ((Class) initParameters.get( CONTAINER_CLASS
)).newInstance();
+ }
+ catch ( Exception e )
+ {
+ instance = null;
+ if ( getLogger().isFatalErrorEnabled() )
+ {
+ getLogger().fatalError( "Cannot set up the Container,
this is an error I cannot recover from.", e );
+ }
+ return;
+ }
+
+ validator = new ComponentStateValidator( instance );
+
+ try
+ {
+ if ( instance instanceof LogEnabled )
+ {
+ validator.checkLogEnabled();
+ ( (LogEnabled) instance ).enableLogging( logger );
+ }
+
+ if ( instance instanceof Contextualizable )
+ {
+ validator.checkContextualized();
+ ( (Contextualizable) instance ).contextualize(
containerContext );
+ }
+
+ if ( instance instanceof Composable )
+ {
+ validator.checkComposed();
+ ( (Composable) instance ).compose( (ComponentManager)
initParameters.get(COMPONENT_MANAGER) );
+ }
+
+ if ( instance instanceof Configurable )
+ {
+ validator.checkConfigured();
+ ( (Configurable) instance ).configure( (Configuration)
initParameters.get(CONFIGURATION) );
+ }
+
+ if ( instance instanceof Parameterizable )
+ {
+ validator.checkParameterized();
+ ( (Parameterizable) instance ).parameterize(
(Parameters) initParameters.get(PARAMETERS) );
+ }
+
+ if ( instance instanceof Initializable )
+ {
+ validator.checkInitialized();
+ ( (Initializable) instance ).initialize();
+ }
+
+ if ( instance instanceof Startable )
+ {
+ validator.checkStarted();
+ ( (Startable) instance ).start();
+ }
+ }
+ catch ( Exception e )
+ {
+ instance = null;
+ if ( getLogger().isFatalErrorEnabled() )
+ {
+ getLogger().fatalError( "Cannot set up the Container,
this is an error I cannot recover from.", e );
+ }
+ }
+
+ containerInstance = instance;
+ }
+ }
+
+ protected void disposeContainer()
+ {
+ if ( null != containerInstance )
+ {
+ if ( containerInstance instanceof Startable )
+ {
+ try
+ {
+ validator.checkStopped();
+ ( (Startable) containerInstance ).stop();
+ }
+ catch (Exception e)
+ {
+ if ( getLogger().isWarnEnabled() )
+ {
+ getLogger().warn("Caught an exception when stopping
the Container, continuing with shutdown", e);
+ }
+ }
+ }
+
+ if ( containerInstance instanceof Disposable )
+ {
+ validator.checkDisposed();
+ ( (Disposable) containerInstance ).dispose();
+ }
+
+ validator = null;
+ containerInstance = null;
+ }
+ }
+
+ /**
+ * Override this if you have any special needs for the container (such as
+ * wanting to use your own class).
+ */
+ private final void recycleContainer()
+ throws InitializationException
+ {
+ disposeContainer();
+ initializeContainer();
+ }
+
+ /**
+ * Dispose of the ContainerManager and managed Container
+ */
+ public void dispose()
+ {
+ disposeContainer();
+ }
+
+ /**
+ * Get a reference to your Container. Typically, you would cast this to
+ * whatever interface you will use to interact with it.
+ */
+ public Object getContainer()
+ {
+ return containerInstance;
+ }
+
+ public Logger getLogger() {
+ return logger;
+ }
+
+}
1.32 +41 -100
jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/ContainerManager.java
Index: ContainerManager.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/ContainerManager.java,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -r1.31 -r1.32
--- ContainerManager.java 4 Mar 2002 22:37:01 -0000 1.31
+++ ContainerManager.java 7 Mar 2002 07:03:15 -0000 1.32
@@ -15,105 +15,52 @@
import java.io.File;
/**
- * The ContainerManager is a single point of contact to manage your Container
- * resources. It takes care of creating the other managers that a Container
- * needs to use, as well as initializing the Container. It is designed to be
- * directly instantiated by whatever class needs to initialize your system.
- *
- * <p>
- * The ContainerManager provides some constants used in the initial
- * <code>Parameters</code> passed into the ContainerManager. The
ContainerManager
- * uses these values to create all the pieces necessary for the Container.
- * Below is a table that describes what those options are.
- * </p>
- *
- * <table>
- * <tr>
- * <th>Constant</th>
- * <td>Description</th>
- * </tr>
- * <tr>
- * <td><code>CONTAINER_CLASS</code></td>
- * <td>
- * <code>String</code> fully qualified class name for the container
class.
- * this is absolutely necessary for the ContainerManager to create the
- * Container class instance.
- * </td>
- * </tr>
- * <tr>
- * <td><code>CONTEXT_DIRECTORY</code></td>
- * <td>
- * <code>String</code> path to the Context's root directory. This is
- * used to resolve all relative paths, and the "context:"
psuedo-protocol.
- * Defaults to "./".
- * </td>
- * </tr>
- * <tr>
- * <td><code>WORK_DIRECTORY</code></td>
- * <td>
- * <code>String</code> path to a work area on the file system.
Defaults
- * to "/tmp".
- * </td>
- * </tr>
- * <tr>
- * <td><code>LOG_CATEGORY</code></td>
- * <td>
- * <code>String</code> root category name for the container and its
manager.
- * If no value is given, the default (null) is used.
- * </td>
- * </tr>
- * <tr>
- * <td><code>LOGKIT_CONFIG</code></td>
- * <td>
- * <code>String</code> path to the LogKitLoggerManager configuration
file.
- * If this is not set, ContainerManager tries the uri
- * <code>"context://logkit.xconf"</code>. If nothing is there, the
- * LogKitLoggerManager is not configured. <strong>Note:</strong> If
you
- * passed in a reference to a LoggerManager yourself, ContainerManager
- * ignores this parameter altogether.
- * </td>
- * </tr>
- * <tr>
- * <td><code>ROLE_CONFIG</code></td>
- * <td>
- * <code>String</code> path to the RoleManager configuration file. If
- * this is not set, ContainerManager does not create a RoleManager at
all.
- * </td>
- * </tr>
- * <tr>
- * <td><code>CONTAINER_CONFIG</code></td>
- * <td>
- * <code>String</code> path the points to the container's Configuration
- * file. This value defaults to the uri "context://container.xconf".
- * If the Container is Configurable and no entry can be found, then the
- * Container is given an empty Configuration object.
- * </td>
- * </tr>
- * <tr>
- * <td><code>THREADS_CPU</code></td>
- * <td>
- * <code>Integer</code> that holds the value of the number of threads
used
- * by the ThreadManager per CPU. This defaults to 2.
- * </td>
- * </tr>
- * </table>
+ * A ContainerManager is a pocket universe for a container and its
+ * components.
+ *
+ * <p><b>Case 1: Use by a servlet or other "root" entity</b></p>
+ *
+ * <pre>
+ * <code>
+ * //
+ * ContextBuilder contextBuilder = new ContextBuilder ();
+ * contextBuilder.setContainerClass(
Thread.currentThread().getContextClassLoader().loadClass(
"org.apache.avalon.excalibur.system.test.TestContainer" ) );
+ * contextBuilder.setContextDirectory( "./" );
+ * contextBuilder.setWorkDirectory( "./" );
+ * contextBuilder.setContainerConfiguration(
"resource://org/apache/avalon/excalibur/system/test/ContainerProfile.xconf" );
+ * contextBuilder.setLoggerManagerConfiguration(
"resource://org/apache/avalon/excalibur/system/test/ContainerProfile.xlog" );
+ *
+ * ContextManager contextManager = new ContextManager( null,
contextBuilder.getContext(), null );
+ * contextManager.initialize();
+ *
+ * ContainerManager cm = new DefaultContainerManager(
contextManager.getContextInstance() );
+ * cm.initialize();
+ * </code>
+ * </pre>
+ *
+ * Then, for example, wait for a request and pass it on to the container:
+ *
+ * <pre>
+ * <code>
+ * TestContainer container = (TestContainer) cm.getContainer();
+ * container.handleRequest( ... );
+ * </code>
+ * </pre>
+ *
+ * When done, dispose of the managers.
+ *
+ * <pre>
+ * <code>
+ * containerManager.dispose();
+ * contextManager.dispose();
+ * </code>
+ * </pre>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
- * @version CVS $Revision: 1.31 $ $Date: 2002/03/04 22:37:01 $
+ * @version CVS $Revision: 1.32 $ $Date: 2002/03/07 07:03:15 $
*/
public interface ContainerManager extends Disposable, Initializable
{
- String CONTEXT_DIRECTORY = Container.CONTEXT_DIRECTORY;
- String WORK_DIRECTORY = Container.WORK_DIRECTORY;
- String CONTAINER_CLASS = "container.class";
- String LOGKIT_CONFIG = "container.loggerConfig";
- String ROLE_CONFIG = "container.roles";
- String THREADS_CPU = "container.threadsPerCPU";
- String THREAD_TIMEOUT = "container.threadTimeout";
- String CONTAINER_CONFIG = "container.configFile";
- String LOG_CATEGORY = "container.logCategory";
- String PARENT_COMPONENT_MANAGER = "container.componentManager";
-
/**
* Get a reference to your Container. Typically, you would cast this to
* whatever interface you will use to interact with it.
@@ -124,10 +71,4 @@
* Get a reference the default Logger.
*/
Logger getLogger();
-
- /**
- * Get a reference to the ContextManager. The ContextManager is used to
- * set up the initial properties for the system.
- */
- ContextManager getContextManager();
}
1.8 +2 -9
jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/Container.java
Index: Container.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/Container.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- Container.java 25 Feb 2002 16:13:36 -0000 1.7
+++ Container.java 7 Mar 2002 07:03:15 -0000 1.8
@@ -16,16 +16,9 @@
* ContainerManager to the Container is through the Context object.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
- * @version CVS $Revision: 1.7 $ $Date: 2002/02/25 16:13:36 $
+ * @version CVS $Revision: 1.8 $ $Date: 2002/03/07 07:03:15 $
*/
-public interface Container
+public interface Container extends ContainerConstants
{
- String CONTEXT_CLASSLOADER = "container.classloader";
- String CONTEXT_DIRECTORY = "context-root";
- String WORK_DIRECTORY = "container.workDir";
- String LOGGER_MANAGER = "container.logManager";
- String COMMAND_QUEUE = "container.commandQueue";
- String POOL_MANAGER = "container.poolManager";
- String ROLE_MANAGER = "container.roleManager";
}
1.1
jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/ContainerManagerConstants.java
Index: ContainerManagerConstants.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;
/**
* Provides constants used to access the Context object for container
managers.
* A container manager can assume that all these elements are present in
* the initial context.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Leo Sutic</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/03/07 07:03:15 $
*/
public interface ContainerManagerConstants extends ContainerConstants
{
/**
* Class: The class of the container.
*/
public static final String CONTAINER_CLASS =
"container.class";
/**
* ComponentManager: The component manager to give to the container.
*/
public static final String COMPONENT_MANAGER =
"container.componentManager";
/**
* Configuration: The configuration to give to the container.
*/
public static final String CONFIGURATION =
"container.configuration";
/**
* Parameters: The Parameters object to give to the container.
*/
public static final String PARAMETERS =
"container.parameters";
}
1.1
jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/system/ContainerConstants.java
Index: ContainerConstants.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;
/**
* Provides constants used to access the Context object for containers.
* A container should allow these values to propagate down to child
containers,
* so that they may create child containers in turn.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/03/07 07:03:15 $
*/
public interface ContainerConstants
{
public static final String CONTEXT_CLASSLOADER = "container.classloader";
public static final String CONTEXT_DIRECTORY = "context-root";
public static final String WORK_DIRECTORY = "container.workDir";
public static final String LOGGER_MANAGER = "container.logManager";
public static final String COMMAND_QUEUE = "container.commandQueue";
public static final String POOL_MANAGER = "container.poolManager";
public static final String ROLE_MANAGER = "container.roleManager";
public static final String THREADS_CPU =
"container.threadsPerCPU";
public static final String THREAD_TIMEOUT =
"container.threadTimeout";
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>