1 package org.wamblee.system;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collection;
6 import java.util.HashMap;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.wamblee.system.Component.Status;
15 * Abstract subsystem class making it easy to implement new subsystems.
17 public abstract class AbstractComponent implements Component {
19 private static final Log LOG = LogFactory.getLog(AbstractComponent.class);
21 private Status _status;
23 private ServiceRegistry _registry;
24 private List<ProvidedInterface> _provided;
25 private List<RequiredInterface> _required;
26 private Map<ProvidedInterface, Service> _running;
29 * Constructs the subsystem.
32 * Registry of services.
40 protected AbstractComponent(String aName, ServiceRegistry aRegistry, ProvidedInterface[] aProvided,
41 RequiredInterface[] aRequired) {
42 _status = Status.NOT_STARTED;
44 _registry = aRegistry;
45 _provided = new ArrayList<ProvidedInterface>();
46 _provided.addAll(Arrays.asList(aProvided));
47 _required = new ArrayList<RequiredInterface>();
48 _required.addAll(Arrays.asList(aRequired));
49 _running = new HashMap<ProvidedInterface, Service>();
53 public Status getStatus() {
58 public final String getName() {
62 public ServiceRegistry getRegistry() {
67 public final ProvidedInterface[] getProvidedServices() {
68 return _provided.toArray(new ProvidedInterface[0]);
72 public final RequiredInterface[] getRequiredServices() {
73 return _required.toArray(new RequiredInterface[0]);
77 public final Service[] start(String aContext,
78 Service[] aRequiredServices) {
79 LOG.info("Initializing '" + aContext + "." + _name + "' with "
80 + Arrays.asList(aRequiredServices));
81 doStart(aContext + "." + getName(), aRequiredServices);
82 _status = Status.RUNNING;
83 return _running.values().toArray(new Service[0]);
87 * Must be implemented for initializing the subsystem. The implementation
88 * must call {@link #addService(Service)} for each service that is started.
90 * @param aRequiredServices
91 * Services that are already running from other subsystems that
94 protected abstract void doStart(String aContext,
95 Service[] aRequiredServices);
98 * Implementations must call this method to indicate that a new service has
104 protected final void addService(String aContext,
105 ProvidedInterface aDescriptor, Object aService) {
106 LOG.info(aContext + ": service '" + aService + "' started.");
107 Service svc = getRegistry().register(aDescriptor, aService);
108 _running.put(svc.getDescriptor(), svc);
112 public Service[] getRunningServices() {
113 return _running.values().toArray(new Service[0]);
119 for (Service svc: _running.values()) {
120 getRegistry().remove(svc);
122 _status = Status.STOPPED;
125 protected abstract void doStop();
128 public String toString() {