From fbc49e79596b00f3565f87383058734a00a43af0 Mon Sep 17 00:00:00 2001 From: erik Date: Wed, 9 Apr 2008 18:25:37 +0000 Subject: [PATCH] Better solution for setting the context. The context is now known as soon as components are added. --- .../org/wamblee/system/AbstractComponent.java | 25 +++++++++++++------ .../java/org/wamblee/system/Component.java | 8 ++++-- .../java/org/wamblee/system/Container.java | 11 +++++--- .../java/org/wamblee/system/Application.java | 2 +- .../org/wamblee/system/ContainerTest.java | 20 +++++++-------- .../java/org/wamblee/system/Environment.java | 6 ++--- .../system/spring/SpringComponent.java | 10 ++++---- .../system/spring/SpringComponentTest.java | 12 ++++----- 8 files changed, 55 insertions(+), 39 deletions(-) diff --git a/trunk/system/general/src/main/java/org/wamblee/system/AbstractComponent.java b/trunk/system/general/src/main/java/org/wamblee/system/AbstractComponent.java index 3b472583..8b6109f2 100644 --- a/trunk/system/general/src/main/java/org/wamblee/system/AbstractComponent.java +++ b/trunk/system/general/src/main/java/org/wamblee/system/AbstractComponent.java @@ -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 remaining = new ArrayList(_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); } diff --git a/trunk/system/general/src/main/java/org/wamblee/system/Component.java b/trunk/system/general/src/main/java/org/wamblee/system/Component.java index 0234005f..e0653266 100644 --- a/trunk/system/general/src/main/java/org/wamblee/system/Component.java +++ b/trunk/system/general/src/main/java/org/wamblee/system/Component.java @@ -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. diff --git a/trunk/system/general/src/main/java/org/wamblee/system/Container.java b/trunk/system/general/src/main/java/org/wamblee/system/Container.java index bd059902..65d10f06 100644 --- a/trunk/system/general/src/main/java/org/wamblee/system/Container.java +++ b/trunk/system/general/src/main/java/org/wamblee/system/Container.java @@ -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 provided = new ArrayList(); // 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 allProvided = new ArrayList(); // 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(); diff --git a/trunk/system/general/src/test/java/org/wamblee/system/Application.java b/trunk/system/general/src/test/java/org/wamblee/system/Application.java index 5bc62401..cb177f20 100644 --- a/trunk/system/general/src/test/java/org/wamblee/system/Application.java +++ b/trunk/system/general/src/test/java/org/wamblee/system/Application.java @@ -40,7 +40,7 @@ public class Application extends AbstractComponent { } @Override - protected void doStart(String aContext) { + protected void doStart() { track("start." + getName()); } diff --git a/trunk/system/general/src/test/java/org/wamblee/system/ContainerTest.java b/trunk/system/general/src/test/java/org/wamblee/system/ContainerTest.java index 33f3c00d..775e8006 100644 --- a/trunk/system/general/src/test/java/org/wamblee/system/ContainerTest.java +++ b/trunk/system/general/src/test/java/org/wamblee/system/ContainerTest.java @@ -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; } diff --git a/trunk/system/general/src/test/java/org/wamblee/system/Environment.java b/trunk/system/general/src/test/java/org/wamblee/system/Environment.java index 1fdc079e..07a2814d 100644 --- a/trunk/system/general/src/test/java/org/wamblee/system/Environment.java +++ b/trunk/system/general/src/test/java/org/wamblee/system/Environment.java @@ -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()); } diff --git a/trunk/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java b/trunk/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java index 763875f6..0e54deae 100644 --- a/trunk/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java +++ b/trunk/system/spring/src/main/java/org/wamblee/system/spring/SpringComponent.java @@ -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); } } diff --git a/trunk/system/spring/src/test/java/org/wamblee/system/spring/SpringComponentTest.java b/trunk/system/spring/src/test/java/org/wamblee/system/spring/SpringComponentTest.java index 97e0417e..72912fe9 100644 --- a/trunk/system/spring/src/test/java/org/wamblee/system/spring/SpringComponentTest.java +++ b/trunk/system/spring/src/test/java/org/wamblee/system/spring/SpringComponentTest.java @@ -46,7 +46,7 @@ public class SpringComponentTest extends TestCase { new String[] { HELLO_SERVICE_SPRING_XML }, new HashMap(), new HashMap()); - 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()); - 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(), new HashMap()); - 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()); -- 2.31.1