added databasecomponent and now using Derby in the unit tests.
[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         public AbstractComponent addProvidedInterface(ProvidedInterface aProvided) { 
67                 _provided.add(aProvided);
68                 return this; 
69         }
70         
71         public 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         @Override
91         public String getContext() {
92                 return _context;
93         }
94
95         @Override
96         public String getQualifiedName() {
97                 if (_context == null) {
98                         return getName();
99                 }
100                 return _context + "." + getName();
101         }
102
103         @Override
104         public final ProvidedInterface[] getProvidedInterfaces() {
105                 return _provided.toArray(new ProvidedInterface[0]);
106         }
107
108         @Override
109         public final RequiredInterface[] getRequiredInterfaces() {
110                 return _required.toArray(new RequiredInterface[0]);
111         }
112
113         @Override
114         public final Type start(Scope aScope) {
115                 LOG.info("Initialization starting '" + getQualifiedName() + "'");
116                 List<ProvidedInterface> oldRemaining = _remaining.get();
117                 _remaining.set(new ArrayList<ProvidedInterface>(Arrays.asList(getProvidedInterfaces())));
118                 try {
119                         Type runtime = doStart(aScope);
120                         checkNotStartedInterfaces();
121                         LOG.info("Initialization finished '" + getQualifiedName() + "'");
122                         return runtime;
123                 } finally {
124                         _remaining.set(oldRemaining);
125                 }
126         }
127
128         private void checkNotStartedInterfaces() {
129                 if (_remaining.get().size() > 0) {
130                         String notProvided = "";
131                         for (ProvidedInterface provided : _remaining.get()) {
132                                 notProvided += "\nComponent " + getQualifiedName()
133                                                 + " did not start interface " + provided;
134                         }
135                         throw new SystemAssemblyException(notProvided);
136                 }
137         }
138
139         /**
140          * Must be implemented for initializing the subsystem. The implementation
141          * must call {@link #addService(Service)} for each service that is started.
142          * 
143          * @return Returns the runtime of the component.
144          */
145         protected abstract Type doStart(Scope aScope);
146
147         /**
148          * Implementations must call this method to indicate that a new service has
149          * been started.
150          * 
151          * @param aDescriptor
152          *            Provided interface.
153          * @param aService
154          *            Implementation of the interface.
155          * @param aScope
156          *            scope in which to publish the implementation.
157          */
158         protected final void addInterface(ProvidedInterface aDescriptor,
159                         Object aService, Scope aScope) {
160                 LOG.info("Interface '" + getQualifiedName() + "."
161                                 + aDescriptor.getName() + "' started.");
162                 if ( !_remaining.get().remove(aDescriptor) ) { 
163                     throw new SystemAssemblyException("Component '" + getQualifiedName() + "' started an unexpected interface '" + 
164                             aDescriptor + "' that was not registerd as a provided interface before");
165                 }
166                 aScope.publishInterface(aDescriptor, aService);
167         }
168
169         @Override
170         public void stop(Type aRuntime) {
171             LOG.info("Stopping initiated '" + getQualifiedName() + "'");
172                 doStop(aRuntime);
173                 LOG.info("Stopping completed '" + getQualifiedName() + "'");
174         }
175
176         protected abstract void doStop(Type aRuntime);
177
178         @Override
179         public String toString() {
180                 return getQualifiedName();
181         }
182
183 }