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;
/**
* Constructs the subsystem.
+ * @param aRegistry Registry of services.
* @param aName Name of the system.
* @param aProvided Provided services.
* @param aRequired Required services.
}
@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]);
}
* 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
}
@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) {
* 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 + ")";
}
}
--- /dev/null
+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]);
+ }
+}
* 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.
--- /dev/null
+package org.wamblee.system;
+
+public interface ServiceRegistry {
+
+ Service register(ServiceDescriptor aDescriptor, Object aService);
+
+ Service find(String aId);
+
+ Service[] listAllServices();
+}
* 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.
/**
* 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>();
}
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);
}
@Override
- protected void doInitialize(String aContext, Service[] aRequiredServices) {
+ protected void doStart(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices) {
// Empty, no services provided externally.
}
};
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)));
}
}
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() {
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;
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();
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();
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;
}
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();