tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From svkr...@apache.org
Subject svn commit: r572113 - in /incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl: BindingUtil.java CompositeConfigurationBuilderImpl.java CompositeWireBuilderImpl.java
Date Sun, 02 Sep 2007 19:12:00 GMT
Author: svkrish
Date: Sun Sep  2 12:11:59 2007
New Revision: 572113

URL: http://svn.apache.org/viewvc?rev=572113&view=rev
Log:
moved policy aggregation code out from config builder to wire builder since wirebuilder has a more complete model with promoted services and reference resolved etc., also code seems tobe a bit cleaner

Modified:
    incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/BindingUtil.java
    incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeConfigurationBuilderImpl.java
    incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeWireBuilderImpl.java

Modified: incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/BindingUtil.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/BindingUtil.java?rev=572113&r1=572112&r2=572113&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/BindingUtil.java (original)
+++ incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/BindingUtil.java Sun Sep  2 12:11:59 2007
@@ -27,18 +27,36 @@
 import org.apache.tuscany.sca.assembly.ComponentService;
 import org.apache.tuscany.sca.assembly.SCABinding;
 import org.apache.tuscany.sca.assembly.WireableBinding;
+import org.apache.tuscany.sca.policy.PolicySet;
+import org.apache.tuscany.sca.policy.PolicySetAttachPoint;
 
 /**
  * This class encapsulates utility methods to deal with binding definitions
  */
 class BindingUtil {
+    private static boolean hasCompatiblePolicySets(Binding refBinding, Binding svcBinding) {
+        if ( refBinding instanceof PolicySetAttachPoint && svcBinding instanceof PolicySetAttachPoint ) {
+            //TODO : need to add more compatibility checks at the policy attachment levels
+            for ( PolicySet svcPolicySet : ((PolicySetAttachPoint)svcBinding).getPolicySets() ) {
+                for ( PolicySet refPolicySet : ((PolicySetAttachPoint)refBinding).getPolicySets() ) {
+                    if ( !svcPolicySet.equals(refPolicySet) ) {
+                        return false;
+                    }
+                }
+            }
+        }
+        return true;
+    }
+    
     
     private static Binding matchBinding(Component component, ComponentService service, List<Binding> source, List<Binding> target) {
         List<Binding> matched = new ArrayList<Binding>();
         // Find the corresponding bindings from the service side
         for (Binding binding : source) {
             for (Binding serviceBinding : target) {
-                if (binding.getClass() == serviceBinding.getClass()) {
+                if (binding.getClass() == serviceBinding.getClass() && 
+                    hasCompatiblePolicySets(binding, serviceBinding)) {
+                    
                     Binding cloned = binding;
                     if (binding instanceof WireableBinding) {
                         // TODO: We need to clone the reference binding

Modified: incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeConfigurationBuilderImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeConfigurationBuilderImpl.java?rev=572113&r1=572112&r2=572113&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeConfigurationBuilderImpl.java (original)
+++ incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeConfigurationBuilderImpl.java Sun Sep  2 12:11:59 2007
@@ -20,22 +20,16 @@
 package org.apache.tuscany.sca.assembly.builder.impl;
 
 import java.net.URI;
-import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 
-import javax.xml.namespace.QName;
-
 import org.apache.tuscany.sca.assembly.AssemblyFactory;
-import org.apache.tuscany.sca.assembly.Base;
 import org.apache.tuscany.sca.assembly.Binding;
 import org.apache.tuscany.sca.assembly.Component;
 import org.apache.tuscany.sca.assembly.ComponentProperty;
 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.Implementation;
 import org.apache.tuscany.sca.assembly.Property;
@@ -48,12 +42,6 @@
 import org.apache.tuscany.sca.assembly.builder.Problem.Severity;
 import org.apache.tuscany.sca.interfacedef.InterfaceContract;
 import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper;
-import org.apache.tuscany.sca.policy.Intent;
-import org.apache.tuscany.sca.policy.IntentAttachPoint;
-import org.apache.tuscany.sca.policy.IntentAttachPointType;
-import org.apache.tuscany.sca.policy.PolicySet;
-import org.apache.tuscany.sca.policy.PolicySetAttachPoint;
-import org.apache.tuscany.sca.policy.QualifiedIntent;
 
 public class CompositeConfigurationBuilderImpl {
 
@@ -120,30 +108,10 @@
             }
         }
 
-        List<Intent> compositeIntents = null;
-        List<PolicySet> compositePolicySets = null;
-        if (composite instanceof PolicySetAttachPoint) {
-            compositeIntents = ((PolicySetAttachPoint)composite).getRequiredIntents();
-            compositePolicySets = ((PolicySetAttachPoint)composite).getPolicySets();
-        }
-
         // Initialize service bindings
         for (Service service : composite.getServices()) {
-            // Set default binding names and inherit intents and policysets
-            // defined at composite level
-            addInheritedIntents(compositeIntents, service.getRequiredIntents());
-            addInheritedPolicySets(compositePolicySets, service.getPolicySets(), false);
+            // Set default binding names 
             
-            //inherit intents and policies from promoted service
-            if ( service instanceof CompositeService ) {
-                CompositeService compService = (CompositeService)service;
-                addInheritedIntents(compService.getPromotedService().getRequiredIntents(), 
-                                     service.getRequiredIntents());
-                addInheritedPolicySets(compService.getPromotedService().getPolicySets(), 
-                                     service.getPolicySets(), true);
-            }
-            
-
             // Create default SCA binding
             if (service.getBindings().isEmpty()) {
                 SCABinding scaBinding = scaBindingFactory.createSCABinding();
@@ -175,46 +143,16 @@
             }
             
             if (service.getCallback() != null) {
-                addInheritedIntents(service.getRequiredIntents(), 
-                                    service.getCallback().getRequiredIntents());
-                addInheritedPolicySets(service.getPolicySets(), 
-                                       service.getCallback().getPolicySets(), 
-                                       false);
                 for (Binding binding : service.getCallback().getBindings()) {
                     if (binding.getName() == null) {
                         binding.setName(service.getName());
                     }
                 }
             }
-            computeIntents(service.getBindings(), service.getRequiredIntents());
-            computePolicySets(service, service.getBindings(), service.getPolicySets());
-            
-            if ( service.getCallback() != null ) {
-                computeIntents(service.getCallback().getBindings(), 
-                               service.getCallback().getRequiredIntents());
-                computePolicySets(service, 
-                                  service.getCallback().getBindings(), 
-                                  service.getCallback().getPolicySets());
-            }
         }
 
         // Initialize reference bindings
         for (Reference reference : composite.getReferences()) {
-            addInheritedIntents(compositeIntents, reference.getRequiredIntents());
-            addInheritedPolicySets(compositePolicySets, reference.getPolicySets(), false);
-
-            //inherit intents and policies from promoted service
-            if ( reference instanceof CompositeReference ) {
-                CompositeReference compReference = (CompositeReference)reference;
-                for ( Reference promotedReference : compReference.getPromotedReferences() ) {
-                    addInheritedIntents(promotedReference.getRequiredIntents(), 
-                                         reference.getRequiredIntents());
-                
-                    addInheritedPolicySets(promotedReference.getPolicySets(), 
-                                         reference.getPolicySets(), true);
-                }
-            }
-            
             // Create default SCA binding
             if (reference.getBindings().isEmpty()) {
                 SCABinding scaBinding = scaBindingFactory.createSCABinding();
@@ -237,17 +175,6 @@
                     }
                 }
             }
-            
-            computeIntents(reference.getBindings(), reference.getRequiredIntents());
-            computePolicySets(reference, reference.getBindings(), reference.getPolicySets());
-            
-            if ( reference.getCallback() != null ) {
-                computeIntents(reference.getCallback().getBindings(), 
-                               reference.getCallback().getRequiredIntents());
-                computePolicySets(reference, 
-                                  reference.getCallback().getBindings(), 
-                                  reference.getCallback().getPolicySets());
-            }
         }
 
         // Initialize all component services and references
@@ -269,12 +196,6 @@
                 component.setAutowire(composite.getAutowire());
             }
 
-            // Inherit intents defined at the composite level
-            addInheritedIntents(compositeIntents, component.getRequiredIntents());
-            
-            // Inherit policysets defined at the composite level
-            addInheritedPolicySets(compositePolicySets, component.getPolicySets(), false);
-
             if (component.getImplementation() instanceof ComponentPreProcessor) {
                 ((ComponentPreProcessor)component.getImplementation()).preProcess(component);
             }
@@ -549,16 +470,8 @@
 
         // Reconcile each component reference with its reference
         for (ComponentReference componentReference : component.getReferences()) {
-            // inherit intents and policysets from component
-            addInheritedIntents(component.getRequiredIntents(), componentReference.getRequiredIntents());
-            addInheritedPolicySets(component.getPolicySets(), componentReference.getPolicySets(), false);
-
             Reference reference = componentReference.getReference();
             if (reference != null) {
-                // reconcile intents and policysets
-                addInheritedIntents(reference.getRequiredIntents(), componentReference.getRequiredIntents());
-                addInheritedPolicySets(reference.getPolicySets(), componentReference.getPolicySets(), true);
-
                 // Reconcile multiplicity
                 if (componentReference.getMultiplicity() != null) {
                     if (!ReferenceUtil.isValidMultiplicityOverride(reference.getMultiplicity(),
@@ -621,25 +534,6 @@
                     componentReference.getTargets().addAll(reference.getTargets());
                 }
             }
-            
-            //compute intents and policyset for each binding
-            computeIntents(componentReference.getBindings(), 
-                           componentReference.getRequiredIntents());
-            computePolicySets(componentReference,
-                              componentReference.getBindings(),
-                              componentReference.getPolicySets());
-            
-            if ( componentReference.getCallback() != null ) {
-                //compute intents and policyset for each callback binding
-                computeIntents(componentReference.getCallback().getBindings(), 
-                               componentReference.getRequiredIntents());
-                computePolicySets(componentReference,
-                                  componentReference.getCallback().getBindings(),
-                                  componentReference.getPolicySets());
-                
-
-            }
-            
         }
     }
 
@@ -684,17 +578,8 @@
 
         //Reconcile each component service with its service
         for (ComponentService componentService : component.getServices()) {
-            // inherit intents and policysets from component
-            addInheritedIntents(component.getRequiredIntents(), componentService.getRequiredIntents());
-            addInheritedPolicySets(component.getPolicySets(), componentService.getPolicySets(), false);
-
             Service service = componentService.getService();
             if (service != null) {
-                // reconcile intents and policysets
-                addInheritedIntents(service.getRequiredIntents(), componentService.getRequiredIntents());
-                addInheritedPolicySets(service.getPolicySets(), 
-                                       componentService.getPolicySets(), 
-                                       true);
                 // Reconcile interface
                 if (componentService.getInterfaceContract() != null) {
                     if (!componentService.getInterfaceContract().equals(service
@@ -730,21 +615,6 @@
                         .getBindings());
                 }
             }
-            //compute intents and policyset for each binding
-            computeIntents(componentService.getBindings(), 
-                           componentService.getRequiredIntents());
-            computePolicySets(componentService,
-                              componentService.getBindings(),
-                              componentService.getPolicySets());
-            
-            if ( componentService.getCallback() != null ) {
-                //compute intents and policyset for each binding
-                computeIntents(componentService.getCallback().getBindings(), 
-                               componentService.getRequiredIntents());
-                computePolicySets(componentService,
-                                  componentService.getCallback().getBindings(),
-                                  componentService.getPolicySets());
-            }
         }
     }
 
@@ -856,13 +726,6 @@
                     if (service.getBindings().isEmpty()) {
                         service.getBindings().addAll(reference.getCallback().getBindings());
                     }
-                    if (service.getPolicySets().isEmpty()) {
-                        service.getPolicySets().addAll(reference.getCallback().getPolicySets());
-                    }
-                    if (service.getRequiredIntents().isEmpty()) {
-                        service.getRequiredIntents().addAll(reference.getCallback()
-                            .getRequiredIntents());
-                    }
                 }
                 reference.setCallbackService(service);
             }
@@ -928,13 +791,6 @@
                     if (reference.getBindings().isEmpty()) {
                         reference.getBindings().addAll(service.getCallback().getBindings());
                     }
-                    if (reference.getPolicySets().isEmpty()) {
-                        reference.getPolicySets().addAll(service.getCallback().getPolicySets());
-                    }
-                    if (reference.getRequiredIntents().isEmpty()) {
-                        reference.getRequiredIntents().addAll(service.getCallback()
-                            .getRequiredIntents());
-                    }
                 }
                 service.setCallbackReference(reference);
             }
