You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2009/03/20 06:36:20 UTC

svn commit: r756332 - /tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositePolicyBuilderImpl.java

Author: rfeng
Date: Fri Mar 20 05:36:19 2009
New Revision: 756332

URL: http://svn.apache.org/viewvc?rev=756332&view=rev
Log:
[Work In Progress] Start to implement the policy builder 

Modified:
    tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositePolicyBuilderImpl.java

Modified: tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositePolicyBuilderImpl.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositePolicyBuilderImpl.java?rev=756332&r1=756331&r2=756332&view=diff
==============================================================================
--- tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositePolicyBuilderImpl.java (original)
+++ tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositePolicyBuilderImpl.java Fri Mar 20 05:36:19 2009
@@ -19,15 +19,20 @@
 
 package org.apache.tuscany.sca.assembly.builder.impl;
 
+import java.util.HashSet;
+import java.util.Set;
 
 import org.apache.tuscany.sca.assembly.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.Binding;
 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.CompositeReference;
 import org.apache.tuscany.sca.assembly.CompositeService;
+import org.apache.tuscany.sca.assembly.Endpoint2;
 import org.apache.tuscany.sca.assembly.EndpointFactory;
+import org.apache.tuscany.sca.assembly.EndpointReference2;
 import org.apache.tuscany.sca.assembly.Implementation;
 import org.apache.tuscany.sca.assembly.Reference;
 import org.apache.tuscany.sca.assembly.Service;
@@ -36,7 +41,8 @@
 import org.apache.tuscany.sca.definitions.Definitions;
 import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper;
 import org.apache.tuscany.sca.monitor.Monitor;
