X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=system%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsystem%2Fcore%2FAbstractComponent.java;h=c7145cdf20fb37fb02ba472221faf00c0f1f437f;hb=dec278a67997ea8e85d10662e31548afd8890ed3;hp=5d89e4a46f839078c7adc5876bae684a3d2cdb8f;hpb=4845c677310814de4c98e22aad483e49b9dfc594;p=utils diff --git a/system/general/src/main/java/org/wamblee/system/core/AbstractComponent.java b/system/general/src/main/java/org/wamblee/system/core/AbstractComponent.java index 5d89e4a4..c7145cdf 100644 --- a/system/general/src/main/java/org/wamblee/system/core/AbstractComponent.java +++ b/system/general/src/main/java/org/wamblee/system/core/AbstractComponent.java @@ -1,5 +1,5 @@ /* - * Copyright 2007 the original author or authors. + * Copyright 2005-2010 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. @@ -15,138 +15,225 @@ */ package org.wamblee.system.core; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import java.util.ArrayList; import java.util.Arrays; -import java.util.HashSet; +import java.util.Collections; import java.util.List; -import java.util.Set; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; /** * Abstract subsystem class making it easy to implement new subsystems. + * */ -public abstract class AbstractComponent implements Component { - - private static final Log LOG = LogFactory.getLog(AbstractComponent.class); - - private Status _status; - private String _context; - private String _name; - private List _provided; - private List _required; - private Set _running; - - /** - * Constructs the subsystem. - * - * @param aName - * Name of the system. - * @param aProvided - * Provided services. - * @param aRequired - * Required services. - */ - protected AbstractComponent(String aName, ProvidedInterface[] aProvided, - RequiredInterface[] aRequired) { - _status = Status.NOT_STARTED; - _context = null; - _name = aName; - _provided = new ArrayList(); - _provided.addAll(Arrays.asList(aProvided)); - _required = new ArrayList(); - _required.addAll(Arrays.asList(aRequired)); - _running = new HashSet(); - } - - @Override - public Status getStatus() { - return _status; - } - - @Override - public final String getName() { - return _name; - } - - @Override - public void addContext(String aContext) { - if (_context == null ) { - _context = aContext; - } - else { - _context = aContext + "." + _context; - } - } - - @Override - public String getQualifiedName() { - if ( _context == null ) { - return getName(); - } - return _context + "." + getName(); - } - - @Override - public final ProvidedInterface[] getProvidedServices() { - return _provided.toArray(new ProvidedInterface[0]); - } - - @Override - public final RequiredInterface[] getRequiredServices() { - return _required.toArray(new RequiredInterface[0]); - } - - @Override - public final void start() { - LOG.info("Initializing '" + getQualifiedName() + "'"); - doStart(); - _status = Status.RUNNING; - if ( _running.size() != _provided.size()) { - List remaining = - new ArrayList(_provided); - remaining.removeAll(_running); - throw new SystemAssemblyException(getQualifiedName() + ": 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. - */ - protected abstract void doStart(); - - /** - * Implementations must call this method to indicate that a new service has - * been started. - * - * @param aService - * Service. - */ - protected final void addService( - ProvidedInterface aDescriptor, Object aService) { - LOG.info("Interface '" + getQualifiedName() + "." + aDescriptor.getName() + "' started."); - _running.add(aDescriptor); - aDescriptor.publish(aService); - } - - @Override - public ProvidedInterface[] getRunningServices() { - return _running.toArray(new ProvidedInterface[0]); - } - - @Override - public void stop() { - doStop(); - _status = Status.STOPPED; - } - - protected abstract void doStop(); - - @Override - public String toString() { - return _name; - } +public abstract class AbstractComponent implements Component { + private static final Log LOG = LogFactory.getLog(AbstractComponent.class); + + private ThreadLocal> remaining; + + private String context; + + private String name; + + private List provided; + + private List required; + + /** + * Constructs the subsystem. + * + * @param aName + * Name of the system. + * @param aProvided + * Provided services. + * @param aRequired + * Required services. + */ + protected AbstractComponent(String aName, + List aProvided, List aRequired) { + remaining = new ThreadLocal>(); + context = null; + name = aName; + provided = new ArrayList(aProvided); + required = new ArrayList(aRequired); + } + + /** + * Constructs the subsystem. + * + * @param aName + * Name of the system. + * @param aProvided + * Provided services. + * @param aRequired + * Required services. + */ + protected AbstractComponent(String aName, ProvidedInterface[] aProvided, + RequiredInterface[] aRequired) { + this(aName, Arrays.asList(aProvided), Arrays.asList(aRequired)); + } + + /** + * Creates a new AbstractComponent object. + * + */ + protected AbstractComponent(String aName) { + this(aName, new ProvidedInterface[0], new RequiredInterface[0]); + } + + public AbstractComponent addProvidedInterface( + ProvidedInterface aProvided) { + provided.add(aProvided); + + return this; + } + + public AbstractComponent addRequiredInterface( + RequiredInterface aRequired) { + required.add(aRequired); + + return this; + } + + @Override + public final String getName() { + return name; + } + + @Override + public void addContext(String aContext) { + if (context == null) { + context = aContext; + } else { + context = aContext + "." + context; + } + } + + @Override + public String getContext() { + return context; + } + + @Override + public String getQualifiedName() { + if (context == null) { + return getName(); + } + + return context + "." + getName(); + } + + @Override + public final List getProvidedInterfaces() { + return Collections.unmodifiableList(provided); + } + + @Override + public final List getRequiredInterfaces() { + return Collections.unmodifiableList(required); + } + + @Override + public final Type start(Scope aScope) { + LOG.info("Initialization starting '" + getQualifiedName() + "'"); + + List oldRemaining = remaining.get(); + remaining + .set(new ArrayList(getProvidedInterfaces())); + + try { + Type runtime = doStart(aScope); + checkNotStartedInterfaces(); + LOG.info("Initialization finished '" + getQualifiedName() + "'"); + + return runtime; + } finally { + remaining.set(oldRemaining); + } + } + + private void checkNotStartedInterfaces() { + if (remaining.get().size() > 0) { + StringBuffer notProvided = new StringBuffer(); + + for (ProvidedInterface providedIntf : remaining.get()) { + notProvided.append("\nComponent " + getQualifiedName() + + " did not start interface " + providedIntf); + } + + throw new SystemAssemblyException(notProvided.toString()); + } + } + + /** + * Must be implemented for initializing the subsystem. The implementation + * must call {@link #addInterface(ProvidedInterface, Object, Scope)} for + * each service that is started. + * + * + * @return Returns the runtime of the component. + */ + protected abstract Type doStart(Scope aScope); + + /** + * Implementations must call this method to indicate that a new service has + * been started. + * + * @param aDescriptor + * Provided interface. + * @param aService + * Implementation of the interface. + * @param aScope + * scope in which to publish the implementation. + * + */ + protected final void addInterface(ProvidedInterface aDescriptor, + Object aService, Scope aScope) { + LOG.info("Interface '" + getQualifiedName() + "." + + aDescriptor.getName() + "' started."); + + if (!remaining.get().remove(aDescriptor)) { + throw new SystemAssemblyException("Component '" + + getQualifiedName() + "' started an unexpected interface '" + + aDescriptor + + "' that was not registerd as a provided interface before"); + } + + aScope.publishInterface(aDescriptor, aService); + } + + @Override + public void stop(Type aRuntime) { + LOG.info("Stopping initiated '" + getQualifiedName() + "'"); + doStop(aRuntime); + LOG.info("Stopping completed '" + getQualifiedName() + "'"); + } + + protected abstract void doStop(Type aRuntime); + + @Override + public String toString() { + return getQualifiedName(); + } + + public ProvidedInterface findProvidedInterface(String aName) { + for (ProvidedInterface providedIntf : getProvidedInterfaces()) { + if (providedIntf.getName().equals(aName)) { + return providedIntf; + } + } + + return null; + } + + public RequiredInterface findRequiredInterface(String aName) { + for (RequiredInterface requiredIntf : getRequiredInterfaces()) { + if (requiredIntf.getName().equals(aName)) { + return requiredIntf; + } + } + return null; + } }