import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.wamblee.system.Component.Status;
/**
* Abstract subsystem class making it easy to implement new subsystems.
private static final Log LOG = LogFactory.getLog(AbstractComponent.class);
+ private Status _status;
private String _name;
private ServiceRegistry _registry;
private List<InterfaceDescriptor> _provided;
*/
protected AbstractComponent(String aName, ServiceRegistry aRegistry, InterfaceDescriptor[] aProvided,
InterfaceDescriptor[] aRequired) {
+ _status = Status.NOT_STARTED;
_name = aName;
_registry = aRegistry;
_provided = new ArrayList<InterfaceDescriptor>();
_required.addAll(Arrays.asList(aRequired));
_running = new HashMap<InterfaceDescriptor, Service>();
}
+
+ @Override
+ public Status getStatus() {
+ return _status;
+ }
@Override
public final String getName() {
LOG.info("Initializing '" + aContext + "." + _name + "' with "
+ Arrays.asList(aRequiredServices));
doStart(aContext + "." + getName(), aRequiredServices);
+ _status = Status.RUNNING;
return _running.values().toArray(new Service[0]);
}
for (Service svc: _running.values()) {
getRegistry().remove(svc);
}
+ _status = Status.STOPPED;
}
protected abstract void doStop();
*
* @author Erik Brakkee
*/
-public class CompositeSystem extends AbstractComponent {
+public class CompositeComponent extends AbstractComponent {
- private static final Log LOG = LogFactory.getLog(CompositeSystem.class);
+ private static final Log LOG = LogFactory.getLog(CompositeComponent.class);
private Component[] _systems;
* @param aProvided Provided services of the system.
* @param aRequired Required services by the system.
*/
- public CompositeSystem(String aName, ServiceRegistry aRegistry, Component[] aSystems,
+ public CompositeComponent(String aName, ServiceRegistry aRegistry, Component[] aSystems,
InterfaceDescriptor[] aProvided, InterfaceDescriptor[] aRequired) {
super(aName, aRegistry, aProvided, aRequired);
_systems = aSystems;
@Override
protected void doStop() {
- // Empty.
+ for (int i = _systems.length-1; i >= 0; i--) {
+ _systems[i].stop();
+ }
}
private void info(String aMsg) {
import java.util.ArrayList;
import java.util.Arrays;
+import org.wamblee.system.Component.Status;
import org.wamblee.test.AssertionUtils;
import junit.framework.TestCase;
public void testComposite() {
Component environment = new Environment(_registry);
Component application = new Application(_registry);
- CompositeSystem system = new CompositeSystem("all", _registry,
+ assertEquals(Status.NOT_STARTED, environment.getStatus());
+ assertEquals(Status.NOT_STARTED, application.getStatus());
+ CompositeComponent system = new CompositeComponent("all", _registry,
new Component[] { environment, application },
new InterfaceDescriptor[0], new InterfaceDescriptor[0]);
+ assertEquals(Status.NOT_STARTED, system.getStatus());
system.start("root", new Service[0]);
InterfaceDescriptor[] required = system.getRequiredServices();
assertEquals(0, required.length);
InterfaceDescriptor[] provided = system.getProvidedServices();
assertEquals(0, provided.length);
+ assertEquals(Status.RUNNING, environment.getStatus());
+ assertEquals(Status.RUNNING, application.getStatus());
+ assertEquals(Status.RUNNING, system.getStatus());
+
+ system.stop();
+ assertEquals(Status.STOPPED, environment.getStatus());
+ assertEquals(Status.STOPPED, application.getStatus());
+ assertEquals(Status.STOPPED, system.getStatus());
}
public void testCompositeWithWrongProvidedInfo() {
try {
Component environment = new Environment(_registry);
Component application = new Application(_registry);
- CompositeSystem system = new CompositeSystem(
+ CompositeComponent system = new CompositeComponent(
"all",
_registry,
new Component[] { environment, application },
public void testCompositeWithSuperfluousRequiredInfo() {
Component environment = new Environment(_registry);
Component application = new Application(_registry);
- CompositeSystem system = new CompositeSystem("all", _registry,
+ CompositeComponent system = new CompositeComponent("all", _registry,
new Component[] { environment, application },
new InterfaceDescriptor[0],
new InterfaceDescriptor[] { new DefaultRequiredInterfaceDescriptor(
try {
Component environment = new Environment(_registry);
Component application = new Application(_registry);
- CompositeSystem system = new CompositeSystem("all", _registry,
+ CompositeComponent system = new CompositeComponent("all", _registry,
new Component[] { application }, new InterfaceDescriptor[0],
application.getRequiredServices());
system.start("root", new Service[0]);
Component environment = new Environment(_registry);
Component application = new Application(_registry);
- CompositeSystem system = new CompositeSystem("all", _registry,
+ CompositeComponent system = new CompositeComponent("all", _registry,
new Component[] { application }, new InterfaceDescriptor[0],
application.getRequiredServices());
Service[] envServices = environment.start("env", new Service[0]);