@@ -1141,150 +997,6 @@
 
             // No promoted service
             return null;
-        }
-    }
-
-    private void computeIntents(List<Binding> bindings, List<Intent> inheritedIntents) {
-        boolean found = false;
-
-        for (Binding binding : bindings) {
-            if (binding instanceof IntentAttachPoint) {
-                IntentAttachPoint policiedBinding = (IntentAttachPoint)binding;
-                IntentAttachPointType bindingType = policiedBinding.getType();
-
-                // validate intents specified for the binding
-                for (Intent intent : policiedBinding.getRequiredIntents()) {
-                    for (QName constrained : intent.getConstrains()) {
-                        if (bindingType.getName().getNamespaceURI().equals(constrained
-                            .getNamespaceURI()) && bindingType.getName().getLocalPart()
-                            .startsWith(constrained.getLocalPart())) {
-                            found = true;
-                            break;
-                        }
-                    }
-
-                    if (!found) {
-                        warning("Policy Intent '" + intent.getName()
-                            + "' does not constrain binding type  "
-                            + bindingType, binding);
-                    }
-                }
-
-                for (Intent intent : inheritedIntents) {
-                    for (QName constrained : intent.getConstrains()) {
-                        if (bindingType.getName().getNamespaceURI().equals(constrained
-                            .getNamespaceURI()) && bindingType.getName().getLocalPart()
-                            .startsWith(constrained.getLocalPart())) {
-                            policiedBinding.getRequiredIntents().add(intent);
-                            break;
-                        }
-                    }
-                }
-                
-                Map<QName, Intent> intentsTable = new HashMap<QName, Intent>();
-                //remove duplicates
-                for ( Intent intent : policiedBinding.getRequiredIntents() ) {
-                    intentsTable.put(intent.getName(), intent);
-                }
-                
-                //if qualified form of intent exists remove the unqualified form
-                for ( Intent intent : intentsTable.values() ) {
-                    if ( intent instanceof QualifiedIntent ) {
-                        QualifiedIntent qualifiedIntent = (QualifiedIntent)intent;
-                        if ( intentsTable.get(qualifiedIntent.getQualifiableIntent().getName()) != null ) {
-                            intentsTable.remove(qualifiedIntent.getQualifiableIntent().getName());
-                        }
-                    }
-                }
-                policiedBinding.getRequiredIntents().clear();
-                policiedBinding.getRequiredIntents().addAll(intentsTable.values());
-            }
-        }
-    }
-
-    private void computePolicySets(Base parent,
-                                   List<Binding> bindings,
-                                   List<PolicySet> inheritedPolicySets) {
-        String appliesTo = null;
-        HashMap<QName, PolicySet> policySetTable = new HashMap<QName, PolicySet>();
-        
-        for (Binding binding : bindings) {
-            if (binding instanceof PolicySetAttachPoint) {
-                PolicySetAttachPoint policiedBinding = (PolicySetAttachPoint)binding;
-                IntentAttachPointType bindingType = policiedBinding.getType();
-
-                //validate policysets specified for the binding
-                for (PolicySet policySet : policiedBinding.getPolicySets()) {
-                    appliesTo = policySet.getAppliesTo();
-
-                    if (!isPolicySetApplicable(parent, appliesTo, bindingType)) {
-                        warning("Policy Set '" + policySet.getName()
-                            + "' does not apply to binding type  "
-                            + bindingType, binding);
-
-                    }
-                }
-                
-                for (PolicySet policySet : inheritedPolicySets) {
-                    appliesTo = policySet.getAppliesTo();
-                    if (isPolicySetApplicable(parent, appliesTo, bindingType)) {
-                        policiedBinding.getPolicySets().add(policySet);
-                    }
-                }
-                
-                //get rid of duplicate entries
-                for ( PolicySet policySet : policiedBinding.getPolicySets() ) {
-                    policySetTable.put(policySet.getName(), policySet);
-                }
-                policiedBinding.getPolicySets().clear();
-                policiedBinding.getPolicySets().addAll(policySetTable.values());
-            }
-        }
-    }
-
-    private boolean isPolicySetApplicable(Base parent,
-                                          String xpath,
-                                          IntentAttachPointType bindingType) {
-        //create a xml node out of the parent object.. i.e. write the parent object as scdl fragment
-        //invoke PropertyUtil.evaluate(null, node, xpath)
-        //verify the result Node's QName against the bindingType's name
-        
-        if (parent instanceof ComponentReference) {
-        } else if (parent instanceof ComponentReference) {
-        } else if (parent instanceof Component) {
-        } else if (parent instanceof CompositeService) {
-        } else if (parent instanceof CompositeReference) {
-
-        }
-        return true;
-    }
-
-    private void addInheritedIntents(List<Intent> sourceList, List<Intent> targetList) {
-        if (sourceList != null) {
-            targetList.addAll(sourceList);
-        }
-    }
-    
-    private  void addInheritedPolicySets(List<PolicySet> sourceList, List<PolicySet> targetList, boolean checkOverrides) {
-        //check overrides is true when policysets are to be copied from componentType to component level
-        if ( checkOverrides ) {
-            //aggregate all the provided intents present in the target
-            List<Intent> targetProvidedIntents = new ArrayList<Intent>();
-            for ( PolicySet policySet : targetList ) {
-                targetProvidedIntents.addAll(policySet.getProvidedIntents());
-            }
-            
-            //for every policy set in the source check if it provides one of the intents that is 
-            //already provided by the policysets in the destination and do not copy them.
-            for ( PolicySet policySet : sourceList ) {
-                for ( Intent sourceProvidedIntent : policySet.getProvidedIntents() ) {
-                    if ( !targetProvidedIntents.contains(sourceProvidedIntent) ) {
-                        targetList.add(policySet);
-                    }
-                }
-            }
-        } else {
-            targetList.addAll(sourceList);
         }
     }
 }

