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 List<ProvidedInterface> _provided;
39 private List<RequiredInterface> _required;
42 * Constructs the subsystem.
51 protected AbstractComponent(String aName, ProvidedInterface[] aProvided,
52 RequiredInterface[] aRequired) {
53 _remaining = new ThreadLocal<List<ProvidedInterface>>();
56 _provided = new ArrayList<ProvidedInterface>();
57 _provided.addAll(Arrays.asList(aProvided));
58 _required = new ArrayList<RequiredInterface>();
59 _required.addAll(Arrays.asList(aRequired));
63 public final String getName() {
68 public void addContext(String aContext) {
69 if (_context == null) {
72 _context = aContext + "." + _context;
77 public String getQualifiedName() {
78 if (_context == null) {
81 return _context + "." + getName();
85 public final ProvidedInterface[] getProvidedInterfaces() {
86 return _provided.toArray(new ProvidedInterface[0]);
90 public final RequiredInterface[] getRequiredInterfaces() {
91 return _required.toArray(new RequiredInterface[0]);
95 public final Type start(Scope aScope) {
96 LOG.info("Initializing '" + getQualifiedName() + "'");
97 List<ProvidedInterface> oldRemaining = _remaining.get();
98 _remaining.set(new ArrayList<ProvidedInterface>(Arrays.asList(getProvidedInterfaces())));
100 Type runtime = doStart(aScope);
101 checkNotStartedInterfaces();
104 _remaining.set(oldRemaining);
108 private void checkNotStartedInterfaces() {
109 if (_remaining.get().size() > 0) {
110 String notProvided = "";
111 for (ProvidedInterface provided : _remaining.get()) {
112 notProvided += "\nComponent " + getQualifiedName()
113 + " did not start interface " + provided;
115 throw new SystemAssemblyException(notProvided);
120 * Must be implemented for initializing the subsystem. The implementation
121 * must call {@link #addService(Service)} for each service that is started.
123 * @return Returns the runtime of the component.
125 protected abstract Type doStart(Scope aScope);
128 * Implementations must call this method to indicate that a new service has
132 * Provided interface.
134 * Implementation of the interface.
136 * scope in which to publish the implementation.
138 protected final void addInterface(ProvidedInterface aDescriptor,
139 Object aService, Scope aScope) {
140 LOG.info("Interface '" + getQualifiedName() + "."
141 + aDescriptor.getName() + "' started.");
142 _remaining.get().remove(aDescriptor);
143 aScope.publishInterface(aDescriptor, aService);
147 public void stop(Type aRuntime) {
151 protected abstract void doStop(Type aRuntime);
154 public String toString() {
155 return getQualifiedName();