Better solution for setting the context. The context is now known as soon as componen...
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Wed, 9 Apr 2008 18:25:37 +0000 (18:25 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Wed, 9 Apr 2008 18:25:37 +0000 (18:25 +0000)
trunk/system/general/src/main/java/org/wamblee/system/AbstractComponent.java
trunk/system/general/src/main/java/org/wamblee/system/Component.java
trunk/system/general/src/main/java/org/wamblee/system/Container.java
trunk/system/general/src/test/java/org/wamblee/system/Application.java
trunk/system/general/src/test/java/org/wamblee/system/ContainerTest.java
trunk/system/general/src/test/java/org/wamblee/system/Environment.java
trunk/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java
trunk/system/spring/src/test/java/org/wamblee/system/spring/SpringComponentTest.java

index 3b4725835155e3cfc558fd370967e3fd4467ebd2..8b6109f233a951cf72edae3a6eb5765abc320e2a 100644 (file)
@@ -70,6 +70,16 @@ public abstract class AbstractComponent implements Component {
                return _name;
        }
        
+       @Override
+       public void addContext(String aContext) {
+               if (_context == null ) { 
+                       _context = aContext; 
+               }
+               else { 
+                       _context = aContext + "." + _context;
+               }
+       }
+       
        @Override
        public String getQualifiedName() {
                if ( _context == null ) { 
@@ -89,16 +99,15 @@ public abstract class AbstractComponent implements Component {
        }
 
        @Override
-       public final void start(String aContext) {
-               LOG.info("Initializing '" + aContext + "." + _name + "'");
-               _context = aContext; 
-               doStart(aContext + "." + getName());
+       public final void start() {
+               LOG.info("Initializing '" + getQualifiedName() + "'");
+               doStart();
                _status = Status.RUNNING;
                if ( _running.size() != _provided.size()) { 
                        List<ProvidedInterface> remaining = 
                                new ArrayList<ProvidedInterface>(_provided);
                        remaining.removeAll(_running);
-                       throw new SystemAssemblyException(aContext + "." + getName() + ": not all services were started, missing " + remaining);
+                       throw new SystemAssemblyException(getQualifiedName() + ": not all services were started, missing " + remaining);
                }
        }
 
@@ -106,7 +115,7 @@ public abstract class AbstractComponent implements Component {
         * Must be implemented for initializing the subsystem. The implementation
         * must call {@link #addService(Service)} for each service that is started.
         */
-       protected abstract void doStart(String aContext);
+       protected abstract void doStart();
 
        /**
         * Implementations must call this method to indicate that a new service has
@@ -115,9 +124,9 @@ public abstract class AbstractComponent implements Component {
         * @param aService
         *            Service.
         */
-       protected final void addService(String aContext,
+       protected final void addService(
                        ProvidedInterface aDescriptor, Object aService) {
-               LOG.info(aContext + ": service '" + aService + "' started.");
+               LOG.info("Interface '" + getQualifiedName() + "." + aDescriptor.getName() + "' started.");
                _running.add(aDescriptor);
                aDescriptor.publish(aService);
        }
index 0234005f958be3922c8eb719ccf27323a47fda38..e06532669ce5a49c179fc5d54c0214c9b5e20d45 100644 (file)
@@ -39,6 +39,11 @@ public interface Component {
         */
        String getName();
        
+       /**
+        * Prepends the context with a super context. 
+        */
+       void addContext(String aContext);
+       
        /**
         * Gets the fully qualified name of the component which includes
         * the context of the component.  
@@ -63,9 +68,8 @@ public interface Component {
        /**
         * Initialises the subsytem by starting all the services that
         * it described as provided. 
-        * @param aContext Unique name for the subsystem.  
         */
-       void start(String aContext);
+       void start();
        
        /**
         * Stops a subsystem. 
index bd059902d5f2a5995c5bfe6cf283fbe95f315536..65d10f06e2960d74eff34b5ed63cd560d6fa1337 100644 (file)
@@ -72,6 +72,9 @@ public class Container extends AbstractComponent {
                        ProvidedInterface[] aProvided, RequiredInterface[] aRequired) {
                super(aName, aProvided, aRequired);
                _systems = aSystems;
+               for (Component component: aSystems) { 
+                       component.addContext(getQualifiedName());
+               }
                validate(aRequired);
        }
 
@@ -130,7 +133,7 @@ public class Container extends AbstractComponent {
        }
 
        @Override
-       protected void doStart(String aContext) {
+       protected void doStart() {
                List<ProvidedInterface> provided = new ArrayList<ProvidedInterface>();
                
                // all interfaces from the required list of this container are
@@ -139,7 +142,7 @@ public class Container extends AbstractComponent {
                for (RequiredInterface intf: required) { 
                    ProvidedInterface provider = intf.getProvider(); 
                    if ( provider == null ) { 
-                       throw new SystemAssemblyException(aContext + ": required interface '" + intf +"' is not provided");
+                       throw new SystemAssemblyException(getQualifiedName() + ": required interface '" + intf +"' is not provided");
                    }
                        provided.add(intf.getProvider());
                }
@@ -155,7 +158,7 @@ public class Container extends AbstractComponent {
         *            started before.
         */
        private void startImpl() {
-               LOG.info("Starting '" + "'");
+               LOG.info("Starting '" + getQualifiedName() + "'");
                List<ProvidedInterface> allProvided = new ArrayList<ProvidedInterface>();
                
                // Add the provides of all externally required interfaces to the list of available
@@ -194,7 +197,7 @@ public class Container extends AbstractComponent {
                        }
                        
                        // Start the service. 
-                       system.start(getQualifiedName());
+                       system.start();
 
                        // add all provided services
                        ProvidedInterface[] provided = system.getProvidedServices();
index 5bc6240151c410f0b0c9c07dc209d722291435a1..cb177f20259de7de21512c6576d4e30ce4f37540 100644 (file)
@@ -40,7 +40,7 @@ public class Application extends AbstractComponent {
        }
 
        @Override
-       protected void doStart(String aContext) {
+       protected void doStart() {
                track("start." + getName()); 
        }
        
index 33f3c00da080b6b42945fffbd9ba8098ae11b907..775e80064af58f5e797d4e45c0285efb166964a8 100644 (file)
@@ -82,7 +82,7 @@ public class ContainerTest extends TestCase {
                                environment, application }, new ProvidedInterface[0],
                                new RequiredInterface[0]);
                                
-               container.start("top");
+               container.start();
                AssertionUtils.assertEquals(new String[] { "start.environment",
                                "start.application" }, _tracker.getEvents(
                                Thread.currentThread()).toArray(new String[0]));
@@ -102,7 +102,7 @@ public class ContainerTest extends TestCase {
                                        new Component[] {
                                        application, environment }, 
                                        new ProvidedInterface[0], new RequiredInterface[0]);
-                       container.start("top");
+                       container.start();
                } catch (SystemAssemblyException e) {
                        // e.printStackTrace();
                        return;
@@ -121,7 +121,7 @@ public class ContainerTest extends TestCase {
                                application }, new ProvidedInterface[0],
                                new RequiredInterface[0]);
                assertEquals(Status.NOT_STARTED, system.getStatus());
-               system.start("root");
+               system.start();
                RequiredInterface[] required = system.getRequiredServices();
                assertEquals(0, required.length);
                ProvidedInterface[] provided = system.getProvidedServices();
@@ -170,7 +170,7 @@ public class ContainerTest extends TestCase {
                                        environment, application }, new ProvidedInterface[0],
                                        new RequiredInterface[] { new DefaultRequiredInterface(
                                                        "string", String.class) });
-                       system.start("root");
+                       system.start();
                } catch (SystemAssemblyException e) {
                        return;
                }
@@ -186,7 +186,7 @@ public class ContainerTest extends TestCase {
                                                "string", String.class) });
                system.getRequiredServices()[0]
                                .setProvider(new DefaultProvidedInterface("hallo", String.class));
-               system.start("root");
+               system.start();
                RequiredInterface[] required = system.getRequiredServices();
                assertEquals(1, required.length);
                ProvidedInterface[] provided = system.getProvidedServices();
@@ -200,7 +200,7 @@ public class ContainerTest extends TestCase {
                        Container system = new Container("all",
                                        new Component[] { application }, new ProvidedInterface[0],
                                        application.getRequiredServices());
-                       system.start("root");
+                       system.start();
                } catch (SystemAssemblyException e) {
                        return;
                }
@@ -215,13 +215,13 @@ public class ContainerTest extends TestCase {
                Container system = new Container("all",
                                new Component[] { application }, new ProvidedInterface[0],
                                application.getRequiredServices());
-               environment.start("env");
+               environment.start();
                system.getRequiredServices()[0].setProvider(environment
                                .getProvidedServices()[0]);
                system.getRequiredServices()[1].setProvider(environment
                                .getProvidedServices()[1]);
 
-               system.start("root");
+               system.start();
                RequiredInterface[] required = system.getRequiredServices();
                assertEquals(2, required.length);
                ProvidedInterface[] provided = system.getProvidedServices();
@@ -237,7 +237,7 @@ public class ContainerTest extends TestCase {
                        Container container = new Container("root", new Component[] {
                                        environment1, environment2, application },
                                        new ProvidedInterface[0], new RequiredInterface[0]);
-                       container.start("top");
+                       container.start();
 
                } catch (SystemAssemblyException e) {
                        return;
@@ -251,7 +251,7 @@ public class ContainerTest extends TestCase {
                        Container system = new Container("all",
                                        new Component[] { application }, new ProvidedInterface[0],
                                        new RequiredInterface[0]);
-                       system.start("root");
+                       system.start();
                } catch (SystemAssemblyException e) {
                        return;
                }
index 1fdc079e08908222604e1e4952ea4251e781ca4e..07a2814d49327314607ec2f8e957926b725d58b5 100644 (file)
@@ -41,9 +41,9 @@ public class Environment extends AbstractComponent {
        }
        
        @Override
-       protected void doStart(String aContext) {
-           addService(aContext, getProvidedServices()[0], new Integer(1));
-           addService(aContext, getProvidedServices()[1], new Integer(2));
+       protected void doStart() {
+           addService(getProvidedServices()[0], new Integer(1));
+           addService(getProvidedServices()[1], new Integer(2));
            track("start." + getName());
        }
 
index 763875f6072f770df99312748e120c9bb2b34f44..0e54deaec72ff12b194348704632f9bb8fdf7c4e 100644 (file)
@@ -100,7 +100,7 @@ public class SpringComponent extends AbstractComponent {
        }
 
        @Override
-       protected void doStart(String aContext) {
+       protected void doStart() {
 
                SpringComponent old = THIS.get();
                THIS.set(this);
@@ -117,7 +117,7 @@ public class SpringComponent extends AbstractComponent {
                                        .addBeanFactoryPostProcessor(new PropertySetter(_properties));
                        _context.refresh();
 
-                       exposeProvidedServices(aContext);
+                       exposeProvidedServices();
                } catch (Exception e) {
                        throw new SystemAssemblyException(
                                        "Failed to assemble spring system", e);
@@ -126,16 +126,16 @@ public class SpringComponent extends AbstractComponent {
                }
        }
 
-       private void exposeProvidedServices(String aContext) {
+       private void exposeProvidedServices() {
                // Call addService for each provided service.
 
                for (String name : _provided.keySet()) {
                        Object svc = _context.getBean(name);
                        if (svc == null) {
-                               throw new IllegalArgumentException(aContext + ": service '"
+                               throw new IllegalArgumentException(getQualifiedName() + ": service '"
                                                + name + "' is null");
                        }
-                       addService(aContext, _provided.get(name), svc);
+                       addService(_provided.get(name), svc);
                }
        }
 
index 97e0417e9ec9f7db9a6d55455c2cae9b28d1b790..72912fe9fa32e9b68a1244e70341f2a6cada7933 100644 (file)
@@ -46,7 +46,7 @@ public class SpringComponentTest extends TestCase {
                                new String[] { HELLO_SERVICE_SPRING_XML },
                                new HashMap<String, ProvidedInterface>(),
                                new HashMap<RequiredInterface, String>());
-               system.start("Hello");
+               system.start();
                ProvidedInterface[] services = system.getRunningServices();
                assertEquals(0, services.length);
                
@@ -61,7 +61,7 @@ public class SpringComponentTest extends TestCase {
                SpringComponent system = new SpringComponent("system", 
                                new String[] { HELLO_SERVICE_SPRING_XML }, provided,
                                new HashMap<RequiredInterface, String>());
-               system.start("Hello");
+               system.start();
                ProvidedInterface[] services = system.getRunningServices();
                assertEquals(1, services.length);
                assertTrue(services[0].getImplementation() instanceof HelloService);
@@ -82,7 +82,7 @@ public class SpringComponentTest extends TestCase {
                props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
                system.addProperties(props);
                
-               system.start("Hello");
+               system.start();
                ProvidedInterface[] services = system.getRunningServices();
                assertEquals("Property Value", 
                                ((HelloService)services[0].getImplementation()).say());
@@ -94,7 +94,7 @@ public class SpringComponentTest extends TestCase {
                                        new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
                                        new HashMap<String, ProvidedInterface>(),
                                        new HashMap<RequiredInterface, String>());
-                       system.start("Bla");
+                       system.start();
                } catch (SystemAssemblyException e) {
                        //e.printStackTrace();
                        return;
@@ -115,7 +115,7 @@ public class SpringComponentTest extends TestCase {
                helloService.publish(helloObject);
                system.getRequiredServices()[0].setProvider(helloService);
                
-               system.start("Bla");
+               system.start();
                system.stop();
        }
        
@@ -136,7 +136,7 @@ public class SpringComponentTest extends TestCase {
                        new DefaultProvidedInterface("hello", HelloService.class);
                helloService.publish(helloObject);
                system.getRequiredServices()[0].setProvider(helloService);
-               system.start("Bla");
+               system.start();
                ProvidedInterface started = system.getProvidedServices()[0];
                
            assertNotNull(started.getImplementation());