(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Mon, 24 Mar 2008 12:55:26 +0000 (12:55 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Mon, 24 Mar 2008 12:55:26 +0000 (12:55 +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/DefaultService.java
trunk/system/general/src/main/java/org/wamblee/system/DefaultServiceRegistry.java [new file with mode: 0644]
trunk/system/general/src/main/java/org/wamblee/system/Service.java
trunk/system/general/src/main/java/org/wamblee/system/ServiceRegistry.java [new file with mode: 0644]
trunk/system/general/src/main/java/org/wamblee/system/SubSystem.java
trunk/system/general/src/main/java/org/wamblee/system/SystemAssembler.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

index 0837d5f67f233ffe6c7d9e5ddd6c73bf52d05917..06fd5bb82c823aa3481e278753a6338504919c66 100644 (file)
@@ -16,7 +16,7 @@ import org.apache.commons.logging.LogFactory;
 public abstract class AbstractSubSystem implements SubSystem {
        
        private static final Log LOG = LogFactory.getLog(AbstractSubSystem.class);
-       
+
        private String _name; 
        private List<ServiceDescriptor> _provided;
        private List<ServiceDescriptor> _required;
@@ -24,6 +24,7 @@ public abstract class AbstractSubSystem implements SubSystem {
        
        /**
         * Constructs the subsystem. 
+        * @param aRegistry Registry of services. 
         * @param aName Name of the system. 
         * @param aProvided Provided services. 
         * @param aRequired Required services. 
@@ -54,9 +55,9 @@ public abstract class AbstractSubSystem implements SubSystem {
        }
 
        @Override
-       public final Service[] initialize(String aContext, Service[] aRequiredServices) {
+       public final Service[] start(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices) {
                LOG.info("Initializing '" + aContext + "." + _name + "' with " + Arrays.asList(aRequiredServices));
-               doInitialize(aContext + "." + getName(), aRequiredServices); 
+               doStart(aContext + "." + getName(), aRegistry, aRequiredServices); 
                return _running.values().toArray(new Service[0]);
        }
        
@@ -64,10 +65,11 @@ public abstract class AbstractSubSystem implements SubSystem {
         * 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. 
         */
-       protected abstract void doInitialize(String aContext, Service[] aRequiredServices); 
+       protected abstract void doStart(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices); 
        
        /**
         * Implementations must call this method to indicate that
index f3b1bf752c08f0cbb8bdf2f3cb6ff2afc811a40f..b3abf07186da3774dd0a586b595a12a2c62ff75b 100644 (file)
@@ -78,14 +78,14 @@ public class CompositeSystem extends AbstractSubSystem {
        }
 
        @Override
-       protected void doInitialize(String aContext, Service[] aRequiredServices) {
+       protected void doStart(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices) {
                List<ServiceDescriptor> descriptors = new ArrayList<ServiceDescriptor>();
                for (Service service : aRequiredServices) {
                        descriptors.add(service.getDescriptor());
                }
                SystemAssembler assembler = new SystemAssembler(getName(), _systems,
                                descriptors.toArray(new ServiceDescriptor[0]));
-               assembler.start(aRequiredServices);
+               assembler.start(aRegistry, aRequiredServices);
        }
 
        private void info(String aMsg) {
index fb2128ee940664966bd31ba41c26d9be79b017ea..a110dfda5eb63bb4344fa6f2703e6eddd22ad866 100644 (file)
@@ -4,32 +4,43 @@ package org.wamblee.system;
  * Default service implementation.
  */
 public class DefaultService implements Service {
-       
-       private ServiceDescriptor _descriptor; 
-       private Object _service; 
-       
+
+       private String _id;
+       private ServiceDescriptor _descriptor;
+       private Object _service;
+
        /**
-        * Constructs the service. 
-        * @param aDescriptor Descriptor to use. 
-        * @param aService Service. 
+        * Constructs the service.
+        * 
+        * @param aDescriptor
+        *            Descriptor to use.
+        * @param aService
+        *            Service.
         */
-       public DefaultService(ServiceDescriptor aDescriptor, Object aService) { 
-               _descriptor = aDescriptor; 
+       public DefaultService(String aId, ServiceDescriptor aDescriptor,
+                       Object aService) {
+               _id = aId;
+               _descriptor = aDescriptor;
                _service = aService;
        }
 
+       @Override
+       public String getId() {
+               return _id;
+       }
+
        @Override
        public ServiceDescriptor getDescriptor() {
-               return _descriptor; 
+               return _descriptor;
        }
 
        @Override
        public <T> T reference(Class<T> aClass) {
-               return (T)_service; 
+               return (T) _service;
        }
 
        @Override
        public String toString() {
-               return "(" + _descriptor + ", " + _service + ")"; 
+               return "(" + _descriptor + ", " + _service + ")";
        }
 }
diff --git a/trunk/system/general/src/main/java/org/wamblee/system/DefaultServiceRegistry.java b/trunk/system/general/src/main/java/org/wamblee/system/DefaultServiceRegistry.java
new file mode 100644 (file)
index 0000000..3fe5c71
--- /dev/null
@@ -0,0 +1,38 @@
+package org.wamblee.system;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+
+public class DefaultServiceRegistry implements ServiceRegistry {
+
+       private int _count;
+       private Map<String, Service> _services;
+
+       public DefaultServiceRegistry() {
+               _count = 0;
+               _services = new HashMap<String, Service>();
+       }
+
+       @Override
+       public synchronized Service register(ServiceDescriptor aDescriptor,
+                       Object aService) {
+               _count++;
+               String id = "" + _count;
+               Service svc = new DefaultService(id, aDescriptor, aService);
+               _services.put(id, svc);
+               return svc;
+       }
+
+       @Override
+       public synchronized Service find(String id) {
+               // TODO Auto-generated method stub
+               return null;
+       }
+
+       @Override
+       public Service[] listAllServices() {
+               return new ArrayList<Service>(_services.values())
+                               .toArray(new Service[0]);
+       }
+}
index 0db48511d90562d2a423dc22cddb19c7cf6ef144..61428399580e3caf86566445db392aed6b5cb0c4 100644 (file)
@@ -4,6 +4,13 @@ package org.wamblee.system;
  * Represents a running service.
  */
 public interface Service {
+       
+       /**
+        * Gets the ide of the service in the registry. 
+        * @return Service id. 
+        */
+       String getId(); 
+       
        /**
         * Gets the descriptor of the service. 
         * @return Descriptor. 
diff --git a/trunk/system/general/src/main/java/org/wamblee/system/ServiceRegistry.java b/trunk/system/general/src/main/java/org/wamblee/system/ServiceRegistry.java
new file mode 100644 (file)
index 0000000..62224dd
--- /dev/null
@@ -0,0 +1,10 @@
+package org.wamblee.system;
+
+public interface ServiceRegistry {
+
+       Service register(ServiceDescriptor aDescriptor, Object aService);
+       
+       Service find(String aId); 
+       
+       Service[] listAllServices(); 
+}
index 9d68ee4e39a98c312c21790fd8b7ac5d1d03e8ee..babb9e025edbd7ce2e15bdeb114e576451b19aaf 100644 (file)
@@ -29,11 +29,13 @@ public interface SubSystem {
         * Initialises the subsytem by starting all the services that
         * it described as provided. 
         * @param aContext Unique name for the subsystem. 
+        * @param aRegistry Registry of service to which the subsystem must register the services it
+        *   creates. 
         * @param aRequiredServices Running services from other 
         * subsystems that are required by this subsystem. 
         * @return Services that are running in the subsystem. 
         */
-       Service[] initialize(String aContext, Service[] aRequiredServices);
+       Service[] start(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices);
        
        /**
         * Gets the list of running services in the subsystem. 
index 3bff0564b2829901633520e9138de98746e95be2..5b2c0e143f0080f3a6df43a9c27eb592e5a9a9c8 100644 (file)
@@ -80,10 +80,11 @@ public class SystemAssembler {
 
        /**
         * Starts the subsystems.
+        * @param aRegistry Service registry to which created services must be registered. 
         * @param aRequiredServices Services that are available from
         *  other systems that have been started before. 
         */
-       public void start(Service[] aRequiredServices) {
+       public void start(ServiceRegistry aRegistry, Service[] aRequiredServices) {
                LOG.info("Starting '" + _context + "'");
                Map<ServiceDescriptor, Service> allProvided = new HashMap<ServiceDescriptor, Service>();
 
@@ -101,7 +102,7 @@ public class SystemAssembler {
                                }
                                services.add(required);
                        }
-                       Service[] provided = system.initialize(_context, services
+                       Service[] provided = system.start(_context, aRegistry, services
                                        .toArray(new Service[0]));
                        for (Service service : provided) {
                                allProvided.put(service.getDescriptor(), service);
index 1a725b3c14d7c890f262f6027baf73b16d555f27..77c76cb3120413b71163ad7af6a670b13480ec9e 100644 (file)
@@ -14,7 +14,7 @@ public class Application extends AbstractSubSystem {
        }
 
        @Override
-       protected void doInitialize(String aContext, Service[] aRequiredServices) {
+       protected void doStart(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices) {
                // Empty, no services provided externally. 
        }
 
index bd014c5a0caeb298d49498e96d8bc0d75d75fd78..7209c273f17e705ce68fe9e5492bbb3294978c90 100644 (file)
@@ -12,12 +12,12 @@ public class Environment extends AbstractSubSystem {
        };
 
        public Environment() { 
-               super("environment", PROVIDED, new ServiceDescriptor[0]); 
+               super("environment", PROVIDED, new ServiceDescriptor[0]);
        }
        
        @Override
-       protected void doInitialize(String aContext, Service[] aRequiredServices) {
-           addService(aContext, new DefaultService(PROVIDED[0], new Integer(1)));
-           addService(aContext, new DefaultService(PROVIDED[1], new Integer(2)));
+       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)));
        }
 }
index 94bf63c0cb88e8547e31eaf1215d7c822693a9ae..60d1b587007387dc710ef1e56cdfe0b32b8f04b7 100644 (file)
@@ -3,17 +3,26 @@ package org.wamblee.system;
 import junit.framework.TestCase;
 
 public class SystemAssemblerTest extends TestCase {
+       
+       private ServiceRegistry _registry; 
+       
+       @Override
+       protected void setUp() throws Exception {
+               super.setUp();
+               _registry = new DefaultServiceRegistry(); 
+       }
 
        public void testEnvironmentApplication() {
                SubSystem environment = new Environment();
                SubSystem application = new Application();
                SystemAssembler assembler = new SystemAssembler(new SubSystem[] {
                                environment, application }, new ServiceDescriptor[0]);
-               assembler.start(new Service[0]);
+               assembler.start(_registry, new Service[0]);
                Service[] envServices = environment.getRunningServices();
                assertEquals(2, envServices.length);
                Service[] appServices = environment.getRunningServices();
                assertEquals(2, appServices.length);
+               assertEquals(2, _registry.listAllServices().length);
        }
 
        public void testApplicationEnvironment() {
@@ -22,7 +31,7 @@ public class SystemAssemblerTest extends TestCase {
                        SubSystem application = new Application();
                        SystemAssembler assembler = new SystemAssembler(new SubSystem[] {
                                        application, environment }, new ServiceDescriptor[0]);
-                       assembler.start(new Service[0]);
+                       assembler.start(_registry, new Service[0]);
                } catch (SystemAssemblyException e) {
                        // e.printStackTrace();
                        return;
@@ -36,7 +45,7 @@ public class SystemAssemblerTest extends TestCase {
                CompositeSystem system = new CompositeSystem("all", new SubSystem[] {
                                environment, application }, new ServiceDescriptor[0],
                                new ServiceDescriptor[0]);
-               system.initialize("root", new Service[0]);
+               system.start("root", _registry, new Service[0]);
                ServiceDescriptor[] required = system.getRequiredServices();
                assertEquals(0, required.length);
                ServiceDescriptor[] provided = system.getProvidedServices();
@@ -64,7 +73,7 @@ public class SystemAssemblerTest extends TestCase {
                                environment, application }, new ServiceDescriptor[0],
                                new ServiceDescriptor[] { new DefaultServiceDescriptor(
                                                String.class) });
-               system.initialize("root", new Service[0]);
+               system.start("root", _registry, new Service[0]);
                ServiceDescriptor[] required = system.getRequiredServices();
                assertEquals(1, required.length);
                ServiceDescriptor[] provided = system.getProvidedServices();
@@ -78,7 +87,7 @@ public class SystemAssemblerTest extends TestCase {
                        CompositeSystem system = new CompositeSystem("all",
                                        new SubSystem[] { application }, new ServiceDescriptor[0],
                                        application.getRequiredServices());
-                       system.initialize("root", new Service[0]);
+                       system.start("root", _registry, new Service[0]);
                } catch (SystemAssemblyException e) {
                        return;
                }
@@ -93,8 +102,8 @@ public class SystemAssemblerTest extends TestCase {
                CompositeSystem system = new CompositeSystem("all",
                                new SubSystem[] { application }, new ServiceDescriptor[0],
                                application.getRequiredServices());
-               Service[] envServices = environment.initialize("env", new Service[0]);
-               system.initialize("root", envServices);
+               Service[] envServices = environment.start("env", _registry,new Service[0]);
+               system.start("root", _registry, envServices);
                ServiceDescriptor[] required = system.getRequiredServices();
                assertEquals(2, required.length);
                ServiceDescriptor[] provided = system.getProvidedServices();