updated coding rules.
[utils] / system / general / src / main / java / org / wamblee / system / container / Container.java
index e4466d4a3f5b972f6aab3d96c2e6bd510faf0013..cb7a0981df0140a3097b363fee3bc5cf99e9bbf6 100644 (file)
@@ -17,9 +17,7 @@ package org.wamblee.system.container;
 
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.HashSet;
 import java.util.List;
-import java.util.Set;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -47,9 +45,9 @@ public class Container extends AbstractComponent<Scope> {
 
     private static final Log LOG = LogFactory.getLog(Container.class);
 
-    private List<Component> _components;
-    private CompositeEdgeFilter _edgeFilter; 
-    private boolean _sealed;
+    private List<Component> components;
+    private CompositeEdgeFilter edgeFilter; 
+    private boolean sealed;
 
     /**
      * Constructs the container
@@ -66,10 +64,10 @@ public class Container extends AbstractComponent<Scope> {
     public Container(String aName, Component[] aComponents,
             List<ProvidedInterface> aProvided, List<RequiredInterface> aRequired) {
         super(aName, aProvided, aRequired);
-        _components = new ArrayList<Component>();
+        components = new ArrayList<Component>();
 
-        _edgeFilter = new CompositeEdgeFilter();
-        _sealed = false;
+        edgeFilter = new CompositeEdgeFilter();
+        sealed = false;
         for (Component component : aComponents) {
             addComponent(component);
         }
@@ -109,7 +107,7 @@ public class Container extends AbstractComponent<Scope> {
             throw new SystemAssemblyException("Duplicate component '"
                     + aComponent.getName() + "'");
         }
-        _components.add(aComponent);
+        components.add(aComponent);
         aComponent.addContext(getQualifiedName());
         return this;
     }
@@ -148,7 +146,7 @@ public class Container extends AbstractComponent<Scope> {
                                        + aProvidedInterface + "'");
                }
         }
-        _edgeFilter.add(new ConnectRequiredProvidedEdgeFilter(aClientComponent, aRequiredInterface, aServerComponent, aProvidedInterface));
+        edgeFilter.add(new ConnectRequiredProvidedEdgeFilter(aClientComponent, aRequiredInterface, aServerComponent, aProvidedInterface));
     }
     
     /**
@@ -178,14 +176,33 @@ public class Container extends AbstractComponent<Scope> {
                                        + aExternalRequiredInterface + "'");
                }
         }
-        _edgeFilter.add(new ConnectRequiredExternallyRequiredEdgeFilter(
+        edgeFilter.add(new ConnectRequiredExternallyRequiredEdgeFilter(
                 aComponent, aRequiredInterface, aExternalRequiredInterface));
     }
     
     public void connectExternalProvided(String aExternalProvided, String aComponent, String aProvidedInterface) {
         checkSealed();
-        // TODO validate
-        _edgeFilter.add(new ConnectExternalProvidedProvidedFilter(aExternalProvided, aComponent, aProvidedInterface));
+        Component server = findComponent(aComponent);
+       
+      
+        if ( server == null ) { 
+               throw new SystemAssemblyException("No component '" + aComponent + "' in the container");
+        }
+        if ( aProvidedInterface != null ) { 
+               if ( findInterface(server.getProvidedInterfaces(), aProvidedInterface) == null) { 
+                       throw new SystemAssemblyException(
+                                       getQualifiedName() + ": Component '" + aComponent + "' does not have a provided interface named '" 
+                                       + aProvidedInterface + "'");
+               }
+        }
+        if ( aExternalProvided != null ) { 
+               if ( findInterface(getProvidedInterfaces(), aExternalProvided) == null) { 
+                       throw new SystemAssemblyException(
+                                       getQualifiedName() + ": Container does not have a provided interface named '" 
+                                       + aExternalProvided + "'");
+               }
+        }
+        edgeFilter.add(new ConnectExternalProvidedProvidedFilter(aExternalProvided, aComponent, aProvidedInterface));
     }
 
 
@@ -206,7 +223,7 @@ public class Container extends AbstractComponent<Scope> {
     @Override
     public void addContext(String aContext) {
         super.addContext(aContext);
-        for (Component component : _components) {
+        for (Component component : components) {
             component.addContext(aContext);
         }
     }
@@ -229,7 +246,7 @@ public class Container extends AbstractComponent<Scope> {
      * be added.
      */
     public void seal() {
-        _sealed = true;
+        sealed = true;
     }
 
     /**
@@ -238,7 +255,7 @@ public class Container extends AbstractComponent<Scope> {
      * @return True iff the container is sealed.
      */
     public boolean isSealed() {
-        return _sealed;
+        return sealed;
     }
 
     /**
@@ -254,7 +271,6 @@ public class Container extends AbstractComponent<Scope> {
 
     @Override
     protected Scope doStart(Scope aExternalScope) {
-        checkSealed();
         validate();
         Scope scope = new DefaultScope(getProvidedInterfaces().toArray(new ProvidedInterface[0]), aExternalScope);
         ComponentGraph graph = doStartOptionalDryRun(scope, false);
@@ -275,14 +291,12 @@ public class Container extends AbstractComponent<Scope> {
     private ComponentGraph doStartOptionalDryRun(Scope aScope, boolean aDryRun) {
         ComponentGraph graph = createComponentGraph();
         graph.validate();
-        if (!aDryRun) {
-            graph.link();
-        }
+        graph.link();
 
         LOG.info("Starting '" + getQualifiedName() + "'");
 
         List<Component> started = new ArrayList<Component>();
-        for (Component component : _components) {
+        for (Component component : components) {
             try {
                 // Start the service.
                 if (!aDryRun) {
@@ -307,14 +321,14 @@ public class Container extends AbstractComponent<Scope> {
         for (RequiredInterface req : getRequiredInterfaces()) {
             graph.addRequiredInterface(this, req);
         }
-        for (Component comp : _components) {
+        for (Component comp : components) {
             graph.addComponent(comp);
         }
         for (ProvidedInterface prov: getProvidedInterfaces()) { 
             graph.addProvidedInterface(this, prov);
         }
 
-        graph.addEdgeFilter(_edgeFilter);
+        graph.addEdgeFilter(edgeFilter);
         return graph;
     }
 
@@ -335,15 +349,15 @@ public class Container extends AbstractComponent<Scope> {
 
     @Override
     protected void doStop(Scope aScope) {
-        for (int i = _components.size() - 1; i >= 0; i--) {
-            Component component = _components.get(i);
+        for (int i = components.size() - 1; i >= 0; i--) {
+            Component component = components.get(i);
             Object runtime = aScope.getRuntime(component);
             component.stop(runtime);
         }
     }
 
     private void checkSealed() {
-        if (_sealed) {
+        if (sealed) {
             throw new SystemAssemblyException("Container is sealed");
         }
     }
@@ -354,7 +368,7 @@ public class Container extends AbstractComponent<Scope> {
      * @return Component or null if not found. 
      */
     public Component findComponent(String aName) { 
-       for (Component<?> component: _components) { 
+       for (Component<?> component: components) { 
                if ( component.getName().equals(aName)) { 
                        return component; 
                }