1 package org.wamblee.system;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.HashSet;
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
13 * Abstract subsystem class making it easy to implement new subsystems.
15 public abstract class AbstractComponent implements Component {
17 private static final Log LOG = LogFactory.getLog(AbstractComponent.class);
19 private Status _status;
21 private List<ProvidedInterface> _provided;
22 private List<RequiredInterface> _required;
23 private Set<ProvidedInterface> _running;
26 * Constructs the subsystem.
35 protected AbstractComponent(String aName, ProvidedInterface[] aProvided,
36 RequiredInterface[] aRequired) {
37 _status = Status.NOT_STARTED;
39 _provided = new ArrayList<ProvidedInterface>();
40 _provided.addAll(Arrays.asList(aProvided));
41 _required = new ArrayList<RequiredInterface>();
42 _required.addAll(Arrays.asList(aRequired));
43 _running = new HashSet<ProvidedInterface>();
47 public Status getStatus() {
52 public final String getName() {
57 public final ProvidedInterface[] getProvidedServices() {
58 return _provided.toArray(new ProvidedInterface[0]);
62 public final RequiredInterface[] getRequiredServices() {
63 return _required.toArray(new RequiredInterface[0]);
67 public final void start(String aContext) {
68 LOG.info("Initializing '" + aContext + "." + _name + "'");
69 doStart(aContext + "." + getName());
70 _status = Status.RUNNING;
74 * Must be implemented for initializing the subsystem. The implementation
75 * must call {@link #addService(Service)} for each service that is started.
77 protected abstract void doStart(String aContext);
80 * Implementations must call this method to indicate that a new service has
86 protected final void addService(String aContext,
87 ProvidedInterface aDescriptor, Object aService) {
88 LOG.info(aContext + ": service '" + aService + "' started.");
89 _running.add(aDescriptor);
90 aDescriptor.publish(aService);
94 public ProvidedInterface[] getRunningServices() {
95 return _running.toArray(new ProvidedInterface[0]);
101 _status = Status.STOPPED;
104 protected abstract void doStop();
107 public String toString() {