From: Erik Brakkee Date: Mon, 24 Mar 2008 12:55:26 +0000 (+0000) Subject: (no commit message) X-Git-Tag: wamblee-utils-0.7~829 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=4c7e63f11337abfaa6ea13eab4b6ca11891f4977;hp=35b19fe3a3158e865125153d53cd7d106ab2fae4;p=utils --- diff --git a/system/general/src/main/java/org/wamblee/system/AbstractSubSystem.java b/system/general/src/main/java/org/wamblee/system/AbstractSubSystem.java index 0837d5f6..06fd5bb8 100644 --- a/system/general/src/main/java/org/wamblee/system/AbstractSubSystem.java +++ b/system/general/src/main/java/org/wamblee/system/AbstractSubSystem.java @@ -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 _provided; private List _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 diff --git a/system/general/src/main/java/org/wamblee/system/CompositeSystem.java b/system/general/src/main/java/org/wamblee/system/CompositeSystem.java index f3b1bf75..b3abf071 100644 --- a/system/general/src/main/java/org/wamblee/system/CompositeSystem.java +++ b/system/general/src/main/java/org/wamblee/system/CompositeSystem.java @@ -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 descriptors = new ArrayList(); 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) { diff --git a/system/general/src/main/java/org/wamblee/system/DefaultService.java b/system/general/src/main/java/org/wamblee/system/DefaultService.java index fb2128ee..a110dfda 100644 --- a/system/general/src/main/java/org/wamblee/system/DefaultService.java +++ b/system/general/src/main/java/org/wamblee/system/DefaultService.java @@ -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 reference(Class aClass) { - return (T)_service; + return (T) _service; } @Override public String toString() { - return "(" + _descriptor + ", " + _service + ")"; + return "(" + _descriptor + ", " + _service + ")"; } } diff --git a/system/general/src/main/java/org/wamblee/system/DefaultServiceRegistry.java b/system/general/src/main/java/org/wamblee/system/DefaultServiceRegistry.java new file mode 100644 index 00000000..3fe5c71d --- /dev/null +++ b/system/general/src/main/java/org/wamblee/system/DefaultServiceRegistry.java @@ -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 _services; + + public DefaultServiceRegistry() { + _count = 0; + _services = new HashMap(); + } + + @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(_services.values()) + .toArray(new Service[0]); + } +} diff --git a/system/general/src/main/java/org/wamblee/system/Service.java b/system/general/src/main/java/org/wamblee/system/Service.java index 0db48511..61428399 100644 --- a/system/general/src/main/java/org/wamblee/system/Service.java +++ b/system/general/src/main/java/org/wamblee/system/Service.java @@ -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/system/general/src/main/java/org/wamblee/system/ServiceRegistry.java b/system/general/src/main/java/org/wamblee/system/ServiceRegistry.java new file mode 100644 index 00000000..62224dd3 --- /dev/null +++ b/system/general/src/main/java/org/wamblee/system/ServiceRegistry.java @@ -0,0 +1,10 @@ +package org.wamblee.system; + +public interface ServiceRegistry { + + Service register(ServiceDescriptor aDescriptor, Object aService); + + Service find(String aId); + + Service[] listAllServices(); +} diff --git a/system/general/src/main/java/org/wamblee/system/SubSystem.java b/system/general/src/main/java/org/wamblee/system/SubSystem.java index 9d68ee4e..babb9e02 100644 --- a/system/general/src/main/java/org/wamblee/system/SubSystem.java +++ b/system/general/src/main/java/org/wamblee/system/SubSystem.java @@ -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. diff --git a/system/general/src/main/java/org/wamblee/system/SystemAssembler.java b/system/general/src/main/java/org/wamblee/system/SystemAssembler.java index 3bff0564..5b2c0e14 100644 --- a/system/general/src/main/java/org/wamblee/system/SystemAssembler.java +++ b/system/general/src/main/java/org/wamblee/system/SystemAssembler.java @@ -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 allProvided = new HashMap(); @@ -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); diff --git a/system/general/src/test/java/org/wamblee/system/Application.java b/system/general/src/test/java/org/wamblee/system/Application.java index 1a725b3c..77c76cb3 100644 --- a/system/general/src/test/java/org/wamblee/system/Application.java +++ b/system/general/src/test/java/org/wamblee/system/Application.java @@ -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. } diff --git a/system/general/src/test/java/org/wamblee/system/Environment.java b/system/general/src/test/java/org/wamblee/system/Environment.java index bd014c5a..7209c273 100644 --- a/system/general/src/test/java/org/wamblee/system/Environment.java +++ b/system/general/src/test/java/org/wamblee/system/Environment.java @@ -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))); } } diff --git a/system/general/src/test/java/org/wamblee/system/SystemAssemblerTest.java b/system/general/src/test/java/org/wamblee/system/SystemAssemblerTest.java index 94bf63c0..60d1b587 100644 --- a/system/general/src/test/java/org/wamblee/system/SystemAssemblerTest.java +++ b/system/general/src/test/java/org/wamblee/system/SystemAssemblerTest.java @@ -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();