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 implements Component {
32 private static final Log LOG = LogFactory.getLog(AbstractComponent.class);
34 private Status _status;
35 private String _context;
37 private List<ProvidedInterface> _provided;
38 private List<RequiredInterface> _required;
39 private Set<ProvidedInterface> _running;
42 * Constructs the subsystem.
51 protected AbstractComponent(String aName, ProvidedInterface[] aProvided,
52 RequiredInterface[] aRequired) {
53 _status = Status.NOT_STARTED;
56 _provided = new ArrayList<ProvidedInterface>();
57 _provided.addAll(Arrays.asList(aProvided));
58 _required = new ArrayList<RequiredInterface>();
59 _required.addAll(Arrays.asList(aRequired));
60 _running = new HashSet<ProvidedInterface>();
64 public Status getStatus() {
69 public final String getName() {
74 public void addContext(String aContext) {
75 if (_context == null ) {
79 _context = aContext + "." + _context;
84 public String getQualifiedName() {
85 if ( _context == null ) {
88 return _context + "." + getName();
92 public final ProvidedInterface[] getProvidedInterfaces() {
93 return _provided.toArray(new ProvidedInterface[0]);
97 public final RequiredInterface[] getRequiredInterfaces() {
98 return _required.toArray(new RequiredInterface[0]);
102 public final void start() {
103 LOG.info("Initializing '" + getQualifiedName() + "'");
105 _status = Status.RUNNING;
106 if ( _running.size() != _provided.size()) {
107 List<ProvidedInterface> remaining =
108 new ArrayList<ProvidedInterface>(_provided);
109 remaining.removeAll(_running);
110 throw new SystemAssemblyException(getQualifiedName() + ": not all services were started, missing " + remaining);
115 * Must be implemented for initializing the subsystem. The implementation
116 * must call {@link #addService(Service)} for each service that is started.
118 protected abstract void doStart();
121 * Implementations must call this method to indicate that a new service has
127 protected final void addInterface(
128 ProvidedInterface aDescriptor, Object aService) {
129 LOG.info("Interface '" + getQualifiedName() + "." + aDescriptor.getName() + "' started.");
130 _running.add(aDescriptor);
131 aDescriptor.publish(aService);
135 public ProvidedInterface[] getRunningInterfaces() {
136 return _running.toArray(new ProvidedInterface[0]);
142 if ( _running.size() > 0 ) {
143 // programming error.
144 throw new RuntimeException(getQualifiedName() + ": still services running after the stop call.");
146 _status = Status.STOPPED;
149 protected abstract void doStop();
152 * Implementations must call this method to indicate that a running service has
158 protected final void removeInterface(
159 ProvidedInterface aDescriptor) {
160 LOG.info("Interface '" + getQualifiedName() + "." + aDescriptor.getName() + "' stopped.");
161 _running.remove(aDescriptor);
162 aDescriptor.publish(null);
166 public String toString() {
167 return getQualifiedName();