now allowing components and interfaces to be added after construction.
[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 List<ProvidedInterface> _provided;
39         private List<RequiredInterface> _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 ArrayList<ProvidedInterface>();
57                 _provided.addAll(Arrays.asList(aProvided));
58                 _required = new ArrayList<RequiredInterface>();
59                 _required.addAll(Arrays.asList(aRequired));
60         }
61         
62         protected AbstractComponent(String aName) {
63                 this(aName, new ProvidedInterface[0], new RequiredInterface[0]);
64         }
65         
66         protected AbstractComponent addProvidedInterface(ProvidedInterface aProvided) { 
67                 _provided.add(aProvided);
68                 return this; 
69         }
70         
71         protected AbstractComponent addRequiredInterface(RequiredInterface aRequired) { 
72                 _required.add(aRequired);
73                 return this;
74         }
75         
76         @Override
77         public final String getName() {
78                 return _name;
79         }
80
81         @Override
82         public void addContext(String aContext) {
83                 if (_context == null) {
84                         _context = aContext;
85                 } else {
86                         _context = aContext + "." + _context;
87                 }
88         }
89         
90         public String getContext() {
91                 return _context;
92         }
93
94         @Override
95         public String getQualifiedName() {
96                 if (_context == null) {
97                         return getName();
98                 }
99                 return _context + "." + getName();
100         }
101
102         @Override
103         public final ProvidedInterface[] getProvidedInterfaces() {
104                 return _provided.toArray(new ProvidedInterface[0]);
105         }
106
107         @Override
108         public final RequiredInterface[] getRequiredInterfaces() {
109                 return _required.toArray(new RequiredInterface[0]);
110         }
111
112         @Override
113         public final Type start(Scope aScope) {
114                 LOG.info("Initializing '" + getQualifiedName() + "'");
115                 List<ProvidedInterface> oldRemaining = _remaining.get();
116                 _remaining.set(new ArrayList<ProvidedInterface>(Arrays.asList(getProvidedInterfaces())));
117                 try {
118                         Type runtime = doStart(aScope);
119                         checkNotStartedInterfaces();
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                 _remaining.get().remove(aDescriptor);
161                 aScope.publishInterface(aDescriptor, aService);
162         }
163
164         @Override
165         public void stop(Type aRuntime) {
166                 doStop(aRuntime);
167         }
168
169         protected abstract void doStop(Type aRuntime);
170
171         @Override
172         public String toString() {
173                 return getQualifiedName();
174         }
175
176 }