Modified: incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeWireBuilderImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeWireBuilderImpl.java?rev=572113&r1=572112&r2=572113&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeWireBuilderImpl.java (original)
+++ incubator/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeWireBuilderImpl.java Sun Sep  2 12:11:59 2007
@@ -26,7 +26,10 @@
 import java.util.Map;
 import java.util.Set;
 
+import javax.xml.namespace.QName;
+
 import org.apache.tuscany.sca.assembly.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.Base;
 import org.apache.tuscany.sca.assembly.Binding;
 import org.apache.tuscany.sca.assembly.Component;
 import org.apache.tuscany.sca.assembly.ComponentProperty;
@@ -46,6 +49,13 @@
 import org.apache.tuscany.sca.assembly.builder.CompositeBuilderMonitor;
 import org.apache.tuscany.sca.assembly.builder.Problem.Severity;
 import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper;
+import org.apache.tuscany.sca.policy.Intent;
+import org.apache.tuscany.sca.policy.IntentAttachPoint;
+import org.apache.tuscany.sca.policy.IntentAttachPointType;
+import org.apache.tuscany.sca.policy.PolicySet;
+import org.apache.tuscany.sca.policy.PolicySetAttachPoint;
+import org.apache.tuscany.sca.policy.ProfileIntent;
+import org.apache.tuscany.sca.policy.QualifiedIntent;
 
 public class CompositeWireBuilderImpl {
 
@@ -53,6 +63,25 @@
     private AssemblyFactory assemblyFactory;
     private InterfaceContractMapper interfaceContractMapper;
     
+    //Represents a target component and service
+    private class Target {
+        Component component;
+        ComponentService service;
+        
+        Target(Component component, ComponentService service) {
+            this.component = component;
+            this.service = service;
+        }
+        
+        Component getComponent() {
+            return component;
+        }
+        
+        ComponentService getService() {
+            return service;
+        }
+    };
+    
     public CompositeWireBuilderImpl(AssemblyFactory assemblyFactory, InterfaceContractMapper interfaceContractMapper, CompositeBuilderMonitor monitor) {
         this.assemblyFactory = assemblyFactory;
         this.interfaceContractMapper = interfaceContractMapper;
@@ -86,6 +115,9 @@
         connectCompositeServices(composite, components, componentServices);
         connectCompositeReferences(composite, componentReferences);
 
+        //compute the policies before connecting component references
+        computePolicies(composite);
+
         // Connect component references to their targets
         connectComponentReferences(composite, components, componentServices, componentReferences);
 
@@ -294,155 +326,148 @@
             }
         }
     }
