3b4725835155e3cfc558fd370967e3fd4467ebd2
[utils] / system / general / src / main / java / org / wamblee / system / AbstractComponent.java
1 /*
2  * Copyright 2007 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.system;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Set;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 /**
28  * Abstract subsystem class making it easy to implement new subsystems.
29  */
30 public abstract class AbstractComponent implements Component {
31
32         private static final Log LOG = LogFactory.getLog(AbstractComponent.class);
33
34         private Status _status;
35         private String _context; 
36         private String _name; 
37         private List<ProvidedInterface> _provided;
38         private List<RequiredInterface> _required;
39         private Set<ProvidedInterface> _running;
40         
41         /**
42          * Constructs the subsystem.
43          *
44          * @param aName
45          *            Name of the system.
46          * @param aProvided
47          *            Provided services.
48          * @param aRequired
49          *            Required services.
50          */
51         protected AbstractComponent(String aName, ProvidedInterface[] aProvided,
52                         RequiredInterface[] aRequired) {
53                 _status = Status.NOT_STARTED;
54                 _context = null; 
55                 _name = aName;
56                 _provided = new ArrayList<ProvidedInterface>();
57                 _provided.addAll(Arrays.asList(aProvided));
58                 _required = new ArrayList<RequiredInterface>();
59                 _required.addAll(Arrays.asList(aRequired));
60                 _running = new HashSet<ProvidedInterface>();
61         }
62         
63         @Override
64         public Status getStatus() {
65                 return _status; 
66         }
67
68         @Override
69         public final String getName() {
70                 return _name;
71         }
72         
73         @Override
74         public String getQualifiedName() {
75                 if ( _context == null ) { 
76                         return getName(); 
77                 }
78                 return _context + "." + getName(); 
79         }
80
81         @Override
82         public final ProvidedInterface[] getProvidedServices() {
83                 return _provided.toArray(new ProvidedInterface[0]);
84         }
85
86         @Override
87         public final RequiredInterface[] getRequiredServices() {
88                 return _required.toArray(new RequiredInterface[0]);
89         }
90
91         @Override
92         public final void start(String aContext) {
93                 LOG.info("Initializing '" + aContext + "." + _name + "'");
94                 _context = aContext; 
95                 doStart(aContext + "." + getName());
96                 _status = Status.RUNNING;
97                 if ( _running.size() != _provided.size()) { 
98                         List<ProvidedInterface> remaining = 
99                                 new ArrayList<ProvidedInterface>(_provided);
100                         remaining.removeAll(_running);
101                         throw new SystemAssemblyException(aContext + "." + getName() + ": not all services were started, missing " + remaining);
102                 }
103         }
104
105         /**
106          * Must be implemented for initializing the subsystem. The implementation
107          * must call {@link #addService(Service)} for each service that is started.
108          */
109         protected abstract void doStart(String aContext);
110
111         /**
112          * Implementations must call this method to indicate that a new service has
113          * been started.
114          * 
115          * @param aService
116          *            Service.
117          */
118         protected final void addService(String aContext,
119                         ProvidedInterface aDescriptor, Object aService) {
120                 LOG.info(aContext + ": service '" + aService + "' started.");
121                 _running.add(aDescriptor);
122                 aDescriptor.publish(aService);
123         }
124
125         @Override
126         public ProvidedInterface[] getRunningServices() {
127                 return _running.toArray(new ProvidedInterface[0]);
128         }
129         
130         @Override
131         public void stop() {
132                 doStop();       
133                 _status = Status.STOPPED;
134         }
135         
136         protected abstract void doStop(); 
137
138         @Override
139         public String toString() {
140                 return _name;
141         }
142
143 }