some major refactoring.
[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         }
72
73         /**
74          * Must be implemented for initializing the subsystem. The implementation
75          * must call {@link #addService(Service)} for each service that is started.
76          */
77         protected abstract void doStart(String aContext);
78
79         /**
80          * Implementations must call this method to indicate that a new service has
81          * been started.
82          * 
83          * @param aService
84          *            Service.
85          */
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);
91         }
92
93         @Override
94         public ProvidedInterface[] getRunningServices() {
95                 return _running.toArray(new ProvidedInterface[0]);
96         }
97         
98         @Override
99         public void stop() {
100                 doStop();       
101                 _status = Status.STOPPED;
102         }
103         
104         protected abstract void doStop(); 
105
106         @Override
107         public String toString() {
108                 return _name;
109         }
110
111 }