Author: slaws
Date: Thu Jun 23 13:05:00 2011
New Revision: 1138864

URL: http://svn.apache.org/viewvc?rev=1138864&view=rev
Log:
TUSCANY-3873 - A stop gap solution to running the appliesTo algorithm against a 
matched endpoint reference. Waiting on the discussion from TUSCANY-3877 before 
producing the full solution. 

Modified:
    tuscany/sca-java-2.x/trunk/modules/builder/META-INF/MANIFEST.MF
    
tuscany/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java
    
tuscany/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/impl/EndpointReferenceBinderImpl.java

Modified: tuscany/sca-java-2.x/trunk/modules/builder/META-INF/MANIFEST.MF
URL: 
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/builder/META-INF/MANIFEST.MF?rev=1138864&r1=1138863&r2=1138864&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/builder/META-INF/MANIFEST.MF (original)
+++ tuscany/sca-java-2.x/trunk/modules/builder/META-INF/MANIFEST.MF Thu Jun 23 
13:05:00 2011
@@ -19,6 +19,7 @@ Import-Package: javax.xml.namespace,
  org.apache.tuscany.sca.assembly.xsd;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.context;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",

Modified: 
tuscany/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java
URL: 
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java?rev=1138864&r1=1138863&r2=1138864&view=diff
==============================================================================
--- 
tuscany/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java
 (original)
+++ 
tuscany/sca-java-2.x/trunk/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAppliesToBuilderImpl.java
 Thu Jun 23 13:05:00 2011
@@ -39,10 +39,13 @@ import org.apache.tuscany.sca.assembly.I
 import org.apache.tuscany.sca.assembly.builder.BuilderContext;
 import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException;
 
+import org.apache.tuscany.sca.context.CompositeContext;
 import org.apache.tuscany.sca.core.ExtensionPointRegistry;
 import org.apache.tuscany.sca.definitions.Definitions;
 import org.apache.tuscany.sca.policy.PolicySet;
 import org.apache.tuscany.sca.policy.PolicySubject;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeEndpointReference;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -50,7 +53,7 @@ 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 
+ * expression can be evaluated. For each element that holds a policy set it 
calculates the 
  * appliesTo nodes and checks that the current element is in the set. If not 
the policySet is
  * removed from the element   
  *
@@ -66,6 +69,7 @@ public class PolicyAppliesToBuilderImpl 
         return "org.apache.tuscany.sca.policy.builder.PolicyAppliesToBuilder";
     }
 
+    // Build the whole composite
     public Composite build(Composite composite, BuilderContext context)
         throws CompositeBuilderException {
         try {
@@ -90,6 +94,74 @@ public class PolicyAppliesToBuilderImpl 
         }
     }
     