-
-    /**
-     * Connect references to their targets.
-     * 
-     * @param composite
-     * @param componentServices
-     * @param componentReferences
-     * @param problems
-     */
-    private void connectComponentReferences(Composite composite,
-                                            Map<String, Component> components,
-                                            Map<String, ComponentService> componentServices,
-                                            Map<String, ComponentReference> componentReferences) {
-        
-        // Represents a target component and service
-        class Target {
-            Component component;
-            ComponentService service;
-            
-            Target(Component component, ComponentService service) {
-                this.component = component;
-                this.service = service;
-            }
-            
-            Component getComponent() {
-                return component;
-            }
-            
-            ComponentService getService() {
-                return service;
-            }
-        };
-    
-        for (ComponentReference componentReference : componentReferences.values()) {
-            List<Target> targets = new ArrayList<Target>();
-    
-            if (componentReference.getAutowire() == Boolean.TRUE) {
     
-                // Find suitable targets in the current composite for an
-                // autowired reference
-                Multiplicity multiplicity = componentReference.getMultiplicity();
-                for (Component targetComponent : composite.getComponents()) {
-                    for (ComponentService targetComponentService : targetComponent.getServices()) {
-                        if (componentReference.getInterfaceContract() == null ||
-                            interfaceContractMapper.isCompatible(componentReference.getInterfaceContract(), targetComponentService.getInterfaceContract())) {
-                            Target target = new Target(targetComponent, targetComponentService);
-                            targets.add(target);
-                            if (multiplicity == Multiplicity.ZERO_ONE || multiplicity == Multiplicity.ONE_ONE) {
-                                break;
-                            }
+    private List<Target> createComponentReferenceTargets(Composite composite,
+                                                         Map<String, Component> components,
+                                                         Map<String, ComponentService> componentServices,
+                                                         ComponentReference componentReference) {
+        List<Target> targets = new ArrayList<Target>();
+        
+        if (componentReference.getAutowire() == Boolean.TRUE) {
+
+            // Find suitable targets in the current composite for an
+            // autowired reference
+            Multiplicity multiplicity = componentReference.getMultiplicity();
+            for (Component targetComponent : composite.getComponents()) {
+                for (ComponentService targetComponentService : targetComponent.getServices()) {
+                    if (componentReference.getInterfaceContract() == null ||
+                        interfaceContractMapper.isCompatible(componentReference.getInterfaceContract(), targetComponentService.getInterfaceContract())) {
+                        Target target = new Target(targetComponent, targetComponentService);
+                        targets.add(target);
+                        if (multiplicity == Multiplicity.ZERO_ONE || multiplicity == Multiplicity.ONE_ONE) {
+                            break;
                         }
                     }
                 }
+            }
+            
+            if (multiplicity == Multiplicity.ONE_N || multiplicity == Multiplicity.ONE_ONE) {
+                if (targets.size() == 0) {
+                    warning("No target services found for the component reference to be autowired: " + componentReference
+                                .getName(),
+                            componentReference);
+                }
+            }
+
+        } else if (!componentReference.getTargets().isEmpty()) {
+
+            // Resolve targets specified on the component reference
+            for (ComponentService componentService : componentReference.getTargets()) {
                 
-                if (multiplicity == Multiplicity.ONE_N || multiplicity == Multiplicity.ONE_ONE) {
-                    if (targets.size() == 0) {
-                        warning("No target services found for the component reference to be autowired: " + componentReference
-                                    .getName(),
-                                componentReference);
-                    }
+                // Resolve the target component and service
+                String name = componentService.getName();
+                ComponentService targetComponentService = componentServices.get(name);
+                Component targetComponent;
+                int s = name.indexOf('/');
+                if (s == -1) {
+                    targetComponent = components.get(name);
+                } else {
+                    targetComponent = components.get(name.substring(0, s));
                 }
-    
-            } else if (!componentReference.getTargets().isEmpty()) {
-    
-                // Resolve targets specified on the component reference
-                for (ComponentService componentService : componentReference.getTargets()) {
-                    
-                    // Resolve the target component and service
-                    String name = componentService.getName();
-                    ComponentService targetComponentService = componentServices.get(name);
-                    Component targetComponent;
-                    int s = name.indexOf('/');
-                    if (s == -1) {
-                        targetComponent = components.get(name);
+                
+                if (targetComponentService != null) {
+
+                    // Check that the target component service provides
+                    // a superset of the component reference interface
+                    if (componentReference.getInterfaceContract() == null ||
+                        interfaceContractMapper.isCompatible(componentReference.getInterfaceContract(), targetComponentService.getInterfaceContract())) {
+
+                        Target target = new Target(targetComponent, targetComponentService);
+                        targets.add(target);
+
+                        // mark the reference target as resolved. Used later when we are looking to 
+                        // see if an sca binding is associated with a resolved target or not
+                        componentService.setUnresolved(false);
                     } else {
-                        targetComponent = components.get(name.substring(0, s));
+                        warning("Incompatible interfaces on component reference and target: " + componentReference
+                                    .getName()
+                                    + " : "
+                                    + componentService.getName(),
+                                composite);
                     }
+                } else {
+                    // clone all the reference bindings into the target so that they
+                    // can be used for comparison when the target is resolved at runtime
+                    componentService.getBindings().addAll(componentReference.getBindings());
                     
-                    if (targetComponentService != null) {
+                    warning("Component reference target not found, it might be a remote service: " + componentService.getName(), composite);
+                }
+            }
+        } else if (componentReference.getReference() != null) {
 
-                        // Check that the target component service provides
-                        // a superset of the component reference interface
-                        if (componentReference.getInterfaceContract() == null ||
-                            interfaceContractMapper.isCompatible(componentReference.getInterfaceContract(), targetComponentService.getInterfaceContract())) {
-
-                            Target target = new Target(targetComponent, targetComponentService);
-                            targets.add(target);
-
-                            // mark the reference target as resolved. Used later when we are looking to 
-                            // see if an sca binding is associated with a resolved target or not
-                            componentService.setUnresolved(false);
-                        } else {
-                            warning("Incompatible interfaces on component reference and target: " + componentReference
-                                        .getName()
-                                        + " : "
-                                        + componentService.getName(),
-                                    composite);
-                        }
-                    } else {
-                        // clone all the reference bindings into the target so that they
-                        // can be used for comparison when the target is resolved at runtime
-                        componentService.getBindings().addAll(componentReference.getBindings());
-                        
-                        warning("Component reference target not found, it might be a remote service: " + componentService.getName(), composite);
-                    }
+            // Resolve targets from the corresponding reference in the
+            // componentType
+            for (ComponentService componentService : componentReference.getReference().getTargets()) {
+
+                // Resolve the target component and service
+                String name = componentService.getName();
+                ComponentService targetComponentService = componentServices.get(name);
+                Component targetComponent;
+                int s = name.indexOf('/');
+                if (s == -1) {
+                    targetComponent = components.get(name);
+                } else {
+                    targetComponent = components.get(name.substring(0, s));
                 }
-            } else if (componentReference.getReference() != null) {
-    
-                // Resolve targets from the corresponding reference in the
-                // componentType
-                for (ComponentService componentService : componentReference.getReference().getTargets()) {
-
-                    // Resolve the target component and service
-                    String name = componentService.getName();
-                    ComponentService targetComponentService = componentServices.get(name);
-                    Component targetComponent;
-                    int s = name.indexOf('/');
-                    if (s == -1) {
-                        targetComponent = components.get(name);
-                    } else {
-                        targetComponent = components.get(name.substring(0, s));
-                    }
-                    
-                    if (targetComponentService != null) {
+                
+                if (targetComponentService != null) {
 
-                        // Check that the target component service provides
-                        // a superset of
-                        // the component reference interface
-                        if (componentReference.getInterfaceContract() == null ||
-                            interfaceContractMapper.isCompatible(componentReference.getInterfaceContract(), targetComponentService.getInterfaceContract())) {
-
-                            Target target = new Target(targetComponent, targetComponentService);
-                            targets.add(target);
-                            
-                            // mark the reference target as resolved. Used later when we are looking to 
-                            // see if an sca binding is associated with a resolved target or not
-                            componentService.setUnresolved(false);
-                        } else {
-                            warning("Incompatible interfaces on component reference and target: " + componentReference
-                                        .getName()
-                                        + " : "
-                                        + componentService.getName(),
-                                    composite);
-                        }
+                    // Check that the target component service provides
+                    // a superset of
+                    // the component reference interface
+                    if (componentReference.getInterfaceContract() == null ||
+                        interfaceContractMapper.isCompatible(componentReference.getInterfaceContract(), targetComponentService.getInterfaceContract())) {
+
+                        Target target = new Target(targetComponent, targetComponentService);
+                        targets.add(target);
+                        
+                        // mark the reference target as resolved. Used later when we are looking to 
+                        // see if an sca binding is associated with a resolved target or not
+                        componentService.setUnresolved(false);
                     } else {
-                        warning("Reference target not found: " + componentService.getName(), composite);
+                        warning("Incompatible interfaces on component reference and target: " + componentReference
+                                    .getName()
+                                    + " : "
+                                    + componentService.getName(),
+                                composite);
                     }
+                } else {
+                    warning("Reference target not found: " + componentService.getName(), composite);
                 }
             }
+        }
+        return targets;
+    }
+
+
+    /**
+     * Connect references to their targets.
+     * 
+     * @param composite
+     * @param componentServices
+     * @param componentReferences
+     * @param problems
+     */
+    private void connectComponentReferences(Composite composite,
+                                            Map<String, Component> components,
+                                            Map<String, ComponentService> componentServices,
+                                            Map<String, ComponentReference> componentReferences) {
+        for (ComponentReference componentReference : componentReferences.values()) {
+            
+            List<Target> targets = createComponentReferenceTargets(composite, 
+                                                                   components, 
+                                                                   componentServices, 
+                                                                   componentReference);
 
             // Select the reference bindings matching the target service bindings 
             List<Binding> selectedBindings = new ArrayList<Binding>();
@@ -463,6 +488,8 @@
                     // Find the promoted component service
                     targetComponentService = ((CompositeService)targetComponentService.getService()).getPromotedService();
                 }
+                
+                determinePolicySet(componentReference, targetComponentService);
 
                 // Match the binding against the bindings of the target service
                 Binding selected = BindingUtil.resolveBindings(componentReference, targetComponent, targetComponentService);
@@ -803,5 +830,386 @@
             }
         }
     }
+    
+    public void computePolicies(Composite composite) {
+        List<Intent> compositeIntents = null;
+        List<PolicySet> compositePolicySets = null;
+        if (composite instanceof PolicySetAttachPoint) {
+            compositeIntents = ((PolicySetAttachPoint)composite).getRequiredIntents();
+            compositePolicySets = ((PolicySetAttachPoint)composite).getPolicySets();
+        }
+        
+        //Initialize service bindings
+        for (Service service : composite.getServices()) {
+            CompositeService compositeService = (CompositeService)service;
+            
+            //inherit intents and policysets defined at composite level
+            addInheritedIntents(compositeIntents, service.getRequiredIntents());
+            addInheritedPolicySets(compositePolicySets, service.getPolicySets(), false);
+            
+            //inherit intents and policies from promoted service
+            addInheritedIntents(compositeService.getPromotedService().getRequiredIntents(), 
+                                compositeService.getRequiredIntents());
+            addInheritedPolicySets(compositeService.getPromotedService().getPolicySets(), 
+                                   compositeService.getPolicySets(), true);
+            
+            if (compositeService.getCallback() != null) {
+                addInheritedIntents(compositeService.getRequiredIntents(), 
+                                    compositeService.getCallback().getRequiredIntents());
+                addInheritedPolicySets(compositeService.getPolicySets(), 
+                                       compositeService.getCallback().getPolicySets(), 
+                                       false);
+            }
+            
+            computeBindingIntentsAndPolicySets(service);
+        }
+        
+        for (Reference reference : composite.getReferences()) {
+            addInheritedIntents(compositeIntents, reference.getRequiredIntents());
+            addInheritedPolicySets(compositePolicySets, reference.getPolicySets(), false);
+            
+            CompositeReference compReference = (CompositeReference)reference;
+            for ( Reference promotedReference : compReference.getPromotedReferences() ) {
+                addInheritedIntents(promotedReference.getRequiredIntents(), 
+                                     reference.getRequiredIntents());
+            
+                addInheritedPolicySets(promotedReference.getPolicySets(), 
+                                     reference.getPolicySets(), true);
+            }
+            
+            if (compReference.getCallback() != null) {
+                addInheritedIntents(compReference.getRequiredIntents(), 
+                                    compReference.getCallback().getRequiredIntents());
+                addInheritedPolicySets(compReference.getPolicySets(), 
+                                       compReference.getCallback().getPolicySets(), 
+                                       false);
+            }
+            
+            computeBindingIntentsAndPolicySets(reference);
+        }
+        
+        for (Component component : composite.getComponents()) {
+            //Inherit intents defined at the composite level
+            addInheritedIntents(compositeIntents, component.getRequiredIntents());
+            
+            // Inherit policysets defined at the composite level
+            addInheritedPolicySets(compositePolicySets, component.getPolicySets(), false);
+            
+            for (ComponentService componentService : component.getServices()) {
+                //inherit intents and policysets from component
+                addInheritedIntents(component.getRequiredIntents(), componentService.getRequiredIntents());
+                addInheritedPolicySets(component.getPolicySets(), componentService.getPolicySets(), false);
+
+                Service service = componentService.getService();
+                if (service != null) {
+                    // reconcile intents and policysets
+                    addInheritedIntents(service.getRequiredIntents(), componentService.getRequiredIntents());
+                    addInheritedPolicySets(service.getPolicySets(), componentService.getPolicySets(), true);
+                }
+                
+                if ( componentService.getCallback() != null ) {
+                    addInheritedIntents(componentService.getRequiredIntents(), 
+                                        componentService.getCallback().getRequiredIntents());
+                    addInheritedPolicySets(componentService.getPolicySets(), 
+                                           componentService.getCallback().getPolicySets(), 
+                                           false);
+                }
+                
+                //compute intents and policyset for each binding
+                computeBindingIntentsAndPolicySets(componentService);
+
+            }
+            
+            for (ComponentReference componentReference : component.getReferences()) {
+                //inherit intents and policysets from component
+                addInheritedIntents(component.getRequiredIntents(), componentReference.getRequiredIntents());
+                addInheritedPolicySets(component.getPolicySets(), componentReference.getPolicySets(), false);
+
+                Reference reference = componentReference.getReference();
+                if (reference != null) {
+                    // reconcile intents and policysets
+                    addInheritedIntents(reference.getRequiredIntents(), componentReference.getRequiredIntents());
+                    addInheritedPolicySets(reference.getPolicySets(), componentReference.getPolicySets(), true);
+                }
+                
+                if ( componentReference.getCallback() != null ) {
+                    addInheritedIntents(componentReference.getRequiredIntents(), 
+                                        componentReference.getCallback().getRequiredIntents());
+                    addInheritedPolicySets(componentReference.getPolicySets(), 
+                                           componentReference.getCallback().getPolicySets(), 
+                                           false);
+                }
+                
+                //compute intents and policyset for each binding
+                computeBindingIntentsAndPolicySets(componentReference);
+
+            }
+        }
+        
+    }
+    
+    private void addInheritedIntents(List<Intent> sourceList, List<Intent> targetList) {
+        if (sourceList != null) {
+            targetList.addAll(sourceList);
+        }
+    }
+    
+    private  void addInheritedPolicySets(List<PolicySet> sourceList, List<PolicySet> targetList, boolean checkOverrides) {
+        //check overrides is true when policysets are to be copied from componentType to component level
+        if ( checkOverrides ) {
+            //aggregate all the provided intents present in the target
+            List<Intent> targetProvidedIntents = new ArrayList<Intent>();
+            for ( PolicySet policySet : targetList ) {
+                targetProvidedIntents.addAll(policySet.getProvidedIntents());
+            }
+            
+            //for every policy set in the source check if it provides one of the intents that is 
+            //already provided by the policysets in the destination and do not copy them.
+            for ( PolicySet policySet : sourceList ) {
+                for ( Intent sourceProvidedIntent : policySet.getProvidedIntents() ) {
+                    if ( !targetProvidedIntents.contains(sourceProvidedIntent) ) {
+                        targetList.add(policySet);
+                    }
+                }
+            }
+        } else {
+            targetList.addAll(sourceList);
+        }
+    }
+    
+    private void computeBindingIntentsAndPolicySets(Service service) {
+        computeIntents(service.getBindings(), service.getRequiredIntents());
+        computePolicySets(service, service.getBindings(), service.getPolicySets());
+        if ( service.getCallback() != null ) {
+            computeIntents(service.getCallback().getBindings(), 
+                           service.getCallback().getRequiredIntents());
+            computePolicySets(service, 
+                              service.getCallback().getBindings(), 
+                              service.getCallback().getPolicySets());
+        }
+        trimProvidedIntents(service.getBindings());
+    }
+    
+    private void computeBindingIntentsAndPolicySets(Reference reference) {
+        computeIntents(reference.getBindings(), reference.getRequiredIntents());
+        computePolicySets(reference, reference.getBindings(), reference.getPolicySets());
+        
+        if ( reference.getCallback() != null ) {
+            computeIntents(reference.getCallback().getBindings(), 
+                           reference.getCallback().getRequiredIntents());
+            computePolicySets(reference, 
+                              reference.getCallback().getBindings(), 
+                              reference.getCallback().getPolicySets());
+        }
+        trimProvidedIntents(reference.getBindings());
+    }
+    
+    private void computeIntents(List<Binding> bindings, List<Intent> inheritedIntents) {
+        boolean found = false;
+
+        for (Binding binding : bindings) {
+            if (binding instanceof IntentAttachPoint) {
+                IntentAttachPoint policiedBinding = (IntentAttachPoint)binding;
+                IntentAttachPointType bindingType = policiedBinding.getType();
+
+                //expand profile intents
+                List<Intent> expandedIntents = expandProfileIntents(policiedBinding.getRequiredIntents());
+                policiedBinding.getRequiredIntents().clear();
+                policiedBinding.getRequiredIntents().addAll(expandedIntents);
+                
+                //validate intents specified for the binding
+                for (Intent intent : policiedBinding.getRequiredIntents()) {
+                    for (QName constrained : intent.getConstrains()) {
+                        if (bindingType.getName().getNamespaceURI().equals(constrained
+                            .getNamespaceURI()) && bindingType.getName().getLocalPart()
+                            .startsWith(constrained.getLocalPart())) {
+                            found = true;
+                            break;
+                        }
+                    }
+
+                    if (!found) {
+                        warning("Policy Intent '" + intent.getName()
+                            + "' does not constrain binding type  "
+                            + bindingType, binding);
+                    }
+                }
+                
+                //expand profile intents in inherited intents
+                expandedIntents = expandProfileIntents(inheritedIntents);
+                inheritedIntents.clear();
+                inheritedIntents.addAll(expandedIntents);
+
+                //validate if inherited intent applies to the binding and 
+                //only add such intents to the binding
+                for (Intent intent : inheritedIntents) {
+                    for (QName constrained : intent.getConstrains()) {
+                        if (bindingType.getName().getNamespaceURI().equals(constrained
+                            .getNamespaceURI()) && bindingType.getName().getLocalPart()
+                            .startsWith(constrained.getLocalPart())) {
+                            policiedBinding.getRequiredIntents().add(intent);
+                            break;
+                        }
+                    }
+                }
+                
+                Map<QName, Intent> intentsTable = new HashMap<QName, Intent>();
+                //remove duplicates
+                for ( Intent intent : policiedBinding.getRequiredIntents() ) {
+                    intentsTable.put(intent.getName(), intent);
+                }
+                
+                Map<QName, Intent> intentsTableCopy = new HashMap<QName, Intent>(intentsTable);
+                //if qualified form of intent exists remove the unqualified form
+                for ( Intent intent : intentsTableCopy.values() ) {
+                    if ( intent instanceof QualifiedIntent ) {
+                        QualifiedIntent qualifiedIntent = (QualifiedIntent)intent;
+                        if ( intentsTable.get(qualifiedIntent.getQualifiableIntent().getName()) != null ) {
+                            intentsTable.remove(qualifiedIntent.getQualifiableIntent().getName());
+                        }
+                    }
+                }
+                policiedBinding.getRequiredIntents().clear();
+                policiedBinding.getRequiredIntents().addAll(intentsTable.values());
+                
+                //exclude intents that are inherently supported by the binding type
+                List<Intent> requiredIntents = new ArrayList<Intent>(policiedBinding.getRequiredIntents());
+                for ( Intent intent : requiredIntents ) {
+                    if ( bindingType.getAlwaysProvidedIntents() != null &&
+                        bindingType.getAlwaysProvidedIntents().contains(intent) ) {
+                        policiedBinding.getRequiredIntents().remove(intent);
+                    }
+                }
+                
+            }
+        }
+    }
+
+    private void computePolicySets(Base parent,
+                                   List<Binding> bindings,
+                                   List<PolicySet> inheritedPolicySets) {
+        String appliesTo = null;
+        HashMap<QName, PolicySet> policySetTable = new HashMap<QName, PolicySet>();
+        
+        for (Binding binding : bindings) {
+            if (binding instanceof PolicySetAttachPoint) {
+                PolicySetAttachPoint policiedBinding = (PolicySetAttachPoint)binding;
+                IntentAttachPointType bindingType = policiedBinding.getType();
+
+                //validate policysets specified for the binding
+                for (PolicySet policySet : policiedBinding.getPolicySets()) {
+                    appliesTo = policySet.getAppliesTo();
+
+                    if (!isPolicySetApplicable(parent, appliesTo, bindingType)) {
+                        warning("Policy Set '" + policySet.getName()
+                            + "' does not apply to binding type  "
+                            + bindingType, binding);
+
+                    }
+                }
+                
+                for (PolicySet policySet : inheritedPolicySets) {
+                    appliesTo = policySet.getAppliesTo();
+                    if (isPolicySetApplicable(parent, appliesTo, bindingType)) {
+                        policiedBinding.getPolicySets().add(policySet);
+                    }
+                }
+                
+                //get rid of duplicate entries
+                for ( PolicySet policySet : policiedBinding.getPolicySets() ) {
+                    policySetTable.put(policySet.getName(), policySet);
+                }
+                policiedBinding.getPolicySets().clear();
+                policiedBinding.getPolicySets().addAll(policySetTable.values());
+                
+                //expand profile intents
+                List<Intent> expandedIntents = null;
+                for ( PolicySet policySet : policiedBinding.getPolicySets() ) {
+                    expandedIntents = expandProfileIntents(policySet.getProvidedIntents());
+                    policySet.getProvidedIntents().clear();
+                    policySet.getProvidedIntents().addAll(expandedIntents);
+                }
+            }
+        }
+    }
+    
+    private boolean isPolicySetApplicable(Base parent,
+                                          String xpath,
+                                          IntentAttachPointType bindingType) {
+        //create a xml node out of the parent object.. i.e. write the parent object as scdl fragment
+        //invoke PropertyUtil.evaluate(null, node, xpath)
+        //verify the result Node's QName against the bindingType's name
+        
+        if (parent instanceof ComponentReference) {
+        } else if (parent instanceof ComponentReference) {
+        } else if (parent instanceof Component) {
+        } else if (parent instanceof CompositeService) {
+        } else if (parent instanceof CompositeReference) {
+
+        }
+        return true;
+    }
+    
+    private List<Intent> expandProfileIntents(List<Intent> intents) {
+        List<Intent> expandedIntents = new ArrayList<Intent>();
+        for ( Intent intent : intents ) {
+            if ( intent instanceof ProfileIntent ) {
+                ProfileIntent profileIntent = (ProfileIntent)intent;
+                List<Intent> requiredIntents = profileIntent.getRequiredIntents();
+                expandedIntents.addAll(expandProfileIntents(requiredIntents));
+            } else {
+                expandedIntents.add(intent);
+            }
+        }
+        return expandedIntents;
+    }
+    
+    private void trimProvidedIntents(List<Binding> bindings) {
+        for (Binding binding : bindings) {
+            if (binding instanceof PolicySetAttachPoint) {
+                trimProvidiedIntents((PolicySetAttachPoint)binding);
+            }
+        }
+    }
+    
+    private void trimProvidiedIntents(PolicySetAttachPoint policiedBinding) {
+        for ( PolicySet policySet : policiedBinding.getPolicySets() ) {
+            for ( Intent providedIntent : policySet.getProvidedIntents() ) {
+                if ( policiedBinding.getRequiredIntents().contains(providedIntent) ) {
+                    policiedBinding.getRequiredIntents().remove(providedIntent);
+                }
+            }
+        }
+    }
+    
+    private void determinePolicySet(ComponentReference componentReference, ComponentService componentService) {
+        //add the target component's intents to the binding
+        for ( Binding aBinding : componentReference.getBindings() ) {
+            if ( aBinding instanceof PolicySetAttachPoint ) {
+                PolicySetAttachPoint policiedBinding = (PolicySetAttachPoint)aBinding;
+                IntentAttachPointType bindingType = policiedBinding.getType();
+                for ( Intent intent : componentService.getRequiredIntents() ) {
+                    if ( !policiedBinding.getRequiredIntents().contains(intent) ) {
+                        for (QName constrained : intent.getConstrains()) {
+                            if (bindingType.getName().getNamespaceURI().equals(constrained
+                                .getNamespaceURI()) && bindingType.getName().getLocalPart()
+                                .startsWith(constrained.getLocalPart())) {
+                                policiedBinding.getRequiredIntents().add(intent);
+                                break;
+                            }
+                        }
+                    }
+                }
+                trimProvidiedIntents(policiedBinding);
+                //determine additional policysets that match remaining intents
+                if ( policiedBinding.getRequiredIntents().size() > 0 ) {
+                    //TODO: resolved to domain policy registry and attach suitable policy sets to the binding
+                    //if there are intents that are not provided by any policy set throw a warning
+                }
+            }
+            
+        }
+    }
+
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org


Mime
View raw message