(no commit message)
[utils] / system / general / src / main / java / org / wamblee / system / AbstractComponent.java
1 package org.wamblee.system;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Set;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 /**
13  * Abstract subsystem class making it easy to implement new subsystems.
14  */
15 public abstract class AbstractComponent implements Component {
16
17         private static final Log LOG = LogFactory.getLog(AbstractComponent.class);
18
19         private Status _status; 
20         private String _name; 
21         private List<ProvidedInterface> _provided;
22         private List<RequiredInterface> _required;
23         private Set<ProvidedInterface> _running;
24         
25         /**
26          * Constructs the subsystem.
27          *
28          * @param aName
29          *            Name of the system.
30          * @param aProvided
31          *            Provided services.
32          * @param aRequired
33          *            Required services.
34          */
35         protected AbstractComponent(String aName, ProvidedInterface[] aProvided,
36                         RequiredInterface[] aRequired) {
37                 _status = Status.NOT_STARTED;
38                 _name = aName;
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>();
44         }
45         
46         @Override
47         public Status getStatus() {
48                 return _status; 
49         }
50
51         @Override
52         public final String getName() {
53                 return _name;
54         }
55
56         @Override
57         public final ProvidedInterface[] getProvidedServices() {
58                 return _provided.toArray(new ProvidedInterface[0]);
59         }
60
61         @Override
62         public final RequiredInterface[] getRequiredServices() {
63                 return _required.toArray(new RequiredInterface[0]);
64         }
65
66         @Override
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);
76                 }
77         }
78
79         /**
80          * Must be implemented for initializing the subsystem. The implementation
81          * must call {@link #addService(Service)} for each service that is started.
82          */
83         protected abstract void doStart(String aContext);
84
85         /**
86          * Implementations must call this method to indicate that a new service has
87          * been started.
88          * 
89          * @param aService
90          *            Service.
91          */
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);
97         }
98
99         @Override
100         public ProvidedInterface[] getRunningServices() {
101                 return _running.toArray(new ProvidedInterface[0]);
102         }
103         
104         @Override
105         public void stop() {
106                 doStop();       
107                 _status = Status.STOPPED;
108         }
109         
110         protected abstract void doStop(); 
111
112         @Override
113         public String toString() {
114                 return _name;
115         }
116
117 }