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 aName Name of the system.
28 * @param aProvided Provided services.
29 * @param aRequired Required services.
31 protected AbstractSubSystem(String aName, ServiceDescriptor[] aProvided,
32 ServiceDescriptor[] aRequired) {
34 _provided = new ArrayList<ServiceDescriptor>();
35 _provided.addAll(Arrays.asList(aProvided));
36 _required = new ArrayList<ServiceDescriptor>();
37 _required.addAll(Arrays.asList(aRequired));
38 _running = new HashMap<ServiceDescriptor, Service>();
42 public final String getName() {
47 public final ServiceDescriptor[] getProvidedServices() {
48 return _provided.toArray(new ServiceDescriptor[0]);
52 public final ServiceDescriptor[] getRequiredServices() {
53 return _required.toArray(new ServiceDescriptor[0]);
57 public final Service[] initialize(String aContext, Service[] aRequiredServices) {
58 LOG.info("Initializing '" + aContext + "." + _name + "' with " + Arrays.asList(aRequiredServices));
59 doInitialize(aContext + "." + getName(), aRequiredServices);
60 return _running.values().toArray(new Service[0]);
64 * Must be implemented for initializing the subsystem.
65 * The implementation must call {@link #addService(Service)}
66 * for each service that is started.
67 * @param aRequiredServices Services that are already running
68 * from other subsystems that may be used.
70 protected abstract void doInitialize(String aContext, Service[] aRequiredServices);
73 * Implementations must call this method to indicate that
74 * a new service has been started.
75 * @param aService Service.
77 protected final void addService(String aContext, Service aService) {
78 LOG.info(aContext + ": service '" + aService + "' started.");
79 _running.put(aService.getDescriptor(), aService);
83 public Service[] getRunningServices() {
84 return _running.values().toArray(new Service[0]);
88 public String toString() {