mcconnell 2003/10/16 23:44:50 Modified: merlin/activation/impl/src/java/org/apache/avalon/activation/appliance/impl DefaultAppliance.java merlin/activation/impl/src/java/org/apache/avalon/activation/lifestyle/impl AbstractLifestyleHandler.java PooledLifestyleHandler.java SingletonLifestyleHandler.java ThreadLifestyleHandler.java TransientLifestyleHandler.java merlin/activation/spi/src/java/org/apache/avalon/activation/lifecycle Factory.java merlin/kernel/unit/src/java/org/apache/avalon/merlin/unit AbstractMerlinTestCase.java merlin/kernel/unit/src/test/org/apache/avalon/merlin/unit StandardTestCase.java Log: Updating lifestyle handles to support collection polices. Revision Changes Path 1.3 +11 -1 avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/appliance/impl/DefaultAppliance.java Index: DefaultAppliance.java =================================================================== RCS file: /home/cvs/avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/appliance/impl/DefaultAppliance.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- DefaultAppliance.java 17 Oct 2003 03:26:28 -0000 1.2 +++ DefaultAppliance.java 17 Oct 2003 06:44:49 -0000 1.3 @@ -1240,6 +1240,16 @@ private class StandardFactory implements Factory { /** + * Return the component deployment model. + * + * @exception LifecycleException + */ + public DeploymentModel getDeploymentModel() + { + return m_model; + } + + /** * Create a new instance of a component. * * @exception LifecycleException 1.3 +72 -3 avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/lifestyle/impl/AbstractLifestyleHandler.java Index: AbstractLifestyleHandler.java =================================================================== RCS file: /home/cvs/avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/lifestyle/impl/AbstractLifestyleHandler.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- AbstractLifestyleHandler.java 17 Oct 2003 03:26:28 -0000 1.2 +++ AbstractLifestyleHandler.java 17 Oct 2003 06:44:49 -0000 1.3 @@ -50,9 +50,16 @@ package org.apache.avalon.activation.lifestyle.impl; +import java.lang.ref.Reference; +import java.lang.ref.SoftReference; +import java.lang.ref.WeakReference; +import java.lang.ref.ReferenceQueue; + +import org.apache.avalon.activation.lifecycle.Factory; +import org.apache.avalon.activation.lifestyle.LifestyleHandler; +import org.apache.avalon.meta.info.InfoDescriptor; import org.apache.avalon.framework.logger.AbstractLogEnabled; import org.apache.avalon.framework.logger.Logger; -import org.apache.avalon.activation.lifestyle.LifestyleHandler; /** * Abstract implentation class for a lifestyle handler. @@ -63,14 +70,76 @@ public abstract class AbstractLifestyleHandler extends AbstractLogEnabled implements LifestyleHandler { + private final Factory m_factory; + + private final InfoDescriptor m_info; + + private final ReferenceQueue m_liberals = new ReferenceQueue(); + + private final ReferenceQueue m_democrats = new ReferenceQueue(); /** * Creation of a new instance. * @param logger the logging channel */ - public AbstractLifestyleHandler( Logger logger ) + public AbstractLifestyleHandler( Logger logger, Factory factory ) { enableLogging( logger ); + m_info = factory.getDeploymentModel().getType().getInfo(); + m_factory = factory; + } + + Factory getFactory() + { + return m_factory; + } + + Reference getReference( Object instance ) + { + if( m_info.isLiberal() ) + { + return new WeakReference( instance, m_liberals ); + } + else if( m_info.isDemocrat() ) + { + return new SoftReference( instance ); + } + else + { + return new StrongReference( instance ); + } } + void disposeInstance( Object instance ) + { + if( instance != null ) + { + synchronized( getFactory() ) + { + m_factory.destroy( instance ); + } + } + } + + class StrongReference extends WeakReference + { + private final Object m_instance; + + public StrongReference( Object instance ) + { + super( instance ); + m_instance = instance; + } + + public Object get() + { + return m_instance; + } + } + + Object newInstance() throws Exception + { + return m_factory.newInstance(); + } + } 1.3 +2 -2 avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/lifestyle/impl/PooledLifestyleHandler.java Index: PooledLifestyleHandler.java =================================================================== RCS file: /home/cvs/avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/lifestyle/impl/PooledLifestyleHandler.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- PooledLifestyleHandler.java 17 Oct 2003 03:26:28 -0000 1.2 +++ PooledLifestyleHandler.java 17 Oct 2003 06:44:49 -0000 1.3 @@ -62,7 +62,7 @@ { public PooledLifestyleHandler( Logger logger, Factory factory ) { - super( logger ); + super( logger, factory ); } /** 1.3 +36 -18 avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/lifestyle/impl/SingletonLifestyleHandler.java Index: SingletonLifestyleHandler.java =================================================================== RCS file: /home/cvs/avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/lifestyle/impl/SingletonLifestyleHandler.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- SingletonLifestyleHandler.java 17 Oct 2003 03:26:28 -0000 1.2 +++ SingletonLifestyleHandler.java 17 Oct 2003 06:44:49 -0000 1.3 @@ -50,6 +50,8 @@ package org.apache.avalon.activation.lifestyle.impl; +import java.lang.ref.Reference; +import java.lang.ref.SoftReference; import java.lang.ref.WeakReference; import org.apache.avalon.activation.lifecycle.Factory; @@ -63,15 +65,13 @@ public class SingletonLifestyleHandler extends AbstractLifestyleHandler implements Disposable { - private final Factory m_factory; - private Object m_instance; + private Reference m_reference; public SingletonLifestyleHandler( Logger logger, Factory factory ) { - super( logger ); + super( logger, factory ); if( factory == null ) throw new IllegalStateException( "factory" ); - m_factory = factory; } /** @@ -83,18 +83,23 @@ */ public Object resolve() throws Exception { - // - // TODO: Add support for ref handling - first off - // virify classes in ref are assignable from the - // instance class, and secondly, build a proxy - // using the ref array. - // + Object instance = null; - if( m_instance != null ) return m_instance; - synchronized( m_factory ) + if( m_reference == null ) { - m_instance = m_factory.newInstance(); - return m_instance; + return refreshReference(); + } + else + { + instance = m_reference.get(); + if( instance == null ) + { + return refreshReference(); + } + else + { + return instance; + } } } @@ -105,7 +110,7 @@ */ public void release( Object instance ) { - // don't release the singleton + // don't release singleton types } /** @@ -113,9 +118,22 @@ */ public void dispose() { - synchronized( m_factory ) + if( m_reference != null ) { - if( m_instance != null ) m_factory.destroy( m_instance ); + disposeInstance( m_reference.get() ); + m_reference = null; } } + + private Object refreshReference() throws Exception + { + synchronized( getFactory() ) + { + Object instance = getFactory().newInstance(); + m_reference = getReference( instance ); + return instance; + } + } + + } 1.3 +10 -2 avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/lifestyle/impl/ThreadLifestyleHandler.java Index: ThreadLifestyleHandler.java =================================================================== RCS file: /home/cvs/avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/lifestyle/impl/ThreadLifestyleHandler.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- ThreadLifestyleHandler.java 17 Oct 2003 03:26:28 -0000 1.2 +++ ThreadLifestyleHandler.java 17 Oct 2003 06:44:49 -0000 1.3 @@ -72,6 +72,14 @@ m_factory = factory; } + // + // TODO: the current implementation is hard coded to CONSERVATIVE + // collection policy - we need to update the ThreadLocalHolder so + // it regenerates the value relative to DEMOCRAT or LIBERAL policies + // (but I just need to check docs on thread local state access + // semantics) + // + protected Object initialValue() { try @@ -96,7 +104,7 @@ public ThreadLifestyleHandler( Logger logger, Factory factory ) { - super( logger ); + super( logger, factory ); m_factory = factory; } 1.3 +11 -22 avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/lifestyle/impl/TransientLifestyleHandler.java Index: TransientLifestyleHandler.java =================================================================== RCS file: /home/cvs/avalon/merlin/activation/impl/src/java/org/apache/avalon/activation/lifestyle/impl/TransientLifestyleHandler.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- TransientLifestyleHandler.java 17 Oct 2003 03:26:28 -0000 1.2 +++ TransientLifestyleHandler.java 17 Oct 2003 06:44:49 -0000 1.3 @@ -50,14 +50,13 @@ package org.apache.avalon.activation.lifestyle.impl; +import java.lang.ref.Reference; import java.lang.ref.WeakReference; -import java.lang.ref.ReferenceQueue; - import java.util.ArrayList; import org.apache.avalon.activation.lifecycle.Factory; -import org.apache.avalon.framework.logger.Logger; import org.apache.avalon.framework.activity.Disposable; +import org.apache.avalon.framework.logger.Logger; /** * @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team</a> @@ -65,16 +64,11 @@ */ public class TransientLifestyleHandler extends AbstractLifestyleHandler implements Disposable { - private final Factory m_factory; - - private final ReferenceQueue m_queue = new ReferenceQueue(); - private ArrayList m_list = new ArrayList(); public TransientLifestyleHandler( Logger logger, Factory factory ) { - super( logger ); - m_factory = factory; + super( logger, factory ); } /** @@ -86,12 +80,12 @@ */ public Object resolve() throws Exception { - // TODO: setup a background thread to check m_queue for + // TODO: setup a background thread to check queues for // released references and remove them from our list, otherwise we // have a memory leak due to accumulation of weak references - Object instance = m_factory.newInstance(); - WeakReference reference = new WeakReference( instance, m_queue ); + Object instance = newInstance(); + Reference reference = getReference( instance ); m_list.add( reference ); return instance; } @@ -103,7 +97,7 @@ */ public void release( Object instance ) { - m_factory.destroy( instance ); + disposeInstance( instance ); } /** @@ -111,16 +105,11 @@ */ public synchronized void dispose() { - WeakReference[] refs = (WeakReference[]) m_list.toArray( new WeakReference[0] ); + Reference[] refs = (Reference[]) m_list.toArray( new Reference[0] ); for( int i=0; i<refs.length; i++ ) { - WeakReference ref = refs[i]; - Object instance = ref.get(); - if( instance != null ) - { - m_factory.destroy( instance ); - ref.clear(); - } + Reference ref = refs[i]; + disposeInstance( refs[i].get() ); } m_list.clear(); } 1.2 +9 -1 avalon/merlin/activation/spi/src/java/org/apache/avalon/activation/lifecycle/Factory.java Index: Factory.java =================================================================== RCS file: /home/cvs/avalon/merlin/activation/spi/src/java/org/apache/avalon/activation/lifecycle/Factory.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- Factory.java 24 Sep 2003 09:31:00 -0000 1.1 +++ Factory.java 17 Oct 2003 06:44:49 -0000 1.2 @@ -51,6 +51,7 @@ package org.apache.avalon.activation.lifecycle; import org.apache.avalon.activation.lifecycle.LifecycleException; +import org.apache.avalon.composition.model.DeploymentModel; /** * The Factory interface exposes an operation though which a @@ -61,6 +62,13 @@ */ public interface Factory { + /** + * Return the component deployment model. + * + * @exception LifecycleException + */ + DeploymentModel getDeploymentModel(); + /** * Create a new instance of a component. * 1.9 +0 -1 avalon/merlin/kernel/unit/src/java/org/apache/avalon/merlin/unit/AbstractMerlinTestCase.java Index: AbstractMerlinTestCase.java =================================================================== RCS file: /home/cvs/avalon/merlin/kernel/unit/src/java/org/apache/avalon/merlin/unit/AbstractMerlinTestCase.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- AbstractMerlinTestCase.java 17 Oct 2003 03:26:29 -0000 1.8 +++ AbstractMerlinTestCase.java 17 Oct 2003 06:44:49 -0000 1.9 @@ -88,7 +88,6 @@ public static final File MERLIN_DEFAULT_CONFIG_FILE = getProjectFile( "conf/config.xml" ); - private static URL CONF = getProjectPath( "conf/config.xml", false ); //------------------------------------------------------------------- // state 1.5 +0 -2 avalon/merlin/kernel/unit/src/test/org/apache/avalon/merlin/unit/StandardTestCase.java Index: StandardTestCase.java =================================================================== RCS file: /home/cvs/avalon/merlin/kernel/unit/src/test/org/apache/avalon/merlin/unit/StandardTestCase.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- StandardTestCase.java 16 Oct 2003 03:57:51 -0000 1.4 +++ StandardTestCase.java 17 Oct 2003 06:44:50 -0000 1.5 @@ -61,8 +61,6 @@ public class StandardTestCase extends AbstractMerlinTestCase { - private static URL CONF = getProjectPath( "conf/cofig.xml", false ); - //-------------------------------------------------------- // constructors //--------------------------------------------------------
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]