Now the component provides read-only access to the interfaces.
[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<Type> implements Component<Type> {
31
32         private static final Log LOG = LogFactory.getLog(AbstractComponent.class);
33
34         private ThreadLocal<List<ProvidedInterface>> _remaining;
35
36         private String _context;
37         private String _name;
38         private ReadWriteProvidedInterfaces _provided;
39         private ReadWriteRequiredInterfaces _required;
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                 _remaining = new ThreadLocal<List<ProvidedInterface>>();
54                 _context = null;
55                 _name = aName;
56                 _provided = new ReadWriteProvidedInterfaces(aProvided);
57                 _required = new ReadWriteRequiredInterfaces(aRequired);
58         }
59         
60         protected AbstractComponent(String aName) {
61                 this(aName, new ProvidedInterface[0], new RequiredInterface[0]);
62         }
63         
64         public AbstractComponent addProvidedInterface(ProvidedInterface aProvided) { 
65                 _provided.add(aProvided);
66                 return this; 
67         }
68         
69         public AbstractComponent addRequiredInterface(RequiredInterface aRequired) { 
70                 _required.add(aRequired);
71                 return this;
72         }
73         
74         @Override
75         public final String getName() {
76                 return _name;
77         }
78
79         @Override
80         public void addContext(String aContext) {
81                 if (_context == null) {
82                         _context = aContext;
83                 } else {
84                         _context = aContext + "." + _context;
85                 }
86         }
87         
88         @Override
89         public String getContext() {
90                 return _context;
91         }
92
93         @Override
94         public String getQualifiedName() {
95                 if (_context == null) {
96                         return getName();
97                 }
98                 return _context + "." + getName();
99         }
100
101         @Override
102         public final ProvidedInterfaces getProvidedInterfaces() {
103                 return _provided.readOnlyView();
104         }
105
106         @Override
107         public final RequiredInterfaces getRequiredInterfaces() {
108                 return _required.readOnlyView();
109         }
110
111         @Override
112         public final Type start(Scope aScope) {
113                 LOG.info("Initialization starting '" + getQualifiedName() + "'");
114                 List<ProvidedInterface> oldRemaining = _remaining.get();
115                 _remaining.set(new ArrayList<ProvidedInterface>(Arrays.asList(getProvidedInterfaces().toArray())));
116                 try {
117                         Type runtime = doStart(aScope);
118                         checkNotStartedInterfaces();
119                         LOG.info("Initialization finished '" + getQualifiedName() + "'");
120                         return runtime;
121                 } finally {
122                         _remaining.set(oldRemaining);
123                 }
124         }
125
126         private void checkNotStartedInterfaces() {
127                 if (_remaining.get().size() > 0) {
128                         String notProvided = "";
129                         for (ProvidedInterface provided : _remaining.get()) {
130                                 notProvided += "\nComponent " + getQualifiedName()
131                                                 + " did not start interface " + provided;
132                         }
133                         throw new SystemAssemblyException(notProvided);
134                 }
135         }
136
137         /**
138          * Must be implemented for initializing the subsystem. The implementation
139          * must call {@link #addService(Service)} for each service that is started.
140          * 
141          * @return Returns the runtime of the component.
142          */
143         protected abstract Type doStart(Scope aScope);
144
145         /**
146          * Implementations must call this method to indicate that a new service has
147          * been started.
148          * 
149          * @param aDescriptor
150          *            Provided interface.
151          * @param aService
152          *            Implementation of the interface.
153          * @param aScope
154          *            scope in which to publish the implementation.
155          */
156         protected final void addInterface(ProvidedInterface aDescriptor,
157                         Object aService, Scope aScope) {
158                 LOG.info("Interface '" + getQualifiedName() + "."
159                                 + aDescriptor.getName() + "' started.");
160                 if ( !_remaining.get().remove(aDescriptor) ) { 
161                     throw new SystemAssemblyException("Component '" + getQualifiedName() + "' started an unexpected interface '" + 
162                             aDescriptor + "' that was not registerd as a provided interface before");
163                 }
164                 aScope.publishInterface(aDescriptor, aService);
165         }
166
167         @Override
168         public void stop(Type aRuntime) {
169             LOG.info("Stopping initiated '" + getQualifiedName() + "'");
170                 doStop(aRuntime);
171                 LOG.info("Stopping completed '" + getQualifiedName() + "'");
172         }
173
174         protected abstract void doStop(Type aRuntime);
175
176         @Override
177         public String toString() {
178                 return getQualifiedName();
179         }
180         
181         public ProvidedInterface findProvidedInterface(String aName) { 
182             for (ProvidedInterface provided: getProvidedInterfaces()) { 
183                 if ( provided.getName().equals(aName)) { 
184                     return provided; 
185                 }
186             }
187             return null; 
188         }
189         
190         public RequiredInterface findRequiredInterface(String aName) { 
191             for (RequiredInterface required: getRequiredInterfaces()) { 
192                 if ( required.getName().equals(aName)) { 
193                     return required; 
194                 }
195             }
196             return null; 
197         }
198
199 }