You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by le...@apache.org on 2002/06/24 23:34:21 UTC
cvs commit: jakarta-avalon-excalibur/microcontainer/src/java/org/apache/excalibur/microcontainer Handler.java MicroContainer.java MicroContainerProxy.java
leosutic 2002/06/24 14:34:21
Modified: microcontainer/src/java/org/apache/excalibur/microcontainer
Handler.java MicroContainer.java
MicroContainerProxy.java
Log:
no message
Revision Changes Path
1.2 +114 -13 jakarta-avalon-excalibur/microcontainer/src/java/org/apache/excalibur/microcontainer/Handler.java
Index: Handler.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/microcontainer/src/java/org/apache/excalibur/microcontainer/Handler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Handler.java 24 Jun 2002 20:53:46 -0000 1.1
+++ Handler.java 24 Jun 2002 21:34:20 -0000 1.2
@@ -6,6 +6,8 @@
import java.util.Map;
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.component.ComponentManager;
import org.apache.avalon.framework.component.Composable;
import org.apache.avalon.framework.configuration.Configuration;
@@ -14,6 +16,8 @@
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.logger.LogEnabled;
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.parameters.Parameterizable;
/**
* InvocationHandler for MicroContainerProxies.
@@ -32,6 +36,11 @@
private final ComponentManager manager;
/**
+ * The parameters for the component.
+ */
+ private final Parameters parameters;
+
+ /**
* The context of the component.
*/
private final Context context;
@@ -46,6 +55,9 @@
*/
private final Class componentClass;
+ /**
+ * Logger used by the component.
+ */
private final Logger logger;
/**
@@ -54,13 +66,14 @@
*/
private final boolean shared;
- public Handler (Class componentClass, Logger logger, Map components, Configuration configuration, Context context, boolean shared)
+ public Handler (Class componentClass, Logger logger, Map components, Parameters parameters, Configuration configuration, Context context, boolean shared)
{
this.componentClass = componentClass;
this.manager = new MicroContainerComponentManager( components );
this.shared = shared;
this.context = context;
this.configuration = configuration;
+ this.parameters = parameters;
this.logger = logger;
this.instance = newInstance ();
@@ -75,33 +88,107 @@
return method.invoke( instance, args );
}
- public Object newInstance ()
+ private Object newInstance ()
{
+ Object inst = null;
try
{
- Object inst = componentClass.newInstance();
+ inst = componentClass.newInstance();
+ }
+ catch( Exception e )
+ {
+ throw new MicroContainerException( "Unable to instantiate component class " + componentClass.getName(), e );
+ }
+
+ try
+ {
if (inst instanceof LogEnabled)
{
((LogEnabled) inst).enableLogging( logger );
}
+ }
+ catch( Exception e )
+ {
+ throw new MicroContainerException( "Exception thrown during enableLogging().", e );
+ }
+
+ try
+ {
if (inst instanceof Contextualizable)
{
((Contextualizable) inst).contextualize( context );
}
+ }
+ catch( Exception e )
+ {
+ throw new MicroContainerException( "Exception thrown during contextualize().", e );
+ }
+
+ try
+ {
if (inst instanceof Configurable)
{
((Configurable) inst).configure( configuration );
}
+ }
+ catch( Exception e )
+ {
+ throw new MicroContainerException( "Exception thrown during configure().", e );
+ }
+
+
+ try
+ {
+ if (inst instanceof Parameterizable)
+ {
+ ((Parameterizable) inst).parameterize( parameters );
+ }
+ }
+ catch( Exception e )
+ {
+ throw new MicroContainerException( "Exception thrown during parameterize().", e );
+ }
+
+ try
+ {
if (inst instanceof Composable)
{
((Composable) inst).compose( manager );
}
- return inst;
+
+ }
+ catch( Exception e )
+ {
+ throw new MicroContainerException( "Exception thrown during compose().", e );
+ }
+
+ try
+ {
+ if (inst instanceof Initializable)
+ {
+ ((Initializable) inst).initialize();
+ }
+
+ }
+ catch( Exception e )
+ {
+ throw new MicroContainerException( "Exception thrown during initialize().", e );
+ }
+
+ try
+ {
+ if (inst instanceof Startable)
+ {
+ ((Startable) inst).start();
+ }
+
}
catch( Exception e )
{
- throw new MicroContainerException( e );
+ throw new MicroContainerException( "Exception thrown during start().", e );
}
+
+ return inst;
}
public synchronized Object MicroContainerProxy__newInstance ()
@@ -116,22 +203,36 @@
}
}
- public synchronized void MicroContainerProxy__releaseInstance(Object o)
+ private void decommission(Object o)
{
- if ( !shared )
+ if (o instanceof Startable)
{
- if (o instanceof Disposable)
+ try
{
- ((Disposable) o).dispose ();
+ ((Startable) o).stop ();
+ }
+ catch( Exception e )
+ {
+ throw new MicroContainerException( "Exception thrown during stop().", e );
}
}
- }
+
+ if (o instanceof Disposable)
+ {
+ ((Disposable) o).dispose ();
+ }
+ }
- public void MicroContainerProxy__release()
+ public synchronized void MicroContainerProxy__releaseInstance(Object o)
{
- if (instance instanceof Disposable)
+ if ( !shared )
{
- ((Disposable) instance).dispose ();
+ decommission(o);
}
+ }
+
+ public void MicroContainerProxy__release()
+ {
+ decommission( instance );
}
}
1.2 +131 -18 jakarta-avalon-excalibur/microcontainer/src/java/org/apache/excalibur/microcontainer/MicroContainer.java
Index: MicroContainer.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/microcontainer/src/java/org/apache/excalibur/microcontainer/MicroContainer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MicroContainer.java 24 Jun 2002 20:53:46 -0000 1.1
+++ MicroContainer.java 24 Jun 2002 21:34:20 -0000 1.2
@@ -6,6 +6,7 @@
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.logger.NullLogger;
import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.parameters.Parameters;
import java.lang.reflect.Proxy;
import java.util.HashMap;
@@ -15,9 +16,9 @@
/**
* Factory class for MicroContainers. Usage follows the named parameter idiom:
* <pre><code>
- * Component myComponent = (Component) new MicroContainer( ComponentImpl.class )<br/>
- * .sharedInstance( true )<br/>
- * .components( componentMap )<br/>
+ * Component myComponent = (Component) new MicroContainer( ComponentImpl.class )
+ * .sharedInstance( true )
+ * .components( componentMap )
* .create();
* </code></pre>
*/
@@ -26,14 +27,16 @@
private static Logger defaultLogger = new NullLogger ();
private boolean sharedInstance = true;
- private Map components = new HashMap ();
- private final Class implementation;
+ private Map components = new HashMap();
+ private final Class componentClass;
private Configuration config = null;
- private Map configMap = new HashMap ();
+ private Map configMap = new HashMap();
private Context context = null;
- private Map contextMap = new HashMap ();
+ private Map contextMap = new HashMap();
+
+ private Parameters parameters = new Parameters();
private Logger logger = defaultLogger;
@@ -42,9 +45,9 @@
* Note that you are not interested in this class, but the object you get when
* calling <code>create()</code>.
*/
- public MicroContainer( Class implementation )
+ public MicroContainer( Class componentClass )
{
- this.implementation = implementation;
+ this.componentClass = componentClass;
}
/**
@@ -69,12 +72,34 @@
return this;
}
+ /**
+ * Add a single role -> component mapping for this component.
+ *
+ * @param role the role name.
+ * @param component the component. This must be a proxy obtained via this class.
+ *
+ * @throws IllegalArgumentException if the component isn't a MicroContainerProxy.
+ */
public MicroContainer addComponent( String role, Object component )
{
- components.put( role, component );
- return this;
+ if( component instanceof MicroContainerProxy )
+ {
+ components.put( role, component );
+ return this;
+ }
+ else
+ {
+ throw new IllegalArgumentException( "component is not a MicroContainerProxy:" + component.getClass().getName() );
+ }
}
+ /**
+ * Adds an entry to the context for this component.
+ * This method may not be called after you have called <code>context()</code>.
+ *
+ * @param config the <code>Context</code> for this component.
+ * @throws IllegalStateException if <code>context</code> has been called before.
+ */
public MicroContainer addContextEntry( String key, Object entry )
{
if( context != null )
@@ -86,8 +111,20 @@
return this;
}
+ /**
+ * Sets the context for this component.
+ * This method may not be called after you have called <code>addContextEntry()</code>.
+ *
+ * @param config the <code>Context</code> for this component.
+ * @throws IllegalStateException if <code>addContextEntry</code> has been called before.
+ */
public MicroContainer context( Context context )
{
+ if( context == null )
+ {
+ throw new IllegalArgumentException( "context may not be null" );
+ }
+
if( contextMap.size() > 0 )
{
throw new IllegalStateException( "You can not set the context " +
@@ -97,6 +134,14 @@
return this;
}
+ /**
+ * Adds an attribute to the configuration for this component.
+ * This method may not be called after you have called <code>configuration()</code>.
+ *
+ * @param key the attribute name.
+ * @param value the attribute value.
+ * @throws IllegalStateException if <code>configuration</code> has been called before.
+ */
public MicroContainer addConfigurationAttribute( String key, String value )
{
if( config != null )
@@ -109,8 +154,20 @@
return this;
}
- public MicroContainer confinguration( Configuration config )
+ /**
+ * Sets the configuration for this component.
+ * This method may not be called after you have called <code>addConfigurationAttribute()</code>.
+ *
+ * @param config the <code>Configuration</code> for this component.
+ * @throws IllegalStateException if <code>addConfigurationAttribute</code> has been called before.
+ */
+ public MicroContainer configuration( Configuration config )
{
+ if( config == null )
+ {
+ throw new IllegalArgumentException( "configuration may not be null" );
+ }
+
if( configMap.size() > 0 )
{
throw new IllegalStateException( "You can not set the configuration " +
@@ -121,6 +178,11 @@
return this;
}
+ /**
+ * Set the logger for this component.
+ * Default is the logger set via <code>setDefaultLogger</code>, or a <code>NullLogger</code>
+ * if nothing has been set.
+ */
public MicroContainer logger( Logger logger )
{
this.logger = logger;
@@ -128,6 +190,28 @@
}
/**
+ * Sets a value in the Parameters instance for this component.
+ * This will only have an effect if the component implements <code>Parameterizable</code>.
+ */
+ public MicroContainer setParameter( String name, String value )
+ {
+ parameters.setParameter( name, value );
+ return this;
+ }
+
+ /**
+ * Sets the Parameters for this component. This will overwrite any parameters
+ * set via <code>setParameter()</code> and any previous Parameter object set
+ * via this method. This will only have an effect if the component implements
+ * <code>Parameterizable</code>.
+ */
+ public MicroContainer parameters( Parameters parameters )
+ {
+ this.parameters = parameters;
+ return this;
+ }
+
+ /**
* Creates a new MicroContainer proxy. The returned object implements all interfaces
* of the implementation class.
*/
@@ -135,7 +219,7 @@
{
try
{
- Class[] implementationInterfaces = implementation.getInterfaces ();
+ Class[] implementationInterfaces = componentClass.getInterfaces ();
Class[] proxyInterfaces = new Class[implementationInterfaces.length + 1];
for (int i = 0; i < implementationInterfaces.length; i++)
{
@@ -164,7 +248,7 @@
return (MicroContainerProxy) Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
proxyInterfaces,
- new Handler (implementation, logger, components, config, context, sharedInstance)
+ new Handler (componentClass, logger, components, parameters, config, context, sharedInstance)
);
}
catch (Exception e)
@@ -173,19 +257,48 @@
}
}
+ /**
+ * Sets the default logger to use for all components created.
+ * Default is a <code>NullLogger</code>.
+ *
+ * @param logger the new default logger.
+ */
public static void setDefaultLogger( Logger logger )
{
defaultLogger = logger;
}
/**
- * Disposes of a MicroContainer proxy.
+ * Disposes of a MicroContainer proxy. This method should be called with
+ * every proxy obtained via this class once you are done with the component.
+ *
+ * <code>
+ * <pre>
+ * Component myComponent = new MicroContainer( ComponentImpl.class ).create();
+ * try
+ * {
+ * // Do stuff with myComponent.
+ * }
+ * finally
+ * {
+ * MicroContainer.release( myComponent );
+ * }
+ * </pre>
+ * </code>
+ *
+ * @param component the component proxy to release. This parameter may be <code>null</code>,
+ * in which case nothing is done.
*/
- public static void release(Object o)
+ public static void release(Object component)
{
- if (o instanceof MicroContainerProxy)
+ if( component == null )
+ {
+ return;
+ }
+
+ if( component instanceof MicroContainerProxy )
{
- ((MicroContainerProxy) o).MicroContainerProxy__release();
+ ((MicroContainerProxy) component).MicroContainerProxy__release();
}
}
}
1.2 +2 -1 jakarta-avalon-excalibur/microcontainer/src/java/org/apache/excalibur/microcontainer/MicroContainerProxy.java
Index: MicroContainerProxy.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/microcontainer/src/java/org/apache/excalibur/microcontainer/MicroContainerProxy.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MicroContainerProxy.java 24 Jun 2002 20:53:46 -0000 1.1
+++ MicroContainerProxy.java 24 Jun 2002 21:34:20 -0000 1.2
@@ -3,7 +3,8 @@
/**
* The "factory" interface for proxies. The method names have been
- * chose so as to not conflict with any known interface.
+ * chosen so as to not conflict with any known interface.
+ * This interface is only used internally by MicroContainer.
*/
public interface MicroContainerProxy
{
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>