(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Mon, 24 Mar 2008 13:15:54 +0000 (13:15 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Mon, 24 Mar 2008 13:15:54 +0000 (13:15 +0000)
trunk/system/general/src/main/java/org/wamblee/system/AbstractSubSystem.java
trunk/system/general/src/main/java/org/wamblee/system/CompositeSystem.java
trunk/system/general/src/main/java/org/wamblee/system/DefaultServiceRegistry.java
trunk/system/general/src/main/java/org/wamblee/system/ServiceRegistry.java
trunk/system/general/src/main/java/org/wamblee/system/SubSystem.java
trunk/system/general/src/test/java/org/wamblee/system/Application.java
trunk/system/general/src/test/java/org/wamblee/system/Environment.java
trunk/system/general/src/test/java/org/wamblee/system/SystemAssemblerTest.java
trunk/system/pom.xml
trunk/system/spring/pom.xml [new file with mode: 0644]

index 06fd5bb82c823aa3481e278753a6338504919c66..7a6724f32ea8af702df9d9f45527f1dc53b09c98 100644 (file)
@@ -10,43 +10,47 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 /**
- * Abstract subsystem class making it easy to implement new 
- * subsystems.
+ * Abstract subsystem class making it easy to implement new subsystems.
  */
 public abstract class AbstractSubSystem implements SubSystem {
-       
+
        private static final Log LOG = LogFactory.getLog(AbstractSubSystem.class);
 
-       private String _name; 
+       private String _name;
        private List<ServiceDescriptor> _provided;
        private List<ServiceDescriptor> _required;
-       private Map<ServiceDescriptor, Service> _running; 
+       private Map<ServiceDescriptor, Service> _running;
        
        /**
-        * Constructs the subsystem. 
-        * @param aRegistry Registry of services. 
-        * @param aName Name of the system. 
-        * @param aProvided Provided services. 
-        * @param aRequired Required services. 
+        * Constructs the subsystem.
+        * 
+        * @param aRegistry
+        *            Registry of services.
+        * @param aName
+        *            Name of the system.
+        * @param aProvided
+        *            Provided services.
+        * @param aRequired
+        *            Required services.
         */
-       protected AbstractSubSystem(String aName, ServiceDescriptor[] aProvided, 
-                       ServiceDescriptor[] aRequired) { 
-               _name = aName; 
-               _provided = new ArrayList<ServiceDescriptor>(); 
+       protected AbstractSubSystem(String aName, ServiceDescriptor[] aProvided,
+                       ServiceDescriptor[] aRequired) {
+               _name = aName;
+               _provided = new ArrayList<ServiceDescriptor>();
                _provided.addAll(Arrays.asList(aProvided));
-               _required = new ArrayList<ServiceDescriptor>(); 
+               _required = new ArrayList<ServiceDescriptor>();
                _required.addAll(Arrays.asList(aRequired));
-               _running = new HashMap<ServiceDescriptor, Service>(); 
+               _running = new HashMap<ServiceDescriptor, Service>();
        }
 
        @Override
        public final String getName() {
-               return _name; 
+               return _name;
        }
 
        @Override
        public final ServiceDescriptor[] getProvidedServices() {
-               return _provided.toArray(new ServiceDescriptor[0]); 
+               return _provided.toArray(new ServiceDescriptor[0]);
        }
 
        @Override
@@ -55,30 +59,40 @@ public abstract class AbstractSubSystem implements SubSystem {
        }
 
        @Override
-       public final Service[] start(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices) {
-               LOG.info("Initializing '" + aContext + "." + _name + "' with " + Arrays.asList(aRequiredServices));
-               doStart(aContext + "." + getName(), aRegistry, aRequiredServices); 
+       public final Service[] start(String aContext, ServiceRegistry aRegistry,
+                       Service[] aRequiredServices) {
+               LOG.info("Initializing '" + aContext + "." + _name + "' with "
+                               + Arrays.asList(aRequiredServices));
+               doStart(aContext + "." + getName(), aRegistry, aRequiredServices);
                return _running.values().toArray(new Service[0]);
        }
-       
+
        /**
-        * Must be implemented for initializing the subsystem. 
-        * The implementation must call {@link #addService(Service)}
-        * for each service that is started. 
-        * @param aRegistry Registry to which the subsystem must register its services. 
-        * @param aRequiredServices Services that are already running
-        * from other subsystems that may be used. 
+        * Must be implemented for initializing the subsystem. The implementation
+        * must call {@link #addService(Service)} for each service that is started.
+        * 
+        * @param aRegistry Service registry. 
+        * @param aRequiredServices
+        *            Services that are already running from other subsystems that
+        *            may be used.
         */
-       protected abstract void doStart(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices); 
-       
+       protected abstract void doStart(String aContext,
+                       ServiceRegistry aRegistry,
+                       Service[] aRequiredServices);
+
        /**
-        * Implementations must call this method to indicate that
-        * a new service has been started. 
-        * @param aService Service. 
+        * Implementations must call this method to indicate that a new service has
+        * been started.
+        * 
+        * @param aService
+        *            Service.
         */
-       protected final void addService(String aContext, Service aService) { 
+       protected final void addService(String aContext,
+                       ServiceRegistry aRegistry, 
+                       ServiceDescriptor aDescriptor, Object aService) {
                LOG.info(aContext + ": service '" + aService + "' started.");
-               _running.put(aService.getDescriptor(), aService);
+               Service svc = aRegistry.register(aDescriptor, aService);
+               _running.put(svc.getDescriptor(), svc);
        }
 
        @Override
@@ -86,9 +100,19 @@ public abstract class AbstractSubSystem implements SubSystem {
                return _running.values().toArray(new Service[0]);
        }
        
+       @Override
+       public void stop(String aContext, ServiceRegistry aRegistry) {
+               doStop(aContext);       
+               for (Service svc: _running.values()) { 
+                       aRegistry.remove(svc);
+               }
+       }
+       
+       protected abstract void doStop(String aContext); 
+
        @Override
        public String toString() {
-               return _name; 
+               return _name;
        }
 
 }
index b3abf07186da3774dd0a586b595a12a2c62ff75b..3255149810e8f6bfe0e21b4c3f081543eaa9e2f1 100644 (file)
@@ -83,10 +83,15 @@ public class CompositeSystem extends AbstractSubSystem {
                for (Service service : aRequiredServices) {
                        descriptors.add(service.getDescriptor());
                }
-               SystemAssembler assembler = new SystemAssembler(getName(), _systems,
+               SystemAssembler assembler = new SystemAssembler(aContext + "." + getName(), _systems,
                                descriptors.toArray(new ServiceDescriptor[0]));
                assembler.start(aRegistry, aRequiredServices);
        }
+       
+       @Override
+       protected void doStop(String aContext) {
+               // Empty.       
+       }
 
        private void info(String aMsg) {
                LOG.info(getName() + ": " + aMsg);
index 3fe5c71dc2e18bfca4cf17f904a02f5eed14e40c..4d8bd465d0feffeef64bc8ff6b72aced868d1173 100644 (file)
@@ -23,6 +23,14 @@ public class DefaultServiceRegistry implements ServiceRegistry {
                _services.put(id, svc);
                return svc;
        }
+       
+       @Override
+       public void remove(Service aService) {
+               Service svc = _services.remove(aService.getId());
+               if ( svc == null ) { 
+                       throw new IllegalArgumentException("Service '" + aService.getId() + "' does not exist");
+               }
+       }
 
        @Override
        public synchronized Service find(String id) {
index 62224dd3922e766ae95e5b3723b937c055f1a456..ebe7229e1c42b3072bb6239f473abdeef3d2f25c 100644 (file)
@@ -4,6 +4,8 @@ public interface ServiceRegistry {
 
        Service register(ServiceDescriptor aDescriptor, Object aService);
        
+       void remove(Service aService); 
+       
        Service find(String aId); 
        
        Service[] listAllServices(); 
index babb9e025edbd7ce2e15bdeb114e576451b19aaf..16b37aab0936ca61d486c086a9c5bca7b57fd4fb 100644 (file)
@@ -37,6 +37,13 @@ public interface SubSystem {
         */
        Service[] start(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices);
        
+       /**
+        * Stops a service. 
+        * @param aContext Context
+        * @param aRegistry Registry from which services must be removed. 
+        */
+       void stop(String aContext, ServiceRegistry aRegistry); 
+       
        /**
         * Gets the list of running services in the subsystem. 
         * 
index 77c76cb3120413b71163ad7af6a670b13480ec9e..3587917b93e98653d2bd95294e770c505477896d 100644 (file)
@@ -17,5 +17,9 @@ public class Application extends AbstractSubSystem {
        protected void doStart(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices) {
                // Empty, no services provided externally. 
        }
-
+       
+       @Override
+       protected void doStop(String aContext) {
+               // Empty.       
+       }
 }
index 7209c273f17e705ce68fe9e5492bbb3294978c90..901c67d75bf60c0c3ddb0c0b10bc8f31142d786a 100644 (file)
@@ -10,14 +10,19 @@ public class Environment extends AbstractSubSystem {
                        new DefaultServiceDescriptor(DataSource.class), 
                        new DefaultServiceDescriptor(Integer.class)
        };
-
+       
        public Environment() { 
                super("environment", PROVIDED, new ServiceDescriptor[0]);
        }
        
        @Override
        protected void doStart(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices) {
-           addService(aContext, aRegistry.register(PROVIDED[0], new Integer(1)));
-           addService(aContext, aRegistry.register(PROVIDED[1], new Integer(2)));
+           addService(aContext, aRegistry, PROVIDED[0], new Integer(1));
+           addService(aContext, aRegistry, PROVIDED[1], new Integer(2));
+       }
+       
+       @Override
+       protected void doStop(String aContext) {
+               // Empty.       
        }
 }
index 60d1b587007387dc710ef1e56cdfe0b32b8f04b7..70558250b547d1b4bfdc92b7c56c0e61cbdbada2 100644 (file)
@@ -23,6 +23,12 @@ public class SystemAssemblerTest extends TestCase {
                Service[] appServices = environment.getRunningServices();
                assertEquals(2, appServices.length);
                assertEquals(2, _registry.listAllServices().length);
+               
+               environment.stop("", _registry);
+               assertEquals(0, _registry.listAllServices().length);
+               
+               application.stop("", _registry); 
+               assertEquals(0, _registry.listAllServices().length);
        }
 
        public void testApplicationEnvironment() {
index 202365ef09be9e474b2ddd7f73a9ff387d431198..c79ad3124118cf0d0e33334a1b53bcc0f7f1ab7c 100644 (file)
@@ -16,5 +16,6 @@
   <url>http://wamblee.org</url>
   <modules>
     <module>general</module>
+    <module>spring</module>
   </modules>
 </project>
diff --git a/trunk/system/spring/pom.xml b/trunk/system/spring/pom.xml
new file mode 100644 (file)
index 0000000..bc1737e
--- /dev/null
@@ -0,0 +1,24 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+  <parent>
+    <groupId>org.wamblee</groupId>
+    <artifactId>wamblee-utils</artifactId>
+    <version>0.2-SNAPSHOT</version>
+  </parent>
+
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.wamblee</groupId>
+  <artifactId>wamblee-system-spring</artifactId>
+  <packaging>jar</packaging>
+  <name>wamblee.org system general</name>
+  <url>http://wamblee.org</url>
+  <dependencies>
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+    </dependency>
+  </dependencies>
+
+</project>