X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=system%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsystem%2FAbstractComponent.java;h=a9752d1f09e7104cee6af9c75685ca9ae7b53a75;hb=7526813969742844aefbd25e8a4a056985dc7f65;hp=9354a5c3f8dde26b52ec885c1fb67de039383cc5;hpb=55962d29047c49a40aad45fdaeb79483e25a828d;p=utils diff --git a/system/general/src/main/java/org/wamblee/system/AbstractComponent.java b/system/general/src/main/java/org/wamblee/system/AbstractComponent.java index 9354a5c3..a9752d1f 100644 --- a/system/general/src/main/java/org/wamblee/system/AbstractComponent.java +++ b/system/general/src/main/java/org/wamblee/system/AbstractComponent.java @@ -1,15 +1,28 @@ +/* + * Copyright 2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.wamblee.system; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; +import java.util.HashSet; import java.util.List; -import java.util.Map; +import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wamblee.system.Component.Status; /** * Abstract subsystem class making it easy to implement new subsystems. @@ -19,17 +32,14 @@ public abstract class AbstractComponent implements Component { private static final Log LOG = LogFactory.getLog(AbstractComponent.class); private Status _status; - private String _name; - private ServiceRegistry _registry; - private List _provided; - private List _required; - private Map _running; + private String _name; + private List _provided; + private List _required; + private Set _running; /** * Constructs the subsystem. - * - * @param aRegistry - * Registry of services. + * * @param aName * Name of the system. * @param aProvided @@ -37,16 +47,15 @@ public abstract class AbstractComponent implements Component { * @param aRequired * Required services. */ - protected AbstractComponent(String aName, ServiceRegistry aRegistry, ProvidedInterfaceDescriptor[] aProvided, - RequiredInterfaceDescriptor[] aRequired) { + protected AbstractComponent(String aName, ProvidedInterface[] aProvided, + RequiredInterface[] aRequired) { _status = Status.NOT_STARTED; _name = aName; - _registry = aRegistry; - _provided = new ArrayList(); + _provided = new ArrayList(); _provided.addAll(Arrays.asList(aProvided)); - _required = new ArrayList(); + _required = new ArrayList(); _required.addAll(Arrays.asList(aRequired)); - _running = new HashMap(); + _running = new HashSet(); } @Override @@ -58,41 +67,35 @@ public abstract class AbstractComponent implements Component { public final String getName() { return _name; } - - public ServiceRegistry getRegistry() { - return _registry; - } @Override - public final ProvidedInterfaceDescriptor[] getProvidedServices() { - return _provided.toArray(new ProvidedInterfaceDescriptor[0]); + public final ProvidedInterface[] getProvidedServices() { + return _provided.toArray(new ProvidedInterface[0]); } @Override - public final RequiredInterfaceDescriptor[] getRequiredServices() { - return _required.toArray(new RequiredInterfaceDescriptor[0]); + public final RequiredInterface[] getRequiredServices() { + return _required.toArray(new RequiredInterface[0]); } @Override - public final Service[] start(String aContext, - Service[] aRequiredServices) { - LOG.info("Initializing '" + aContext + "." + _name + "' with " - + Arrays.asList(aRequiredServices)); - doStart(aContext + "." + getName(), aRequiredServices); + public final void start(String aContext) { + LOG.info("Initializing '" + aContext + "." + _name + "'"); + doStart(aContext + "." + getName()); _status = Status.RUNNING; - return _running.values().toArray(new Service[0]); + if ( _running.size() != _provided.size()) { + List remaining = + new ArrayList(_provided); + remaining.removeAll(_running); + throw new SystemAssemblyException(aContext + "." + getName() + ": not all services were started, missing " + remaining); + } } /** * Must be implemented for initializing the subsystem. The implementation * must call {@link #addService(Service)} for each service that is started. - * - * @param aRequiredServices - * Services that are already running from other subsystems that - * may be used. */ - protected abstract void doStart(String aContext, - Service[] aRequiredServices); + protected abstract void doStart(String aContext); /** * Implementations must call this method to indicate that a new service has @@ -102,23 +105,20 @@ public abstract class AbstractComponent implements Component { * Service. */ protected final void addService(String aContext, - ProvidedInterfaceDescriptor aDescriptor, Object aService) { + ProvidedInterface aDescriptor, Object aService) { LOG.info(aContext + ": service '" + aService + "' started."); - Service svc = getRegistry().register(aDescriptor, aService); - _running.put(svc.getDescriptor(), svc); + _running.add(aDescriptor); + aDescriptor.publish(aService); } @Override - public Service[] getRunningServices() { - return _running.values().toArray(new Service[0]); + public ProvidedInterface[] getRunningServices() { + return _running.toArray(new ProvidedInterface[0]); } @Override public void stop() { doStop(); - for (Service svc: _running.values()) { - getRegistry().remove(svc); - } _status = Status.STOPPED; }