2 * Copyright 2007 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.system.core;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.List;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
27 * Abstract subsystem class making it easy to implement new subsystems.
29 public abstract class AbstractComponent<Type> implements Component<Type> {
31 private static final Log LOG = LogFactory.getLog(AbstractComponent.class);
33 private ThreadLocal<List<ProvidedInterface>> _remaining;
35 private String _context;
37 private List<ProvidedInterface> _provided;
38 private List<RequiredInterface> _required;
41 * Constructs the subsystem.
50 protected AbstractComponent(String aName, List<ProvidedInterface> aProvided,
51 List<RequiredInterface> aRequired) {
52 _remaining = new ThreadLocal<List<ProvidedInterface>>();
55 _provided = new ArrayList<ProvidedInterface>(aProvided);
56 _required = new ArrayList<RequiredInterface>(aRequired);
60 * Constructs the subsystem.
69 protected AbstractComponent(String aName, ProvidedInterface[] aProvided,
70 RequiredInterface[] aRequired) {
71 this(aName, Arrays.asList(aProvided), Arrays.asList(aRequired));
74 protected AbstractComponent(String aName) {
75 this(aName, new ProvidedInterface[0], new RequiredInterface[0]);
78 public AbstractComponent<Type> addProvidedInterface(ProvidedInterface aProvided) {
79 _provided.add(aProvided);
83 public AbstractComponent<Type> addRequiredInterface(RequiredInterface aRequired) {
84 _required.add(aRequired);
89 public final String getName() {
94 public void addContext(String aContext) {
95 if (_context == null) {
98 _context = aContext + "." + _context;
103 public String getContext() {
108 public String getQualifiedName() {
109 if (_context == null) {
112 return _context + "." + getName();
116 public final List<ProvidedInterface> getProvidedInterfaces() {
117 return Collections.unmodifiableList(_provided);
121 public final List<RequiredInterface> getRequiredInterfaces() {
122 return Collections.unmodifiableList(_required);
126 public final Type start(Scope aScope) {
127 LOG.info("Initialization starting '" + getQualifiedName() + "'");
128 List<ProvidedInterface> oldRemaining = _remaining.get();
129 _remaining.set(new ArrayList<ProvidedInterface>(getProvidedInterfaces()));
131 Type runtime = doStart(aScope);
132 checkNotStartedInterfaces();
133 LOG.info("Initialization finished '" + getQualifiedName() + "'");
136 _remaining.set(oldRemaining);
140 private void checkNotStartedInterfaces() {
141 if (_remaining.get().size() > 0) {
142 String notProvided = "";
143 for (ProvidedInterface provided : _remaining.get()) {
144 notProvided += "\nComponent " + getQualifiedName()
145 + " did not start interface " + provided;
147 throw new SystemAssemblyException(notProvided);
152 * Must be implemented for initializing the subsystem. The implementation
153 * must call {@link #addInterface(ProvidedInterface, Object, Scope)} for each service that is started.
155 * @return Returns the runtime of the component.
157 protected abstract Type doStart(Scope aScope);
160 * Implementations must call this method to indicate that a new service has
164 * Provided interface.
166 * Implementation of the interface.
168 * scope in which to publish the implementation.
170 protected final void addInterface(ProvidedInterface aDescriptor,
171 Object aService, Scope aScope) {
172 LOG.info("Interface '" + getQualifiedName() + "."
173 + aDescriptor.getName() + "' started.");
174 if ( !_remaining.get().remove(aDescriptor) ) {
175 throw new SystemAssemblyException("Component '" + getQualifiedName() + "' started an unexpected interface '" +
176 aDescriptor + "' that was not registerd as a provided interface before");
178 aScope.publishInterface(aDescriptor, aService);
182 public void stop(Type aRuntime) {
183 LOG.info("Stopping initiated '" + getQualifiedName() + "'");
185 LOG.info("Stopping completed '" + getQualifiedName() + "'");
188 protected abstract void doStop(Type aRuntime);
191 public String toString() {
192 return getQualifiedName();
195 public ProvidedInterface findProvidedInterface(String aName) {
196 for (ProvidedInterface provided: getProvidedInterfaces()) {
197 if ( provided.getName().equals(aName)) {
204 public RequiredInterface findRequiredInterface(String aName) {
205 for (RequiredInterface required: getRequiredInterfaces()) {
206 if ( required.getName().equals(aName)) {