package org.wamblee.system; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import sun.util.LocaleServiceProviderPool.LocalizedObjectGetter; /** * Composite system consisting of multiple subsystems. * * @author Erik Brakkee */ public class Container extends AbstractComponent { private static final Log LOG = LogFactory.getLog(Container.class); private Component[] _systems; /** * Construcst the composite system. * @param aName Name of the system. * @param aRegistry Service registry. * @param aSystems Subsystems. * @param aProvided Provided services of the system. * @param aRequired Required services by the system. */ public Container(String aName, ServiceRegistry aRegistry, Component[] aSystems, ProvidedInterfaceDescriptor[] aProvided, RequiredInterfaceDescriptor[] aRequired) { super(aName, aRegistry, aProvided, aRequired); _systems = aSystems; validate(); } /** * Validates the subsystems together to check that there are * no required services not in the required list and * no services in the provided list that cannot be provided. * Also logs a warning in case of superfluous requirements. */ private void validate() { List provided = new ArrayList(); for (Component system : _systems) { provided.addAll(Arrays.asList(system.getProvidedServices())); } List required = new ArrayList(); for (Component system : _systems) { required.addAll(Arrays.asList(system.getRequiredServices())); } for (InterfaceDescriptor service : getProvidedServices()) { if (!(provided.contains(service))) { throw new SystemAssemblyException(getName() + ": Service '" + service + "' is not provided by any of the subsystems"); } } for (RequiredInterfaceDescriptor service : getRequiredServices()) { if (!(required.contains(service))) { info("Service '" + service + "' indicated as required is not actually required by any of the subsystems"); } } List reallyRequired = new ArrayList( required); for (ProvidedInterfaceDescriptor service : provided) { reallyRequired.remove(service); } for (RequiredInterfaceDescriptor service: getRequiredServices()) { reallyRequired.remove(service); } String missingRequired = ""; for (RequiredInterfaceDescriptor service: reallyRequired) { missingRequired += service + "\n"; } if ( missingRequired.length() > 0 ) { throw new SystemAssemblyException(getName() + ": missing required services\n" + missingRequired); } } @Override protected void doStart(String aContext, Service[] aRequiredServices) { List descriptors = new ArrayList(); for (Service service : aRequiredServices) { descriptors.add(service.getDescriptor()); } SystemAssembler assembler = new SystemAssembler(aContext + "." + getName(), _systems, descriptors.toArray(new ProvidedInterfaceDescriptor[0])); assembler.start(getRegistry(), aRequiredServices); } @Override protected void doStop() { for (int i = _systems.length-1; i >= 0; i--) { _systems[i].stop(); } } private void info(String aMsg) { LOG.info(getName() + ": " + aMsg); } }