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;
71 if ( _running.size() != _provided.size()) {
72 List<ProvidedInterface> remaining =
73 new ArrayList<ProvidedInterface>(_provided);
74 remaining.removeAll(_running);
75 throw new SystemAssemblyException(aContext + "." + getName() + ": not all services were started, missing " + remaining);
80 * Must be implemented for initializing the subsystem. The implementation
81 * must call {@link #addService(Service)} for each service that is started.
83 protected abstract void doStart(String aContext);
86 * Implementations must call this method to indicate that a new service has
92 protected final void addService(String aContext,
93 ProvidedInterface aDescriptor, Object aService) {
94 LOG.info(aContext + ": service '" + aService + "' started.");
95 _running.add(aDescriptor);
96 aDescriptor.publish(aService);
100 public ProvidedInterface[] getRunningServices() {
101 return _running.toArray(new ProvidedInterface[0]);
107 _status = Status.STOPPED;
110 protected abstract void doStop();
113 public String toString() {