Modified: 
tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeBindingConfigurationBuilderImpl.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeBindingConfigurationBuilderImpl.java?rev=724435&r1=724434&r2=724435&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeBindingConfigurationBuilderImpl.java
 (original)
+++ 
tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeBindingConfigurationBuilderImpl.java
 Mon Dec  8 10:22:09 2008
@@ -35,10 +35,13 @@
 /**
  * A composite builder that performs any additional building steps that
  * composite service bindings may need.  Used for WSDL generation.
+ * 
+ * TODO - What is this actually used for? I can't find any references in the
+ *        code base
  *
  * @version $Rev$ $Date$
  */
-public class CompositeBindingConfigurationBuilderImpl extends 
BaseConfigurationBuilderImpl implements CompositeBuilder {
+public class CompositeBindingConfigurationBuilderImpl extends 
CompositeBindingURIBuilderImpl implements CompositeBuilder {
 
     public CompositeBindingConfigurationBuilderImpl(FactoryExtensionPoint 
factories, InterfaceContractMapper mapper) {
         super(factories.getFactory(AssemblyFactory.class),

Modified: 
tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeBindingURIBuilderImpl.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeBindingURIBuilderImpl.java?rev=724435&r1=724434&r2=724435&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeBindingURIBuilderImpl.java
 (original)
+++ 
tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositeBindingURIBuilderImpl.java
 Mon Dec  8 10:22:09 2008
@@ -19,12 +19,29 @@
 
 package org.apache.tuscany.sca.assembly.builder.impl;
 
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.transform.TransformerFactory;
 
 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.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.Contract;
+import org.apache.tuscany.sca.assembly.Implementation;
+import org.apache.tuscany.sca.assembly.Property;
+import org.apache.tuscany.sca.assembly.Reference;
+import org.apache.tuscany.sca.assembly.SCABinding;
 import org.apache.tuscany.sca.assembly.SCABindingFactory;
+import org.apache.tuscany.sca.assembly.Service;
 import org.apache.tuscany.sca.assembly.builder.CompositeBuilder;
 import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException;
 import org.apache.tuscany.sca.definitions.Definitions;
@@ -36,7 +53,7 @@
  *
  * @version $Rev$ $Date$
  */
-public class CompositeBindingURIBuilderImpl extends 
BaseConfigurationBuilderImpl implements CompositeBuilder {
+public class CompositeBindingURIBuilderImpl extends BaseBuilderImpl implements 
CompositeBuilder {
 
     @Deprecated
     public CompositeBindingURIBuilderImpl(AssemblyFactory assemblyFactory,
@@ -64,4 +81,456 @@
         configureBindingURIsAndNames(composite, definitions, monitor);
     }
     
+    /**
+     * Called by CompositeBindingURIBuilderImpl
+     *  
+     * @param composite the composite to be configured
+     */
+    protected void configureBindingURIsAndNames(Composite composite, 
Definitions definitions, Monitor monitor) throws CompositeBuilderException {
+        configureBindingURIs(composite, null, definitions, null, monitor);
+        configureBindingNames(composite, monitor);
+    }
+
+    /**
+     * Fully resolve the binding URIs based on available information. This 
includes information
+     * from the ".composite" files, from resources associated with the 
binding, e.g. WSDL files, 
+     * from any associated policies and from the default information for each 
binding type.
+     *  
+     * @param composite the composite to be configured
+     * @param defaultBindings list of default binding configurations
+     */
+    protected void configureBindingURIs(Composite composite,
+                                        Definitions definitions, List<Binding> 
defaultBindings,
+                                        Monitor monitor) throws 
CompositeBuilderException {
+        configureBindingURIs(composite, null, definitions, defaultBindings, 
monitor);
+    }
+       
+     /**
+      * Fully resolve the binding URIs based on available information. This 
includes information
+      * from the ".composite" files, from resources associated with the 
binding, e.g. WSDL files, 
+      * from any associated policies and from the default information for each 
binding type.
+      * 
+      * NOTE: This method repeats some of the processing performed by the 
configureComponents()
+      *       method above.  The duplication is needed because 
NodeConfigurationServiceImpl
+      *       calls this method without previously calling 
configureComponents().  In the
+      *       normal builder sequence used by CompositeBuilderImpl, both of 
these methods
+      *       are called.
+      *
+      * TODO: Share the URL calculation algorithm with the 
configureComponents() method above
+      *       although keeping the configureComponents() methods signature as 
is because when
+      *       a composite is actually build in a node the node default 
information is currently
+      *       available
+      *  
+      * @param composite the composite to be configured
+      * @param uri the path to the composite provided through any nested 
composite component implementations
+      * @param defaultBindings list of default binding configurations
+      */
+    private void configureBindingURIs(Composite composite, String uri,
+                                      Definitions definitions, List<Binding> 
defaultBindings,
+                                      Monitor monitor) throws 
CompositeBuilderException {
+        
+        String parentComponentURI = uri;
+        
+        // Process nested composites recursively
+        for (Component component : composite.getComponents()) {
+
+            // Initialize component URI
+            String componentURI;
+            if (parentComponentURI == null) {
+                componentURI = component.getName();
+            } else {
+                componentURI = URI.create(parentComponentURI + 
'/').resolve(component.getName()).toString();
+            }
+            component.setURI(componentURI);
+
+            Implementation implementation = component.getImplementation();
+            if (implementation instanceof Composite) {
+
+                // Process nested composite
+                configureBindingURIs((Composite)implementation, componentURI, 
definitions, defaultBindings, monitor);
+            }
+        }  
+        
+        // Initialize composite service binding URIs
+        List<Service> compositeServices = composite.getServices();
+        for (Service service : compositeServices) {
+            // Set default binding names 
+            
+            // Create default SCA binding
+            if (service.getBindings().isEmpty()) {
+                SCABinding scaBinding = createSCABinding(definitions);
+                service.getBindings().add(scaBinding);
+            }
+    
+            // Initialize binding names and URIs
+            for (Binding binding : service.getBindings()) {  
+                constructBindingName(service, binding, monitor);
+                constructBindingURI(parentComponentURI, composite, service, 
binding, defaultBindings, monitor);
+            }
+        }
+        
+        // Initialize component service binding URIs
+        for (Component component : composite.getComponents()) {
+            
+            // Index properties, services and references
+            Map<String, Service> services = new HashMap<String, Service>();
+            Map<String, Reference> references = new HashMap<String, 
Reference>();
+            Map<String, Property> properties = new HashMap<String, Property>();
+            indexImplementationPropertiesServicesAndReferences(component,
+                                                               services,
+                                                               references,
+                                                               properties,
+                                                               monitor);
+
+            // Index component services, references and properties
+            // Also check for duplicates
+            Map<String, ComponentService> componentServices =
+                new HashMap<String, ComponentService>();
+            Map<String, ComponentReference> componentReferences =
+                new HashMap<String, ComponentReference>();
+            Map<String, ComponentProperty> componentProperties =
+                new HashMap<String, ComponentProperty>();
+            indexComponentPropertiesServicesAndReferences(component,
+                                                          componentServices,
+                                                          componentReferences,
+                                                          componentProperties,
+                                                          monitor);
+
+            // Reconcile component services/references/properties and
+            // implementation services/references and create component
+            // services/references/properties for the services/references
+            // declared by the implementation
+            reconcileServices(component, services, componentServices, monitor);
+            reconcileReferences(component, references, componentReferences, 
monitor);
+            reconcileProperties(component, properties, componentProperties, 
monitor);
+            
+            for (ComponentService service : component.getServices()) {
+    
+                // Create default SCA binding
+                if (service.getBindings().isEmpty()) {
+                    SCABinding scaBinding = createSCABinding(definitions);
+                    service.getBindings().add(scaBinding);
+                }
+    
+                // Initialize binding names and URIs
+                for (Binding binding : service.getBindings()) {
+                    
+                    constructBindingName(service, binding, monitor);
+                    constructBindingURI(component, service, binding, 
defaultBindings, monitor);
+                }
+            } 
+        }
+    }
+
+    /**
+     * Add default names for callback bindings and reference bindings.  Needs 
to be
+     * separate from configureBindingURIs() because configureBindingURIs() is 
called
+     * by NodeConfigurationServiceImpl as well as by CompositeBuilderImpl.
+     */
+    private void configureBindingNames(Composite composite, Monitor monitor) {
+        
+        // Process nested composites recursively
+        for (Component component : composite.getComponents()) {
+
+            Implementation implementation = component.getImplementation();
+            if (implementation instanceof Composite) {
+
+                // Process nested composite
+                configureBindingNames((Composite)implementation, monitor);
+            }
+        }  
+        
+        // Initialize composite service callback binding names
+        for (Service service : composite.getServices()) {
+
+            if (service.getCallback() != null) {
+                for (Binding binding : service.getCallback().getBindings()) {
+                    constructBindingName(service, binding, monitor);
+                }
+            }
+        }
+        
+        // Initialize composite reference binding names
+        for (Reference reference : composite.getReferences()) {
+
+            for (Binding binding : reference.getBindings()) {  
+                constructBindingName(reference, binding, monitor);
+            }
+
+            if (reference.getCallback() != null) {
+                for (Binding binding : reference.getCallback().getBindings()) {
+                    constructBindingName(reference, binding, monitor);
+                }
+            }
+        }
+        
+        // Initialize component service and reference binding names
+        for (Component component : composite.getComponents()) {
+
+            // Initialize component service callback binding names
+            for (ComponentService service : component.getServices()) {
+
+                if (service.getCallback() != null) {
+                    for (Binding binding : 
service.getCallback().getBindings()) {
+                        constructBindingName(service, binding, monitor);
+                    }
+                }
+            } 
+        
+            // Initialize component reference binding names
+            for (ComponentReference reference : component.getReferences()) {
+
+                // Initialize binding names
+                for (Binding binding : reference.getBindings()) {  
+                    constructBindingName(reference, binding, monitor);
+                }
+
+                if (reference.getCallback() != null) {
+                    for (Binding binding : 
reference.getCallback().getBindings()) {
+                        constructBindingName(reference, binding, monitor);
+                    }
+                }
+            }
+        }
+    }
+    
+    /**
+     * If a binding name is not provided by the user, construct it based on 
the service
+     * or reference name
+     * 
+     * @param contract the service or reference
+     * @param binding
+     */
+    private void constructBindingName(Contract contract, Binding binding, 
Monitor monitor) {
+        
+        // set the default binding name if one is required        
+        // if there is no name on the binding then set it to the service or 
reference name 
+        if (binding.getName() == null){
+            binding.setName(contract.getName());
+        }
+            
+        // Check that multiple bindings do not have the same name
+        for (Binding otherBinding : contract.getBindings()) {
+            if (otherBinding == binding) {
+                // Skip the current binding
+                continue;
+            }
+            if (binding.getClass() != otherBinding.getClass()) {
+                // Look for a binding of the same type
+                continue;
+            }
+            if (binding.getName().equals(otherBinding.getName())) {
+                warning(monitor, contract instanceof Service ? 
"MultipleBindingsForService" : "MultipleBindingsForReference",
+                        binding, contract.getName(), binding.getName());
+            }
+        }
+    }
+
+    /**
+     * URI construction for composite bindings based on Assembly Specification 
section 1.7.2, This method
+     * assumes that the component URI part of the binding URI is formed from 
the part to the 
+     * composite in question and just calls the generic constructBindingURI 
method with this 
+     * information
+     * 
+     * @param parentComponentURI
+     * @param composite
+     * @param service
+     * @param binding
+     * @param defaultBindings
+     */
+    private void constructBindingURI(String parentComponentURI, Composite 
composite, Service service,
+                                     Binding binding, List<Binding> 
defaultBindings, Monitor monitor) 
+    throws CompositeBuilderException{
+        // This is a composite service so there is no component to provide a 
component URI
+        // The path to this composite (through nested composites) is used.
+        boolean includeBindingName = composite.getServices().size() != 1;
+        constructBindingURI(parentComponentURI, service, binding, 
includeBindingName, defaultBindings, monitor);
+    }
+
+     /**
+      * URI construction for component bindings based on Assembly 
Specification section 1.7.2. This method
+      * calculates the component URI part based on component information 
before calling the generic
+      * constructBindingURI method
+      *
+      * @param component the component that holds the service
+      * @param service the service that holds the binding
+      * @param binding the binding for which the URI is being constructed
+      * @param defaultBindings the list of default binding configurations
+      */
+    private void constructBindingURI(Component component, Service service,
+                                     Binding binding, List<Binding> 
defaultBindings, Monitor monitor)
+        throws CompositeBuilderException{
+        boolean includeBindingName = component.getServices().size() != 1;
+        constructBindingURI(component.getURI(), service, binding, 
includeBindingName, defaultBindings, monitor);
+    }
+            
+    /**
+     * Generic URI construction for bindings based on Assembly Specification 
section 1.7.2
+     * 
+     * @param componentURIString the string version of the URI part that comes 
from the component name
+     * @param service the service in question
+     * @param binding the binding for which the URI is being constructed
+     * @param includeBindingName when set true the serviceBindingURI part 
should be used
+     * @param defaultBindings the list of default binding configurations
+     * @throws CompositeBuilderException
+     */
+    private void constructBindingURI(String componentURIString, Service 
service, Binding binding,
+                                     boolean includeBindingName, List<Binding> 
defaultBindings, Monitor monitor) 
+        throws CompositeBuilderException{
+        
+        try {
+            // calculate the service binding URI
+            URI bindingURI;
+            if (binding.getURI() != null){
+                bindingURI = new URI(binding.getURI());
+
+                // if the user has provided an absolute binding URI then use it
+                if (bindingURI.isAbsolute()){
+                    binding.setURI(bindingURI.toString());
+                    return;
+                }
+            } else {
+                bindingURI = null;
+            }
+            
+            // Get the service binding name
+            URI bindingName;
+            if (binding.getName() != null) {
+                bindingName = new URI(binding.getName());
+            } else {
+                bindingName = new URI("");
+            }
+            
+            // calculate the component URI  
+            URI componentURI;
+            if (componentURIString != null) {
+                componentURI = new URI(addSlashToPath(componentURIString));
+            } else {
+                componentURI = null;
+            }
+            
+            // if the user has provided an absolute component URI then use it
+            if (componentURI != null && componentURI.isAbsolute()){
+                binding.setURI(constructBindingURI(null, componentURI, 
bindingURI, includeBindingName, bindingName));
+                return;
+            }         
+            
+            // calculate the base URI
+            URI baseURI = null;
+            if (defaultBindings != null) {
+                for (Binding defaultBinding : defaultBindings){
+                    if (binding.getClass() == defaultBinding.getClass()){
+                        baseURI = new 
URI(addSlashToPath(defaultBinding.getURI()));
+                        break;
+                    }
+                }
+            }
+            
+            binding.setURI(constructBindingURI(baseURI, componentURI, 
bindingURI, includeBindingName, bindingName));
+        } catch (URISyntaxException ex) {
+            error(monitor, "URLSyntaxException", binding, componentURIString, 
service.getName(), binding.getName());
+        }      
+    }
+    
+    /**
+     * Use to ensure that URI paths end in "/" as here we want to maintain the
+     * last path element of an base URI when other URI are resolved against 
it. This is
+     * not the default behaviour of URI resolution as defined in RFC 2369
+     * 
+     * @param path the path string to which the "/" is to be added
+     * @return the resulting path with a "/" added if it not already there
+     */
+    private static String addSlashToPath(String path){
+        if (path.endsWith("/") || path.endsWith("#")){
+            return path;
+        } else {
+            return path + "/";
+        }
+    }
+    
+    /**
+     * Concatenate binding URI parts together based on Assembly Specification 
section 1.7.2
+     * 
+     * @param baseURI the base of the binding URI
+     * @param componentURI the middle part of the binding URI derived from the 
component name
+     * @param bindingURI the end part of the binding URI
+     * @param includeBindingName when set true the binding name part should be 
used
+     * @param bindingName the binding name
+     * @return the resulting URI as a string
+     */
+    private static String constructBindingURI(URI baseURI, URI componentURI, 
URI bindingURI, boolean includeBindingName, URI bindingName){        
+        String uriString;
+        
+        if (baseURI == null) {
+            if (componentURI == null){
+                if (bindingURI != null ) {
+                    uriString = bindingURI.toString();
+                } else {
+                    uriString = bindingName.toString();
+                }
+            } else {
+                if (bindingURI != null ) {
+                    uriString = componentURI.resolve(bindingURI).toString();
+                } else {
+                    if (includeBindingName) {
+                        uriString = 
componentURI.resolve(bindingName).toString();
+                    } else {
+                        uriString = componentURI.toString();
+                    }
+                }
+            }
+        } else {
+            if (componentURI == null) {
+                if (bindingURI != null ) {
+                    uriString = basedURI(baseURI, bindingURI).toString();
+                } else {
+                    if (includeBindingName) {
+                        uriString = basedURI(baseURI, bindingName).toString();
+                    } else {
+                        uriString = baseURI.toString();
+                    }
+                }
+            } else {
+                if (bindingURI != null ) {
+                    uriString = basedURI(baseURI, 
componentURI.resolve(bindingURI)).toString();
+                } else {
+                    if (includeBindingName) {
+                        uriString = basedURI(baseURI, 
componentURI.resolve(bindingName)).toString();
+                    } else {
+                        uriString = basedURI(baseURI, componentURI).toString();
+                    }
+                }
+            }
+        }
+        
+        // tidy up by removing any trailing "/"
+        if (uriString.endsWith("/")){
+            uriString = uriString.substring(0, uriString.length()-1);   
+        }
+        
+        URI uri = URI.create(uriString);
+        if (!uri.isAbsolute()) {
+            uri = URI.create("/").resolve(uri);
+        }
+        return uri.toString();
+    }
+
+    /**
+     * Combine a URI with a base URI.
+     * 
+     * @param baseURI
+     * @param uri
+     * @return
+     */
+    private static URI basedURI(URI baseURI, URI uri) {
+        if (uri.getScheme() != null) {
+            return uri;
+        }
+        String str = uri.toString();
+        if (str.startsWith("/")) {
+            str = str.substring(1);
+        }
+        return URI.create(baseURI.toString() + str).normalize();
+    }    
+    
 }

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=724435&r1=724434&r2=724435&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
 Mon Dec  8 10:22:09 2008
@@ -20,14 +20,28 @@
 package org.apache.tuscany.sca.assembly.builder.impl;
 
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.apache.tuscany.sca.assembly.AssemblyFactory;
+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.ConfiguredOperation;
 import org.apache.tuscany.sca.assembly.EndpointFactory;
+import org.apache.tuscany.sca.assembly.Implementation;
+import org.apache.tuscany.sca.assembly.OperationsConfigurator;
+import org.apache.tuscany.sca.assembly.Reference;
+import org.apache.tuscany.sca.assembly.Service;
 import org.apache.tuscany.sca.assembly.builder.CompositeBuilder;
 import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException;
 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;
 
 /**
  * A composite builder that computes policy sets based on attached intents and 
policy sets.
@@ -36,10 +50,10 @@
  *
  * @version $Rev$ $Date$
  */
-public class CompositePolicyBuilderImpl extends BaseWireBuilderImpl implements 
CompositeBuilder {
+public class CompositePolicyBuilderImpl extends BaseBuilderImpl implements 
CompositeBuilder {
 
     public CompositePolicyBuilderImpl(AssemblyFactory assemblyFactory, 
EndpointFactory endpointFactory, InterfaceContractMapper 
interfaceContractMapper) {
-        super(assemblyFactory, endpointFactory, interfaceContractMapper);
+        super(assemblyFactory, null, null, null, interfaceContractMapper);
     }
 
     public String getID() {
@@ -49,4 +63,200 @@
     public void build(Composite composite, Definitions definitions, Monitor 
monitor) throws CompositeBuilderException {
         computePolicies(composite, monitor);
     }
+    
+    protected void computePolicies(Composite composite, Monitor monitor) {
+        
+        // compute policies recursively
+        for (Component component : composite.getComponents()) {
+            Implementation implementation = component.getImplementation();
+            if (implementation instanceof Composite) {
+                computePolicies((Composite)implementation, monitor);
+            }
+        }
+    
+        for (Component component : composite.getComponents()) {
+
+            // 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());
+
+            Implementation implemenation = component.getImplementation(); 
+            try {
+                
PolicyConfigurationUtil.computeImplementationIntentsAndPolicySets(implemenation,
 component);
+            } catch ( Exception e ) {
+                error(monitor, "PolicyRelatedException", implemenation, e);
+                //throw new RuntimeException(e);
+            }
+
+            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);
+                     
+                     //reconcile intents and policysets for operations 
+                     boolean notFound;
+                     List<ConfiguredOperation> opsFromComponentType = new 
ArrayList<ConfiguredOperation>();
+                     for ( ConfiguredOperation ctsConfOp : 
service.getConfiguredOperations() ) {
+                         notFound = true;
+                         for ( ConfiguredOperation csConfOp : 
componentService.getConfiguredOperations() ) {
+                             if ( 
csConfOp.getName().equals(ctsConfOp.getName()) ) {
+                                 
PolicyComputationUtils.addInheritedIntents(ctsConfOp.getRequiredIntents(), 
csConfOp.getRequiredIntents());
+                                 
PolicyComputationUtils.addInheritedPolicySets(ctsConfOp.getPolicySets(), 
csConfOp.getPolicySets(), true);
+                                 notFound = false;
+                             } 
+                         }
+                         
+                         if ( notFound ) {
+                             opsFromComponentType.add(ctsConfOp);
+                         }
+                     }
+                     
componentService.getConfiguredOperations().addAll(opsFromComponentType);
+                }
+                
+                try {
+                    //compute the intents for operations under service element
+                    
PolicyConfigurationUtil.computeIntentsForOperations(componentService);
+                    //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 (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 the intents for operations under reference 
element
+                    
PolicyConfigurationUtil.computeIntentsForOperations(componentReference);
+                    //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);
+                    }
+                } catch ( Exception e ) {
+                    error(monitor, "PolicyRelatedException", 
componentReference, e);
+                    //throw new RuntimeException(e);
+                }
+            }
+        }
+
+        PolicyConfigurationUtil.inheritDefaultPolicies(composite, 
composite.getServices());
+        PolicyConfigurationUtil.inheritDefaultPolicies(composite, 
composite.getReferences());
+
+        //compute policies for composite service bindings
+        for (Service service : composite.getServices()) {
+            addPoliciesFromPromotedService((CompositeService)service);
+            try {
+                //compute the intents for operations under service element
+                PolicyConfigurationUtil.computeIntentsForOperations(service);
+                //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);
+                //throw new RuntimeException(e);
+            }
+                
+        }
+    
+        for (Reference reference : composite.getReferences()) {
+            CompositeReference compReference = (CompositeReference)reference;
+            addPoliciesFromPromotedReference(compReference);
+            try {
+                //compute the intents for operations under service element
+                PolicyConfigurationUtil.computeIntentsForOperations(reference);
+                //addInheritedOpConfOnBindings(reference);
+                
+                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);
+            }
+        }
+    }
+    
+    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);
+        
addInheritedOperationConfigurations(compositeService.getPromotedService(), 
compositeService);
+    }
+    
+    private void addPoliciesFromPromotedReference(CompositeReference 
compositeReference) {
+        for ( Reference promotedReference : 
compositeReference.getPromotedReferences() ) {
+           
PolicyComputationUtils.addInheritedIntents(promotedReference.getRequiredIntents(),
 
+                               compositeReference.getRequiredIntents());
+       
+           
PolicyComputationUtils.addInheritedPolicySets(promotedReference.getPolicySets(),
 
+                                  compositeReference.getPolicySets(), true);
+           addInheritedOperationConfigurations(promotedReference, 
compositeReference);
+        }
+    } 
+    
+    private void addInheritedOperationConfigurations(
+            OperationsConfigurator source, OperationsConfigurator target) {
+        boolean found = false;
+
+        List<ConfiguredOperation> additionalOperations = new 
ArrayList<ConfiguredOperation>();
+        for (ConfiguredOperation sourceConfOp : source
+                .getConfiguredOperations()) {
+            for (ConfiguredOperation targetConfOp : target
+                    .getConfiguredOperations()) {
+                if (sourceConfOp.getName().equals(targetConfOp.getName())) {
+                    PolicyComputationUtils.addInheritedIntents(sourceConfOp
+                            .getRequiredIntents(), targetConfOp
+                            .getRequiredIntents());
+                    PolicyComputationUtils.addInheritedPolicySets(sourceConfOp
+                            .getPolicySets(), targetConfOp.getPolicySets(),
+                            true);
+                    found = true;
+                    break;
+                }
+            }
+
+            if (!found) {
+                additionalOperations.add(sourceConfOp);
+            }
+        }
+
+        if (!additionalOperations.isEmpty()) {
+            target.getConfiguredOperations().addAll(additionalOperations);
+        }
+    }    
 }

