2 * Copyright 2007 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.system;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
25 import sun.util.LocaleServiceProviderPool.LocalizedObjectGetter;
28 * Composite system consisting of multiple subsystems.
30 * @author Erik Brakkee
32 public class Container extends AbstractComponent {
34 private static final Log LOG = LogFactory.getLog(Container.class);
36 private Component[] _systems;
39 * Construcst the composite system.
40 * @param aName Name of the system.
41 * @param aRegistry Service registry.
42 * @param aSystems Subsystems.
43 * @param aProvided Provided services of the system.
44 * @param aRequired Required services by the system.
46 public Container(String aName, Component[] aSystems,
47 ProvidedInterface[] aProvided, RequiredInterface[] aRequired) {
48 super(aName, aProvided, aRequired);
54 * Validates the subsystems together to check that there are
55 * no required services not in the required list and
56 * no services in the provided list that cannot be provided.
57 * Also logs a warning in case of superfluous requirements.
59 private void validate(RequiredInterface[] aRequired) {
60 List<ProvidedInterface> provided = new ArrayList<ProvidedInterface>();
61 for (Component system : _systems) {
62 provided.addAll(Arrays.asList(system.getProvidedServices()));
65 List<RequiredInterface> required = new ArrayList<RequiredInterface>();
66 for (Component system : _systems) {
67 required.addAll(Arrays.asList(system.getRequiredServices()));
70 for (ProvidedInterface service : getProvidedServices()) {
71 if (!(provided.contains(service))) {
72 throw new SystemAssemblyException(getName() + ": Service '" + service
73 + "' is not provided by any of the subsystems");
77 for (RequiredInterface service : getRequiredServices()) {
78 if (!(required.contains(service))) {
81 + "' indicated as required is not actually required by any of the subsystems");
85 List<RequiredInterface> reallyRequired = new ArrayList<RequiredInterface>(
87 // Compute all required interfaces that are not provided
88 for (ProvidedInterface service : provided) {
89 List<RequiredInterface> fulfilled =
90 Arrays.asList(SystemAssembler.filterRequiredServices(service,
92 reallyRequired.removeAll(fulfilled);
94 // Now the remaining interfaces should be covered by the required
96 reallyRequired.removeAll(Arrays.asList(aRequired));
98 String missingRequired = "";
99 for (RequiredInterface service: reallyRequired) {
100 missingRequired += service + "\n";
102 if ( missingRequired.length() > 0 ) {
103 throw new SystemAssemblyException(getName() + ": missing required services\n" + missingRequired);
108 protected void doStart(String aContext) {
109 List<ProvidedInterface> provided = new ArrayList<ProvidedInterface>();
111 // all interfaces from the required list of this container are
112 // provided to the components inside it.
113 RequiredInterface[] required = getRequiredServices();
114 for (RequiredInterface intf: required) {
115 ProvidedInterface provider = intf.getProvider();
116 if ( provider == null ) {
117 throw new SystemAssemblyException(aContext + ": required interface '" + intf +"' is not provided");
119 provided.add(intf.getProvider());
122 SystemAssembler assembler = new SystemAssembler( _systems,
123 provided.toArray(new ProvidedInterface[0]));
128 protected void doStop() {
129 for (int i = _systems.length-1; i >= 0; i--) {
134 private void info(String aMsg) {
135 LOG.info(getName() + ": " + aMsg);