From: erik Date: Sun, 20 Apr 2008 21:54:37 +0000 (+0000) Subject: Less duplication of validation and start. Validation should be a dry-run X-Git-Tag: wamblee-utils-0.2@603~164 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=c170e9d304f3be5544f93b20142866dce266cdf8;p=utils Less duplication of validation and start. Validation should be a dry-run of starting. --- diff --git a/trunk/system/general/src/main/java/org/wamblee/system/core/Container.java b/trunk/system/general/src/main/java/org/wamblee/system/core/Container.java index b9056971..8e98755b 100644 --- a/trunk/system/general/src/main/java/org/wamblee/system/core/Container.java +++ b/trunk/system/general/src/main/java/org/wamblee/system/core/Container.java @@ -78,13 +78,12 @@ public class Container extends AbstractComponent { ProvidedInterface[] aProvided, RequiredInterface[] aRequired) { super(aName, aProvided, aRequired); _components = new ArrayList(); - + _componentNames = new HashSet(); _sealed = false; for (Component component : aComponents) { addComponent(component); } - validate(); } public Container(String aName) { @@ -144,46 +143,28 @@ public class Container extends AbstractComponent { required.addAll(Arrays.asList(component.getRequiredInterfaces())); } - validateProvidedInterfaces(provided); + validateProvidedInterfacesArePresent(provided); validateRequiredInterfaces(required); - List reallyRequired = validateRequiredProvidedMatch( - provided, required); - - String missingRequired = ""; - for (RequiredInterface service : reallyRequired) { - missingRequired += service + "\n"; - } - if (missingRequired.length() > 0) { - throw new SystemAssemblyException(getName() - + ": missing required services\n" + missingRequired); - } + addProvidersOfRequiredInterfaces(provided); + validateRequiredProvidedMatch(provided, required); } - private List validateRequiredProvidedMatch( + /** + * + * @param aProvided + * All provided interfaces. + * @param aRequired + * All required interfaces. + * @return + */ + private void validateRequiredProvidedMatch( List aProvided, List aRequired) { - List reallyRequired = new ArrayList( - aRequired); - // Compute all required interfaces that are not provided - - for (ProvidedInterface service : aProvided) { - List fulfilled = Arrays - .asList(filterRequiredServices(service, reallyRequired)); - reallyRequired.removeAll(fulfilled); - } - // Now remove all optional interfaces from the list. - for (Iterator i = reallyRequired.iterator(); i - .hasNext();) { - RequiredInterface req = i.next(); - if (req.isOptional()) { - i.remove(); - } + + for (Component component : _components) { + initializeProvidersForRequiredInterfaces(aProvided, component, true); } - // Now the remaining interfaces should be covered by the required - // list. - reallyRequired.removeAll(Arrays.asList(getRequiredInterfaces())); - return reallyRequired; } private void validateRequiredInterfaces(List aRequired) { @@ -216,7 +197,8 @@ public class Container extends AbstractComponent { } } - private void validateProvidedInterfaces(List aProvided) { + private void validateProvidedInterfacesArePresent( + List aProvided) { for (ProvidedInterface service : getProvidedInterfaces()) { // TODO provided interfaces by components could be // provide subclasses or implementations of the @@ -270,26 +252,13 @@ public class Container extends AbstractComponent { List allProvided = new ArrayList(); - // all interfaces from the required list of this container are - // provided to the components inside it. - RequiredInterface[] required = getRequiredInterfaces(); - for (RequiredInterface intf : required) { - ProvidedInterface provider = intf.getProvider(); - if (provider != null) { - allProvided.add(provider); - } else { - if (!intf.isOptional()) { - throw new SystemAssemblyException(getQualifiedName() - + ": required interface '" + intf - + "' is not provided"); - } - } - } + addProvidersOfRequiredInterfaces(allProvided); List started = new ArrayList(); for (Component component : _components) { try { - checkAllRequiredServicesAlreadyProvided(allProvided, component); + initializeProvidersForRequiredInterfaces(allProvided, + component, false); // Start the service. Object runtime = component.start(scope); @@ -312,6 +281,25 @@ public class Container extends AbstractComponent { return scope; } + private void addProvidersOfRequiredInterfaces( + List allProvided) { + // all interfaces from the required list of this container are + // provided to the components inside it. + RequiredInterface[] required = getRequiredInterfaces(); + for (RequiredInterface intf : required) { + ProvidedInterface provider = intf.getProvider(); + if (provider != null) { + allProvided.add(provider); + } else { + if (!intf.isOptional()) { + throw new SystemAssemblyException(getQualifiedName() + + ": required interface '" + intf + + "' is not provided"); + } + } + } + } + private void stopAlreadyStartedComponents(List aStarted, Scope aScope) { // an exception occurred, stop the successfully started @@ -326,8 +314,20 @@ public class Container extends AbstractComponent { } } - private void checkAllRequiredServicesAlreadyProvided( - List aAllProvided, Component aComponent) { + /** + * Sets the provided interface or a component. + * + * @param aAllProvided + * All available provided interfaces. + * @param aComponent + * Component whose required interfaces we are looking at. + * @param aValidateOnly + * If true then the provider will not be set for required + * interfaces. + */ + private void initializeProvidersForRequiredInterfaces( + List aAllProvided, Component aComponent, + boolean aValidateOnly) { // Check if all required services are already provided by // earlier // systems. @@ -336,7 +336,9 @@ public class Container extends AbstractComponent { ProvidedInterface[] filtered = filterProvidedServices(descriptor, aAllProvided); if (filtered.length == 1) { - descriptor.setProvider(filtered[0]); + if (!aValidateOnly) { + descriptor.setProvider(filtered[0]); + } } else if (filtered.length > 1) { throw new SystemAssemblyException( "Service '" diff --git a/trunk/system/general/src/test/java/org/wamblee/system/core/ContainerTest.java b/trunk/system/general/src/test/java/org/wamblee/system/core/ContainerTest.java index 8b6c61e8..90723ed1 100644 --- a/trunk/system/general/src/test/java/org/wamblee/system/core/ContainerTest.java +++ b/trunk/system/general/src/test/java/org/wamblee/system/core/ContainerTest.java @@ -84,7 +84,6 @@ public class ContainerTest extends TestCase { Container container = new Container("root", new Component[] { environment, application }, new ProvidedInterface[0], new RequiredInterface[0]); - Scope scope = container.start(); assertTrue(container.isSealed()); AssertionUtils.assertEquals(new String[] { "start.environment", @@ -164,6 +163,7 @@ public class ContainerTest extends TestCase { new ProvidedInterface[] { new DefaultProvidedInterface( "float", Float.class) }, new DefaultRequiredInterface[0]); + system.validate(); } catch (SystemAssemblyException e) { return; }