-import org.apache.tuscany.sca.policy.util.PolicyComputationUtils;
+import org.apache.tuscany.sca.policy.Intent;
+import org.apache.tuscany.sca.policy.PolicySubject;
 
 /**
  * A composite builder that computes policy sets based on attached intents and policy sets.
@@ -47,7 +53,9 @@
  */
 public class CompositePolicyBuilderImpl extends BaseBuilderImpl implements CompositeBuilder {
 
-    public CompositePolicyBuilderImpl(AssemblyFactory assemblyFactory, EndpointFactory endpointFactory, InterfaceContractMapper interfaceContractMapper) {
+    public CompositePolicyBuilderImpl(AssemblyFactory assemblyFactory,
+                                      EndpointFactory endpointFactory,
+                                      InterfaceContractMapper interfaceContractMapper) {
         super(assemblyFactory, null, null, null, interfaceContractMapper);
     }
 
@@ -58,9 +66,125 @@
     public void build(Composite composite, Definitions definitions, Monitor monitor) throws CompositeBuilderException {
         computePolicies(composite, monitor);
     }
-    
+
+    /**
+     * Inherit the intents and policySets from the list of models
+     * @param intents
+     * @param policySets
+     * @param models
+     */
+    private void inherit(PolicySubject policySubject, Object... models) {
+        for (Object model : models) {
+            if (model instanceof PolicySubject) {
+                PolicySubject subject = (PolicySubject)model;
+                policySubject.getRequiredIntents().addAll(subject.getRequiredIntents());
+                policySubject.getPolicySets().addAll(subject.getPolicySets());
+            }
+        }
+    }
+
+    /**
+     * Check if two policy subjects requires multually exclusive intents 
+     * @param subject1
+     * @param subject2
+     * @return
+     */
+    private boolean isMutualExclusive(PolicySubject subject1, PolicySubject subject2) {
+        if (subject1 == subject2 || subject1 == null || subject2 == null) {
+            return false;
+        }
+        for (Intent i1 : subject1.getRequiredIntents()) {
+            for (Intent i2 : subject1.getRequiredIntents()) {
+                if (i1.getExcludedIntents().contains(i2) || i2.getExcludedIntents().contains(i1)) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    private void inheritFromService(PolicySubject subject, Service service) {
+        if (service instanceof ComponentService) {
+            inheritFromService(subject, ((ComponentService)service).getService());
+        } else if (service instanceof CompositeService) {
+            CompositeService compositeService = (CompositeService)service;
+            inherit(subject, compositeService.getPromotedComponent());
+            inheritFromService(subject, compositeService.getPromotedService());
+        }
+        inherit(subject, service);
+    }
+
+    private void inheritFromReference(PolicySubject subject, Reference reference) {
+        if (reference instanceof ComponentReference) {
+            inheritFromReference(subject, ((ComponentReference)reference).getReference());
+        } else if (reference instanceof CompositeReference) {
+            CompositeReference compositeReference = (CompositeReference)reference;
+            for (ComponentReference componentReference : compositeReference.getPromotedReferences()) {
+                inheritFromReference(subject, componentReference);
+            }
+        }
+        inherit(subject, reference);
+    }
+
+    /**
+     * Check if two names are equal
+     * @param name1
+     * @param name2
+     * @return
+     */
+    private boolean isEqual(String name1, String name2) {
+        if (name1 == name2) {
+            return true;
+        }
+        if (name1 != null) {
+            return name1.equals(name2);
+        } else {
+            return name2.equals(name1);
+        }
+    }
+
+    private void validate(PolicySubject subject) {
+        Set<Intent> intents = new HashSet<Intent>(subject.getRequiredIntents());
+
+        // Replace profile intents with their required intents
+        boolean profileIntentsFound = false;
+        while (true) {
+            Set<Intent> copy = new HashSet<Intent>(intents);
+            for (Intent i : copy) {
+                if (!i.getRequiredIntents().isEmpty()) {
+                    intents.remove(i);
+                    intents.addAll(i.getRequiredIntents());
+                    profileIntentsFound = true;
+                }
+            }
+            if (!profileIntentsFound) {
+                // No more profileIntents
+                break;
+            }
+        }
+
+        // Remove the intents whose @contraints do not include the current element
+        // Replace unqualified intents if there is a qualified intent in the list
+        Set<Intent> copy = new HashSet<Intent>(intents);
+        for (Intent i : copy) {
+            if (i.getQualifiableIntent() != null) {
+                intents.remove(i.getQualifiableIntent());
+            }
+        }
+
+        // Replace qualifiable intents with the default qualified intent
+        copy = new HashSet<Intent>(intents);
+        for (Intent i : copy) {
+            if (i.getDefaultQualifiedIntent() != null) {
+                intents.remove(i);
+                intents.add(i.getDefaultQualifiedIntent());
+            }
+        }
+
+    }
+
     protected void computePolicies(Composite composite, Monitor monitor) {
-        
+
         // compute policies recursively
         for (Component component : composite.getComponents()) {
             Implementation implementation = component.getImplementation();
@@ -68,131 +192,120 @@
                 computePolicies((Composite)implementation, monitor);
             }
         }
-    
+
         for (Component component : composite.getComponents()) {
+            isMutualExclusive(component, component.getImplementation());
 
-            // Inherit default policies from the component to component-level contracts.
-            // This must be done BEFORE computing implementation policies because the
-            // implementation policy computer removes from the component any
-            // intents and policy sets that don't apply to implementations.
-            PolicyConfigurationUtil.inheritDefaultPolicies(component, component.getServices());
-            PolicyConfigurationUtil.inheritDefaultPolicies(component, component.getReferences());
+            for (ComponentService componentService : component.getServices()) {
+                isMutualExclusive(componentService, componentService.getService());
 
-            Implementation implemenation = component.getImplementation(); 
-            try {
-                PolicyConfigurationUtil.computeImplementationIntentsAndPolicySets(implemenation, component);
-            } catch ( Exception e ) {
-                error(monitor, "PolicyRelatedException", implemenation, e);
-                //throw new RuntimeException(e);
-            }
+                if (componentService.getInterfaceContract() != null) {
+                    isMutualExclusive(componentService.getInterfaceContract().getInterface(), componentService
+                        .getService().getInterfaceContract().getInterface());
+                    isMutualExclusive(componentService.getInterfaceContract().getCallbackInterface(), componentService
+                        .getService().getInterfaceContract().getCallbackInterface());
+                }
 
-            for (ComponentService componentService : component.getServices()) {
-                Service service = componentService.getService();
-                if (service != null) {
-                    // reconcile intents and policysets from componentType
-                     PolicyComputationUtils.addInheritedIntents(service.getRequiredIntents(), componentService.getRequiredIntents());
-                     PolicyComputationUtils.addInheritedPolicySets(service.getPolicySets(), componentService.getPolicySets(), true);
-                     
-                }
-                
-                try {
-                    //compute intents and policyset for each binding
-                    //addInheritedOpConfOnBindings(componentService);
-                    PolicyConfigurationUtil.computeBindingIntentsAndPolicySets(componentService);
-                    PolicyConfigurationUtil.determineApplicableBindingPolicySets(componentService, null);
-    
-                } catch ( Exception e ) {
-                    error(monitor, "PolicyRelatedException", componentService, e);
-                    //throw new RuntimeException(e);
+                for (Endpoint2 ep : componentService.getEndpoints()) {
+                    // Inherit from the componentType.service.interface
+                    inherit(ep, componentService.getService().getInterfaceContract().getInterface());
+                    // Inherit from the component.service.interface
+                    inherit(ep, componentService.getInterfaceContract().getInterface());
+                    // Inherit from the componentType/service 
+                    inherit(ep, component.getImplementation(), componentService.getService());
+                    // Find the corresponding binding in the componentType and inherit the intents/policySets
+                    for (Binding binding : componentService.getService().getBindings()) {
+                        if (isEqual(ep.getBinding().getName(), binding.getName()) && (binding instanceof PolicySubject)) {
+                            isMutualExclusive((PolicySubject)ep.getBinding(), (PolicySubject)binding);
+                            // Inherit from componentType.service.binding
+                            inherit(ep, binding);
+                            break;
+                        }
+                    }
+                    // Inherit from composite/component/service/binding
+                    inherit(ep, composite, ep.getComponent(), ep.getService(), ep.getBinding());
+
+                    // Replace profile intents with their required intents
+                    // Remove the intents whose @contraints do not include the current element
+                    // Replace unqualified intents if there is a qualified intent in the list
+                    // Replace qualifiable intents with the default qualied intent
                 }
             }
-        
+
             for (ComponentReference componentReference : component.getReferences()) {
-                Reference reference = componentReference.getReference();
-                if (reference != null) {
-                    // reconcile intents and policysets
-                    PolicyComputationUtils.addInheritedIntents(reference.getRequiredIntents(), componentReference.getRequiredIntents());
-                    PolicyComputationUtils.addInheritedPolicySets(reference.getPolicySets(), componentReference.getPolicySets(), true);
-                }
-                
-               
-                try {
-                    //compute intents and policyset for each binding
-                    //addInheritedOpConfOnBindings(componentReference);
-                    PolicyConfigurationUtil.computeBindingIntentsAndPolicySets(componentReference);
-                    PolicyConfigurationUtil.determineApplicableBindingPolicySets(componentReference, null);
-    
-                
-                    if ( componentReference.getCallback() != null ) {
-                        PolicyComputationUtils.addInheritedIntents(componentReference.getRequiredIntents(), 
-                                            componentReference.getCallback().getRequiredIntents());
-                        PolicyComputationUtils.addInheritedPolicySets(componentReference.getPolicySets(), 
-                                               componentReference.getCallback().getPolicySets(), 
-                                               false);
+                isMutualExclusive(componentReference, componentReference.getReference());
+
+                if (componentReference.getInterfaceContract() != null) {
+                    isMutualExclusive(componentReference.getInterfaceContract().getInterface(), componentReference
+                        .getReference().getInterfaceContract().getInterface());
+                    isMutualExclusive(componentReference.getInterfaceContract().getCallbackInterface(),
+                                      componentReference.getReference().getInterfaceContract().getCallbackInterface());
+                }
+
+                for (EndpointReference2 epr : componentReference.getEndpointReferences()) {
+                    // Inherit from the componentType.reference.interface
+                    inherit(epr, componentReference.getReference().getInterfaceContract().getInterface());
+                    // Inherit from the component.reference.interface
+                    inherit(epr, componentReference.getInterfaceContract().getInterface());
+                    // Inherit from the componentType/reference 
+                    inherit(epr, component.getImplementation(), componentReference.getReference());
+                    // Find the corresponding binding in the componentType and inherit the intents/policySets
+                    for (Binding binding : componentReference.getReference().getBindings()) {
+                        if (isEqual(epr.getBinding().getName(), binding.getName()) && (binding instanceof PolicySubject)) {
+                            isMutualExclusive((PolicySubject)epr.getBinding(), (PolicySubject)binding);
+                            // Inherit from componentType.reference.binding
+                            inherit(epr, binding);
+                            break;
+                        }
                     }
-                } catch ( Exception e ) {
-                    error(monitor, "PolicyRelatedException", componentReference, e);
-                    //throw new RuntimeException(e);
+                    // Inherit from composite/component/reference/binding
+                    inherit(epr, composite, epr.getComponent(), epr.getReference(), epr.getBinding());
+
+                    // Replace profile intents with their required intents
+                    // Remove the intents whose @contraints do not include the current element
+                    // Replace unqualified intents if there is a qualified intent in the list
+                    // Replace qualifiable intents with the default qualied intent
                 }
             }
-        }
 
-        PolicyConfigurationUtil.inheritDefaultPolicies(composite, composite.getServices());
-        PolicyConfigurationUtil.inheritDefaultPolicies(composite, composite.getReferences());
-
-        //compute policies for composite service bindings
-        for (Service service : composite.getServices()) {
-            addPoliciesFromPromotedService((CompositeService)service);
+            Implementation implemenation = component.getImplementation();
             try {
-                //add or merge service operations to the binding
-                //addInheritedOpConfOnBindings(service);
-                PolicyConfigurationUtil.computeBindingIntentsAndPolicySets(service);
-                PolicyConfigurationUtil.determineApplicableBindingPolicySets(service, null);
-            } catch ( Exception e ) {
-                error(monitor, "PolicyRelatedException", service, e);
+                PolicyConfigurationUtil.computeImplementationIntentsAndPolicySets(implemenation, component);
+            } catch (Exception e) {
+                error(monitor, "PolicyRelatedException", implemenation, e);
                 //throw new RuntimeException(e);
             }
-                
         }
-    
+
+        //compute policies for composite service bindings
+        for (Service service : composite.getServices()) {
+            CompositeService compositeService = (CompositeService)service;
+
+            // Composite service inherits the policySets and intents from the promoted component service
+            Component promotedComponent = compositeService.getPromotedComponent();
+            // Promoted component service inherits from the component type service
+            // as well as the structural hierarchy, i.e., composite/promotedComponent
+            ComponentService promotedService = compositeService.getPromotedService();
+            // We need to calculate the inherited intents/policySets for the promoted
+            // service first
+            isMutualExclusive(compositeService, promotedService);
+        }
+
+        //compute policies for composite reference bindings
         for (Reference reference : composite.getReferences()) {
-            CompositeReference compReference = (CompositeReference)reference;
-            addPoliciesFromPromotedReference(compReference);
-            try {
-               
-                if (compReference.getCallback() != null) {
-                    PolicyComputationUtils.addInheritedIntents(compReference.getRequiredIntents(), 
-                                        compReference.getCallback().getRequiredIntents());
-                    PolicyComputationUtils.addInheritedPolicySets(compReference.getPolicySets(), 
-                                           compReference.getCallback().getPolicySets(), 
-                                           false);
-                }
-                
-                PolicyConfigurationUtil.computeBindingIntentsAndPolicySets(reference);
-                PolicyConfigurationUtil.determineApplicableBindingPolicySets(reference, null);
-            } catch ( Exception e ) {
-                error(monitor, "PolicyRelatedException", reference, e);
-                //throw new RuntimeException(e);
+            CompositeReference compositeReference = (CompositeReference)reference;
+
+            // Composite reference inherits the policySets and intents from the promoted component references
+            for (ComponentReference promotedReference : compositeReference.getPromotedReferences()) {
+
+                // Promoted component reference inherits from the component type reference
+                // as well as the structural hierarchy, i.e., composite/promotedComponent
+                // We need to calculate the inherited intents/policySets for the promoted
+                // reference first
+                isMutualExclusive(compositeReference, promotedReference);
+
             }
         }
+
     }
-    
-    private void addPoliciesFromPromotedService(CompositeService compositeService) {
-        //inherit intents and policies from promoted service
-        PolicyComputationUtils.addInheritedIntents(compositeService.getPromotedService().getRequiredIntents(), 
-                            compositeService.getRequiredIntents());
-        PolicyComputationUtils.addInheritedPolicySets(compositeService.getPromotedService().getPolicySets(), 
-                               compositeService.getPolicySets(), true);
-    }
-    
-    private void addPoliciesFromPromotedReference(CompositeReference compositeReference) {
-        for ( Reference promotedReference : compositeReference.getPromotedReferences() ) {
-           PolicyComputationUtils.addInheritedIntents(promotedReference.getRequiredIntents(), 
-                               compositeReference.getRequiredIntents());
-       
-           PolicyComputationUtils.addInheritedPolicySets(promotedReference.getPolicySets(), 
-                                  compositeReference.getPolicySets(), true);
-        }
-    } 
-    
 }