(no commit message)
[utils] / system / general / src / main / java / org / wamblee / system / Container.java
index a23a7373aba0b9635ec9ad4e7c9b5d802d753281..a1bb29c24554967812b739636c4ebf443b96e1ee 100644 (file)
@@ -1,3 +1,18 @@
+/*
+ * 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;
@@ -28,11 +43,11 @@ public class Container extends AbstractComponent {
         * @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);
        }
 
        /**
@@ -41,7 +56,7 @@ public class Container extends AbstractComponent {
         * 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()));
@@ -69,12 +84,17 @@ public class Container extends AbstractComponent {
 
                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";
@@ -85,14 +105,23 @@ public class Container extends AbstractComponent {
        }
 
        @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