Modified: 
tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositePromotionBuilderImpl.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositePromotionBuilderImpl.java?rev=724435&r1=724434&r2=724435&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositePromotionBuilderImpl.java
 (original)
+++ 
tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/CompositePromotionBuilderImpl.java
 Mon Dec  8 10:22:09 2008
@@ -20,12 +20,25 @@
 package org.apache.tuscany.sca.assembly.builder.impl;
 
 
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 import org.apache.tuscany.sca.assembly.AssemblyFactory;
+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.EndpointFactory;
+import org.apache.tuscany.sca.assembly.Implementation;
+import org.apache.tuscany.sca.assembly.Reference;
+import org.apache.tuscany.sca.assembly.Service;
 import org.apache.tuscany.sca.assembly.builder.CompositeBuilder;
 import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException;
 import org.apache.tuscany.sca.definitions.Definitions;
+import org.apache.tuscany.sca.interfacedef.InterfaceContract;
 import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper;
 import org.apache.tuscany.sca.monitor.Monitor;
 
@@ -35,10 +48,10 @@
  *
  * @version $Rev$ $Date$
  */
-public class CompositePromotionBuilderImpl extends BaseWireBuilderImpl 
implements CompositeBuilder {
+public class CompositePromotionBuilderImpl extends BaseBuilderImpl implements 
CompositeBuilder {
 
     public CompositePromotionBuilderImpl(AssemblyFactory assemblyFactory, 
EndpointFactory endpointFactory, InterfaceContractMapper 
interfaceContractMapper) {
-        super(assemblyFactory, endpointFactory, interfaceContractMapper);
+        super(assemblyFactory, null, null, null, interfaceContractMapper);
     }
 
     public String getID() {
@@ -48,4 +61,168 @@
     public void build(Composite composite, Definitions definitions, Monitor 
monitor) throws CompositeBuilderException {
         connectCompositeReferencesAndServices(composite, monitor);
     }
+    
+    /**
+     * Connect composite references and services to the reference and services 
that they promote.
+     * 
+     * @param composite
+     * @param componentServices
+     * @param problems
+     */
+    protected void connectCompositeReferencesAndServices(Composite composite, 
Monitor monitor){
+        // Wire nested composites recursively
+        for (Component component : composite.getComponents()) {
+            Implementation implementation = component.getImplementation();
+            if (implementation instanceof Composite) {
+                
connectCompositeReferencesAndServices((Composite)implementation, monitor);
+            }
+        }
+
+        // Index components, services and references
+        Map<String, Component> components = new HashMap<String, Component>();
+        Map<String, ComponentService> componentServices = new HashMap<String, 
ComponentService>();
+        Map<String, ComponentReference> componentReferences = new 
HashMap<String, ComponentReference>();
+        indexComponentsServicesAndReferences(composite, components, 
componentServices, componentReferences);
+
+        // Connect composite services and references to the component
+        // services and references that they promote
+        connectCompositeServices(composite, components, componentServices, 
monitor);
+        connectCompositeReferences(composite, componentReferences, monitor);
+    }
+            
+    /**
+     * Connect composite services to the component services that they promote.
+     * 
+     * @param composite
+     * @param componentServices
+     * @param problems
+     */
+    private void connectCompositeServices(Composite composite,
+                                          Map<String, Component> components,
+                                          Map<String, ComponentService> 
componentServices,
+                                          Monitor monitor) {
+    
+        // Propagate interfaces from inner composite components' services to
+        // their component services
+        for (Component component : composite.getComponents()) {
+            if (component.getImplementation() instanceof Composite) {
+                for (ComponentService componentService : 
component.getServices()) {
+                    Service service = componentService.getService();
+                    if (service != null) {
+                        if (componentService.getInterfaceContract() == null) {
+                            
componentService.setInterfaceContract(service.getInterfaceContract());
+                        }
+                    }
+                }
+            }
+        }
+    
+        // Connect composite services to the component services that they
+        // promote
+        for (Service service : composite.getServices()) {
+            CompositeService compositeService = (CompositeService)service;
+            ComponentService componentService = 
compositeService.getPromotedService();
+            if (componentService != null && componentService.isUnresolved()) {
+                
+                String promotedComponentName = 
compositeService.getPromotedComponent().getName(); 
+                String promotedServiceName;
+                if (componentService.getName() != null) {
+                    promotedServiceName = promotedComponentName + '/' + 
componentService.getName();
+                } else {
+                    promotedServiceName = promotedComponentName;
+                }
+                ComponentService promotedService = 
componentServices.get(promotedServiceName);
+                if (promotedService != null) {
+    
+                    // Point to the resolved component
+                    Component promotedComponent = 
components.get(promotedComponentName);
+                    compositeService.setPromotedComponent(promotedComponent);
+                    
+                    // Point to the resolved component service
+                    compositeService.setPromotedService(promotedService);
+    
+                    // Use the interface contract from the component service if
+                    // none is specified on the composite service
+                    InterfaceContract compositeServiceInterfaceContract = 
compositeService.getInterfaceContract();
+                    InterfaceContract promotedServiceInterfaceContract = 
promotedService.getInterfaceContract();
+                    if (compositeServiceInterfaceContract == null) {
+                        
compositeService.setInterfaceContract(promotedServiceInterfaceContract);
+                    } else if (promotedServiceInterfaceContract != null) {
+                        // Check the compositeServiceInterfaceContract and 
promotedServiceInterfaceContract
+                        boolean isCompatible = 
interfaceContractMapper.isCompatible(compositeServiceInterfaceContract, 
promotedServiceInterfaceContract);
+                        if(!isCompatible){
+                            warning(monitor, "ServiceInterfaceNotSubSet", 
compositeService, promotedServiceName);
+                        }
+                    }
+    
+                } else {
+                    warning(monitor, "PromotedServiceNotFound", composite, 
composite.getName().toString(), promotedServiceName);
+                }
+            }
+        }
+    
+    }
+
+    /**
+     * Resolves promoted references.
+     * 
+     * @param composite
+     * @param componentReferences
+     * @param problems
+     */
+    private void connectCompositeReferences(Composite composite,
+                                            Map<String, ComponentReference> 
componentReferences, Monitor monitor) {
+    
+        // Propagate interfaces from inner composite components' references to
+        // their component references
+        for (Component component : composite.getComponents()) {
+            if (component.getImplementation() instanceof Composite) {
+                for (ComponentReference componentReference : 
component.getReferences()) {
+                    Reference reference = componentReference.getReference();
+                    if (reference != null) {
+                        if (componentReference.getInterfaceContract() == null) 
{
+                            
componentReference.setInterfaceContract(reference.getInterfaceContract());
+                        }
+                    }
+                }
+            }
+        }
+    
+        // Connect composite references to the component references
+        // that they promote
+        for (Reference reference : composite.getReferences()) {
+            CompositeReference compositeReference = 
(CompositeReference)reference;
+            List<ComponentReference> promotedReferences = 
compositeReference.getPromotedReferences();
+            for (int i = 0, n = promotedReferences.size(); i < n; i++) {
+                ComponentReference componentReference = 
promotedReferences.get(i);
+                if (componentReference.isUnresolved()) {
+                    String componentReferenceName = 
componentReference.getName();
+                    componentReference = 
componentReferences.get(componentReferenceName);
+                    if (componentReference != null) {
+    
+                        // Point to the resolved component reference
+                        promotedReferences.set(i, componentReference);
+    
+                        // Use the interface contract from the component
+                        // reference if none
+                        // is specified on the composite reference
+                        
+                        InterfaceContract compositeReferenceInterfaceContract 
= compositeReference.getInterfaceContract();
+                        InterfaceContract componentReferenceInterfaceContract 
= componentReference.getInterfaceContract();
+                        if (compositeReferenceInterfaceContract == null) {
+                            
compositeReference.setInterfaceContract(componentReferenceInterfaceContract);
+                        } else if (componentReferenceInterfaceContract != 
null) {
+                            // Check the compositeInterfaceContract and 
componentInterfaceContract
+                            boolean isCompatible = 
interfaceContractMapper.isCompatible(compositeReferenceInterfaceContract, 
componentReferenceInterfaceContract);
+                            if (!isCompatible) {
+                                warning(monitor, 
"ReferenceInterfaceNotSubSet", compositeReference, componentReferenceName);
+                            }
+                        }
+                    } else {
+                        warning(monitor, "PromotedReferenceNotFound", 
composite, composite.getName().toString(), componentReferenceName);
+                    }
+                }
+            }
+        }
+    }    
 }

Modified: 
tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/EndpointBuilderImpl.java
URL: 
http://svn.apache.org/viewvc/tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/EndpointBuilderImpl.java?rev=724435&r1=724434&r2=724435&view=diff
==============================================================================
--- 
tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/EndpointBuilderImpl.java
 (original)
+++ 
tuscany/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/builder/impl/EndpointBuilderImpl.java
 Mon Dec  8 10:22:09 2008
@@ -19,14 +19,22 @@
 
 package org.apache.tuscany.sca.assembly.builder.impl;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.apache.tuscany.sca.assembly.Binding;
+import org.apache.tuscany.sca.assembly.Component;
 import org.apache.tuscany.sca.assembly.ComponentService;
 import org.apache.tuscany.sca.assembly.CompositeService;
 import org.apache.tuscany.sca.assembly.Endpoint;
+import org.apache.tuscany.sca.assembly.OptimizableBinding;
+import org.apache.tuscany.sca.assembly.SCABinding;
 import org.apache.tuscany.sca.assembly.builder.EndpointBuilder;
 import org.apache.tuscany.sca.monitor.Monitor;
 import org.apache.tuscany.sca.monitor.Problem;
 import org.apache.tuscany.sca.monitor.Problem.Severity;
+import org.apache.tuscany.sca.policy.PolicySet;
+import org.apache.tuscany.sca.policy.PolicySetAttachPoint;
 
 /**
  * A factory for the Endpoint model.
@@ -92,10 +100,10 @@
         
 
         // Match the binding against the bindings of the target service
-        Binding resolvedBinding = 
BindingConfigurationUtil.matchBinding(endpoint.getTargetComponent(),
-                                                                        
endpoint.getTargetComponentService(),
-                                                                        
endpoint.getCandidateBindings(),
-                                                                        
endpoint.getTargetComponentService().getBindings());
+        Binding resolvedBinding = matchBinding(endpoint.getTargetComponent(),
+                                               
endpoint.getTargetComponentService(),
+                                               endpoint.getCandidateBindings(),
+                                               
endpoint.getTargetComponentService().getBindings());
         if (resolvedBinding == null) {
             warning(monitor, "NoMatchingBinding", 
                     endpoint.getSourceComponentReference(),
@@ -106,10 +114,10 @@
         }
         
         if (bidirectional) {
-            Binding resolvedCallbackBinding = 
BindingConfigurationUtil.matchBinding(endpoint.getTargetComponent(),
-                                                                               
     endpoint.getTargetComponentService(),
-                                                                               
     endpoint.getSourceComponentReference().getCallback().getBindings(),
-                                                                               
     endpoint.getTargetComponentService().getCallback().getBindings());
+            Binding resolvedCallbackBinding = 
matchBinding(endpoint.getTargetComponent(),
+                                                           
endpoint.getTargetComponentService(),
+                                                           
endpoint.getSourceComponentReference().getCallback().getBindings(),
+                                                           
endpoint.getTargetComponentService().getCallback().getBindings());
             if (resolvedBinding == null) {
                 warning(monitor, "NoMatchingCallbackBinding", 
                         endpoint.getSourceComponentReference(),
@@ -121,4 +129,81 @@
         }       
     }  
     
+    private boolean hasCompatiblePolicySets(Binding refBinding, Binding 
svcBinding) {
+        boolean isCompatible = true;
+        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() ) {
+                isCompatible = false;
+                for ( PolicySet refPolicySet : 
((PolicySetAttachPoint)refBinding).getPolicySets() ) {
+                    if ( svcPolicySet.equals(refPolicySet) ) {
+                        isCompatible = true;
+                        break;
+                    }
+                }
+                //if there exists no matching policy set in the reference 
binding
+                if ( !isCompatible ) {
+                    return isCompatible;
+                }
+            }
+        }
+        return isCompatible;
+    }
+    
+    
+    private Binding matchBinding(Component targetComponent, ComponentService 
targetComponentService, 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() && 
+                    hasCompatiblePolicySets(binding, serviceBinding)) {
+
+                    try {
+                        Binding cloned = (Binding)binding.clone();
+                        
+                        //Customise the binding name to make it unique 
+                        // regardless of how many bindings or targets there are
+                        if ( targetComponent != null){
+                            cloned.setName(binding.getName() + "#" + 
targetComponent.getName() + "/" + serviceBinding.getName());
+                        } else {
+                            cloned.setName(binding.getName() + "#" + 
serviceBinding.getName());
+                        }
+                        
+                        // Set the binding URI to the URI of the target service
+                        // that has been matched
+                        if (binding.getURI() == null) {
+                            cloned.setURI(serviceBinding.getURI());
+                        }
+                        
+                        if (binding instanceof OptimizableBinding) {
+                            OptimizableBinding endpoint = 
((OptimizableBinding)cloned);
+                            endpoint.setTargetComponent(targetComponent);
+                            
endpoint.setTargetComponentService(targetComponentService);
+                            endpoint.setTargetBinding(serviceBinding);
+                        } 
+                           
+                        matched.add(cloned);
+                        break;
+                    } catch (Exception ex) {
+                        // do nothing 
+                    }                   
+                }
+            }
+        }
+        if (matched.isEmpty()) {
+            // No matching binding
+            return null;
+        } else {
+            for (Binding binding : matched) {
+                // If binding.sca is present, use it
+                if (SCABinding.class.isInstance(binding)) {
+                    return binding;
+                }
+            }
+            // Use the first one
+            return matched.get(0);
+        }
+    }    
+    
 }


Reply via email to