rename service to interface.
[utils] / system / general / src / main / java / org / wamblee / system / core / 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.core;
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 void addContext(String aContext) {
75                 if (_context == null ) { 
76                         _context = aContext; 
77                 }
78                 else { 
79                         _context = aContext + "." + _context;
80                 }
81         }
82         
83         @Override
84         public String getQualifiedName() {
85                 if ( _context == null ) { 
86                         return getName(); 
87                 }
88                 return _context + "." + getName(); 
89         }
90
91         @Override
92         public final ProvidedInterface[] getProvidedInterfaces() {
93                 return _provided.toArray(new ProvidedInterface[0]);
94         }
95
96         @Override
97         public final RequiredInterface[] getRequiredInterfaces() {
98                 return _required.toArray(new RequiredInterface[0]);
99         }
100
101         @Override
102         public final void start() {
103                 LOG.info("Initializing '" + getQualifiedName() + "'");
104                 doStart();
105                 _status = Status.RUNNING;
106                 if ( _running.size() != _provided.size()) { 
107                         List<ProvidedInterface> remaining = 
108                                 new ArrayList<ProvidedInterface>(_provided);
109                         remaining.removeAll(_running);
110                         throw new SystemAssemblyException(getQualifiedName() + ": not all services were started, missing " + remaining);
111                 }
112         }
113
114         /**
115          * Must be implemented for initializing the subsystem. The implementation
116          * must call {@link #addService(Service)} for each service that is started.
117          */
118         protected abstract void doStart();
119
120         /**
121          * Implementations must call this method to indicate that a new service has
122          * been started.
123          * 
124          * @param aService
125          *            Service.
126          */
127         protected final void addService(
128                         ProvidedInterface aDescriptor, Object aService) {
129                 LOG.info("Interface '" + getQualifiedName() + "." + aDescriptor.getName() + "' started.");
130                 _running.add(aDescriptor);
131                 aDescriptor.publish(aService);
132         }
133
134         @Override
135         public ProvidedInterface[] getRunningServices() {
136                 return _running.toArray(new ProvidedInterface[0]);
137         }
138         
139         @Override
140         public void stop() {
141                 doStop();       
142                 _status = Status.STOPPED;
143         }
144         
145         protected abstract void doStop(); 
146
147         @Override
148         public String toString() {
149                 return _name;
150         }
151
152 }