You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by bd...@apache.org on 2010/07/26 06:30:04 UTC

svn commit: r979153 - /tuscany/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java

Author: bdaniel
Date: Mon Jul 26 04:30:03 2010
New Revision: 979153

URL: http://svn.apache.org/viewvc?rev=979153&view=rev
Log:
TUSCANY-3630 Add xpath processing of externalAttachment elements in builder

Modified:
    tuscany/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java

Modified: tuscany/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java?rev=979153&r1=979152&r2=979153&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java Mon Jul 26 04:30:03 2010
@@ -21,7 +21,6 @@ package org.apache.tuscany.sca.builder.i
 
 import java.io.IOException;
 import java.io.StringWriter;
-import java.util.Set;
 
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamException;
@@ -49,6 +48,7 @@ import org.apache.tuscany.sca.contributi
 import org.apache.tuscany.sca.core.ExtensionPointRegistry;
 import org.apache.tuscany.sca.definitions.Definitions;
 import org.apache.tuscany.sca.monitor.Monitor;
+import org.apache.tuscany.sca.policy.ExternalAttachment;
 import org.apache.tuscany.sca.policy.PolicySet;
 import org.apache.tuscany.sca.policy.PolicySubject;
 import org.w3c.dom.Document;
@@ -109,7 +109,7 @@ public class PolicyAttachmentBuilderImpl
         monitor.pushContext("Composite: " + composite.getName().toString());
         
         try {
-            if (definitions == null || definitions.getPolicySets().isEmpty()) {
+            if (definitions == null || (definitions.getPolicySets().isEmpty() && definitions.getExternalAttachments().isEmpty()) ) {
                 return composite;
             }
             // Recursively apply the xpath against the composites referenced by <implementation.composite>
@@ -122,67 +122,31 @@ public class PolicyAttachmentBuilderImpl
                     }
                 }
             }
+            
             Document document = null;
     
             for (PolicySet ps : definitions.getPolicySets()) {
-                // First calculate the applicable nodes
-                Set<Node> applicableNodes = null;
-                /*
-                XPathExpression appliesTo = ps.getAppliesToXPathExpression();
-                if (appliesTo != null) {
-                    applicableNodes = new HashSet<Node>();
-                    NodeList nodes = (NodeList)appliesTo.evaluate(document, XPathConstants.NODESET);
-                    for (int i = 0; i < nodes.getLength(); i++) {
-                        applicableNodes.add(nodes.item(i));
-                    }
-                }
-                */
-                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, 
-                                              BUILDER_VALIDATION_BUNDLE, 
-                                              "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, 
-                                        BUILDER_VALIDATION_BUNDLE, 
-                                        "PolicyDOMModelMissmatch", 
-                                        ps.getName().toString(),
-                                        index);
-                            }
-                        }
-                    }
-                }
+            	XPathExpression exp = ps.getAttachToXPathExpression();
+            	if ( exp != null ) {
+            		if ( document == null ) {
+            			document = saveAsDOM(composite);
+            		}
+            		NodeList nodes = (NodeList) exp.evaluate(document, XPathConstants.NODESET);
+            		attachPolicySetToNodes(composite, monitor, nodes, ps);
+            	}
+            }
+            
+            for ( ExternalAttachment ea : definitions.getExternalAttachments() ) {
+            	XPathExpression exp = ea.getAttachToXPathExpression();
+            	if ( exp != null ) {
+            		if ( document == null ) {
+            			document = saveAsDOM(composite);
+            		}
+            		NodeList nodes = (NodeList) exp.evaluate(document, XPathConstants.NODESET);
+            		for ( PolicySet ps : ea.getPolicySets() ) {            		            		                		                		
+                		attachPolicySetToNodes(composite, monitor, nodes, ps);
+                	}
+            	}
             }
             
             return composite;
@@ -191,6 +155,61 @@ public class PolicyAttachmentBuilderImpl
         }            
     }
 
+	private void attachPolicySetToNodes(Composite composite,
+			Monitor monitor, NodeList nodes, PolicySet ps) {	
+					  		 
+		    for (int i = 0; i < nodes.getLength(); i++) {
+		        Node node = nodes.item(i);
+		        
+		        if ( isAttachedToProperty(node) ) {
+		        	   Monitor.error(monitor, 
+			                      this, 
+			                      BUILDER_VALIDATION_BUNDLE, 
+			                      "PolicyAttachedToProperty", 
+			                      ps.getName().toString());		        
+		        }
+		        
+		      
+		        // 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, 
+		        			BUILDER_VALIDATION_BUNDLE, 
+		        			"PolicyDOMModelMissmatch", 
+		        			ps.getName().toString(),
+		        			index);
+		        }
+		        
+		    }				
+	}
+
+	/**
+	 * 	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
+	 * @param node
+	 * @return
+	 */
+	private boolean isAttachedToProperty(Node node) {
+
+		Node testNode = node;
+		while (testNode != null){
+		    if ((node.getNodeType() == Node.ELEMENT_NODE) &&
+		        (node.getLocalName().equals("property"))){
+		    	return true;		       
+		    }                    	
+		    testNode = testNode.getParentNode();
+		}
+		return false;
+	}
+
     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
@@ -228,6 +247,8 @@ public class PolicyAttachmentBuilderImpl
                 String uri = component.getAttributeNS(null, "uri");
                 String reference = ((Element)node).getAttributeNS(null, "name");
                 return uri + "#reference(" + reference + ")";
+            } else if ( new QName(Base.SCA11_NS, "composite").equals(name)) {
+            	return "";
             } else {
                 String localName = node.getLocalName();
                 if (localName.startsWith("binding.")) {
@@ -259,6 +280,8 @@ public class PolicyAttachmentBuilderImpl
     protected PolicySubject lookup(Composite composite, String structuralURI) {
         if (structuralURI == null) {
             return null;
+        } else if ( structuralURI.equals("")) {
+        	return composite;
         }
         int index = structuralURI.indexOf('#');
         String componentURI = structuralURI;