Huge refactoring.
[utils] / system / general / src / main / java / org / wamblee / system / core / Container.java
index df92c0d099dcb4a61e219bb6047a7b5bea672783..f1b8b1ec0380fe4204f7f2021dd97c9bf8e53de9 100644 (file)
@@ -29,7 +29,7 @@ import org.apache.commons.logging.LogFactory;
  * 
  * @author Erik Brakkee
  */
-public class Container extends AbstractComponent {
+public class Container extends AbstractComponent<Scope> {
 
        private static final Log LOG = LogFactory.getLog(Container.class);
 
@@ -176,10 +176,18 @@ public class Container extends AbstractComponent {
                        }
                }
        }
+       
+       public Scope start() { 
+               return super.start(new DefaultScope(new ProvidedInterface[0]));
+       }
 
        @Override
-       protected void doStart() {
+       protected Scope doStart(Scope aExternalScope) {
                LOG.info("Starting '" + getQualifiedName() + "'");
+               
+               Scope scope = new DefaultScope(getProvidedInterfaces(), 
+                               aExternalScope);
+               
                List<ProvidedInterface> allProvided = new ArrayList<ProvidedInterface>();
 
                // all interfaces from the required list of this container are
@@ -203,7 +211,8 @@ public class Container extends AbstractComponent {
                                checkAllRequiredServicesAlreadyProvided(allProvided, component);
 
                                // Start the service.
-                               component.start();
+                               Object runtime = component.start(scope);
+                               scope.addRuntime(component, runtime); 
                                started.add(component);
 
                                // add all provided services
@@ -214,19 +223,19 @@ public class Container extends AbstractComponent {
                        } catch (RuntimeException e) {
                                LOG.error(getQualifiedName() + ": could not start '"
                                                + component.getQualifiedName() + "'", e);
-                               stopAlreadyStartedComponents(started);
+                               stopAlreadyStartedComponents(started, scope);
                                throw e; 
                        }
                }
-
+               return scope; 
        }
 
-       private void stopAlreadyStartedComponents(List<Component> aStarted) {
+       private void stopAlreadyStartedComponents(List<Component> aStarted, Scope aScope) {
                // an exception occurred, stop the successfully started
                // components
                for (int i = aStarted.size() - 1; i >= 0; i--) {
                        try {
-                               aStarted.get(i).stop();
+                               aStarted.get(i).stop(aScope);
                        } catch (Throwable t) {
                                LOG.error(getQualifiedName() + ": error stopping "
                                                + aStarted.get(i).getQualifiedName());
@@ -268,9 +277,11 @@ public class Container extends AbstractComponent {
        }
 
        @Override
-       protected void doStop() {
+       protected void doStop(Scope aScope) {
                for (int i = _components.length - 1; i >= 0; i--) {
-                       _components[i].stop();
+                       Component component = _components[i];
+                       Object runtime = aScope.getRuntime(component);
+                       component.stop(runtime);
                }
        }