From 9c9214b9d88dddf680e0ad4ba82d86f3b0459627 Mon Sep 17 00:00:00 2001 From: slaws Date: Mon, 23 Nov 2009 16:58:53 +0000 Subject: For POL_4028 test that a policy isn't being attached to a property element or one of its children. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@883409 13f79535-47bb-0310-9956-ffa450edef68 --- .../builder/impl/PolicyAttachmentBuilderImpl.java | 121 ++++++++++++++------- 1 file changed, 80 insertions(+), 41 deletions(-) (limited to 'sca-java-2.x/trunk/modules/builder/src/main/java') 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 cbbf54b330..9b21de3354 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 @@ -109,55 +109,90 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { * @throws Exception */ private Composite applyXPath(Composite composite, Definitions definitions, Monitor monitor) throws Exception { - if (definitions == null || definitions.getPolicySets().isEmpty()) { - return composite; - } - // Recursively apply the xpath against the composites referenced by - 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); - } - } - } - Document document = null; - for (PolicySet ps : definitions.getPolicySets()) { - // First calculate the applicable nodes - Set applicableNodes = null; - /* - XPathExpression appliesTo = ps.getAppliesToXPathExpression(); - if (appliesTo != null) { - applicableNodes = new HashSet(); - NodeList nodes = (NodeList)appliesTo.evaluate(document, XPathConstants.NODESET); - for (int i = 0; i < nodes.getLength(); i++) { - applicableNodes.add(nodes.item(i)); + monitor.pushContext("Composite: " + composite.getName().toString()); + + try { + if (definitions == null || definitions.getPolicySets().isEmpty()) { + return composite; + } + // Recursively apply the xpath against the composites referenced by + 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); + } } } - */ - XPathExpression exp = ps.getAttachToXPathExpression(); - if (exp != null) { - if (document == null) { - document = saveAsDOM(composite); + Document document = null; + + for (PolicySet ps : definitions.getPolicySets()) { + // First calculate the applicable nodes + Set applicableNodes = null; + /* + XPathExpression appliesTo = ps.getAppliesToXPathExpression(); + if (appliesTo != null) { + applicableNodes = new HashSet(); + NodeList nodes = (NodeList)appliesTo.evaluate(document, XPathConstants.NODESET); + for (int i = 0; i < nodes.getLength(); i++) { + applicableNodes.add(nodes.item(i)); + } } - NodeList nodes = (NodeList)exp.evaluate(document, XPathConstants.NODESET); - for (int i = 0; i < nodes.getLength(); i++) { - Node node = nodes.item(i); - if (applicableNodes == null || applicableNodes.contains(node)) { - // The node can be a component, service, reference or binding - String index = getStructuralURI(node); - PolicySubject subject = lookup(composite, index); - if (subject != null) { - subject.getPolicySets().add(ps); + */ + XPathExpression exp = ps.getAttachToXPathExpression(); + if (exp != null) { + if (document == null) { + document = saveAsDOM(composite); + } + NodeList nodes = (NodeList)exp.evaluate(document, XPathConstants.NODESET); + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + + // POL_40002 - you can't attach a policy to a property node + // or one of it's children + // walk backwards up the node tree looking for an element called property + // and raise an error if we find one + Node testNode = node; + while (testNode != null){ + if ((node.getNodeType() == Node.ELEMENT_NODE) && + (node.getLocalName().equals("property"))){ + Monitor.error(monitor, + this, + "org.apache.tuscany.sca.builder.builder-validation-messages", + "PolicyAttachedToProperty", + ps.getName().toString()); + break; + } + testNode = testNode.getParentNode(); + } + + if (applicableNodes == null || applicableNodes.contains(node)) { + // The node can be a component, service, reference or binding + String index = getStructuralURI(node); + PolicySubject subject = lookup(composite, index); + if (subject != null) { + subject.getPolicySets().add(ps); + } else { + // raise a warning that the XPath node didn't match a node in the + // models + Monitor.warning(monitor, + this, + "org.apache.tuscany.sca.builder.builder-validation-messages", + "PolicyDOMModelMissmatch", + ps.getName().toString(), + index); + } } } } } - } - - return composite; + + return composite; + } finally { + monitor.popContext(); + } } private Document saveAsDOM(Composite composite) throws XMLStreamException, ContributionWriteException, IOException, @@ -170,6 +205,10 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { writer.close(); Document document = domHelper.load(sw.toString()); + + // Debugging + //System.out.println("\n" + sw.toString()); + return document; } -- cgit v1.2.3