CVS: plexus-container-new/src/test/org/apache/plexus DefaultServiceE.java,NONE,1.1 ServiceE.java,NONE,1.1 DefaultPlexusContainerTest.java,1.3,1.4 configuration.xml,1.5,1.6

[email protected] Mon, 12 May 2003 13:57:30 -0500
Newsgroups gmane.comp.java.plexus.devel
Message-ID <[email protected]>
Update of /cvsroot/plexus/plexus-container-new/src/test/org/apache/plexus
In directory eng.werken.com:/tmp/cvs-serv16424/src/test/org/apache/plexus

Modified Files:
	DefaultPlexusContainerTest.java configuration.xml 
Added Files:
	DefaultServiceE.java ServiceE.java 
Log Message:
o The handling of the lifecycle is now the responsibility of the instance
  manager and not the component repository. The hierarchy may change a little on
  the inside but won't affect usage. When there was only one type of component
  instance this could be dealt with easily from the Component Repository but
  obviously when you get into dealing with different types of instantiation
  strategies the lifecycle must be handled in the entity where the strategy
  resides. I'm sure I'm just discovering what every other container writer
  has discovered along the way.
  
  I found this working on a webapp and I was playing with RunData using a
  poolabel and per-lookup instance manager and after the first lookup the
  component wasn't being run through the lifecycle. So that's been fixed.
  Now the instance manager inside the component manager is responsible
  for this and this will cause a little bit of the internals to be
  shuffled as the component repository still has the method to start
  the lifecycle which it probably doesn't need.




--- NEW FILE: DefaultServiceE.java ---
package org.apache.plexus;

import org.apache.plexus.logging.AbstractLogEnabled;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.activity.Startable;
import org.apache.avalon.framework.logger.Logger;

/** This service implements all the start phases:
 *
 *  LogEnabled
 *  Contexualize
 *  Serviceable
 *  Configurable
 *  Initializable
 *  Startable
 *
 */
public class DefaultServiceE
    extends AbstractLogEnabled
    implements ServiceE, Contextualizable, Serviceable, Configurable, Initializable, Startable
{
    boolean enableLogging;
    boolean contextualize;
    boolean service;
    boolean configure;
    boolean initialize;
    boolean start;
    boolean stop;

    // ----------------------------------------------------------------------
    // Lifecylce Management
    // ----------------------------------------------------------------------

    public void enableLogging( Logger logger )
    {
        enableLogging = true;
    }

    public void contextualize( Context context )
    {
        contextualize = true;
    }

    public void service( ServiceManager serviceManager )
    {
        service = true;
    }

    public void configure( Configuration configuration )
    {
        configure = true;
    }

    public void initialize()
        throws Exception
    {
        initialize = true;
    }

    public void start()
        throws Exception
    {
        start = true;
    }

    public void stop()
        throws Exception
    {
        stop = true;
    }
}

--- NEW FILE: ServiceE.java ---
package org.apache.plexus;

public interface ServiceE
{
    static String ROLE = ServiceE.class.getName();
}

Index: DefaultPlexusContainerTest.java
===================================================================
RCS file: /cvsroot/plexus/plexus-container-new/src/test/org/apache/plexus/DefaultPlexusContainerTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- DefaultPlexusContainerTest.java	1 May 2003 19:35:36 -0000	1.3
+++ DefaultPlexusContainerTest.java	12 May 2003 18:57:28 -0000	1.4
@@ -12,7 +12,7 @@
  *  @author <a href="mailto:[email protected]">Jason van Zyl</a>
  */
 public class DefaultPlexusContainerTest
-     extends TestCase
+    extends TestCase
 {
     /** */
     private InputStream configurationStream;
@@ -25,9 +25,9 @@
      *
      * @param name
      */
-    public DefaultPlexusContainerTest(String name)
+    public DefaultPlexusContainerTest( String name )
     {
-        super(name);
+        super( name );
     }
 
     public void setUp()
@@ -49,11 +49,12 @@
         System.setProperty( "plexus.home", System.getProperty( "basedir" ) + "/target/plexus-home" );
 
         DefaultPlexusContainer container = new DefaultPlexusContainer();
-        container.setConfigurationResource( new InputStreamReader( configurationStream  ) );
+        container.setConfigurationResource( new InputStreamReader( configurationStream ) );
         container.initialize();
         container.start();
 
         int defaultComponents = 0;
+        int testComponents = 5;
 
         // These are some default components that we used internally. These components don't
         // usually need to be replaced but they can be if the user desires.
@@ -84,7 +85,7 @@
         defaultComponents++;
 
         // Make sure all our service descriptors are present.
-        assertEquals( 4 + defaultComponents, container.getComponentRepository().configuredComponents() );
+        assertEquals( testComponents + defaultComponents, container.getComponentRepository().configuredComponents() );
 
         // ----------------------------------------------------------------------
         //  ServiceDescriptors
@@ -124,7 +125,7 @@
         assertEquals( 0 + defaultComponents, container.getComponentRepository().instantiatedComponents() );
 
         // Make sure the number of configured components is still 3.
-        assertEquals( 4 + defaultComponents , container.getComponentRepository().configuredComponents() );
+        assertEquals( testComponents + defaultComponents, container.getComponentRepository().configuredComponents() );
 
         // ----------------------------------------------------------------------
         //  ServiceB
@@ -189,5 +190,39 @@
         assertNotSame( serviceD1, serviceD2 );
         assertNotSame( serviceD2, serviceD3 );
         assertNotSame( serviceD1, serviceD3 );
+
+        // ----------------------------------------------------------------------
+        // Per-lookup component
+        // ----------------------------------------------------------------------
+
+        // Retrieve an instance of service e.
+        DefaultServiceE serviceE1 = (DefaultServiceE) container.getComponentRepository().lookup( ServiceE.ROLE );
+
+        // Make sure the service is alive.
+        assertNotNull( serviceE1 );
+
+        // Check the lifecycle
+        assertEquals( true, serviceE1.enableLogging );
+        assertEquals( true, serviceE1.contextualize );
+        assertEquals( true, serviceE1.service );
+        assertEquals( true, serviceE1.configure );
+        assertEquals( true, serviceE1.initialize );
+        assertEquals( true, serviceE1.start );
+
+        // Retrieve another
+        DefaultServiceE serviceE2 = (DefaultServiceE) container.getComponentRepository().lookup( ServiceE.ROLE );
+
+        // Make sure the service is alive.
+        assertNotNull( serviceE2 );
+
+        // Check the lifecycle
+        assertEquals( true, serviceE2.enableLogging );
+        assertEquals( true, serviceE2.contextualize );
+        assertEquals( true, serviceE2.service );
+        assertEquals( true, serviceE2.configure );
+        assertEquals( true, serviceE2.initialize );
+        assertEquals( true, serviceE2.start );
+
+        assertNotSame( serviceE1, serviceE2 );
     }
 }

Index: configuration.xml
===================================================================
RCS file: /cvsroot/plexus/plexus-container-new/src/test/org/apache/plexus/configuration.xml,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- configuration.xml	10 May 2003 16:39:30 -0000	1.5
+++ configuration.xml	12 May 2003 18:57:28 -0000	1.6
@@ -63,6 +63,19 @@
         <port>10000</port>
       </configuration>
     </component>
+
+    <!--
+     |
+     | E Service
+     |
+     -->
+    <component>
+      <role>org.apache.plexus.ServiceE</role>
+      <implementation>org.apache.plexus.DefaultServiceE</implementation>
+      <instantiation-strategy>per-lookup</instantiation-strategy>
+      <configuration/>
+    </component>
+
   </components>
 
 </plexus>