added default container to support classes directly.
[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("Initializing '" + 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                         return runtime;
122                 } finally {
123                         _remaining.set(oldRemaining);
124                 }
125         }
126
127         private void checkNotStartedInterfaces() {
128                 if (_remaining.get().size() > 0) {
129                         String notProvided = "";
130                         for (ProvidedInterface provided : _remaining.get()) {
131                                 notProvided += "\nComponent " + getQualifiedName()
132                                                 + " did not start interface " + provided;
133                         }
134                         throw new SystemAssemblyException(notProvided);
135                 }
136         }
137
138         /**
139          * Must be implemented for initializing the subsystem. The implementation
140          * must call {@link #addService(Service)} for each service that is started.
141          * 
142          * @return Returns the runtime of the component.
143          */
144         protected abstract Type doStart(Scope aScope);
145
146         /**
147          * Implementations must call this method to indicate that a new service has
148          * been started.
149          * 
150          * @param aDescriptor
151          *            Provided interface.
152          * @param aService
153          *            Implementation of the interface.
154          * @param aScope
155          *            scope in which to publish the implementation.
156          */
157         protected final void addInterface(ProvidedInterface aDescriptor,
158                         Object aService, Scope aScope) {
159                 LOG.info("Interface '" + getQualifiedName() + "."
160                                 + aDescriptor.getName() + "' started.");
161                 _remaining.get().remove(aDescriptor);
162                 aScope.publishInterface(aDescriptor, aService);
163         }
164
165         @Override
166         public void stop(Type aRuntime) {
167                 doStop(aRuntime);
168         }
169
170         protected abstract void doStop(Type aRuntime);
171
172         @Override
173         public String toString() {
174                 return getQualifiedName();
175         }
176
177 }