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;
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;
36 private List<ProvidedInterface> _provided;
37 private List<RequiredInterface> _required;
38 private Set<ProvidedInterface> _running;
41 * Constructs the subsystem.
50 protected AbstractComponent(String aName, ProvidedInterface[] aProvided,
51 RequiredInterface[] aRequired) {
52 _status = Status.NOT_STARTED;
54 _provided = new ArrayList<ProvidedInterface>();
55 _provided.addAll(Arrays.asList(aProvided));
56 _required = new ArrayList<RequiredInterface>();
57 _required.addAll(Arrays.asList(aRequired));
58 _running = new HashSet<ProvidedInterface>();
62 public Status getStatus() {
67 public final String getName() {
72 public final ProvidedInterface[] getProvidedServices() {
73 return _provided.toArray(new ProvidedInterface[0]);
77 public final RequiredInterface[] getRequiredServices() {
78 return _required.toArray(new RequiredInterface[0]);
82 public final void start(String aContext) {
83 LOG.info("Initializing '" + aContext + "." + _name + "'");
84 doStart(aContext + "." + getName());
85 _status = Status.RUNNING;
86 if ( _running.size() != _provided.size()) {
87 List<ProvidedInterface> remaining =
88 new ArrayList<ProvidedInterface>(_provided);
89 remaining.removeAll(_running);
90 throw new SystemAssemblyException(aContext + "." + getName() + ": not all services were started, missing " + remaining);
95 * Must be implemented for initializing the subsystem. The implementation
96 * must call {@link #addService(Service)} for each service that is started.
98 protected abstract void doStart(String aContext);
101 * Implementations must call this method to indicate that a new service has
107 protected final void addService(String aContext,
108 ProvidedInterface aDescriptor, Object aService) {
109 LOG.info(aContext + ": service '" + aService + "' started.");
110 _running.add(aDescriptor);
111 aDescriptor.publish(aService);
115 public ProvidedInterface[] getRunningServices() {
116 return _running.toArray(new ProvidedInterface[0]);
122 _status = Status.STOPPED;
125 protected abstract void doStop();
128 public String toString() {