diff options
Diffstat (limited to 'sca-java-2.x/trunk')
2 files changed, 183 insertions, 96 deletions
diff --git a/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java b/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java new file mode 100644 index 0000000000..2417940b1c --- /dev/null +++ b/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java @@ -0,0 +1,171 @@ +/* + * 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.builder.impl; + + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +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 builder that checks that policy sets apply to the elements to which they are attached. + * Any that don't are removed. It first creates a DOM model for the composite so that the xpath + * expression can be evaluated. For each element that holds a policy set is calculates the + * appliesTo nodes and checks that the current element is in the set. If not the policySet is + * removed from the element + * + * @version $Rev$ $Date$ + */ +public class PolicyAppliesToBuilderImpl extends PolicyAttachmentBuilderImpl { + + public PolicyAppliesToBuilderImpl(ExtensionPointRegistry registry) { + super(registry); + } + + public String getID() { + return "org.apache.tuscany.sca.policy.builder.PolicyAppliesToBuilder"; + } + + public Composite build(Composite composite, BuilderContext context) + throws CompositeBuilderException { + try { + // create a DOM for the Domain Composite Infoset + Document document = saveAsDOM(composite); + + // create a cache of evaluated node against each policy set so we don't + // have to keep evaluating policy sets that appear in multiple places + Map<PolicySet, List<PolicySubject>> appliesToSubjects = new HashMap<PolicySet, List<PolicySubject>>(); + + // for all implementations, endpoint and endpoint references check that + // the policy sets validly apply + return checkAppliesTo(document, appliesToSubjects, composite, context); + + } catch (Exception e) { + throw new CompositeBuilderException(e); + } + } + + private Composite checkAppliesTo(Document document, Map<PolicySet, List<PolicySubject>> appliesToSubjects, Composite composite, BuilderContext context) throws Exception { + // look at policies recursively + for (Component component : composite.getComponents()) { + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + checkAppliesTo(document, appliesToSubjects, (Composite)implementation, context); + } + } + + for (Component component : composite.getComponents()) { + + for (ComponentService componentService : component.getServices()) { + for (Endpoint ep : componentService.getEndpoints()) { + if (ep.getBinding() instanceof PolicySubject) { + checkAppliesToSubject(document, appliesToSubjects, composite, (PolicySubject)ep.getBinding(), ep.getPolicySets()); + } + } + } + + for (ComponentReference componentReference : component.getReferences()) { + for (EndpointReference epr : componentReference.getEndpointReferences()) { + if (epr.getBinding() instanceof PolicySubject) { + checkAppliesToSubject(document, appliesToSubjects, composite, (PolicySubject)epr.getBinding(), epr.getPolicySets()); + } + } + } + + Implementation implementation = component.getImplementation(); + if (implementation != null && + implementation instanceof PolicySubject) { + checkAppliesToSubject(document, appliesToSubjects, composite, implementation, implementation.getPolicySets()); + } + } + + return composite; + } + + /** + * Checks that all the provided policy sets apply to the provided policy subject + * + * @param document + * @param appliesToSubjects + * @param policySubject + * @param policySets + * @return + * @throws Exception + */ + private void checkAppliesToSubject(Document document, Map<PolicySet, List<PolicySubject>> appliesToSubjects, Composite composite, PolicySubject policySubject, List<PolicySet> policySets) throws Exception { + List<PolicySet> policySetsToRemove = new ArrayList<PolicySet>(); + + for (PolicySet policySet : policySets){ + List<PolicySubject> subjects = appliesToSubjects.get(policySet); + + if (subjects == null){ + XPathExpression appliesTo = policySet.getAppliesToXPathExpression(); + if (appliesTo != null) { + NodeList nodes = (NodeList)appliesTo.evaluate(document, XPathConstants.NODESET); + + if (nodes.getLength() > 0){ + subjects = new ArrayList<PolicySubject>(); + appliesToSubjects.put(policySet, subjects); + } + + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + String index = getStructuralURI(node); + PolicySubject subject = lookup(composite, index); + subjects.add(subject); + } + } + } + + if (subjects != null){ + if (!subjects.contains(policySubject)){ + policySetsToRemove.add(policySet); + } + } + + // TODO - If no "appliesTo" is provided does the policy set apply to + // everything or nothing? + + } + + policySets.removeAll(policySetsToRemove); + } +} diff --git a/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java b/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java index c8f4461cdd..e9a49c8842 100644 --- a/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java +++ b/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java @@ -19,14 +19,9 @@ package org.apache.tuscany.sca.builder.impl; -import static javax.xml.XMLConstants.DEFAULT_NS_PREFIX; -import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE; -import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI; - import java.io.IOException; import java.io.StringWriter; import java.util.Set; -import java.util.StringTokenizer; import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamException; @@ -56,7 +51,6 @@ 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.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -66,18 +60,18 @@ import org.xml.sax.SAXException; /** * A builder that attaches policy sets to the domain composite using the xpath defined by * the attachTo attribute. It first creates a DOM model for the composite so that the xpath - * expression can be evaluated. For the nodes selected by the xpath, add the policySets attribute - * to the subject element. Then reload the patched DOM into a Composite model again. + * expression can be evaluated. For the nodes selected by the xpath, caluclate the element + * URI and add the policy set into the composite model * * @version $Rev$ $Date$ */ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { - private static final String BUILDER_VALIDATION_BUNDLE = "org.apache.tuscany.sca.builder.builder-validation-messages"; + protected static final String BUILDER_VALIDATION_BUNDLE = "org.apache.tuscany.sca.builder.builder-validation-messages"; - private StAXHelper staxHelper; - private DOMHelper domHelper; - private ExtensionPointRegistry registry; - private StAXArtifactProcessor<Composite> processor; + protected StAXHelper staxHelper; + protected DOMHelper domHelper; + protected ExtensionPointRegistry registry; + protected StAXArtifactProcessor<Composite> processor; public PolicyAttachmentBuilderImpl(ExtensionPointRegistry registry) { this.registry = registry; @@ -197,7 +191,7 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { } } - private Document saveAsDOM(Composite composite) throws XMLStreamException, ContributionWriteException, IOException, + protected Document saveAsDOM(Composite composite) throws XMLStreamException, ContributionWriteException, IOException, SAXException { // First write the composite into a DOM document so that we can apply the xpath StringWriter sw = new StringWriter(); @@ -218,7 +212,7 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { private static final QName SERVICE = new QName(Base.SCA11_NS, "service"); private static final QName REFERENCE = new QName(Base.SCA11_NS, "reference"); - private static String getStructuralURI(Node node) { + protected static String getStructuralURI(Node node) { if (node != null) { QName name = new QName(node.getNamespaceURI(), node.getLocalName()); if (COMPONENT.equals(name)) { @@ -253,7 +247,7 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { return null; } - private Binding getBinding(Contract contract, String name) { + protected Binding getBinding(Contract contract, String name) { for (Binding binding : contract.getBindings()) { if (name.equals(binding.getName())) { return binding; @@ -262,7 +256,7 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { return null; } - private PolicySubject lookup(Composite composite, String structuralURI) { + protected PolicySubject lookup(Composite composite, String structuralURI) { if (structuralURI == null) { return null; } @@ -287,7 +281,7 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { int pos = path.indexOf('/'); if (pos != -1) { binding = path.substring(pos + 1); - path = path.substring(0, index); + path = path.substring(0, pos); if ("service-binding".equals(prefix)) { service = path; } else if ("reference-binding".equals(prefix)) { @@ -339,82 +333,4 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { } return null; } - - /** - * Attach the policySet to the given DOM node - * @param node The DOM node (should be an element) - * @param policySet The policy set to be attached - * @return true if the element is changed, false if the element already contains the same policy set - * and no change is made - */ - private boolean attach(Node node, PolicySet policySet) { - Element element = (Element)node; - Document document = element.getOwnerDocument(); - - QName qname = policySet.getName(); - String prefix = DOMHelper.getPrefix(element, qname.getNamespaceURI()); - if (prefix == null) { - // Find the a non-conflicting prefix - int i = 0; - while (true) { - prefix = "ns" + i; - String ns = DOMHelper.getNamespaceURI(element, prefix); - if (ns == null) { - break; - } - } - // Declare the namespace - Attr nsAttr = document.createAttributeNS(XMLNS_ATTRIBUTE_NS_URI, XMLNS_ATTRIBUTE + ":" + prefix); - nsAttr.setValue(qname.getNamespaceURI()); - element.setAttributeNodeNS(nsAttr); - } - // Form the value as a qualified name - String qvalue = null; - if (DEFAULT_NS_PREFIX.equals(prefix)) { - qvalue = qname.getLocalPart(); - } else { - qvalue = prefix + ":" + qname.getLocalPart(); - } - - // Check if the attribute exists - Attr attr = element.getAttributeNode("policySets"); - if (attr == null) { - // Create the policySets attr - attr = document.createAttributeNS(null, "policySets"); - attr.setValue(qvalue); - element.setAttributeNodeNS(attr); - return true; - } else { - // Append to the existing value - boolean duplicate = false; - String value = attr.getValue(); - StringTokenizer tokenizer = new StringTokenizer(value); - while (tokenizer.hasMoreTokens()) { - String ps = tokenizer.nextToken(); - int index = ps.indexOf(':'); - String ns = null; - String localName = null; - if (index == -1) { - ns = DOMHelper.getNamespaceURI(element, DEFAULT_NS_PREFIX); - localName = ps; - } else { - ns = DOMHelper.getNamespaceURI(element, ps.substring(0, index)); - localName = ps.substring(index + 1); - } - QName psName = new QName(ns, localName); - if (qname.equals(psName)) { - duplicate = true; - break; - } - } - if (!duplicate) { - // REVIEW: [rfeng] How to comply to POL40012? - value = value + " " + qvalue; - attr.setValue(value.trim()); - return true; - } - return false; - } - } - } |