1 package org.wamblee.system;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.HashMap;
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
13 * Abstract subsystem class making it easy to implement new
16 public abstract class AbstractSubSystem implements SubSystem {
18 private static final Log LOG = LogFactory.getLog(AbstractSubSystem.class);
21 private List<ServiceDescriptor> _provided;
22 private List<ServiceDescriptor> _required;
23 private Map<ServiceDescriptor, Service> _running;
26 * Constructs the subsystem.
27 * @param aRegistry Registry of services.
28 * @param aName Name of the system.
29 * @param aProvided Provided services.
30 * @param aRequired Required services.
32 protected AbstractSubSystem(String aName, ServiceDescriptor[] aProvided,
33 ServiceDescriptor[] aRequired) {
35 _provided = new ArrayList<ServiceDescriptor>();
36 _provided.addAll(Arrays.asList(aProvided));
37 _required = new ArrayList<ServiceDescriptor>();
38 _required.addAll(Arrays.asList(aRequired));
39 _running = new HashMap<ServiceDescriptor, Service>();
43 public final String getName() {
48 public final ServiceDescriptor[] getProvidedServices() {
49 return _provided.toArray(new ServiceDescriptor[0]);
53 public final ServiceDescriptor[] getRequiredServices() {
54 return _required.toArray(new ServiceDescriptor[0]);
58 public final Service[] start(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices) {
59 LOG.info("Initializing '" + aContext + "." + _name + "' with " + Arrays.asList(aRequiredServices));
60 doStart(aContext + "." + getName(), aRegistry, aRequiredServices);
61 return _running.values().toArray(new Service[0]);
65 * Must be implemented for initializing the subsystem.
66 * The implementation must call {@link #addService(Service)}
67 * for each service that is started.
68 * @param aRegistry Registry to which the subsystem must register its services.
69 * @param aRequiredServices Services that are already running
70 * from other subsystems that may be used.
72 protected abstract void doStart(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices);
75 * Implementations must call this method to indicate that
76 * a new service has been started.
77 * @param aService Service.
79 protected final void addService(String aContext, Service aService) {
80 LOG.info(aContext + ": service '" + aService + "' started.");
81 _running.put(aService.getDescriptor(), aService);
85 public Service[] getRunningServices() {
86 return _running.values().toArray(new Service[0]);
90 public String toString() {