+/*
+ * 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;
import java.util.ArrayList;
* @param aProvided Provided services of the system.
* @param aRequired Required services by the system.
*/
- public Container(String aName, ServiceRegistry aRegistry, Component[] aSystems,
+ public Container(String aName, Component[] aSystems,
ProvidedInterface[] aProvided, RequiredInterface[] aRequired) {
- super(aName, aRegistry, aProvided, aRequired);
+ super(aName, aProvided, aRequired);
_systems = aSystems;
- validate();
+ validate(aRequired);
}
/**
* no services in the provided list that cannot be provided.
* Also logs a warning in case of superfluous requirements.
*/
- private void validate() {
+ private void validate(RequiredInterface[] aRequired) {
List<ProvidedInterface> provided = new ArrayList<ProvidedInterface>();
for (Component system : _systems) {
provided.addAll(Arrays.asList(system.getProvidedServices()));
List<RequiredInterface> reallyRequired = new ArrayList<RequiredInterface>(
required);
+ // Compute all required interfaces that are not provided
for (ProvidedInterface service : provided) {
- reallyRequired.remove(service);
- }
- for (RequiredInterface service: getRequiredServices()) {
- reallyRequired.remove(service);
+ List<RequiredInterface> fulfilled =
+ Arrays.asList(SystemAssembler.filterRequiredServices(service,
+ reallyRequired));
+ reallyRequired.removeAll(fulfilled);
}
+ // Now the remaining interfaces should be covered by the required
+ // list.
+ reallyRequired.removeAll(Arrays.asList(aRequired));
+
String missingRequired = "";
for (RequiredInterface service: reallyRequired) {
missingRequired += service + "\n";
}
@Override
- protected void doStart(String aContext, Service[] aRequiredServices) {
- List<ProvidedInterface> descriptors = new ArrayList<ProvidedInterface>();
- for (Service service : aRequiredServices) {
- descriptors.add(service.getDescriptor());
+ protected void doStart(String aContext) {
+ List<ProvidedInterface> provided = new ArrayList<ProvidedInterface>();
+
+ // all interfaces from the required list of this container are
+ // provided to the components inside it.
+ RequiredInterface[] required = getRequiredServices();
+ for (RequiredInterface intf: required) {
+ ProvidedInterface provider = intf.getProvider();
+ if ( provider == null ) {
+ throw new SystemAssemblyException(aContext + ": required interface '" + intf +"' is not provided");
+ }
+ provided.add(intf.getProvider());
}
- SystemAssembler assembler = new SystemAssembler(aContext + "." + getName(), _systems,
- descriptors.toArray(new ProvidedInterface[0]));
- assembler.start(getRegistry(), aRequiredServices);
+
+ SystemAssembler assembler = new SystemAssembler( _systems,
+ provided.toArray(new ProvidedInterface[0]));
+ assembler.start();
}
@Override