1 package org.wamblee.system;
3 import java.util.ArrayList;
4 import java.util.Arrays;
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
10 import sun.util.LocaleServiceProviderPool.LocalizedObjectGetter;
13 * Composite system consisting of multiple subsystems.
15 * @author Erik Brakkee
17 public class Container extends AbstractComponent {
19 private static final Log LOG = LogFactory.getLog(Container.class);
21 private Component[] _systems;
24 * Construcst the composite system.
25 * @param aName Name of the system.
26 * @param aRegistry Service registry.
27 * @param aSystems Subsystems.
28 * @param aProvided Provided services of the system.
29 * @param aRequired Required services by the system.
31 public Container(String aName, ServiceRegistry aRegistry, Component[] aSystems,
32 ProvidedInterfaceDescriptor[] aProvided, RequiredInterfaceDescriptor[] aRequired) {
33 super(aName, aRegistry, aProvided, aRequired);
39 * Validates the subsystems together to check that there are
40 * no required services not in the required list and
41 * no services in the provided list that cannot be provided.
42 * Also logs a warning in case of superfluous requirements.
44 private void validate() {
45 List<ProvidedInterfaceDescriptor> provided = new ArrayList<ProvidedInterfaceDescriptor>();
46 for (Component system : _systems) {
47 provided.addAll(Arrays.asList(system.getProvidedServices()));
50 List<RequiredInterfaceDescriptor> required = new ArrayList<RequiredInterfaceDescriptor>();
51 for (Component system : _systems) {
52 required.addAll(Arrays.asList(system.getRequiredServices()));
55 for (InterfaceDescriptor service : getProvidedServices()) {
56 if (!(provided.contains(service))) {
57 throw new SystemAssemblyException(getName() + ": Service '" + service
58 + "' is not provided by any of the subsystems");
62 for (RequiredInterfaceDescriptor service : getRequiredServices()) {
63 if (!(required.contains(service))) {
66 + "' indicated as required is not actually required by any of the subsystems");
70 List<RequiredInterfaceDescriptor> reallyRequired = new ArrayList<RequiredInterfaceDescriptor>(
72 for (ProvidedInterfaceDescriptor service : provided) {
73 reallyRequired.remove(service);
75 for (RequiredInterfaceDescriptor service: getRequiredServices()) {
76 reallyRequired.remove(service);
78 String missingRequired = "";
79 for (RequiredInterfaceDescriptor service: reallyRequired) {
80 missingRequired += service + "\n";
82 if ( missingRequired.length() > 0 ) {
83 throw new SystemAssemblyException(getName() + ": missing required services\n" + missingRequired);
88 protected void doStart(String aContext, Service[] aRequiredServices) {
89 List<ProvidedInterfaceDescriptor> descriptors = new ArrayList<ProvidedInterfaceDescriptor>();
90 for (Service service : aRequiredServices) {
91 descriptors.add(service.getDescriptor());
93 SystemAssembler assembler = new SystemAssembler(aContext + "." + getName(), _systems,
94 descriptors.toArray(new ProvidedInterfaceDescriptor[0]));
95 assembler.start(getRegistry(), aRequiredServices);
99 protected void doStop() {
100 for (int i = _systems.length-1; i >= 0; i--) {
105 private void info(String aMsg) {
106 LOG.info(getName() + ": " + aMsg);