now allowing components and interfaces to be added after construction.
[utils] / system / general / src / main / java / org / wamblee / system / core / Container.java
index f1b8b1ec0380fe4204f7f2021dd97c9bf8e53de9..a4b0620fccc589ab5fd48796aa3ca8ba34d17768 100644 (file)
@@ -33,7 +33,8 @@ public class Container extends AbstractComponent<Scope> {
 
        private static final Log LOG = LogFactory.getLog(Container.class);
 
-       private Component[] _components;
+       private List<Component> _components;
+       private boolean _sealed; 
 
        public static RequiredInterface[] filterRequiredServices(
                        ProvidedInterface aProvided,
@@ -59,7 +60,7 @@ public class Container extends AbstractComponent<Scope> {
        }
 
        /**
-        * Construcst the container
+        * Constructs the container
         * 
         * @param aName
         *            Name of the container
@@ -73,20 +74,46 @@ public class Container extends AbstractComponent<Scope> {
        public Container(String aName, Component[] aComponents,
                        ProvidedInterface[] aProvided, RequiredInterface[] aRequired) {
                super(aName, aProvided, aRequired);
-               _components = aComponents;
+               _components = new ArrayList<Component>(Arrays.asList(aComponents));
                for (Component component : aComponents) {
                        component.addContext(getQualifiedName());
                }
+               _sealed = false; 
                validate();
        }
+       
+       public Container(String aName) {
+           this(aName, new Component[0], new ProvidedInterface[0], new RequiredInterface[0]);
+       }
+       
+       public Container addComponent(Component aComponent) {
+               checkSealed(); 
+               _components.add(aComponent);
+               return this; 
+       }
 
+       @Override
+       protected Container addProvidedInterface(ProvidedInterface aProvided) {
+               checkSealed();
+               super.addProvidedInterface(aProvided);
+               return this; 
+       }
+       
+       @Override
+       protected Container addRequiredInterface(RequiredInterface aRequired) {
+               checkSealed(); 
+               super.addRequiredInterface(aRequired);
+               return this; 
+       }
+       
        /**
         * Validates the components together to check that there are no required
         * services not in the required list and no services in the provided list
         * that cannot be provided. Also logs a warning in case of superfluous
         * requirements.
+        * @throws SystemAssemblyException in case of any validation problems. 
         */
-       private void validate() {
+       public void validate() {
                List<ProvidedInterface> provided = new ArrayList<ProvidedInterface>();
                for (Component component : _components) {
                        provided.addAll(Arrays.asList(component.getProvidedInterfaces()));
@@ -177,8 +204,32 @@ public class Container extends AbstractComponent<Scope> {
                }
        }
        
-       public Scope start() { 
-               return super.start(new DefaultScope(new ProvidedInterface[0]));
+       /**
+        * Starts the container. After the container is started, the container becomes sealed
+        * meaning that no further components, required or provided interfaces may be added. 
+        * @return Scope. 
+        */
+       public Scope start() {
+               checkSealed();
+               validate();
+           Scope scope = super.start(new DefaultScope(new ProvidedInterface[0]));
+           seal();
+           return scope; 
+       }
+       
+       /**
+        * Seal the container, meaning that no further components or interfaces may be added. 
+        */
+       public void seal() { 
+               _sealed = true; 
+       }
+
+       /**
+        * Checks if the container is sealed. 
+        * @return True iff the container is sealed. 
+        */
+       public boolean isSealed() {
+               return _sealed;
        }
 
        @Override
@@ -278,8 +329,8 @@ public class Container extends AbstractComponent<Scope> {
 
        @Override
        protected void doStop(Scope aScope) {
-               for (int i = _components.length - 1; i >= 0; i--) {
-                       Component component = _components[i];
+               for (int i = _components.size() - 1; i >= 0; i--) {
+                       Component component = _components.get(i);
                        Object runtime = aScope.getRuntime(component);
                        component.stop(runtime);
                }
@@ -320,4 +371,10 @@ public class Container extends AbstractComponent<Scope> {
                }
                return clients; 
        }
+       
+       private void checkSealed() { 
+               if ( _sealed ) { 
+                       throw new SystemAssemblyException("Container is sealed");
+               }
+       }
 }