/* * 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.core; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * 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; public static RequiredInterface[] filterRequiredServices( ProvidedInterface aProvided, Collection aDescriptors) { List required = new ArrayList(); for (RequiredInterface descriptor : aDescriptors) { if (descriptor.implementedBy(aProvided)) { required.add(descriptor); } } return required.toArray(new RequiredInterface[0]); } public static ProvidedInterface[] filterProvidedServices( RequiredInterface aRequired, Collection aProvided) { List provided = new ArrayList(); for (ProvidedInterface descriptor : aProvided) { if (aRequired.implementedBy(descriptor)) { provided.add(descriptor); } } return provided.toArray(new ProvidedInterface[0]); } /** * 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, Component[] aSystems, ProvidedInterface[] aProvided, RequiredInterface[] aRequired) { super(aName, aProvided, aRequired); _systems = aSystems; for (Component component : aSystems) { component.addContext(getQualifiedName()); } 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.getProvidedInterfaces())); } List required = new ArrayList(); for (Component system : _systems) { required.addAll(Arrays.asList(system.getRequiredInterfaces())); } validateProvidedInterfaces(provided); validateRequiredInterfaces(required); List reallyRequired = validateRequiredProvidedMatch( provided, required); String missingRequired = ""; for (RequiredInterface service : reallyRequired) { missingRequired += service + "\n"; } if (missingRequired.length() > 0) { throw new SystemAssemblyException(getName() + ": missing required services\n" + missingRequired); } } private List validateRequiredProvidedMatch( List provided, List required) { List reallyRequired = new ArrayList( required); // Compute all required interfaces that are not provided for (ProvidedInterface service : provided) { List fulfilled = Arrays .asList(filterRequiredServices(service, reallyRequired)); reallyRequired.removeAll(fulfilled); } // Now remove all optional interfaces from the list. for (Iterator i = reallyRequired.iterator(); i.hasNext(); ) { RequiredInterface req = i.next(); if ( req.isOptional() ) { i.remove(); } } // Now the remaining interfaces should be covered by the required // list. reallyRequired.removeAll(Arrays.asList(getRequiredInterfaces())); return reallyRequired; } private void validateRequiredInterfaces(List required) { for (RequiredInterface service : getRequiredInterfaces()) { // TODO required services by the subsystem could be // subclasses or implementations of the requirements // of the contained systems. The code below assumes // an exact match. if (!(required.contains(service))) { info("Service '" + service + "' indicated as required is not actually required by any of the subsystems"); } // Check for the case that the externally required service // is optional whereas the internally required service is // mandatory. if ( service.isOptional()) { for (RequiredInterface intf: required) { if ( intf.equals(service) && !intf.isOptional()) { // TODO indicate which subsystem this is. warn("Required service '" + service + "' indicated as optional is mandatory by one of its subsystems (" + getClients(intf) + ", " + intf + "), this can lead to problems when the system is started and the service is not there."); } } } } } private void validateProvidedInterfaces(List provided) { for (ProvidedInterface service : getProvidedInterfaces()) { // TODO provided interfaces by subsystems could be // provide subclasses or implementations of the // provided interfaces of the container. // The code below assumes an exact match. if (!(provided.contains(service))) { throw new SystemAssemblyException(getName() + ": Service '" + service + "' is not provided by any of the subsystems"); } } } @Override protected void doStart() { LOG.info("Starting '" + getQualifiedName() + "'"); List allProvided = new ArrayList(); // all interfaces from the required list of this container are // provided to the components inside it. RequiredInterface[] required = getRequiredInterfaces(); for (RequiredInterface intf : required) { ProvidedInterface provider = intf.getProvider(); if (provider != null ) { allProvided.add(provider); } else { if ( !intf.isOptional()) { throw new SystemAssemblyException(getQualifiedName() + ": required interface '" + intf + "' is not provided"); } } } List started = new ArrayList(); for (Component system : _systems) { try { // Check if all required services are already provided by // earlier // systems. for (RequiredInterface descriptor : system.getRequiredInterfaces()) { ProvidedInterface[] filtered = filterProvidedServices( descriptor, allProvided); if ( filtered.length == 1 ) { descriptor.setProvider(filtered[0]); } else if ( filtered.length > 1 ) { throw new SystemAssemblyException( "Service '" + descriptor + "' required by system '" + system + "' matches multiple services provided by other systems: " + Arrays.asList(filtered)); } else { // filtered.length == 0 if ( !descriptor.isOptional()) { throw new SystemAssemblyException( "Service '" + descriptor + "' required by system '" + system + "' is not provided by systems that are started earlier"); } } } // Start the service. system.start(); started.add(system); // add all provided services ProvidedInterface[] provided = system.getProvidedInterfaces(); allProvided.addAll(Arrays.asList(provided)); } catch (SystemAssemblyException e) { throw e; } catch (RuntimeException e) { LOG.error(getQualifiedName() + ": could not start '" + system.getQualifiedName() + "'", e); // an exception occurred, stop the successfully started // systems for (int i = started.size() - 1; i >= 0; i--) { try { started.get(i).stop(); } catch (Throwable t) { LOG.error(getQualifiedName() + ": error stopping " + started.get(i).getQualifiedName()); } } throw e; } } } @Override protected void doStop() { for (int i = _systems.length - 1; i >= 0; i--) { _systems[i].stop(); } } private void info(String aMsg) { LOG.info(getQualifiedName() + ": " + aMsg); } private void warn(String aMsg) { LOG.warn(getQualifiedName() + ": " + aMsg); } private List getClients(RequiredInterface aRequirement) { List clients = new ArrayList(); for (Component component: _systems) { for (RequiredInterface required: component.getRequiredInterfaces()) { if ( required.equals(aRequirement) && required.isOptional() == aRequirement.isOptional()) { clients.add(component); } } } return clients; } }