From af278dacffa6bdfa1194c329c067699e9d2b85e8 Mon Sep 17 00:00:00 2001 From: rfeng Date: Mon, 14 Sep 2009 22:57:46 +0000 Subject: Add a module to hold the builder for external policy attachment git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@814899 13f79535-47bb-0310-9956-ffa450edef68 --- .../modules/policy-builder/META-INF/MANIFEST.MF | 26 +++ java/sca/modules/policy-builder/pom.xml | 70 +++++++ .../builder/impl/PolicyAttachmentBuilderImpl.java | 117 ++++++++++++ ...e.tuscany.sca.assembly.builder.CompositeBuilder | 17 ++ .../sca/policy/builder/impl/MockPolicy.java | 39 ++++ .../builder/impl/PolicyAttachmentTestCase.java | 92 +++++++++ .../policy/builder/impl/TestPolicyProcessor.java | 57 ++++++ .../sca/policy/builder/impl/Calculator.composite | 49 +++++ .../sca/policy/builder/impl/test_definitions.xml | 207 +++++++++++++++++++++ 9 files changed, 674 insertions(+) create mode 100644 java/sca/modules/policy-builder/META-INF/MANIFEST.MF create mode 100644 java/sca/modules/policy-builder/pom.xml create mode 100644 java/sca/modules/policy-builder/src/main/java/org/apache/tuscany/sca/policy/builder/impl/PolicyAttachmentBuilderImpl.java create mode 100644 java/sca/modules/policy-builder/src/main/resources/META-INF/services/org.apache.tuscany.sca.assembly.builder.CompositeBuilder create mode 100644 java/sca/modules/policy-builder/src/test/java/org/apache/tuscany/sca/policy/builder/impl/MockPolicy.java create mode 100644 java/sca/modules/policy-builder/src/test/java/org/apache/tuscany/sca/policy/builder/impl/PolicyAttachmentTestCase.java create mode 100644 java/sca/modules/policy-builder/src/test/java/org/apache/tuscany/sca/policy/builder/impl/TestPolicyProcessor.java create mode 100644 java/sca/modules/policy-builder/src/test/resources/org/apache/tuscany/sca/policy/builder/impl/Calculator.composite create mode 100644 java/sca/modules/policy-builder/src/test/resources/org/apache/tuscany/sca/policy/builder/impl/test_definitions.xml (limited to 'java/sca/modules') diff --git a/java/sca/modules/policy-builder/META-INF/MANIFEST.MF b/java/sca/modules/policy-builder/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..5651b19418 --- /dev/null +++ b/java/sca/modules/policy-builder/META-INF/MANIFEST.MF @@ -0,0 +1,26 @@ +Manifest-Version: 1.0 +Bundle-Name: Apache Tuscany SCA Policy Attachment Builder +Created-By: 1.6.0_07 (Sun Microsystems Inc.) +Bundle-Vendor: The Apache Software Foundation +Bundle-Version: 2.0.0 +Bnd-LastModified: 1225397097203 +Bundle-ManifestVersion: 2 +Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt +Bundle-Description: Apache Tuscany SCA Assembly Model +Import-Package: javax.xml.namespace, + javax.xml.xpath, + org.apache.tuscany.sca.assembly;version="2.0.0", + org.apache.tuscany.sca.assembly.builder;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.xpath;version="2.0.0", + org.apache.tuscany.sca.contribution.processor;version="2.0.0", + org.apache.tuscany.sca.contribution.resolver;version="2.0.0";resolution:=optional, + org.apache.tuscany.sca.core;version="2.0.0", + org.apache.tuscany.sca.definitions;version="2.0.0", + org.apache.tuscany.sca.monitor;version="2.0.0", + org.apache.tuscany.sca.policy;version="2.0.0", + org.w3c.dom +Bundle-SymbolicName: org.apache.tuscany.sca.policy.builder +Bundle-DocURL: http://www.apache.org/ +Bundle-RequiredExecutionEnvironment: J2SE-1.5,JavaSE-1.6 diff --git a/java/sca/modules/policy-builder/pom.xml b/java/sca/modules/policy-builder/pom.xml new file mode 100644 index 0000000000..5456a6aa26 --- /dev/null +++ b/java/sca/modules/policy-builder/pom.xml @@ -0,0 +1,70 @@ + + + + 4.0.0 + + org.apache.tuscany.sca + tuscany-modules + 2.0-SNAPSHOT + ../pom.xml + + tuscany-policy-builder + Apache Tuscany SCA Policy Attachment Builder + + + + + org.apache.tuscany.sca + tuscany-assembly + 2.0-SNAPSHOT + + + + org.apache.tuscany.sca + tuscany-assembly-xml + 2.0-SNAPSHOT + test + + + + org.apache.tuscany.sca + tuscany-definitions-xml + 2.0-SNAPSHOT + test + + + + org.apache.tuscany.sca + tuscany-implementation-java + 2.0-SNAPSHOT + test + + + + + org.apache.tuscany.sca + tuscany-contribution + 2.0-SNAPSHOT + + + + + + diff --git a/java/sca/modules/policy-builder/src/main/java/org/apache/tuscany/sca/policy/builder/impl/PolicyAttachmentBuilderImpl.java b/java/sca/modules/policy-builder/src/main/java/org/apache/tuscany/sca/policy/builder/impl/PolicyAttachmentBuilderImpl.java new file mode 100644 index 0000000000..e2550c1e88 --- /dev/null +++ b/java/sca/modules/policy-builder/src/main/java/org/apache/tuscany/sca/policy/builder/impl/PolicyAttachmentBuilderImpl.java @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.policy.builder.impl; + +import java.io.StringWriter; + +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamWriter; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper; +import org.apache.tuscany.sca.common.xml.xpath.XPathHelper; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.definitions.Definitions; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySubject; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * A composite builder that computes policy sets based on attached intents and policy sets. + * Useful if you want to build the model without making any runtime decisions such as + * reference/services matching + * + * @version $Rev$ $Date$ + */ +public class PolicyAttachmentBuilderImpl implements CompositeBuilder { + private XMLOutputFactory xmlOutputFactory; + private StAXHelper staxHelper; + private DOMHelper domHelper; + private XPathHelper xpathHelper; + private StAXArtifactProcessor processor; + + public PolicyAttachmentBuilderImpl(ExtensionPointRegistry registry) { + xmlOutputFactory = registry.getExtensionPoint(FactoryExtensionPoint.class).getFactory(XMLOutputFactory.class); + domHelper = DOMHelper.getInstance(registry); + xpathHelper = XPathHelper.getInstance(registry); + staxHelper = StAXHelper.getInstance(registry); + StAXArtifactProcessorExtensionPoint processors = + registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + processor = processors.getProcessor(Composite.class); + } + + public String getID() { + return "org.apache.tuscany.sca.policy.builder.PolicyAttachmentBuilder"; + } + + public void build(Composite composite, Definitions definitions, Monitor monitor) throws CompositeBuilderException { + try { + applyXPath(composite, definitions, monitor); + } catch (Exception e) { + throw new CompositeBuilderException(e); + } + } + + private void applyXPath(Composite composite, Definitions definitions, Monitor monitor) throws Exception { + // First write the composite into a DOM document so that we can apply the xpath + StringWriter sw = new StringWriter(); + XMLStreamWriter writer = staxHelper.createXMLStreamWriter(sw); + // Write the composite into a DOM document + processor.write(composite, writer); + writer.close(); + + Document document = domHelper.load(sw.toString()); + + for (PolicySet ps : definitions.getPolicySets()) { + XPathExpression exp = ps.getAttachToXPathExpression(); + if (exp != null) { + NodeList nodes = (NodeList)exp.evaluate(document, XPathConstants.NODESET); + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + // The node can be a component, service, reference or binding + node.getNamespaceURI(); + // Use the node to find the corresponding element in the java model + } + } + } + } + + /** + * Look up the corresponding Java model within the composite based on the DOM node + * @param composite + * @param node + * @return + */ + private PolicySubject lookup(Composite composite, Node node) { + return null; + } + +} diff --git a/java/sca/modules/policy-builder/src/main/resources/META-INF/services/org.apache.tuscany.sca.assembly.builder.CompositeBuilder b/java/sca/modules/policy-builder/src/main/resources/META-INF/services/org.apache.tuscany.sca.assembly.builder.CompositeBuilder new file mode 100644 index 0000000000..01d77e63e5 --- /dev/null +++ b/java/sca/modules/policy-builder/src/main/resources/META-INF/services/org.apache.tuscany.sca.assembly.builder.CompositeBuilder @@ -0,0 +1,17 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +org.apache.tuscany.sca.policy.builder.impl.PolicyAttachmentBuilderImpl;id=org.apache.tuscany.sca.policy.builder.PolicyAttachmentBuilder diff --git a/java/sca/modules/policy-builder/src/test/java/org/apache/tuscany/sca/policy/builder/impl/MockPolicy.java b/java/sca/modules/policy-builder/src/test/java/org/apache/tuscany/sca/policy/builder/impl/MockPolicy.java new file mode 100644 index 0000000000..c3c293cdf8 --- /dev/null +++ b/java/sca/modules/policy-builder/src/test/java/org/apache/tuscany/sca/policy/builder/impl/MockPolicy.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.policy.builder.impl; + +import javax.xml.namespace.QName; + +/** + * Mocked Policy + */ +public class MockPolicy { + public QName getName() { + return new QName("http://schemas.xmlsoap.org/ws/2004/09/policy", "PolicyAttachment"); + } + + public boolean isUnresolved() { + return false; + } + + public void setUnresolved(boolean unresolved) { + } + +} diff --git a/java/sca/modules/policy-builder/src/test/java/org/apache/tuscany/sca/policy/builder/impl/PolicyAttachmentTestCase.java b/java/sca/modules/policy-builder/src/test/java/org/apache/tuscany/sca/policy/builder/impl/PolicyAttachmentTestCase.java new file mode 100644 index 0000000000..d2c3f4eeec --- /dev/null +++ b/java/sca/modules/policy-builder/src/test/java/org/apache/tuscany/sca/policy/builder/impl/PolicyAttachmentTestCase.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.sca.policy.builder.impl; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +import javax.xml.stream.FactoryConfigurationError; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.definitions.Definitions; +import org.apache.tuscany.sca.monitor.DefaultMonitorFactory; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.MonitorFactory; +import org.junit.Test; + +/** + * Test reading SCA XML assembly documents. + * + * @version $Rev$ $Date$ + */ +public class PolicyAttachmentTestCase { + + private StAXArtifactProcessor staxProcessor; + private Monitor monitor; + + @Test + public void testBuild() throws FactoryConfigurationError, IOException, XMLStreamException, + ContributionReadException, CompositeBuilderException { + DefaultExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + // Create a monitor + UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); + MonitorFactory monitorFactory = new DefaultMonitorFactory(); + if (monitorFactory != null) { + monitor = monitorFactory.createMonitor(); + utilities.addUtility(monitorFactory); + } + StAXArtifactProcessorExtensionPoint staxProcessors = + extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, null, monitor); + staxProcessors.addArtifactProcessor(new TestPolicyProcessor()); + + URL url = getClass().getResource("test_definitions.xml"); + InputStream urlStream = url.openStream(); + XMLStreamReader reader = inputFactory.createXMLStreamReader(urlStream); + reader.nextTag(); + + Definitions definitions = (Definitions)staxProcessor.read(reader); + reader.close(); + + url = getClass().getResource("Calculator.composite"); + urlStream = url.openStream(); + reader = inputFactory.createXMLStreamReader(urlStream); + reader.nextTag(); + + Composite composite = (Composite)staxProcessor.read(reader); + reader.close(); + + PolicyAttachmentBuilderImpl builder = new PolicyAttachmentBuilderImpl(extensionPoints); + builder.build(composite, definitions, monitor); + } + +} diff --git a/java/sca/modules/policy-builder/src/test/java/org/apache/tuscany/sca/policy/builder/impl/TestPolicyProcessor.java b/java/sca/modules/policy-builder/src/test/java/org/apache/tuscany/sca/policy/builder/impl/TestPolicyProcessor.java new file mode 100644 index 0000000000..8a39e70f6b --- /dev/null +++ b/java/sca/modules/policy-builder/src/test/java/org/apache/tuscany/sca/policy/builder/impl/TestPolicyProcessor.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.sca.policy.builder.impl; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; + +/** + * A PolicyProcessor used for testing. + * + * @version $Rev$ $Date$ + */ +public class TestPolicyProcessor implements StAXArtifactProcessor { + public QName getArtifactType() { + return new QName("http://schemas.xmlsoap.org/ws/2004/09/policy", "PolicyAttachment"); + } + + public MockPolicy read(XMLStreamReader arg0) throws ContributionReadException, XMLStreamException { + return new MockPolicy(); + } + + public void write(MockPolicy arg0, XMLStreamWriter arg1) throws ContributionWriteException, XMLStreamException { + } + + public Class getModelType() { + return MockPolicy.class; + } + + public void resolve(MockPolicy arg0, ModelResolver arg1) throws ContributionResolveException { + + } + +} diff --git a/java/sca/modules/policy-builder/src/test/resources/org/apache/tuscany/sca/policy/builder/impl/Calculator.composite b/java/sca/modules/policy-builder/src/test/resources/org/apache/tuscany/sca/policy/builder/impl/Calculator.composite new file mode 100644 index 0000000000..d6082a936d --- /dev/null +++ b/java/sca/modules/policy-builder/src/test/resources/org/apache/tuscany/sca/policy/builder/impl/Calculator.composite @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/java/sca/modules/policy-builder/src/test/resources/org/apache/tuscany/sca/policy/builder/impl/test_definitions.xml b/java/sca/modules/policy-builder/src/test/resources/org/apache/tuscany/sca/policy/builder/impl/test_definitions.xml new file mode 100644 index 0000000000..dac8e384e3 --- /dev/null +++ b/java/sca/modules/policy-builder/src/test/resources/org/apache/tuscany/sca/policy/builder/impl/test_definitions.xml @@ -0,0 +1,207 @@ + + + + + + + + + + + + Test Intent + + + + + + Protect messages from unauthorized reading or modification + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ... + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ... + + + + + ... + + + + + + + + + + + sp:X509v3 + + + + + + + + + + + + + + + + + + Protect messages from unauthorized reading or modification + + + + + + Protect messages from unauthorized reading or modification + + + + + + + Communitcation thro this binding must prevent + unauthorized users from reading the messages. + + + + + + + + Communitcation thro this binding must prevent + unauthorized modification of the messages. + + + + + + Communitcation thro this binding required + Authentication. + + + + + + All messages to and from this implementation must be logged + + + + + + Need to figure out some description for this + + + + \ No newline at end of file -- cgit v1.2.3