+    // Build just an endpoint reference that has just been matched against an 
endpoint. This
+    // can often happen at runtime so the policy attachTo for a reference 
policy cannot
+    // be assessed at build time
+    // THIS DOESN'T HANDLE THE NESTED COMPOSITE CASE
+    public void build(EndpointReference epr) throws CompositeBuilderException {
+        try {
+            
+            // What we really need to be doing here is...
+            //
+            // - correct the composite model to add the service binding to the 
reference
+            // - turn the composite model into a DOM
+            // - apply the reference policy XPath and assess whether it 
appliesTo the binding
+            //
+            // I've had a go at making the code do that below. However there 
is some question
+            // about what the appliesTo field means and it's hard to get here 
from the 
+            // binder so until we sort out the appliesTo question this code 
isn't used
+                       
+            CompositeContext compositeContext = 
((RuntimeComponent)epr.getComponent()).getComponentContext().getCompositeContext();
+            Composite domainComposite = compositeContext.getDomainComposite();
+            Definitions systemDefinitions = 
compositeContext.getSystemDefinitions();
+            
+            if (systemDefinitions == null || 
+                (systemDefinitions.getPolicySets().isEmpty() && 
+                 systemDefinitions.getExternalAttachments().isEmpty()) ) {
+                return;
+            }
+            
+            // temporarily add the endpoint binding to the reference model so 
that the
+            // XPath expression can be evaluated correctly
+            
epr.getReference().getBindings().add(epr.getTargetEndpoint().getBinding());
+            
+            // create a DOM for the Domain Composite Infoset
+            Document document = saveAsDOM(domainComposite);
+            
+            // remove the binding again to retain the untainted model 
+            
epr.getReference().getBindings().remove(epr.getTargetEndpoint().getBinding());
+            
+            // 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>>();
+            
+            // can we get the composite within which the erp is defined
+            
+            for (PolicySet ps : new ArrayList<PolicySet>(epr.getPolicySets()) 
) {
+                // Check if this PolicySet applies to the binding, component 
reference, component, or composite. If not,
+                // remove it from the list of policy sets for this endpoint. 
+                if ( epr.getBinding() instanceof PolicySubject ) {
+                    if (isApplicableToSubject(document, appliesToSubjects, 
domainComposite, (PolicySubject)epr.getBinding(), ps))
+                        continue;
+                }
+                if (isApplicableToSubject(document, appliesToSubjects, 
domainComposite, epr.getReference(), ps))
+                    continue;
+                else if ( (epr.getReference().getInterfaceContract() != null) 
&& 
+                          (isApplicableToSubject(document, appliesToSubjects, 
domainComposite, epr.getReference().getInterfaceContract().getInterface(), ps)))
+                    continue;
+                else if ( isApplicableToSubject(document, appliesToSubjects, 
domainComposite, epr.getComponent(), ps))
+                    continue;
+                else if ( isApplicableToSubject(document, appliesToSubjects, 
domainComposite, domainComposite, ps))
+                    continue;
+                else
+                    epr.getPolicySets().remove(ps);             
+            }
+          
+        } catch (Exception e) {
+            throw new CompositeBuilderException(e);
+        }
+    }    
+    
     private Composite checkAppliesTo(Document document, Map<PolicySet, 
List<PolicySubject>> appliesToSubjects, Composite topComposite, BuilderContext 
context) throws Exception {
  
        for ( Component component : topComposite.getComponents() ) {
@@ -127,10 +199,16 @@ public class PolicyAppliesToBuilderImpl 
                }
 
                for (ComponentReference componentReference : 
component.getReferences()) {                       
-                       for (EndpointReference epr : 
componentReference.getEndpointReferences()) {   
+                       for (EndpointReference epr : 
componentReference.getEndpointReferences()) {
+                           // don't process the EPR yet if it's not resolved
+                    if (epr.getStatus() == 
EndpointReference.Status.WIRED_TARGET_NOT_FOUND ||
+                        epr.getBinding() == null){ 
+                        continue;
+                    }
+                    
                                for (PolicySet ps : new 
ArrayList<PolicySet>(epr.getPolicySets()) ) {
-                               // Check if this PolicySet applies to the 
binding, component reference, component, or composite. If not,
-                                       // remove it from the list of policy 
sets for this endpoint. 
+                        // Check if this PolicySet applies to the binding, 
component reference, component, or composite. If not,
+                        // remove it from the list of policy sets for this 
endpoint. 
                                        if ( epr.getBinding() instanceof 
PolicySubject ) {
                                                if 
(isApplicableToSubject(document, appliesToSubjects, topComposite, 
(PolicySubject)epr.getBinding(), ps))
                                                        continue;
@@ -161,7 +239,6 @@ public class PolicyAppliesToBuilderImpl 
        }
        return topComposite;
     }
-    
 
        /**
      * Checks that the provided policy sets applies to the provided policy 
subject

Modified: 
tuscany/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/impl/EndpointReferenceBinderImpl.java
URL: 
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/impl/EndpointReferenceBinderImpl.java?rev=1138864&r1=1138863&r2=1138864&view=diff
==============================================================================
--- 
tuscany/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/impl/EndpointReferenceBinderImpl.java
 (original)
+++ 
tuscany/sca-java-2.x/trunk/modules/core/src/main/java/org/apache/tuscany/sca/core/runtime/impl/EndpointReferenceBinderImpl.java
 Thu Jun 23 13:05:00 2011
@@ -38,6 +38,7 @@ import org.apache.tuscany.sca.assembly.S
 import org.apache.tuscany.sca.assembly.builder.BindingBuilder;
 import org.apache.tuscany.sca.assembly.builder.BuilderContext;
 import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint;
+import org.apache.tuscany.sca.assembly.builder.CompositeBuilder;
 import org.apache.tuscany.sca.assembly.builder.PolicyBuilder;
 import org.apache.tuscany.sca.core.ExtensionPointRegistry;
 import org.apache.tuscany.sca.core.FactoryExtensionPoint;
@@ -86,6 +87,7 @@ public class EndpointReferenceBinderImpl
     protected CompositeActivator compositeActivator;
     protected Monitor monitor;
     protected UnknownEndpointHandler unknownEndpointHandler;
+    protected CompositeBuilder policyAppliesToBuilder;
 
 
     public EndpointReferenceBinderImpl(ExtensionPointRegistry extensionPoints) 
{
@@ -103,6 +105,7 @@ public class EndpointReferenceBinderImpl
         this.unknownEndpointHandler = 
utils.getUtility(UnknownEndpointHandler.class);
         
         this.builders = 
extensionPoints.getExtensionPoint(BuilderExtensionPoint.class);
+        
         this.compositeActivator = 
extensionPoints.getExtensionPoint(CompositeActivator.class);
     }
     
@@ -424,11 +427,24 @@ public class EndpointReferenceBinderImpl
             endpointReference.setTargetEndpoint(matchedEndpoint);
             Binding binding = matchedEndpoint.getBinding();
             endpointReference.setBinding(binding);
-            // TUSCANY-3873 - if no policy on the reference add policy from 
the service
+            // TUSCANY-3873 - add policy from the service
             //                we don't care about intents at this stage
-            if (endpointReference.getPolicySets().isEmpty()){
-                
endpointReference.getPolicySets().addAll(matchedEndpoint.getPolicySets());
+            
endpointReference.getPolicySets().addAll(matchedEndpoint.getPolicySets());
+            // TODO - we need to re-run the appliesTo processing here but 
there is some question about what 
+            //        appliesTo means. It's also difficult to get to the 
PolicyAppliesToBuilder from here and
+            //        need a new EntensionInterface to support access. So for 
now I'm just cheating and looking to 
+            //        see if the XPath expression contains the binding type as 
a string while we discuss appliesTo
+            
+            List<PolicySet> psToRemove = new ArrayList<PolicySet>();
+            
+            for (PolicySet ps : endpointReference.getPolicySets() ) {
+                if 
(!ps.getAppliesTo().contains(endpointReference.getBinding().getType().getLocalPart())){
+                    psToRemove.add(ps);
+                }
             }
+            
+            endpointReference.getPolicySets().removeAll(psToRemove);
+            
             build(endpointReference);
             
endpointReference.setStatus(EndpointReference.Status.WIRED_TARGET_FOUND_AND_MATCHED);
             endpointReference.setUnresolved(false);
@@ -794,7 +810,7 @@ public class EndpointReferenceBinderImpl
             }
         }
         
-        if(!eprLanguage.equals(epLanguage)){
+        
if(!eprLanguage.getNamespaceURI().equals(epLanguage.getNamespaceURI())){
             matchAudit.append("No match because the policy sets on either side 
have policies in differnt languages " + 
                               eprLanguage + 
                               " and " +


Reply via email to