bloritsch 02/01/18 13:33:44
Modified: src/scratchpad/org/apache/avalon/excalibur/source
SourceResolverImpl.java
src/scratchpad/org/apache/avalon/excalibur/system
Container.java ContainerManager.java
Log:
add some beginning meat to ContainerManager, adjust SourceResolver
Revision Changes Path
1.12 +30 -20
jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/SourceResolverImpl.java
Index: SourceResolverImpl.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/SourceResolverImpl.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- SourceResolverImpl.java 7 Jan 2002 08:57:56 -0000 1.11
+++ SourceResolverImpl.java 18 Jan 2002 21:33:44 -0000 1.12
@@ -44,13 +44,17 @@
* looking up components from a <code>ComponentManager</code>
* and releasing them.
*
+ * It looks for the base URL in the <code>Context</code> object with
+ * the "container.rootDir" entry. If the entry does not exist, it is
+ * populated with the system property "user.dir".
+ *
* @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a>
- * @version $Id: SourceResolverImpl.java,v 1.11 2002/01/07 08:57:56
cziegeler Exp $
+ * @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
+ * @version $Id: SourceResolverImpl.java,v 1.12 2002/01/18 21:33:44
bloritsch Exp $
*/
public class SourceResolverImpl
extends AbstractLogEnabled
implements Composable,
- Configurable,
Contextualizable,
Disposable,
SourceResolver,
@@ -77,33 +81,39 @@
protected URL userDirectory;
/**
- * Configure the SourceFactories
+ * Get the context
*/
- public void configure(final Configuration conf)
- throws ConfigurationException
+ public void contextualize(Context context)
+ throws ContextException
{
- // set the base URL to the current directory
+ this.context = context;
+
try
{
- this.userDirectory = new
File(System.getProperty("user.dir")).toURL();
- if ( this.getLogger().isDebugEnabled() )
+ this.userDirectory = ((File)
this.context.get("container.rootDir")).toURL();
+ }
+ catch (ContextException ce)
+ {
+ // set the base URL to the current directory
+ try
{
- this.getLogger().debug("SourceResolver: Using base
directory: " + this.userDirectory);
+ this.userDirectory = new
File(System.getProperty("user.dir")).toURL();
+ if ( this.getLogger().isDebugEnabled() )
+ {
+ this.getLogger().debug("SourceResolver: Using base
directory: " + this.userDirectory);
+ }
}
- } catch (MalformedURLException mue)
+ catch (MalformedURLException mue)
+ {
+ throw new ContextException("Malformed URL for user.dir, and
no container.rootDir exists", mue);
+ }
+ }
+ catch (MalformedURLException mue)
{
- throw new ConfigurationException("Malformed URL for user.dir");
+ throw new ContextException("Malformed URL for
container.rootDir", mue);
}
- this.baseURL = this.userDirectory;
- }
- /**
- * Get the context
- */
- public void contextualize(Context context)
- throws ContextException
- {
- this.context = context;
+ this.baseURL = this.userDirectory;
}
/**
1.2 +2 -4
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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Container.java 17 Jan 2002 20:57:43 -0000 1.1
+++ Container.java 18 Jan 2002 21:33:44 -0000 1.2
@@ -16,13 +16,11 @@
* Manager can expose that to the instantiating class.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
+ * @version CVS $Revision: 1.2 $ $Date: 2002/01/18 21:33:44 $
*/
public class Container
{
- public Container( Context initialContext )
- {
- }
-
+ public static final String CONTEXT_CLASSLOADER = "container.classloader";
protected ComponentManager getComponentManager()
{
return null;
1.3 +346 -29
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.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ContainerManager.java 17 Jan 2002 22:02:35 -0000 1.2
+++ ContainerManager.java 18 Jan 2002 21:33:44 -0000 1.3
@@ -7,13 +7,30 @@
*/
package org.apache.avalon.excalibur.system;
+import org.apache.avalon.framework.activity.Disposable;
+import org.apache.avalon.framework.activity.Initializable;
+import org.apache.avalon.framework.activity.Startable;
+import org.apache.avalon.framework.activity.Suspendable;
import org.apache.avalon.framework.context.Context;
+import org.apache.avalon.framework.context.Contextualizable;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.context.DefaultContext;
import org.apache.avalon.framework.component.Component;
import org.apache.avalon.framework.component.ComponentManager;
+import org.apache.avalon.framework.component.DefaultComponentManager;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.component.Composable;
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
+import
org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.parameters.Parameterizable;
+import org.apache.avalon.framework.logger.LogEnabled;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.excalibur.component.RoleManager;
import org.apache.avalon.excalibur.logger.LoggerManager;
import org.apache.avalon.excalibur.logger.LogKitLoggerManager;
+import org.apache.avalon.excalibur.util.ComponentStateValidator;
import java.io.File;
@@ -23,7 +40,95 @@
* 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>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>XML_PARSER</code></td>
+ * <td>
+ * <code>String</code> fully qualified class name of the Parser
component
+ * implementation. It defaults to
+ * "<code>org.apache.avalon.excalibur.xml.JaxpParser</code>".
+ * </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>MAX_THREADS</code></td>
+ * <td>
+ * <code>Integer</code> that holds the value of the maximum number of
+ * threads used by the ThreadManager. This defaults to
+ * <code>CPU_COUNT * 2</code>.
+ * </td>
+ * </tr>
+ * <tr>
+ * <td><code>CPU_COUNT</code></td>
+ * <td>
+ * <code>Integer</code> representing the number of CPUs on a machine.
The
+ * value is used in efficiency calculations, and determining the
number of
+ * threads to have running for the CommandManager. Defaults to
<code>1</code>.
+ * </td>
+ * </tr>
+ * </table>
+ *
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
+ * @version CVS $Revision: 1.3 $ $Date: 2002/01/18 21:33:44 $
*/
public class ContainerManager
{
@@ -35,46 +140,258 @@
public static final String MAX_THREADS = "container.threadPoolMax";
public static final String CONTAINER_CONFIG = "container.configFile";
public static final String LOG_CATEGORY = "container.logCategory";
+ public static final String CPU_COUNT = "os.arch.cpus";
+
+ private final Parameters m_initialParameters;
+ private final ClassLoader m_contextClassLoader;
+ private final File m_contextDirectory;
+ private final File m_workDirectory;
+ private LoggerManager m_logManager;
+ // private final PoolManager m_poolManager;
+ // private final ThreadManager m_threadManager;
+ // private final CommandManager m_commandManager;
+ private ComponentManager m_componentManager;
+ private Logger m_defaultLogger;
+ private Configuration m_containerConfig;
+ private Configuration m_LogKitConfig;
+ private Container m_containerInstance;
+ private ComponentStateValidator m_validator;
+
+ /**
+ * This constructor creates a new ContainerManager with a
LogKitLoggerManager
+ * for managing your logging categories. If you want another
LoggerManager
+ * implementation, then use the constructor with two arguments.
+ */
+ public ContainerManager( final Parameters initialParameters )
+ {
+ this( initialParameters, null );
+ }
+
+ /**
+ * Create a ContainerManager with all the information in the Context,
and the
+ * supplied LoggerManager.
+ */
+ public ContainerManager( final Parameters initialParameters, final
LoggerManager defaultLogManager )
+ {
+ this( initialParameters, defaultLogManager,
Thread.currentThread().getContextClassLoader() );
+ }
+
+ /**
+ * Create a ContainerManager with all the information in the Context,
and the
+ * supplied LoggerManager.
+ */
+ public ContainerManager( final Parameters initialParameters,
+ final LoggerManager defaultLogManager,
+ final ClassLoader rootClassLoader )
+ {
+ m_initialParameters = initialParameters;
+ m_contextClassLoader = rootClassLoader;
+ m_contextDirectory = new File(initialParameters.getParameter(
CONTEXT_DIRECTORY, "./" ) );
+ m_workDirectory = new File( initialParameters.getParameter(
CONTEXT_DIRECTORY, "/tmp" ) );
+
+ m_componentManager = setupComponentManager();
+
+ if ( null == defaultLogManager )
+ {
+ m_logManager = setupLoggerManager();
+ }
+ else
+ {
+ m_logManager = defaultLogManager;
+ }
- private final Context m_context;
- private final File m_contextDirectory;
- private final File m_workDirectory;
- private final LoggerManager m_logManager;
- // private final PoolManager m_poolManager;
- // private final ThreadManager m_threadManager;
- // private final CommandManager m_commandManager;
- private final RoleManager m_roleManager;
- private final ComponentManager m_componentManager;
- private final Logger m_defaultLogger;
-
- public ContainerManager( final Context initialContext )
- {
- this( initialContext, new LogKitLoggerManager() );
- }
-
- public ContainerManager( final Context initialContext, LoggerManager
defaultLogManager )
- {
- m_context = initialContext;
- m_contextDirectory = null;
- m_workDirectory = null;
- m_logManager = defaultLogManager;
- m_roleManager = null;
- m_componentManager = null;
m_defaultLogger = m_logManager.getDefaultLogger();
+
+ recycleContainer();
}
+ /**
+ * Get a reference to your Container. Typically, you would cast this to
+ * whatever interface you will use to interact with it. The actual
+ * initialization process
+ */
Container getContainer()
{
- return null;
+ return m_containerInstance;
+ }
+
+ /**
+ * Get a reference to the LoggerManager used by the ContainerManager.
+ */
+ protected LoggerManager getLoggerManager()
+ {
+ if ( m_logManager == null )
+ {
+ m_logManager = setupLoggerManager();
+ }
+
+ return m_logManager;
+ }
+
+ /**
+ * 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.
+ */
+ public ComponentManager getComponentManager()
+ {
+ return m_componentManager;
+ }
+
+ /**
+ * Override this if you have any special needs for the container (such as
+ * wanting to use your own class).
+ */
+ protected void recycleContainer()
+ {
+ if ( null != m_containerInstance )
+ {
+ if ( m_containerInstance instanceof Startable )
+ {
+ try
+ {
+ ( (Startable) m_containerInstance ).stop();
+ }
+ catch (Exception e)
+ {
+ if ( m_defaultLogger.isWarnEnabled() )
+ {
+ m_defaultLogger.warn("Caught an exception when
stopping the Container, continuing with shutdown", e);
+ }
+ }
+ }
+
+ if ( m_containerInstance instanceof Disposable )
+ {
+ ( (Disposable) m_containerInstance ).dispose();
+ }
+
+ m_containerInstance = null;
+ }
+
+ Container instance = new Container();
+
+ try
+ {
+ /* Begin *Manageable interfaces
+ if ( instance instanceof LoggerManageable )
+ {
+ ( (LoggerManageable) instance ).setLoggerManager(
getLoggerManager() );
+ }
+
+ if ( instance instanceof CommandManageable )
+ {
+ ( (CommandManageable) instance ).setCommandQueue(
m_commandManager.getQueue() );
+ }
+
+ if ( instance instanceof RoleManageable )
+ {
+ ( (RoleManageable) instance ).setRoleManager(
setupRoleManager() );
+ }
+ */
+
+ if ( instance instanceof Contextualizable )
+ {
+ ( (Contextualizable) instance ).contextualize(
setupContext() );
+ }
+
+ if ( instance instanceof LogEnabled )
+ {
+ ( (LogEnabled) instance ).enableLogging( m_defaultLogger );
+ }
+
+ if ( instance instanceof Composable )
+ {
+ ( (Composable) instance ).compose( m_componentManager );
+ }
+
+ if ( instance instanceof Configurable )
+ {
+ ( (Configurable) instance ).configure( m_containerConfig );
+ }
+
+ if ( instance instanceof Parameterizable )
+ {
+ ( (Parameterizable) instance ).parameterize(
Parameters.fromConfiguration( m_containerConfig ) );
+ }
+
+ if ( instance instanceof Initializable )
+ {
+ ( (Initializable) instance ).initialize();
+ }
+
+ if ( instance instanceof Startable )
+ {
+ ( (Startable) instance ).start();
+ }
+ }
+ catch ( Exception e )
+ {
+ instance = null;
+ if ( m_defaultLogger.isFatalErrorEnabled() )
+ {
+ m_defaultLogger.fatalError( "Cannot set up the Container,
this is an error I cannot recover from.", e );
+ }
+ }
+
+ m_containerInstance = instance;
}
- LoggerManager getLoggerManager()
+ /**
+ * Override this if you have any special needs for the Container's
Context.
+ * Typically, this will f
+ */
+ protected Context setupContext()
{
- return null;
+ DefaultContext context = new DefaultContext();
+ context.put( CONTEXT_DIRECTORY, m_contextDirectory );
+ context.put( WORK_DIRECTORY, m_workDirectory );
+ context.put( CPU_COUNT,
+ new Integer(m_initialParameters.getParameterAsInteger(
CPU_COUNT, 1 ) )
+ );
+ context.put( LOG_CATEGORY,
+ m_initialParameters.getParameter(LOG_CATEGORY, null)
+ );
+ context.put( Container.CONTEXT_CLASSLOADER, m_contextClassLoader );
+
+ context.makeReadOnly();
+ return context;
+ }
+
+ /**
+ * Set up a LogKitLoggerManager if none is supplied. This can be
overridden
+ * if you don't want a LogKitLoggerManager.
+ */
+ protected LoggerManager setupLoggerManager()
+ {
+ LogKitLoggerManager logManager = new LogKitLoggerManager(
+ m_initialParameters.getParameter(LOG_CATEGORY, null)
+ );
+ return logManager;
+ }
+
+ /**
+ * Sets up the ContainerManager's ComponentManager. This
ComponentManager
+ * will have all the components necessary to read configuration files and
+ * manage the configuration resources. It will also be passed to the
+ * Container.
+ */
+ protected ComponentManager setupComponentManager()
+ {
+ DefaultComponentManager manager = new DefaultComponentManager();
+ String parser = m_initialParameters.getParameter(XML_PARSER,
"org.apache.avalon.excalibur.xml.JaxpParser");
+ //manager.put();
+
+ return manager;
}
- ComponentManager getComponentManager()
+ /**
+ * Sets up the RoleManager. You may override this method to implement
your
+ * own logic. This is the RoleManager given to the Container so that the
+ * Container can interpret it's Configuration.
+ */
+ protected RoleManager setupRoleManager()
{
- return null;
+ throw new UnsupportedOperationException();
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>