(no commit message)
[utils] / system / general / src / main / java / org / wamblee / system / core / Container.java
index 8e98755b5e7b02b8d8c33c2243150fac045660a5..1eaebf432a0e1c239189e621e3d0a35b9275f1ed 100644 (file)
@@ -133,47 +133,25 @@ public class Container extends AbstractComponent<Scope> {
      *             in case of any validation problems.
      */
     public void validate() {
-        List<ProvidedInterface> provided = new ArrayList<ProvidedInterface>();
-        for (Component component : _components) {
-            provided.addAll(Arrays.asList(component.getProvidedInterfaces()));
-        }
+        validateProvidedInterfacesArePresent();
 
-        List<RequiredInterface> required = new ArrayList<RequiredInterface>();
-        for (Component component : _components) {
-            required.addAll(Arrays.asList(component.getRequiredInterfaces()));
-        }
-
-        validateProvidedInterfacesArePresent(provided);
-
-        validateRequiredInterfaces(required);
+        validateRequiredInterfaces();
 
-        addProvidersOfRequiredInterfaces(provided);
-        validateRequiredProvidedMatch(provided, required);
+        doStartOptionalDryRun(null, true);
     }
 
-    /**
-     * 
-     * @param aProvided
-     *            All provided interfaces.
-     * @param aRequired
-     *            All required interfaces.
-     * @return
-     */
-    private void validateRequiredProvidedMatch(
-            List<ProvidedInterface> aProvided, List<RequiredInterface> aRequired) {
-
+    private void validateRequiredInterfaces() {
+        List<RequiredInterface> required = new ArrayList<RequiredInterface>();
         for (Component component : _components) {
-            initializeProvidersForRequiredInterfaces(aProvided, component, true);
+            required.addAll(Arrays.asList(component.getRequiredInterfaces()));
         }
-    }
 
-    private void validateRequiredInterfaces(List<RequiredInterface> aRequired) {
         for (RequiredInterface service : getRequiredInterfaces()) {
             // TODO required interfaces by the component could be
             // subclasses or implementations of the requirements
             // of the contained components. The code below assumes
             // an exact match.
-            if (!(aRequired.contains(service))) {
+            if (!(required.contains(service))) {
                 info("Service '"
                         + service
                         + "' indicated as required is not actually required by any of the components");
@@ -182,7 +160,7 @@ public class Container extends AbstractComponent<Scope> {
             // is optional whereas the internally required service is
             // mandatory.
             if (service.isOptional()) {
-                for (RequiredInterface intf : aRequired) {
+                for (RequiredInterface intf : required) {
                     if (intf.equals(service) && !intf.isOptional()) {
                         warn("Required service '"
                                 + service
@@ -197,14 +175,17 @@ public class Container extends AbstractComponent<Scope> {
         }
     }
 
-    private void validateProvidedInterfacesArePresent(
-            List<ProvidedInterface> aProvided) {
+    private void validateProvidedInterfacesArePresent() {
+        List<ProvidedInterface> provided = new ArrayList<ProvidedInterface>();
+        for (Component component : _components) {
+            provided.addAll(Arrays.asList(component.getProvidedInterfaces()));
+        }
         for (ProvidedInterface service : getProvidedInterfaces()) {
             // TODO provided interfaces by components could be
             // provide subclasses or implementations of the
             // provided interfaces of the container.
             // The code below assumes an exact match.
-            if (!(aProvided.contains(service))) {
+            if (!(provided.contains(service))) {
                 throw new SystemAssemblyException(getName() + ": Service '"
                         + service
                         + "' is not provided by any of its components");
@@ -246,6 +227,10 @@ public class Container extends AbstractComponent<Scope> {
 
     @Override
     protected Scope doStart(Scope aExternalScope) {
+        return doStartOptionalDryRun(aExternalScope, false);
+    }
+
+    private Scope doStartOptionalDryRun(Scope aExternalScope, boolean aDryRun) {
         LOG.info("Starting '" + getQualifiedName() + "'");
 
         Scope scope = new DefaultScope(getProvidedInterfaces(), aExternalScope);
@@ -258,12 +243,14 @@ public class Container extends AbstractComponent<Scope> {
         for (Component component : _components) {
             try {
                 initializeProvidersForRequiredInterfaces(allProvided,
-                        component, false);
+                        component, aDryRun);
 
                 // Start the service.
-                Object runtime = component.start(scope);
-                scope.addRuntime(component, runtime);
-                started.add(component);
+                if (!aDryRun) {
+                    Object runtime = component.start(scope);
+                    scope.addRuntime(component, runtime);
+                    started.add(component);
+                }
 
                 // add all provided services
                 ProvidedInterface[] provided = component
@@ -306,7 +293,8 @@ public class Container extends AbstractComponent<Scope> {
         // components
         for (int i = aStarted.size() - 1; i >= 0; i--) {
             try {
-                aStarted.get(i).stop(aScope);
+                Component component = aStarted.get(i);
+                aStarted.get(i).stop(aScope.getRuntime(component));
             } catch (Throwable t) {
                 LOG.error(getQualifiedName() + ": error stopping "
                         + aStarted.get(i).getQualifiedName());