You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by rf...@apache.org on 2009/09/28 21:15:07 UTC

svn commit: r819680 [5/5] - in /tuscany/java/sca/modules/builder/src: main/java/org/apache/tuscany/sca/builder/ main/java/org/apache/tuscany/sca/builder/impl/ main/java/org/apache/tuscany/sca/policy/builder/impl/ test/java/org/apache/tuscany/sca/builde...

Added: tuscany/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/StructuralURIBuilderImpl.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/StructuralURIBuilderImpl.java?rev=819680&view=auto
==============================================================================
--- tuscany/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/StructuralURIBuilderImpl.java (added)
+++ tuscany/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/StructuralURIBuilderImpl.java Mon Sep 28 19:15:06 2009
@@ -0,0 +1,562 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.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.namespace.QName;
+
+import org.apache.tuscany.sca.assembly.Binding;
+import org.apache.tuscany.sca.assembly.Component;
+import org.apache.tuscany.sca.assembly.ComponentReference;
+import org.apache.tuscany.sca.assembly.ComponentService;
+import org.apache.tuscany.sca.assembly.Composite;
+import org.apache.tuscany.sca.assembly.Contract;
+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.assembly.builder.DeployedCompositeBuilder;
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.definitions.Definitions;
+import org.apache.tuscany.sca.monitor.Monitor;
+
+/**
+ * Configuration of binding URIs.
+ *
+ * @version $Rev$ $Date$
+ */
+public class StructuralURIBuilderImpl implements CompositeBuilder, DeployedCompositeBuilder {
+
+    public StructuralURIBuilderImpl(ExtensionPointRegistry registry) {
+    }
+
+    /**
+     * 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 bindingBaseURIs list of default binding configurations
+     */
+    private void configureBindingURIs(Composite composite,
+                                      Definitions definitions,
+                                      Map<QName, List<String>> bindingBaseURIs,
+                                      Monitor monitor) throws CompositeBuilderException {
+        configureBindingURIs(composite, null, definitions, bindingBaseURIs, 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 parentComponentURI,
+                                      Definitions definitions,
+                                      Map<QName, List<String>> defaultBindings,
+                                      Monitor monitor) throws CompositeBuilderException {
+
+        monitor.pushContext("Composite: " + composite.getName().toString());
+        try {
+            // Process nested composites recursively
+            for (Component component : composite.getComponents()) {
+
+                // Initialize component URI
+                String componentURI;
+                if (parentComponentURI == null) {
+                    componentURI = component.getName();
+                } else {
+                    componentURI = parentComponentURI + '/' + component.getName();
+                }
+                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) {
+
+                constructBindingNames(service, monitor);
+
+                // Initialize binding names and URIs
+                for (Binding binding : service.getBindings()) {
+                    constructBindingURI(parentComponentURI, composite, service, binding, defaultBindings, monitor);
+                }
+            }
+
+            // Initialize component service binding URIs
+            for (Component component : composite.getComponents()) {
+
+                monitor.pushContext("Component: " + component.getName());
+
+                try {
+
+                    for (ComponentService service : component.getServices()) {
+
+                        constructBindingNames(service, monitor);
+
+                        // Initialize binding names and URIs
+                        for (Binding binding : service.getBindings()) {
+                            constructBindingURI(component.getURI(), service, binding, defaultBindings, monitor);
+                        }
+                    }
+                } finally {
+                    monitor.popContext();
+                }
+            }
+        } finally {
+            monitor.popContext();
+        }
+    }
+
+    /**
+     * 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()) {
+            constructBindingNames(service, monitor);
+        }
+
+        // Initialize composite reference binding names
+        for (Reference reference : composite.getReferences()) {
+            constructBindingNames(reference, monitor);
+        }
+
+        // Initialize component service and reference binding names
+        for (Component component : composite.getComponents()) {
+
+            // Initialize component service callback binding names
+            for (ComponentService service : component.getServices()) {
+                constructBindingNames(service, monitor);
+            }
+
+            // Initialize component reference binding names
+            for (ComponentReference reference : component.getReferences()) {
+                // Initialize binding names
+                constructBindingNames(reference, 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
+     */
+    private void constructBindingNames(Contract contract, Monitor monitor) {
+        List<Binding> bindings = contract.getBindings();
+        Map<String, Binding> bindingMap = new HashMap<String, Binding>();
+        for (Binding binding : bindings) {
+            // 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());
+            }
+            Binding existed = bindingMap.put(binding.getName(), binding);
+            // Check that multiple bindings do not have the same name
+            if (existed != null && existed != binding) {
+                if (contract instanceof Service) {
+                    Monitor.error(monitor, this, "assembly-validation-messages", "MultipleBindingsForService", contract
+                        .getName(), binding.getName());
+                } else {
+                    Monitor.error(monitor,
+                                  this,
+                                  "assembly-validation-messages",
+                                  "MultipleBindingsForReference",
+                                  contract.getName(),
+                                  binding.getName());
+                }
+            }
+        }
+
+        if (contract.getCallback() != null) {
+            bindings = contract.getCallback().getBindings();
+            bindingMap.clear();
+            for (Binding binding : bindings) {
+                // 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());
+                }
+                Binding existed = bindingMap.put(binding.getName(), binding);
+                // Check that multiple bindings do not have the same name
+                if (existed != null && existed != binding) {
+                    if (contract instanceof Service) {
+                        Monitor.error(monitor,
+                                      this,
+                                      "assembly-validation-messages",
+                                      "MultipleBindingsForServiceCallback",
+                                      contract.getName(),
+                                      binding.getName());
+                    } else {
+                        Monitor.error(monitor,
+                                      this,
+                                      "assembly-validation-messages",
+                                      "MultipleBindingsForReferenceCallback",
+                                      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,
+                                     Map<QName, List<String>> 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.
+        constructBindingURI(parentComponentURI, service, binding, 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,
+                                     Map<QName, List<String>> defaultBindings,
+                                     Monitor monitor) throws CompositeBuilderException {
+
+        try {
+
+            boolean includeBindingName = !service.getName().equals(binding.getName());
+
+            // 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()) {
+                    return;
+                }
+            } else {
+                bindingURI = null;
+            }
+
+            String serviceName = service.getName();
+            // Get the service binding name
+            String bindingName;
+            if (binding.getName() != null) {
+                bindingName = binding.getName();
+            } else {
+                bindingName = serviceName;
+            }
+
+            // 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,
+                                                   serviceName,
+                                                   includeBindingName,
+                                                   bindingName));
+                return;
+            }
+
+            // calculate the base URI
+            URI baseURI = null;
+            if (defaultBindings != null) {
+                List<String> uris = defaultBindings.get(binding.getType());
+                if (uris != null && uris.size() > 0) {
+                    baseURI = new URI(addSlashToPath(uris.get(0)));
+                }
+            }
+
+            binding.setURI(constructBindingURI(baseURI,
+                                               componentURI,
+                                               bindingURI,
+                                               serviceName,
+                                               includeBindingName,
+                                               bindingName));
+        } catch (URISyntaxException ex) {
+            Monitor.error(monitor,
+                          this,
+                          "assembly-validation-messages",
+                          "URLSyntaxException",
+                          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,
+                                              String serviceName,
+                                              boolean includeBindingName,
+                                              String bindingName) {
+        String name = includeBindingName ? serviceName + "/" + bindingName : serviceName;
+        String uriString;
+
+        if (baseURI == null) {
+            if (componentURI == null) {
+                if (bindingURI != null) {
+                    uriString = name + "/" + bindingURI.toString();
+                } else {
+                    uriString = name;
+                }
+            } else {
+                if (bindingURI != null) {
+                    if (bindingURI.toString().startsWith("/")) {
+                        uriString = componentURI.resolve(bindingURI).toString();
+                    } else {
+                        uriString = componentURI.resolve(name + "/" + bindingURI).toString();
+                    }
+                } else {
+                    uriString = componentURI.resolve(name).toString();
+                }
+            }
+        } else {
+            if (componentURI == null) {
+                if (bindingURI != null) {
+                    uriString = basedURI(baseURI, bindingURI).toString();
+                } else {
+                    uriString = basedURI(baseURI, URI.create(name)).toString();
+                }
+            } else {
+                if (bindingURI != null) {
+                    uriString = basedURI(baseURI, componentURI.resolve(bindingURI)).toString();
+                } else {
+                    uriString = basedURI(baseURI, componentURI.resolve(name)).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();
+    }
+
+    public Composite build(Composite composite, Definitions definitions, Monitor monitor)
+        throws CompositeBuilderException {
+        configureStructuralURIs(composite, null, definitions, null, monitor);
+        return composite;
+    }
+
+    public String getID() {
+        return "org.apache.tuscany.sca.assembly.builder.StructualURIBuilder";
+    }
+
+    public Composite build(Composite composite,
+                           Definitions definitions,
+                           Map<QName, List<String>> bindingBaseURIs,
+                           Monitor monitor) throws CompositeBuilderException {
+        configureBindingURIs(composite, definitions, bindingBaseURIs, monitor);
+        return composite;
+    }
+
+    private void configureStructuralURIs(Composite composite,
+                                         String parentComponentURI,
+                                         Definitions definitions,
+                                         Map<QName, List<String>> defaultBindings,
+                                         Monitor monitor) throws CompositeBuilderException {
+
+        monitor.pushContext("Composite: " + composite.getName().toString());
+        try {
+            for (Service service : composite.getServices()) {
+                constructBindingNames(service, monitor);
+            }
+
+            for (Reference reference : composite.getReferences()) {
+                constructBindingNames(reference, monitor);
+            }
+
+            // Process nested composites recursively
+            for (Component component : composite.getComponents()) {
+
+                // Initialize component URI
+                String componentURI;
+                if (parentComponentURI == null) {
+                    componentURI = component.getName();
+                } else {
+                    componentURI = parentComponentURI + '/' + component.getName();
+                }
+                component.setURI(componentURI);
+
+                monitor.pushContext("Component: " + component.getName());
+                try {
+                    for (ComponentService service : component.getServices()) {
+                        constructBindingNames(service, monitor);
+
+                        // Initialize binding names and URIs
+                        for (Binding binding : service.getBindings()) {
+                            constructBindingURI(componentURI, service, binding, defaultBindings, monitor);
+                        }
+                    }
+                    for (ComponentReference service : component.getReferences()) {
+                        constructBindingNames(service, monitor);
+                    }
+                } finally {
+                    monitor.popContext();
+                }
+
+                Implementation implementation = component.getImplementation();
+                if (implementation instanceof Composite) {
+                    // Process nested composite
+                    configureStructuralURIs((Composite)implementation,
+                                            componentURI,
+                                            definitions,
+                                            defaultBindings,
+                                            monitor);
+                }
+            }
+
+        } finally {
+            monitor.popContext();
+        }
+    }
+
+}

Propchange: tuscany/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/StructuralURIBuilderImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tuscany/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/StructuralURIBuilderImpl.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: tuscany/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/MockPolicy.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/MockPolicy.java?rev=819680&r1=819676&r2=819680&view=diff
==============================================================================
--- tuscany/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/MockPolicy.java (original)
+++ tuscany/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/MockPolicy.java Mon Sep 28 19:15:06 2009
@@ -17,7 +17,7 @@
  * under the License.    
  */
 
-package org.apache.tuscany.sca.policy.builder.impl;
+package org.apache.tuscany.sca.builder.impl;
 
 import javax.xml.namespace.QName;
 

Modified: tuscany/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentTestCase.java?rev=819680&r1=819676&r2=819680&view=diff
==============================================================================
--- tuscany/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentTestCase.java (original)
+++ tuscany/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentTestCase.java Mon Sep 28 19:15:06 2009
@@ -17,7 +17,7 @@
  * under the License.    
  */
 
-package org.apache.tuscany.sca.policy.builder.impl;
+package org.apache.tuscany.sca.builder.impl;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -35,6 +35,7 @@
 import org.apache.tuscany.sca.assembly.builder.CompositeBuilder;
 import org.apache.tuscany.sca.builder.impl.CompositeCloneBuilderImpl;
 import org.apache.tuscany.sca.builder.impl.CompositeIncludeBuilderImpl;
+import org.apache.tuscany.sca.builder.impl.PolicyAttachmentBuilderImpl;
 import org.apache.tuscany.sca.builder.impl.StructuralURIBuilderImpl;
 import org.apache.tuscany.sca.contribution.processor.ContributionReadException;
 import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor;

Modified: tuscany/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/TestPolicyProcessor.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/TestPolicyProcessor.java?rev=819680&r1=819676&r2=819680&view=diff
==============================================================================
--- tuscany/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/TestPolicyProcessor.java (original)
+++ tuscany/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/TestPolicyProcessor.java Mon Sep 28 19:15:06 2009
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.    
  */
-package org.apache.tuscany.sca.policy.builder.impl;
+package org.apache.tuscany.sca.builder.impl;
 
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamException;