Add logic to apply the xpath against the implementation composites

Add a more complicated test case

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@817714 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
rfeng 2009-09-22 16:11:11 +00:00
parent 399df0e965
commit 86163b4ee4
4 changed files with 94 additions and 22 deletions

View file

@ -11,6 +11,7 @@ Import-Package: javax.xml.namespace,
javax.xml.xpath, javax.xml.xpath,
org.apache.tuscany.sca.assembly;version="2.0.0", org.apache.tuscany.sca.assembly;version="2.0.0",
org.apache.tuscany.sca.assembly.builder;version="2.0.0", org.apache.tuscany.sca.assembly.builder;version="2.0.0",
org.apache.tuscany.sca.assembly.builder.impl;version="2.0.0";resolution:=optional,
org.apache.tuscany.sca.common.xml.dom;version="2.0.0", org.apache.tuscany.sca.common.xml.dom;version="2.0.0",
org.apache.tuscany.sca.common.xml.stax;version="2.0.0", org.apache.tuscany.sca.common.xml.stax;version="2.0.0",
org.apache.tuscany.sca.common.xml.xpath;version="2.0.0", org.apache.tuscany.sca.common.xml.xpath;version="2.0.0",

View file

@ -33,7 +33,9 @@ import javax.xml.stream.XMLStreamWriter;
import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpression;
import org.apache.tuscany.sca.assembly.Component;
import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.assembly.Composite;
import org.apache.tuscany.sca.assembly.Implementation;
import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; import org.apache.tuscany.sca.assembly.builder.CompositeBuilder;
import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException;
import org.apache.tuscany.sca.common.xml.dom.DOMHelper; import org.apache.tuscany.sca.common.xml.dom.DOMHelper;
@ -93,6 +95,16 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder {
* @throws Exception * @throws Exception
*/ */
private Composite applyXPath(Composite composite, Definitions definitions, Monitor monitor) throws Exception { private Composite applyXPath(Composite composite, Definitions definitions, Monitor monitor) throws Exception {
// Recursively apply the xpath against the composites referenced by <implementation.composite>
for (Component component : composite.getComponents()) {
Implementation impl = component.getImplementation();
if (impl instanceof Composite) {
Composite patched = applyXPath((Composite)impl, definitions, monitor);
if (patched != impl) {
component.setImplementation(patched);
}
}
}
// First write the composite into a DOM document so that we can apply the xpath // First write the composite into a DOM document so that we can apply the xpath
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
XMLStreamWriter writer = staxHelper.createXMLStreamWriter(sw); XMLStreamWriter writer = staxHelper.createXMLStreamWriter(sw);

View file

@ -19,22 +19,35 @@
package org.apache.tuscany.sca.policy.builder.impl; package org.apache.tuscany.sca.policy.builder.impl;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamReader;
import org.apache.tuscany.sca.assembly.AssemblyFactory;
import org.apache.tuscany.sca.assembly.Base;
import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.assembly.Composite;
import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint;
import org.apache.tuscany.sca.assembly.builder.CompositeBuilder;
import org.apache.tuscany.sca.assembly.builder.impl.CompositeCloneBuilderImpl;
import org.apache.tuscany.sca.assembly.builder.impl.CompositeIncludeBuilderImpl;
import org.apache.tuscany.sca.contribution.processor.ContributionReadException;
import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor;
import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint;
import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry;
import org.apache.tuscany.sca.core.ExtensionPointRegistry;
import org.apache.tuscany.sca.core.FactoryExtensionPoint;
import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.apache.tuscany.sca.core.UtilityExtensionPoint;
import org.apache.tuscany.sca.definitions.Definitions; import org.apache.tuscany.sca.definitions.Definitions;
import org.apache.tuscany.sca.monitor.DefaultMonitorFactory; import org.apache.tuscany.sca.monitor.DefaultMonitorFactory;
import org.apache.tuscany.sca.monitor.Monitor; import org.apache.tuscany.sca.monitor.Monitor;
import org.apache.tuscany.sca.monitor.MonitorFactory; import org.apache.tuscany.sca.monitor.MonitorFactory;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
/** /**
@ -44,13 +57,20 @@ import org.junit.Test;
*/ */
public class PolicyAttachmentTestCase { public class PolicyAttachmentTestCase {
private StAXArtifactProcessor<Object> staxProcessor; private static StAXArtifactProcessor<Object> staxProcessor;
private Monitor monitor; private static Monitor monitor;
@Test private static ExtensionPointRegistry extensionPoints;
public void testBuild() throws Exception { private static XMLInputFactory inputFactory;
DefaultExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); private static AssemblyFactory assemblyFactory;
XMLInputFactory inputFactory = XMLInputFactory.newInstance(); private static BuilderExtensionPoint builders;
@BeforeClass
public static void init() throws Exception {
extensionPoints = new DefaultExtensionPointRegistry();
FactoryExtensionPoint factories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class);
assemblyFactory = factories.getFactory(AssemblyFactory.class);
inputFactory = factories.getFactory(XMLInputFactory.class);
// Create a monitor // Create a monitor
UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class);
MonitorFactory monitorFactory = new DefaultMonitorFactory(); MonitorFactory monitorFactory = new DefaultMonitorFactory();
@ -63,24 +83,63 @@ public class PolicyAttachmentTestCase {
staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, null, monitor); staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, null, monitor);
staxProcessors.addArtifactProcessor(new TestPolicyProcessor()); staxProcessors.addArtifactProcessor(new TestPolicyProcessor());
URL url = getClass().getResource("test_definitions.xml"); builders = extensionPoints.getExtensionPoint(BuilderExtensionPoint.class);
InputStream urlStream = url.openStream(); }
XMLStreamReader reader = inputFactory.createXMLStreamReader(urlStream);
reader.nextTag();
Definitions definitions = (Definitions)staxProcessor.read(reader); @Test
reader.close(); public void testBuild() throws Exception {
Definitions definitions = load("test_definitions.xml");
url = getClass().getResource("Calculator.composite"); Composite composite = load("Calculator.composite");
urlStream = url.openStream();
reader = inputFactory.createXMLStreamReader(urlStream);
reader.nextTag();
Composite composite = (Composite)staxProcessor.read(reader);
reader.close();
PolicyAttachmentBuilderImpl builder = new PolicyAttachmentBuilderImpl(extensionPoints); PolicyAttachmentBuilderImpl builder = new PolicyAttachmentBuilderImpl(extensionPoints);
builder.build(composite, definitions, monitor); builder.build(composite, definitions, monitor);
} }
private <T> T load(String file) throws IOException, XMLStreamException, ContributionReadException {
URL url = getClass().getResource(file);
InputStream urlStream = url.openStream();
XMLStreamReader reader = inputFactory.createXMLStreamReader(urlStream);
reader.nextTag();
T model = (T)staxProcessor.read(reader);
reader.close();
return model;
}
@Test
public void testComplexBuild() throws Exception {
Definitions definitions = load("definitions.xml");
Composite composite1 = load("Composite1.composite");
Composite composite2 = load("Composite2.composite");
Composite composite3 = load("Composite3.composite");
Composite composite4 = load("Composite4.composite");
composite1.getIncludes().clear();
composite1.getIncludes().add(composite3);
composite1.getComponent("Component1B").setImplementation(composite4);
composite2.getComponent("Component2B").setImplementation(composite4);
Composite domainComposite = assemblyFactory.createComposite();
domainComposite.setName(new QName(Base.SCA11_NS, ""));
domainComposite.getIncludes().add(composite1);
domainComposite.getIncludes().add(composite2);
CompositeBuilder includeBuilder = new CompositeIncludeBuilderImpl();
CompositeBuilder cloneBuilder = new CompositeCloneBuilderImpl();
/*
CompositeBuilder includeBuilder =
builders.getCompositeBuilder("org.apache.tuscany.sca.assembly.builder.CompositeIncludeBuilder");
CompositeBuilder cloneBuilder =
builders.getCompositeBuilder("org.apache.tuscany.sca.assembly.builder.CompositeCloneBuilder");
*/
includeBuilder.build(domainComposite, definitions, monitor);
cloneBuilder.build(domainComposite, definitions, monitor);
PolicyAttachmentBuilderImpl builder = new PolicyAttachmentBuilderImpl(extensionPoints);
builder.build(domainComposite, definitions, monitor);
}
} }

View file

@ -24,7 +24,7 @@
xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200903"> xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200903">
<policySet name="PolicySet1" appliesTo="sca:implementation.java" <policySet name="PolicySet1" appliesTo="sca:implementation.java"
attachTo = "//composite[@name='Composite1' or @name='Composite2']"> attachTo = "//composite[@name='']">
</policySet> </policySet>
<policySet name="PolicySet2" appliesTo="sca:implementation.java" <policySet name="PolicySet2" appliesTo="sca:implementation.java"
@ -32,7 +32,7 @@
</policySet> </policySet>
<policySet name="PolicySet3" appliesTo="sca:binding.ws" <policySet name="PolicySet3" appliesTo="sca:binding.ws"
attachTo = "//sca:component[URIRef('Component2B/Component4A)]"> attachTo = "//sca:component[sca:URIRef('Component2B/Component4A')]">
</policySet> </policySet>
</definitions> </definitions>