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.HashSet;
21 import java.util.List;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
28 * Abstract subsystem class making it easy to implement new subsystems.
30 public abstract class AbstractComponent<Type> implements Component<Type> {
32 private static final Log LOG = LogFactory.getLog(AbstractComponent.class);
34 private ThreadLocal<List<ProvidedInterface>> _remaining;
36 private String _context;
38 private ReadWriteProvidedInterfaces _provided;
39 private ReadWriteRequiredInterfaces _required;
42 * Constructs the subsystem.
51 protected AbstractComponent(String aName, ProvidedInterface[] aProvided,
52 RequiredInterface[] aRequired) {
53 _remaining = new ThreadLocal<List<ProvidedInterface>>();
56 _provided = new ReadWriteProvidedInterfaces(aProvided);
57 _required = new ReadWriteRequiredInterfaces(aRequired);
60 protected AbstractComponent(String aName) {
61 this(aName, new ProvidedInterface[0], new RequiredInterface[0]);
64 public AbstractComponent addProvidedInterface(ProvidedInterface aProvided) {
65 _provided.add(aProvided);
69 public AbstractComponent addRequiredInterface(RequiredInterface aRequired) {
70 _required.add(aRequired);
75 public final String getName() {
80 public void addContext(String aContext) {
81 if (_context == null) {
84 _context = aContext + "." + _context;
89 public String getContext() {
94 public String getQualifiedName() {
95 if (_context == null) {
98 return _context + "." + getName();
102 public final ProvidedInterfaces getProvidedInterfaces() {
103 return _provided.readOnlyView();
107 public final RequiredInterfaces getRequiredInterfaces() {
108 return _required.readOnlyView();
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())));
117 Type runtime = doStart(aScope);
118 checkNotStartedInterfaces();
119 LOG.info("Initialization finished '" + getQualifiedName() + "'");
122 _remaining.set(oldRemaining);
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;
133 throw new SystemAssemblyException(notProvided);
138 * Must be implemented for initializing the subsystem. The implementation
139 * must call {@link #addService(Service)} for each service that is started.
141 * @return Returns the runtime of the component.
143 protected abstract Type doStart(Scope aScope);
146 * Implementations must call this method to indicate that a new service has
150 * Provided interface.
152 * Implementation of the interface.
154 * scope in which to publish the implementation.
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");
164 aScope.publishInterface(aDescriptor, aService);
168 public void stop(Type aRuntime) {
169 LOG.info("Stopping initiated '" + getQualifiedName() + "'");
171 LOG.info("Stopping completed '" + getQualifiedName() + "'");
174 protected abstract void doStop(Type aRuntime);
177 public String toString() {
178 return getQualifiedName();
181 public ProvidedInterface findProvidedInterface(String aName) {
182 for (ProvidedInterface provided: getProvidedInterfaces()) {
183 if ( provided.getName().equals(aName)) {
190 public RequiredInterface findRequiredInterface(String aName) {
191 for (RequiredInterface required: getRequiredInterfaces()) {
192 if ( required.getName().equals(aName)) {