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;
14 * Abstract subsystem class making it easy to implement new subsystems.
16 public abstract class AbstractComponent implements Component {
18 private static final Log LOG = LogFactory.getLog(AbstractComponent.class);
21 private ServiceRegistry _registry;
22 private List<InterfaceDescriptor> _provided;
23 private List<InterfaceDescriptor> _required;
24 private Map<InterfaceDescriptor, Service> _running;
27 * Constructs the subsystem.
30 * Registry of services.
38 protected AbstractComponent(String aName, ServiceRegistry aRegistry, InterfaceDescriptor[] aProvided,
39 InterfaceDescriptor[] aRequired) {
41 _registry = aRegistry;
42 _provided = new ArrayList<InterfaceDescriptor>();
43 _provided.addAll(Arrays.asList(aProvided));
44 _required = new ArrayList<InterfaceDescriptor>();
45 _required.addAll(Arrays.asList(aRequired));
46 _running = new HashMap<InterfaceDescriptor, Service>();
50 public final String getName() {
54 public ServiceRegistry getRegistry() {
59 public final ProvidedInterfaceDescriptor[] getProvidedServices() {
60 return _provided.toArray(new ProvidedInterfaceDescriptor[0]);
64 public final RequiredInterfaceDescriptor[] getRequiredServices() {
65 return _required.toArray(new RequiredInterfaceDescriptor[0]);
69 public final Service[] start(String aContext,
70 Service[] aRequiredServices) {
71 LOG.info("Initializing '" + aContext + "." + _name + "' with "
72 + Arrays.asList(aRequiredServices));
73 doStart(aContext + "." + getName(), aRequiredServices);
74 return _running.values().toArray(new Service[0]);
78 * Must be implemented for initializing the subsystem. The implementation
79 * must call {@link #addService(Service)} for each service that is started.
81 * @param aRequiredServices
82 * Services that are already running from other subsystems that
85 protected abstract void doStart(String aContext,
86 Service[] aRequiredServices);
89 * Implementations must call this method to indicate that a new service has
95 protected final void addService(String aContext,
96 ProvidedInterfaceDescriptor aDescriptor, Object aService) {
97 LOG.info(aContext + ": service '" + aService + "' started.");
98 Service svc = getRegistry().register(aDescriptor, aService);
99 _running.put(svc.getDescriptor(), svc);
103 public Service[] getRunningServices() {
104 return _running.values().toArray(new Service[0]);
110 for (Service svc: _running.values()) {
111 getRegistry().remove(svc);
115 protected abstract void doStop();
118 public String toString() {