diff options
Diffstat (limited to 'branches/sca-java-2.0-M4/modules/builder/src')
32 files changed, 7037 insertions, 0 deletions
diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/BaseBuilderImpl.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/BaseBuilderImpl.java new file mode 100644 index 0000000000..111680b2b4 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/BaseBuilderImpl.java @@ -0,0 +1,703 @@ +/* + * 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.util.Map; + +import javax.xml.namespace.QName; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.TransformerFactory; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +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.BuilderExtensionPoint; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.Messages; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +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; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; +import org.apache.tuscany.sca.policy.ExtensionType; +import org.apache.tuscany.sca.policy.PolicySubject; +import org.w3c.dom.Document; + +/** + * Base class for Builder implementations + * + * @version $Rev$ $Date$ + */ +public abstract class BaseBuilderImpl implements CompositeBuilder { + protected static final String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200903"; + protected static final String BINDING_SCA = "binding.sca"; + protected static final QName BINDING_SCA_QNAME = new QName(SCA11_NS, BINDING_SCA); + + protected AssemblyFactory assemblyFactory; + protected SCABindingFactory scaBindingFactory; + protected InterfaceContractMapper interfaceContractMapper; + protected DocumentBuilderFactory documentBuilderFactory; + protected TransformerFactory transformerFactory; + protected BuilderExtensionPoint builders; + + protected BaseBuilderImpl(BuilderExtensionPoint builders, + AssemblyFactory assemblyFactory, + SCABindingFactory scaBindingFactory, + DocumentBuilderFactory documentBuilderFactory, + TransformerFactory transformerFactory, + InterfaceContractMapper interfaceContractMapper) { + this.builders = builders; + this.assemblyFactory = assemblyFactory; + this.scaBindingFactory = scaBindingFactory; + this.documentBuilderFactory = documentBuilderFactory; + this.transformerFactory = transformerFactory; + this.interfaceContractMapper = interfaceContractMapper; + } + + /** + * Construct a builder from the extension point registry + * @param registry + */ + protected BaseBuilderImpl(ExtensionPointRegistry registry) { + this.builders = registry.getExtensionPoint(BuilderExtensionPoint.class); + FactoryExtensionPoint factoryExtensionPoint = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.assemblyFactory = factoryExtensionPoint.getFactory(AssemblyFactory.class); + this.scaBindingFactory = factoryExtensionPoint.getFactory(SCABindingFactory.class); + this.documentBuilderFactory = factoryExtensionPoint.getFactory(DocumentBuilderFactory.class); + this.transformerFactory = factoryExtensionPoint.getFactory(TransformerFactory.class); + this.interfaceContractMapper = + registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(InterfaceContractMapper.class); + } + + /** + * Report a warning. + * + * @param monitor + * @param problems + * @param message + * @param model + */ + protected void warning(Monitor monitor, String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = + monitor.createProblem(this.getClass().getName(), + Messages.ASSEMBLY_VALIDATION, + Severity.WARNING, + model, + message, + messageParameters); + monitor.problem(problem); + } + } + + /** + * Report a error. + * + * @param monitor + * @param problems + * @param message + * @param model + */ + protected void error(Monitor monitor, String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = + monitor.createProblem(this.getClass().getName(), + Messages.ASSEMBLY_VALIDATION, + Severity.ERROR, + model, + message, + messageParameters); + monitor.problem(problem); + } + } + + /** + * Report a exception. + * + * @param problems + * @param message + * @param model + */ + protected void error(Monitor monitor, String message, Object model, Exception ex) { + if (monitor != null) { + Problem problem = null; + problem = + monitor.createProblem(this.getClass().getName(), + Messages.ASSEMBLY_VALIDATION, + Severity.ERROR, + model, + message, + ex); + monitor.problem(problem); + } + } + + /** + * Index components inside a composite + * + * @param composite + * @param componentServices + + */ + protected void indexComponents(Composite composite, Map<String, Component> components) { + for (Component component : composite.getComponents()) { + // Index components by name + components.put(component.getName(), component); + } + } + + /** + * Index services inside a composite + * + * @param composite + * @param componentServices + */ + protected void indexServices(Composite composite, Map<String, ComponentService> componentServices) { + + for (Component component : composite.getComponents()) { + + ComponentService nonCallbackService = null; + int nonCallbackServiceCount = 0; + + for (ComponentService componentService : component.getServices()) { + // Index component services by component name / service name + String uri = component.getName() + '/' + componentService.getName(); + componentServices.put(uri, componentService); + + // count how many non-callback there are + if (!componentService.isForCallback()) { + + if (nonCallbackServiceCount == 0) { + nonCallbackService = componentService; + } + nonCallbackServiceCount++; + } + } + if (nonCallbackServiceCount == 1) { + // If we have a single non callback service, index it by + // component name as well + componentServices.put(component.getName(), nonCallbackService); + } + } + } + + /** + * Index components, services and references inside a composite. + * @param composite + * @param components + * @param componentServices + * @param componentReferences + */ + protected void indexComponentsServicesAndReferences(Composite composite, + Map<String, Component> components, + Map<String, ComponentService> componentServices, + Map<String, ComponentReference> componentReferences) { + + for (Component component : composite.getComponents()) { + + // Index components by name + components.put(component.getName(), component); + + ComponentService nonCallbackService = null; + int nonCallbackServices = 0; + for (ComponentService componentService : component.getServices()) { + + // Index component services by component name / service name + String uri = component.getName() + '/' + componentService.getName(); + componentServices.put(uri, componentService); + + // TODO - EPR - $promoted$ no longer used but it doesn't do any harm here + boolean promotedService = false; + if (componentService.getName() != null && componentService.getName().indexOf("$promoted$") > -1) { + promotedService = true; + } + + // count how many non-callback, non-promoted services there are + // if there is only one the component name also acts as the service name + if ((!componentService.isForCallback()) && (!promotedService)) { + + // Check how many non callback non-promoted services we have + if (nonCallbackServices == 0) { + nonCallbackService = componentService; + } + nonCallbackServices++; + } + + } + + if (nonCallbackServices == 1) { + // If we have a single non callback service, index it by + // component name as well + componentServices.put(component.getName(), nonCallbackService); + } + + // Index references by component name / reference name + for (ComponentReference componentReference : component.getReferences()) { + String uri = component.getName() + '/' + componentReference.getName(); + componentReferences.put(uri, componentReference); + } + } + } + + protected void indexComponentPropertiesServicesAndReferences(Component component, + Map<String, ComponentService> componentServices, + Map<String, ComponentReference> componentReferences, + Map<String, ComponentProperty> componentProperties, + Monitor monitor) { + for (ComponentService componentService : component.getServices()) { + if (componentServices.containsKey(componentService.getName())) { + // [MJE 13/05/2009] Changed to "error" since allowing these violates the OASIS spec + error(monitor, "DuplicateComponentServiceName", component, component.getName(), componentService + .getName()); + } else { + componentServices.put(componentService.getName(), componentService); + } + } + for (ComponentReference componentReference : component.getReferences()) { + if (componentReferences.containsKey(componentReference.getName())) { + // [MJE 13/05/2009] Changed to "error" since allowing these violates the OASIS spec + error(monitor, "DuplicateComponentReferenceName", component, component.getName(), componentReference + .getName()); + } else { + componentReferences.put(componentReference.getName(), componentReference); + } + } + for (ComponentProperty componentProperty : component.getProperties()) { + if (componentProperties.containsKey(componentProperty.getName())) { + // [MJE 13/05/2009] Changed to "error" since allowing these violates the OASIS spec + error(monitor, "DuplicateComponentPropertyName", component, component.getName(), componentProperty + .getName()); + } else { + componentProperties.put(componentProperty.getName(), componentProperty); + } + } + + } + + protected void indexImplementationPropertiesServicesAndReferences(Component component, + Map<String, Service> services, + Map<String, Reference> references, + Map<String, Property> properties, + Monitor monitor) { + // First check that the component has a resolved implementation + Implementation implementation = component.getImplementation(); + if (implementation == null) { + // A component must have an implementation + error(monitor, "NoComponentImplementation", component, component.getName()); + + } else if (implementation.isUnresolved()) { + + // The implementation must be fully resolved + error(monitor, "UnresolvedComponentImplementation", component, component.getName(), implementation.getURI()); + + } else { + + // Index properties, services and references, also check for + // duplicates + for (Property property : implementation.getProperties()) { + if (properties.containsKey(property.getName())) { + // [MJE 13/05/2009] Changed to "error" since allowing these violates the OASIS spec + error(monitor, "DuplicateImplementationPropertyName", component, component.getName(), property + .getName()); + } else { + properties.put(property.getName(), property); + } + } + for (Service service : implementation.getServices()) { + if (services.containsKey(service.getName())) { + // [MJE 13/05/2009] Changed to "error" since allowing these violates the OASIS spec + error(monitor, "DuplicateImplementationServiceName", component, component.getName(), service + .getName()); + } else { + services.put(service.getName(), service); + } + } + for (Reference reference : implementation.getReferences()) { + if (references.containsKey(reference.getName())) { + // [MJE 13/05/2009] Changed to "error" since allowing these violates the OASIS spec + error(monitor, "DuplicateImplementationReferenceName", component, component.getName(), reference + .getName()); + } else { + references.put(reference.getName(), reference); + } + } + } + + } + + /** + * Reconcile component properties and the properties defined by the + * component type. + * + * @param component + * @param properties + * @param componentProperties + * @param problems + */ + protected void reconcileProperties(Component component, + Map<String, Property> properties, + Map<String, ComponentProperty> componentProperties, + Monitor monitor) { + + // Connect component properties to their properties + for (ComponentProperty componentProperty : component.getProperties()) { + Property property = properties.get(componentProperty.getName()); + if (property != null) { + componentProperty.setProperty(property); + } else { + error(monitor, "PropertyNotFound", component, component.getName(), componentProperty.getName()); + } + } + + // Create component properties for all properties + if (component.getImplementation() != null) { + for (Property property : component.getImplementation().getProperties()) { + if (!componentProperties.containsKey(property.getName())) { + ComponentProperty componentProperty = assemblyFactory.createComponentProperty(); + componentProperty.setName(property.getName()); + componentProperty.setValue(property.getValue()); + componentProperty.setMany(property.isMany()); + componentProperty.setMustSupply(property.isMustSupply()); + componentProperty.setXSDElement(property.getXSDElement()); + componentProperty.setXSDType(property.getXSDType()); + componentProperty.setProperty(property); + component.getProperties().add(componentProperty); + } + } + } + + // Reconcile component properties and their properties + for (ComponentProperty componentProperty : component.getProperties()) { + Property property = componentProperty.getProperty(); + if (property != null) { + + // Check that a component property does not override the + // mustSupply attribute + if (!property.isMustSupply() && componentProperty.isMustSupply()) { + warning(monitor, + "PropertyMustSupplyIncompatible", + component, + component.getName(), + componentProperty.getName()); + } + + // Default to the mustSupply attribute specified on the property + if (!componentProperty.isMustSupply()) + componentProperty.setMustSupply(property.isMustSupply()); + + // Default to the value specified on the component type property + if (!isPropertyValueSet(componentProperty)) { + componentProperty.setValue(property.getValue()); + } + + // Override the property value for the composite + if (component.getImplementation() instanceof Composite) { + property.setValue(componentProperty.getValue()); + } + + // Check that a value is supplied + if (!isPropertyValueSet(componentProperty) && property.isMustSupply()) { + error(monitor, "PropertyMustSupplyNull", component, component.getName(), componentProperty.getName()); + } + + // Check that a component property does not override the + // many attribute + if (!property.isMany() && componentProperty.isMany()) { + warning(monitor, "PropertyOverrideManyAttribute", component, component.getName(), componentProperty.getName()); + } + + // Default to the many attribute defined on the property + componentProperty.setMany(property.isMany()); + + // Default to the type and element defined on the property + if (componentProperty.getXSDType() == null) { + componentProperty.setXSDType(property.getXSDType()); + } + if (componentProperty.getXSDElement() == null) { + componentProperty.setXSDElement(property.getXSDElement()); + } + + // Check that a type or element are specified + if (componentProperty.getXSDElement() == null && componentProperty.getXSDType() == null) { + warning(monitor, "NoTypeForComponentProperty", component, component.getName(), componentProperty.getName()); + } + } + } + } + + /** + * Look to see if any value elements have been set into the property + * A bit involved as the value is stored as a DOM Document + * + * @param property the property to be tested + * @return true is values are present + */ + private boolean isPropertyValueSet(Property property) { + Document value = (Document)property.getValue(); + + if (value == null) { + return false; + } + + if (value.getFirstChild() == null) { + return false; + } + + if (value.getFirstChild().getChildNodes().getLength() == 0) { + return false; + } + + return true; + } + + /** + * Reconcile component references with the references defined on the + * component type. + * + * @param component + * @param references + * @param componentReferences + * @param monitor + */ + protected void reconcileReferences(Component component, + Map<String, Reference> references, + Map<String, ComponentReference> componentReferences, + Monitor monitor) { + + // Connect each component reference to the corresponding reference + for (ComponentReference componentReference : component.getReferences()) { + if (componentReference.getReference() != null || componentReference.isForCallback()) { + continue; + } + Reference reference = references.get(componentReference.getName()); + if (reference != null) { + componentReference.setReference(reference); + } else { + if (!componentReference.getName().startsWith("$self$.")) { + error(monitor, "ReferenceNotFound", component, component.getName(), componentReference.getName()); + } + } + } + + // Create a Component reference for each reference for which there is no declared Component reference + if (component.getImplementation() != null) { + for (Reference reference : component.getImplementation().getReferences()) { + if (!componentReferences.containsKey(reference.getName())) { + ComponentReference componentReference = assemblyFactory.createComponentReference(); + componentReference.setForCallback(reference.isForCallback()); + componentReference.setName(reference.getName()); + componentReference.setReference(reference); + component.getReferences().add(componentReference); + } + } + } + + // Reconcile each component reference with its reference + for (ComponentReference componentReference : component.getReferences()) { + Reference reference = componentReference.getReference(); + if (reference != null) { + // Reconcile multiplicity + if (componentReference.getMultiplicity() != null) { + if (!ReferenceConfigurationUtil.isValidMultiplicityOverride(reference.getMultiplicity(), + componentReference.getMultiplicity())) { + error(monitor, + "ReferenceIncompatibleMultiplicity", + component, + component.getName(), + componentReference.getName()); + } + } else { + componentReference.setMultiplicity(reference.getMultiplicity()); + } + + // Reconcile interface + InterfaceContract interfaceContract = reference.getInterfaceContract(); + if (componentReference.getInterfaceContract() != null) { + if (interfaceContract != null && !componentReference.getInterfaceContract().equals(reference + .getInterfaceContract())) { + if (!interfaceContractMapper.isCompatible(interfaceContract, componentReference + .getInterfaceContract())) { + error(monitor, + "ReferenceIncompatibleComponentInterface", + component, + component.getName(), + componentReference.getName()); + } + } + } else { + componentReference.setInterfaceContract(interfaceContract); + } + + // Reconcile bindings + if (componentReference.getBindings().isEmpty()) { + componentReference.getBindings().addAll(reference.getBindings()); + } + + // Reconcile callback bindings + if (componentReference.getCallback() == null) { + componentReference.setCallback(reference.getCallback()); + if (componentReference.getCallback() == null) { + // Create an empty callback to avoid null check + componentReference.setCallback(assemblyFactory.createCallback()); + } + + } else if (componentReference.getCallback().getBindings().isEmpty() && reference.getCallback() != null) { + componentReference.getCallback().getBindings().addAll(reference.getCallback().getBindings()); + } + + // Propagate autowire setting from the component + if (componentReference.getAutowire() == null) { + componentReference.setAutowire(component.getAutowire()); + } + + // Reconcile targets + if (componentReference.getTargets().isEmpty()) { + componentReference.getTargets().addAll(reference.getTargets()); + } + } + } + } + + /** + * Reconcile component services and services defined on the component type. + * + * @param component + * @param services + * @param componentServices + * @param monitor + */ + protected void reconcileServices(Component component, + Map<String, Service> services, + Map<String, ComponentService> componentServices, + Monitor monitor) { + + // Connect each component service to the corresponding service + for (ComponentService componentService : component.getServices()) { + if (componentService.getService() != null || componentService.isForCallback()) { + continue; + } + Service service = services.get(componentService.getName()); + if (service != null) { + componentService.setService(service); + } else { + warning(monitor, "ServiceNotFoundForComponentService", component, component.getName(), componentService + .getName()); + } + } + + // Create a component service for each service + if (component.getImplementation() != null) { + for (Service service : component.getImplementation().getServices()) { + if (!componentServices.containsKey(service.getName())) { + ComponentService componentService = assemblyFactory.createComponentService(); + componentService.setForCallback(service.isForCallback()); + String name = service.getName(); + componentService.setName(name); + componentService.setService(service); + component.getServices().add(componentService); + componentServices.put(name, componentService); + } + } + } + + //Reconcile each component service with its service + for (ComponentService componentService : component.getServices()) { + Service service = componentService.getService(); + if (service != null) { + // Reconcile interface + InterfaceContract interfaceContract = service.getInterfaceContract(); + if (componentService.getInterfaceContract() != null) { + if (interfaceContract != null && !componentService.getInterfaceContract().equals(interfaceContract)) { + if (!interfaceContractMapper.isCompatible(componentService.getInterfaceContract(), + interfaceContract)) { + // MJE, 16/05/2009 - Upgraded from "warning" to "error" since this is a fatal problem - TUSCANY-3036 + error(monitor, + "ServiceIncompatibleComponentInterface", + component, + component.getName(), + componentService.getName()); + } + } + } else { + componentService.setInterfaceContract(interfaceContract); + } + + // Reconcile bindings + if (componentService.getBindings().isEmpty()) { + componentService.getBindings().addAll(service.getBindings()); + } + + // Reconcile callback bindings + if (componentService.getCallback() == null) { + componentService.setCallback(service.getCallback()); + if (componentService.getCallback() == null) { + // Create an empty callback to avoid null check + componentService.setCallback(assemblyFactory.createCallback()); + } + } else if (componentService.getCallback().getBindings().isEmpty() && service.getCallback() != null) { + componentService.getCallback().getBindings().addAll(service.getCallback().getBindings()); + } + } + } + } + + protected void attachSCABinding(Contract contract, Definitions definitions) { + if (!contract.getBindings().isEmpty()) { + contract.setOverridingBindings(true); + // No need to set binding.sca + return; + } + contract.setOverridingBindings(false); + + // Only add binding.sca for services + // FIXME: The latest OASIS spec only adds binding.sca to services + /* + if (!(contract instanceof Service)) { + return; + } + */ + SCABinding scaBinding = scaBindingFactory.createSCABinding(); + scaBinding.setName(contract.getName()); + + if (definitions != null) { + for (ExtensionType attachPointType : definitions.getBindingTypes()) { + if (attachPointType.getType().equals(BINDING_SCA_QNAME)) { + ((PolicySubject)scaBinding).setExtensionType(attachPointType); + } + } + } + + contract.getBindings().add(scaBinding); + } +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/BindingURIBuilderImpl.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/BindingURIBuilderImpl.java new file mode 100644 index 0000000000..205a3157de --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/BindingURIBuilderImpl.java @@ -0,0 +1,379 @@ +/* + * 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.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.ComponentService; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.assembly.builder.Messages; +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 BindingURIBuilderImpl implements CompositeBuilder { + + public BindingURIBuilderImpl(ExtensionPointRegistry registry) { + } + + /** + * Called by CompositeBindingURIBuilderImpl + * + * @param composite the composite to be configured + */ + public Composite build(Composite composite, BuilderContext context) + throws CompositeBuilderException { + configureBindingURIs(composite, null, context.getDefinitions(), context.getBindingBaseURIs(), context.getMonitor()); + return composite; + } + + + /** + * 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, + Map<QName, List<String>> defaultBindings, + Monitor monitor) throws CompositeBuilderException { + + String parentComponentURI = uri; + + 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 = 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) { + + // 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()) { + + // Initialize binding names and URIs + for (Binding binding : service.getBindings()) { + constructBindingURI(component, service, binding, defaultBindings, monitor); + } + } + } finally { + monitor.popContext(); + } + } + } finally { + monitor.popContext(); + } + } + + /** + * 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); + } + + /** + * 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, + Map<QName, List<String>> defaultBindings, + Monitor monitor) throws CompositeBuilderException { + constructBindingURI(component.getURI(), 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()) { + binding.setURI(bindingURI.toString()); + 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, + Messages.ASSEMBLY_VALIDATION, + "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 String getID() { + return "org.apache.tuscany.sca.assembly.builder.BindingURIBuilder"; + } + +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentBuilderImpl.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentBuilderImpl.java new file mode 100644 index 0000000000..431d21179c --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentBuilderImpl.java @@ -0,0 +1,1244 @@ +/* + * 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.io.InputStream; +import java.net.URI; +import java.net.URL; +import java.net.URLConnection; + +import javax.xml.namespace.QName; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Source; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.sax.SAXSource; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; + +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.CompositeReference; +import org.apache.tuscany.sca.assembly.CompositeService; +import org.apache.tuscany.sca.assembly.Contract; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.assembly.Multiplicity; +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.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint; +import org.apache.tuscany.sca.assembly.builder.ImplementationBuilder; +import org.apache.tuscany.sca.assembly.builder.Messages; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.definitions.Definitions; +import org.apache.tuscany.sca.interfacedef.IncompatibleInterfaceContractException; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.policy.ExtensionType; +import org.apache.tuscany.sca.policy.PolicySubject; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; + +/** + * @version $Rev$ $Date$ + */ +public class ComponentBuilderImpl { + protected static final String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200903"; + protected static final String BINDING_SCA = "binding.sca"; + protected static final QName BINDING_SCA_QNAME = new QName(SCA11_NS, BINDING_SCA); + + private CompositeComponentTypeBuilderImpl componentTypeBuilder; + private AssemblyFactory assemblyFactory; + private SCABindingFactory scaBindingFactory; + private DocumentBuilderFactory documentBuilderFactory; + protected TransformerFactory transformerFactory; + private InterfaceContractMapper interfaceContractMapper; + private BuilderExtensionPoint builders; + + public ComponentBuilderImpl(ExtensionPointRegistry registry) { + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + + FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); + assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); + scaBindingFactory = modelFactories.getFactory(SCABindingFactory.class); + documentBuilderFactory = modelFactories.getFactory(DocumentBuilderFactory.class); + transformerFactory = modelFactories.getFactory(TransformerFactory.class); + + interfaceContractMapper = utilities.getUtility(InterfaceContractMapper.class); + + builders = registry.getExtensionPoint(BuilderExtensionPoint.class); + } + + public void setComponentTypeBuilder(CompositeComponentTypeBuilderImpl componentTypeBuilder) { + this.componentTypeBuilder = componentTypeBuilder; + } + + /** + * Configure the component based on its component type using OASIS rules + * + * @Param parentComposite the composite that contains the component being configured. Required for property processing + * @param component the component to be configured + */ + public void configureComponentFromComponentType(Component outerComponent, Composite parentComposite, Component component, BuilderContext context) { + + Monitor monitor = context.getMonitor(); + monitor.pushContext("Component: " + component.getName().toString()); + + try { + // do any work we need to do before we calculate the component type + // for this component. Anything that needs to be pushed down the promotion + // hierarchy must be done before we calculate the component type + + // check that the implementation is present + if (!isComponentImplementationPresent(component, monitor)){ + return; + } + + // carry out any implementation specific builder processing + Implementation impl = component.getImplementation(); + if (impl != null) { + ImplementationBuilder builder = builders.getImplementationBuilder(impl.getType()); + if (builder != null) { + builder.build(component, impl, context); + } + } + + // Properties on the composite component type are not affected by the components + // that the composite contains. Instead the child components might source + // composite level property values. Hence we have to calculate whether the component + // type property value should be overridden by this component's property value + // before we go ahead and calculate the component type + configureProperties(outerComponent, parentComposite, component, monitor); + + // create the component type for this component + // taking any nested composites into account + createComponentType(component, context); + + // configure services based on the calculated component type + configureServices(component, monitor); + + // configure services based on the calculated component type + configureReferences(component, monitor); + } finally { + monitor.popContext(); + } + } + + /** + * Checks that a component implementation is present and resolved + * before doing anything else + * + * @param component + * @return true if the implementation is present and resolved + */ + private boolean isComponentImplementationPresent(Component component, Monitor monitor){ + Implementation implementation = component.getImplementation(); + if (implementation == null) { + // A component must have an implementation + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "NoComponentImplementation", + component.getName()); + return false; + } else if (implementation.isUnresolved()) { + // The implementation must be fully resolved + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "UnresolvedComponentImplementation", + component, + component.getName(), + implementation.getURI()); + return false; + } + + return true; + } + + /** + * Use the component type builder to build the component type for + * this component. + * + * @param component + */ + private void createComponentType(Component component, BuilderContext context) { + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + componentTypeBuilder.createComponentType(component, (Composite)implementation, context); + } + } + + /** + * Configure this component's services based on the services in its + * component type and the configuration from the composite file + * + * @param component + */ + private void configureServices(Component component, Monitor monitor) { + + // If the component type has services that are not described in this + // component then create services for this component + addServicesFromComponentType(component, monitor); + + // Connect this component's services to the + // services from its component type + connectServicesToComponentType(component, monitor); + + // look at each component service in turn and calculate its + // configuration based on OASIS rules + for (ComponentService componentService : component.getServices()) { + + Service componentTypeService = componentService.getService(); + + if (componentTypeService == null) { + // raise error? + // can be null in some of the assembly-xml unit tests + continue; + } + + // interface contracts + calculateServiceInterfaceContract(component, componentService, componentTypeService, monitor); + + // bindings + calculateBindings(componentService, componentTypeService); + + // add callback reference model objects + createCallbackReference(component, componentService); + + // intents - done later in CompositePolicyBuilder - discuss with RF + //calculateIntents(componentService, + // componentTypeService); + + // policy sets - done later in CompositePolicyBuilder - discuss with RF + // calculatePolicySets(componentService, + // componentTypeService); + + } + } + + /** + * Configure this component's references based on the references in its + * component type and the configuration from the composite file + * + * @param component + */ + private void configureReferences(Component component, Monitor monitor) { + + // If the component type has references that are not described in this + // component then create references for this component + addReferencesFromComponentType(component, monitor); + + // Connect this component's references to the + // references from its component type + connectReferencesToComponentType(component, monitor); + + // look at each component reference in turn and calculate its + // configuration based on OASIS rules + for (ComponentReference componentReference : component.getReferences()) { + Reference componentTypeReference = componentReference.getReference(); + + if (componentTypeReference == null) { + // raise error? + // can be null in some of the assembly-xml unit tests + continue; + } + + // reference multiplicity + reconcileReferenceMultiplicity(component, componentReference, componentTypeReference, monitor); + + // interface contracts + calculateReferenceInterfaceContract(component, componentReference, componentTypeReference, monitor); + + // bindings + calculateBindings(componentReference, componentTypeReference); + + // add callback service model objects + createCallbackService(component, componentReference); + + // intents - done later in CompositePolicyBuilder - discuss with RF + //calculateIntents(componentService, + // componentTypeService); + + // policy sets - done later in CompositePolicyBuilder - discuss with RF + // calculatePolicySets(componentService, + // componentTypeService); + + // Propagate autowire setting from the component down the structural + // hierarchy + if (componentReference.getAutowire() == null) { + componentReference.setAutowire(component.getAutowire()); + } + + // Reconcile targets copying then up the promotion hierarchy + if (componentReference.getTargets().isEmpty()) { + componentReference.getTargets().addAll(componentTypeReference.getTargets()); + } + + } + } + + /** + * Configure this component's properties based on the properties in its + * component type and the configuration from the composite file + * + * @param component + */ + private void configureProperties(Component outerComponent, Composite parentComposite, Component component, Monitor monitor) { + // If the component type has properties that are not described in this + // component then create properties for this component + addPropertiesFromComponentType(component, monitor); + + // Connect this component's properties to the + // properties from its component type + connectPropertiesToComponentType(component, monitor); + + // Reconcile component properties and their component type properties + for (ComponentProperty componentProperty : component.getProperties()) { + reconcileComponentPropertyWithComponentType(component, componentProperty, monitor); + + // configure the property value based on the @source attribute + // At the moment this is done in the parent composite component + // type calculation + processPropertySourceAttribute(outerComponent, parentComposite, component, componentProperty, monitor); + + // configure the property value based on the @file attribute + processPropertyFileAttribute(component, componentProperty, monitor); + + // Check that a value is supplied + if (componentProperty.isMustSupply() && !isPropertyValueSet(componentProperty)) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "PropertyMustSupplyNull", + component.getName(), + componentProperty.getName()); + } + + // check that not too many values are supplied + if (!componentProperty.isMany() && isPropertyManyValued(componentProperty)){ + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "PropertyHasManyValues", + component.getName(), + componentProperty.getName()); + } + + } + } + + private void addServicesFromComponentType(Component component, Monitor monitor) { + + // Create a component service for each service + if (component.getImplementation() != null) { + for (Service service : component.getImplementation().getServices()) { + // check for duplicate service names in implementation + if (service != component.getImplementation().getService(service.getName())){ + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "DuplicateImplementationServiceName", + component.getName(), + service.getName()); + } + + ComponentService componentService = (ComponentService)component.getService(service.getName()); + + // if the component doesn't have a service with the same name as the + // component type service then create one + if (componentService == null) { + componentService = assemblyFactory.createComponentService(); + componentService.setForCallback(service.isForCallback()); + String name = service.getName(); + componentService.setName(name); + component.getServices().add(componentService); + } + } + } + } + + private void addReferencesFromComponentType(Component component, Monitor monitor) { + + // Create a component reference for each reference + if (component.getImplementation() != null) { + for (Reference reference : component.getImplementation().getReferences()) { + // check for duplicate reference names in implementation + if (reference != component.getImplementation().getReference(reference.getName())){ + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "DuplicateImplementationReferenceName", + component.getName(), + reference.getName()); + } + + ComponentReference componentReference = (ComponentReference)component.getReference(reference.getName()); + + // if the component doesn't have a reference with the same name as the + // component type reference then create one + if (componentReference == null) { + componentReference = assemblyFactory.createComponentReference(); + componentReference.setForCallback(reference.isForCallback()); + componentReference.setName(reference.getName()); + componentReference.setReference(reference); + component.getReferences().add(componentReference); + } + } + } + } + + private void addPropertiesFromComponentType(Component component, Monitor monitor) { + + // Create component property for each property + if (component.getImplementation() != null) { + for (Property property : component.getImplementation().getProperties()) { + // check for duplicate property names in implementation + if (property != component.getImplementation().getProperty(property.getName())){ + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "DuplicateImplementationPropertyName", + component.getName(), + property.getName()); + } + ComponentProperty componentProperty = (ComponentProperty)component.getProperty(property.getName()); + + // if the component doesn't have a property with the same name as + // the component type property then create one + if (componentProperty == null) { + componentProperty = assemblyFactory.createComponentProperty(); + componentProperty.setName(property.getName()); + componentProperty.setValue(property.getValue()); + componentProperty.setMany(property.isMany()); + componentProperty.setMustSupply(property.isMustSupply()); + componentProperty.setXSDElement(property.getXSDElement()); + componentProperty.setXSDType(property.getXSDType()); + componentProperty.setProperty(property); + component.getProperties().add(componentProperty); + } + } + } + } + + private void connectServicesToComponentType(Component component, Monitor monitor) { + + // Connect each component service to the corresponding component type service + for (ComponentService componentService : component.getServices()) { + // check for duplicate service names in component + if (componentService != component.getService(componentService.getName())){ + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "DuplicateComponentServiceName", + component.getName(), + componentService.getName()); + } + + if (componentService.getService() != null || componentService.isForCallback()) { + continue; + } + + if (component.getImplementation() == null) { + // is null in some of our basic unit tests + continue; + } + + Service service = component.getImplementation().getService(componentService.getName()); + + if (service != null) { + componentService.setService(service); + } else { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "ServiceNotFoundForComponentService", + component.getName(), + componentService.getName()); + } + } + } + + private void connectReferencesToComponentType(Component component, Monitor monitor) { + + // Connect each component reference to the corresponding component type reference + for (ComponentReference componentReference : component.getReferences()) { + // check for duplicate reference names in component + if (componentReference != component.getReference(componentReference.getName())){ + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "DuplicateComponentReferenceName", + component.getName(), + componentReference.getName()); + } + + if (componentReference.getReference() != null || componentReference.isForCallback()) { + continue; + } + + if (component.getImplementation() == null) { + // is null in some of our basic unit tests + continue; + } + + Reference reference = component.getImplementation().getReference(componentReference.getName()); + + if (reference != null) { + componentReference.setReference(reference); + } else { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "ReferenceNotFoundForComponentReference", + component.getName(), + componentReference.getName()); + } + } + } + + private void connectPropertiesToComponentType(Component component, Monitor monitor) { + // Connect each component property to the corresponding component type property + for (ComponentProperty componentProperty : component.getProperties()) { + // check for duplicate property names in component + if (componentProperty != component.getProperty(componentProperty.getName())){ + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "DuplicateComponentPropertyName", + component.getName(), + componentProperty.getName()); + } + + Property property = component.getImplementation().getProperty(componentProperty.getName()); + + if (property != null) { + componentProperty.setProperty(property); + } else { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "PropertyNotFound", + component.getName(), + componentProperty.getName()); + } + } + } + + private void reconcileReferenceMultiplicity(Component component, + Reference componentReference, + Reference componentTypeReference, + Monitor monitor) { + if (componentReference.getMultiplicity() != null) { + if (!isValidMultiplicityOverride(componentTypeReference.getMultiplicity(), componentReference + .getMultiplicity())) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "ReferenceIncompatibleMultiplicity", + component.getName(), + componentReference.getName()); + } + } else { + componentReference.setMultiplicity(componentTypeReference.getMultiplicity()); + } + } + + private void reconcileComponentPropertyWithComponentType(Component component, ComponentProperty componentProperty, Monitor monitor) { + Property componentTypeProperty = componentProperty.getProperty(); + if (componentTypeProperty != null) { + + // Check that a component property does not override the + // mustSupply attribute + if (!componentTypeProperty.isMustSupply() && componentProperty.isMustSupply()) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "PropertyMustSupplyIncompatible", + component.getName(), + componentProperty.getName()); + } + + // Default to the mustSupply attribute specified on the property + if (!componentProperty.isMustSupply()) + componentProperty.setMustSupply(componentTypeProperty.isMustSupply()); + + // Default to the value specified on the component type property + if (!isPropertyValueSet(componentProperty)) { + componentProperty.setValue(componentTypeProperty.getValue()); + } + + // Override the property value for the composite + if (component.getImplementation() instanceof Composite) { + componentTypeProperty.setValue(componentProperty.getValue()); + } + + // Check that a component property does not override the + // many attribute + if (!componentTypeProperty.isMany() && componentProperty.isMany()) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "PropertyOverrideManyAttribute", + component.getName(), + componentProperty.getName()); + } + + // Default to the many attribute defined on the property + componentProperty.setMany(componentTypeProperty.isMany()); + + // Default to the type and element defined on the property + if (componentProperty.getXSDType() == null) { + componentProperty.setXSDType(componentTypeProperty.getXSDType()); + } + if (componentProperty.getXSDElement() == null) { + componentProperty.setXSDElement(componentTypeProperty.getXSDElement()); + } + + // Check that a type or element are specified + if (componentProperty.getXSDElement() == null && componentProperty.getXSDType() == null) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "NoTypeForComponentProperty", + component.getName(), + componentProperty.getName()); + } + + // check that the types specified in the component type and component property match + if ( componentProperty.getXSDElement() != null && + !componentProperty.getXSDElement().equals(componentTypeProperty.getXSDElement())){ + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "PropertXSDElementsDontMatch", + component.getName(), + componentProperty.getName(), + componentProperty.getXSDElement().toString(), + componentTypeProperty.getXSDElement().toString()); + } + + if ( componentProperty.getXSDType() != null && + !componentProperty.getXSDType().equals(componentTypeProperty.getXSDType())){ + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "PropertXSDTypesDontMatch", + component.getName(), + componentProperty.getName(), + componentProperty.getXSDType().toString(), + componentTypeProperty.getXSDType().toString()); + } + } + } + + /** + * If the property has a source attribute use this to retrieve the value from a + * property in the parent composite + + * + * @param parentCompoent the composite that contains the component + * @param component + * @param componentProperty + */ + private void processPropertySourceAttribute(Component outerComponent, + Composite parentComposite, + Component component, + ComponentProperty componentProperty, + Monitor monitor) { + String source = componentProperty.getSource(); + + if (source != null) { + // $<name>/... + int index = source.indexOf('/'); + if (index == -1) { + // Tolerating $prop + source = source + "/"; + index = source.length() - 1; + } + if (source.charAt(0) == '$') { + String name = source.substring(1, index); + Property sourceProp = null; + if (outerComponent != null) { + sourceProp = outerComponent.getProperty(name); + } else { + sourceProp = parentComposite.getProperty(name); + } + if (sourceProp == null) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "PropertySourceNotFound", + source, + componentProperty.getName(), + component.getName()); + } + + Document sourcePropValue = (Document)sourceProp.getValue(); + + try { + // FIXME: How to deal with namespaces? + Document node = + evaluateXPath(sourcePropValue, + componentProperty.getSourceXPathExpression(), + documentBuilderFactory); + + if (node != null) { + componentProperty.setValue(node); + } else { + Monitor.warning(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "PropertyXpathExpressionReturnedNull", + component.getName(), + componentProperty.getName()); + } + } catch (Exception ex) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "PropertySourceXpathInvalid", + source, + componentProperty.getName(), + component.getName(), + ex); + } + } else { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "PropertySourceValueInvalid", + source, + componentProperty.getName(), + component.getName()); + } + } + } + + /** + * If the property has a file attribute use this to retrieve the value from a + * local file + + * + * @param parentCompoent the composite that contains the component + * @param component + */ + private void processPropertyFileAttribute(Component component, ComponentProperty componentProperty, Monitor monitor) { + String file = componentProperty.getFile(); + if (file != null) { + try { + URI uri = URI.create(file); + // URI resolution for relative URIs is done when the composite is resolved. + URL url = uri.toURL(); + URLConnection connection = url.openConnection(); + connection.setUseCaches(false); + InputStream is = null; + try { + is = connection.getInputStream(); + + Source streamSource = new SAXSource(new InputSource(is)); + DOMResult result = new DOMResult(); + javax.xml.transform.Transformer transformer = transformerFactory.newTransformer(); + transformer.transform(streamSource, result); + + Document document = (Document)result.getNode(); + + // TUSCANY-2377, Add a fake value element so it's consistent with + // the DOM tree loaded from inside SCDL + Element root = document.createElementNS(null, "value"); + root.appendChild(document.getDocumentElement()); + document.appendChild(root); + componentProperty.setValue(document); + } finally { + if (is != null) { + is.close(); + } + } + } catch (Exception ex) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "PropertyFileValueInvalid", + file, + componentProperty.getName(), + component.getName(), + ex); + } + } + + } + + /** + * Evaluate an XPath expression against a Property value, returning the result as a Property value + * @param node - the document root element of a Property value + * @param expression - the XPath expression + * @param documentBuilderFactory - a DOM document builder factory + * @return - a DOM Document representing the result of the evaluation as a Property value + * @throws XPathExpressionException + * @throws ParserConfigurationException + */ + private Document evaluateXPath(Document node, + XPathExpression expression, + DocumentBuilderFactory documentBuilderFactory) throws XPathExpressionException, + ParserConfigurationException { + + // The document element is a <sca:property/> element + Node property = node.getDocumentElement(); + // The first child of the <property/> element is a <value/> element + Node value = property.getFirstChild(); + + Node result = (Node)expression.evaluate(value, XPathConstants.NODE); + if (result == null) { + return null; + } + + if (result instanceof Document) { + return (Document)result; + } else { + Document document = documentBuilderFactory.newDocumentBuilder().newDocument(); + Element newProperty = document.createElementNS(SCA11_NS, "property"); + + if (result.getNodeType() == Node.ELEMENT_NODE) { + // If the result is a <value/> element, use it directly in the result + newProperty.appendChild(document.importNode(result, true)); + } else { + // If the result is not a <value/> element, create a <value/> element to contain the result + Element newValue = document.createElementNS(SCA11_NS, "value"); + newValue.appendChild(document.importNode(result, true)); + newProperty.appendChild(newValue); + } // end if + document.appendChild(newProperty); + + return document; + } + } + + /** + * Create a callback reference for a component service + * + * @param component + * @param service + */ + private void createCallbackReference(Component component, ComponentService service) { + + // if the service has a callback interface create a reference + // to represent the callback + if (service.getInterfaceContract() != null && // can be null in unit tests + service.getInterfaceContract().getCallbackInterface() != null) { + + ComponentReference callbackReference = assemblyFactory.createComponentReference(); + callbackReference.setForCallback(true); + callbackReference.setName(service.getName()); + try { + InterfaceContract contract = (InterfaceContract)service.getInterfaceContract().clone(); + contract.setInterface(contract.getCallbackInterface()); + contract.setCallbackInterface(null); + callbackReference.setInterfaceContract(contract); + } catch (CloneNotSupportedException e) { + // will not happen + } + Service implService = service.getService(); + if (implService != null) { + + // If the implementation service is a CompositeService, ensure that the Reference that is + // created is a CompositeReference, otherwise create a Reference + Reference implReference; + if (implService instanceof CompositeService) { + CompositeReference implCompReference = assemblyFactory.createCompositeReference(); + // Set the promoted component from the promoted component of the composite service + implCompReference.getPromotedComponents().add(((CompositeService)implService) + .getPromotedComponent()); + // Set the promoted service + ComponentReference promotedReference = assemblyFactory.createComponentReference(); + String promotedRefName = + ((CompositeService)implService).getPromotedComponent().getName() + "/" + + ((CompositeService)implService).getPromotedService().getName(); + promotedReference.setName(promotedRefName); + promotedReference.setUnresolved(true); + implCompReference.getPromotedReferences().add(promotedReference); + implReference = implCompReference; + // Add the composite reference to the composite implementation artifact + Implementation implementation = component.getImplementation(); + if (implementation != null && implementation instanceof Composite) { + ((Composite)implementation).getReferences().add(implCompReference); + } + } else { + implReference = assemblyFactory.createReference(); + } + + implReference.setName(implService.getName()); + try { + InterfaceContract implContract = (InterfaceContract)implService.getInterfaceContract().clone(); + implContract.setInterface(implContract.getCallbackInterface()); + implContract.setCallbackInterface(null); + implReference.setInterfaceContract(implContract); + } catch (CloneNotSupportedException e) { + // will not happen + } + callbackReference.setReference(implReference); + } + component.getReferences().add(callbackReference); + + // Set the bindings of the callback reference + if (callbackReference.getBindings().isEmpty()) { + // If there are specific callback bindings set in the SCDL service + // callback element then use them + if (service.getCallback() != null && service.getCallback().getBindings().size() > 0) { + callbackReference.getBindings().addAll(service.getCallback().getBindings()); + } else { + // otherwise create a default binding which + // will cause the EPR for this reference to be + // marked as EndpointReference.NOT_CONFIGURED + createSCABinding(callbackReference, null); + + // TODO - should really use the forward binding here but + // awaiting OASIS decision on what's going to + // happen with callbacks + } + } + service.setCallbackReference(callbackReference); + } + } + + /** + * Create a callback service for a component reference + * + * @param component + * @param service + */ + private void createCallbackService(Component component, ComponentReference reference) { + if (reference.getInterfaceContract() != null && // can be null in unit tests + reference.getInterfaceContract().getCallbackInterface() != null) { + ComponentService componentService = assemblyFactory.createComponentService(); + componentService.setForCallback(true); + componentService.setName(reference.getName()); + try { + InterfaceContract contract = (InterfaceContract)reference.getInterfaceContract().clone(); + contract.setInterface(contract.getCallbackInterface()); + contract.setCallbackInterface(null); + componentService.setInterfaceContract(contract); + } catch (CloneNotSupportedException e) { + // will not happen + } + Reference implReference = reference.getReference(); + if (implReference != null) { + // If the implementation reference is a CompositeReference, ensure that the Service that is + // created is a CompositeService, otherwise create a Service + Service implService; + if (implReference instanceof CompositeReference) { + CompositeService implCompService = assemblyFactory.createCompositeService(); + // TODO The reality here is that the composite reference which has the callback COULD promote more than + // one component reference - and there must be a separate composite callback service for each of these component + // references + // Set the promoted component from the promoted component of the composite reference + implCompService.setPromotedComponent(((CompositeReference)implReference).getPromotedComponents() + .get(0)); + implCompService.setForCallback(true); + // Set the promoted service + ComponentService promotedService = assemblyFactory.createComponentService(); + promotedService.setName(((CompositeReference)implReference).getPromotedReferences().get(0) + .getName()); + promotedService.setUnresolved(true); + promotedService.setForCallback(true); + implCompService.setPromotedService(promotedService); + implService = implCompService; + // Add the composite service to the composite implementation artifact + Implementation implementation = component.getImplementation(); + if (implementation != null && implementation instanceof Composite) { + ((Composite)implementation).getServices().add(implCompService); + } // end if + // + } else { + implService = assemblyFactory.createService(); + } // end if + // + implService.setName(implReference.getName()); + try { + InterfaceContract implContract = (InterfaceContract)implReference.getInterfaceContract().clone(); + implContract.setInterface(implContract.getCallbackInterface()); + implContract.setCallbackInterface(null); + implService.setInterfaceContract(implContract); + } catch (CloneNotSupportedException e) { + // will not happen + } + componentService.setService(implService); + } + component.getServices().add(componentService); + + // configure bindings for the callback service + if (componentService.getBindings().isEmpty()) { + if (reference.getCallback() != null && reference.getCallback().getBindings().size() > 0) { + // set bindings of the callback service based on the information provided in + // SCDL reference callback element + componentService.getBindings().addAll(reference.getCallback().getBindings()); + } else if (reference.getBindings().size() > 0) { + // use any bindings explicitly declared on the forward reference + for (Binding binding : reference.getBindings()) { + try { + Binding clonedBinding = (Binding)binding.clone(); + // binding uri will be calculated during runtime build + clonedBinding.setURI(null); + componentService.getBindings().add(clonedBinding); + } catch (CloneNotSupportedException ex) { + + } + } + } else { + // TODO - should use the binding resolved from the service but + // waiting for OASIS to decide what to do about callbacks + // create a default binding + createSCABinding(componentService, null); + } + } + + reference.setCallbackService(componentService); + } + } + + /** + * Create a default SCA binding in the case that no binding + * is specified by the user + * + * @param contract + * @param definitions + */ + protected void createSCABinding(Contract contract, Definitions definitions) { + + SCABinding scaBinding = scaBindingFactory.createSCABinding(); + scaBinding.setName(contract.getName()); + + if (definitions != null) { + for (ExtensionType attachPointType : definitions.getBindingTypes()) { + if (attachPointType.getType().equals(BINDING_SCA_QNAME)) { + ((PolicySubject)scaBinding).setExtensionType(attachPointType); + } + } + } + + contract.getBindings().add(scaBinding); + contract.setOverridingBindings(false); + } + + /** + * Look to see if any value elements have been set into the property + * A bit involved as the value is stored as a DOM Document + * + * @param property the property to be tested + * @return true is values are present + */ + private boolean isPropertyValueSet(Property property) { + Document value = (Document)property.getValue(); + + if (value == null) { + return false; + } + + if (value.getFirstChild() == null) { + return false; + } + + if (value.getFirstChild().getChildNodes().getLength() == 0) { + return false; + } + + return true; + } + + /** + * Look to see is a property has more than one value + * + * @param property + * @return true is the property has more than one value + */ + private boolean isPropertyManyValued(Property property) { + + if (isPropertyValueSet(property)){ + Document value = (Document)property.getValue(); + if (value.getFirstChild().getChildNodes().getLength() > 1){ + return true; + } + } + return false; + } + + private boolean isValidMultiplicityOverride(Multiplicity definedMul, Multiplicity overridenMul) { + if (definedMul != overridenMul) { + switch (definedMul) { + case ZERO_N: + return overridenMul == Multiplicity.ZERO_ONE || overridenMul == Multiplicity.ONE_ONE + || overridenMul == Multiplicity.ONE_N; + case ONE_N: + return overridenMul == Multiplicity.ONE_ONE; + case ZERO_ONE: + return overridenMul == Multiplicity.ONE_ONE; + default: + return false; + } + } else { + return true; + } + } + + + /** + * Interface contract from higher in the implementation hierarchy takes precedence + * When it comes to checking compatibility the top level service interface is a + * subset of the promoted service interface so treat the top level interface as + * the source + * + * @param topContract the top contract + * @param bottomContract the bottom contract + */ + private void calculateServiceInterfaceContract(Component component, Service topContract, Service bottomContract, Monitor monitor) { + + // Use the interface contract from the bottom level contract if + // none is specified on the top level contract + InterfaceContract topInterfaceContract = topContract.getInterfaceContract(); + InterfaceContract bottomInterfaceContract = bottomContract.getInterfaceContract(); + + if (topInterfaceContract == null) { + topContract.setInterfaceContract(bottomInterfaceContract); + } else if (bottomInterfaceContract != null) { + // Check that the top and bottom interface contracts are compatible + boolean isCompatible = true; + String incompatibilityReason = ""; + try{ + isCompatible = interfaceContractMapper.checkCompatibility(topInterfaceContract, bottomInterfaceContract, false, false); + } catch (IncompatibleInterfaceContractException ex){ + isCompatible = false; + incompatibilityReason = ex.getMessage(); + } + if (!isCompatible) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "ServiceIncompatibleComponentInterface", + component.getName(), + topContract.getName(), + incompatibilityReason); + } + } + } + + /** + * Interface contract from higher in the implementation hierarchy takes precedence + * When it comes to checking compatibility the top level reference interface is a + * superset of the promoted reference interface so treat the treat the promoted + * (bottom) interface as the source + * + * @param topContract the top contract + * @param bottomContract the bottom contract + */ + private void calculateReferenceInterfaceContract(Component component, Reference topContract, Reference bottomContract, Monitor monitor) { + + // Use the interface contract from the bottom level contract if + // none is specified on the top level contract + InterfaceContract topInterfaceContract = topContract.getInterfaceContract(); + InterfaceContract bottomInterfaceContract = bottomContract.getInterfaceContract(); + + if (topInterfaceContract == null) { + topContract.setInterfaceContract(bottomInterfaceContract); + } else if (bottomInterfaceContract != null) { + // Check that the top and bottom interface contracts are compatible + boolean isCompatible = true; + String incompatibilityReason = ""; + try{ + isCompatible = interfaceContractMapper.checkCompatibility(bottomInterfaceContract, topInterfaceContract, false, false); + } catch (IncompatibleInterfaceContractException ex){ + isCompatible = false; + incompatibilityReason = ex.getMessage(); + } + if (!isCompatible) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "ReferenceIncompatibleComponentInterface", + component.getName(), + topContract.getName(), + incompatibilityReason); + } + } + } + + /** + * Bindings from higher in the hierarchy take precedence + * + * @param componentService the top service + * @param componentTypeService the bottom service + */ + private void calculateBindings(Service componentService, Service componentTypeService) { + // forward bindings + if (componentService.getBindings().isEmpty()) { + componentService.getBindings().addAll(componentTypeService.getBindings()); + } + + if (componentService.getBindings().isEmpty()) { + createSCABinding(componentService, null); + } + + // callback bindings + if (componentService.getCallback() == null) { + componentService.setCallback(componentTypeService.getCallback()); + if (componentService.getCallback() == null) { + // Create an empty callback to avoid null check + componentService.setCallback(assemblyFactory.createCallback()); + } + } else if (componentService.getCallback().getBindings().isEmpty() && componentTypeService.getCallback() != null) { + componentService.getCallback().getBindings().addAll(componentTypeService.getCallback().getBindings()); + } + + } + + /** + * Bindings from higher in the hierarchy take precedence + * + * @param componentReference the top service + * @param componentTypeReference the bottom service + */ + private void calculateBindings(Reference componentReference, Reference componentTypeReference) { + // forward bindings + if (componentReference.getBindings().isEmpty()) { + componentReference.getBindings().addAll(componentTypeReference.getBindings()); + } + + // callback bindings + if (componentReference.getCallback() == null) { + componentReference.setCallback(componentTypeReference.getCallback()); + } else if (componentReference.getCallback().getBindings().isEmpty() && componentTypeReference.getCallback() != null) { + componentReference.getCallback().getBindings().addAll(componentTypeReference.getCallback().getBindings()); + } + + } + +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentReferenceBindingBuilderImpl.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentReferenceBindingBuilderImpl.java new file mode 100644 index 0000000000..c48d868105 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentReferenceBindingBuilderImpl.java @@ -0,0 +1,81 @@ +/* + * 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 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.Composite; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.assembly.builder.BindingBuilder; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; + +/** + * A composite builder that performs any additional building steps that + * component reference bindings may need. Used for WSDL generation. + * + * @version $Rev$ $Date$ + */ +public class ComponentReferenceBindingBuilderImpl implements CompositeBuilder { + + private BuilderExtensionPoint builders; + + public ComponentReferenceBindingBuilderImpl(ExtensionPointRegistry registry) { + this.builders = registry.getExtensionPoint(BuilderExtensionPoint.class); + } + + public Composite build(Composite composite, BuilderContext context) + throws CompositeBuilderException { + buildReferenceBindings(composite, context); + return composite; + } + + public String getID() { + return "org.apache.tuscany.sca.assembly.builder.ComponentReferenceBindingBuilder"; + } + + private void buildReferenceBindings(Composite composite, BuilderContext context) { + + // find all the component reference bindings (starting at top level) + for (Component component : composite.getComponents()) { + for (ComponentReference componentReference : component.getReferences()) { + for (Binding binding : componentReference.getBindings()) { + BindingBuilder builder = builders.getBindingBuilder(binding.getType()); + if (builder != null) { + builder.build(component, componentReference, binding, context); + } + } + } + } + + // build bindings recursively + for (Component component : composite.getComponents()) { + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + buildReferenceBindings((Composite)implementation, context); + } + } + } + +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentReferencePromotionBuilderImpl.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentReferencePromotionBuilderImpl.java new file mode 100644 index 0000000000..aaa9cbe82c --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentReferencePromotionBuilderImpl.java @@ -0,0 +1,173 @@ +/* + * 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.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.Composite; +import org.apache.tuscany.sca.assembly.CompositeReference; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.SCABinding; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.monitor.Monitor; + +/** + * A composite builder that handles the creation of promoted composite services. + * + * @version $Rev$ $Date$ + */ +public class ComponentReferencePromotionBuilderImpl implements CompositeBuilder { + private AssemblyFactory assemblyFactory; + + public ComponentReferencePromotionBuilderImpl(ExtensionPointRegistry registry) { + FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.assemblyFactory = factories.getFactory(AssemblyFactory.class); + } + + public Composite build(Composite composite, BuilderContext context) + throws CompositeBuilderException { + configureNestedCompositeReferences(composite, context.getMonitor()); + return composite; + } + + public String getID() { + return "org.apache.tuscany.sca.assembly.builder.ComponentReferencePromotionBuilder"; + } + + /** + * Push down reference configuration into nested composites. + * + * @param composite + * @param problems + */ + private void configureNestedCompositeReferences(Composite composite, Monitor monitor) { + + // Process nested composites recursively + for (Component component : composite.getComponents()) { + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + configureNestedCompositeReferences((Composite)implementation, monitor); + } + } + + // Process component references declared on components in this composite + for (Component component : composite.getComponents()) { + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + for (ComponentReference componentReference : component.getReferences()) { + Reference implReference = componentReference.getReference(); + if (implReference != null && implReference instanceof CompositeReference) { + + // If the component reference is wired, it is a promotion override + if (!componentReference.getEndpointReferences().isEmpty()) { + componentReference.setPromotionOverride(true); + } + + // If the component reference is a promotion override, override the + // configuration of the promoted reference + if (componentReference.isPromotionOverride()) { + CompositeReference compositeReference = (CompositeReference)implReference; + List<ComponentReference> promotedReferences = + ReferenceConfigurationUtil.getPromotedComponentReferences(compositeReference); + for (ComponentReference promotedReference : promotedReferences) { + ReferenceConfigurationUtil.reconcileReferenceBindings(componentReference, + promotedReference, + assemblyFactory, + monitor); + if (componentReference.getInterfaceContract() != null && // can be null in unit tests + componentReference.getInterfaceContract().getCallbackInterface() != null) { + SCABinding scaCallbackBinding = + promotedReference.getCallbackBinding(SCABinding.class); + if (promotedReference.getCallback() != null) { + promotedReference.getCallback().getBindings().clear(); + } else { + promotedReference.setCallback(assemblyFactory.createCallback()); + } + if (scaCallbackBinding != null) { + promotedReference.getCallback().getBindings().add(scaCallbackBinding); + } + if (componentReference.getCallback() != null) { + promotedReference.getCallback().getBindings().addAll(componentReference + .getCallback().getBindings()); + } + } + /* TODO - let endpoint references worry about target service + // Wire the promoted reference to the actual non-composite component services + if (promotedReference.getMultiplicity() == Multiplicity.ONE_ONE) { + // promotedReference.getTargets().clear(); + } + for (ComponentService target : componentReference.getTargets()) { + if (target.getService() instanceof CompositeService) { + + // Wire to the actual component service + // promoted by a composite service + CompositeService compositeService = (CompositeService)target.getService(); + // Find the promoted component service + ComponentService componentService = + ServiceConfigurationUtil.getPromotedComponentService(compositeService); + if (componentService != null) { + promotedReference.getTargets().add(componentService); + } + } else { + + // Wire to a non-composite target service + promotedReference.getTargets().add(target); + } + } + */ + } + } + } + } + } else { + /* TODO - let endpoint references worry about target servicep + for (ComponentReference componentReference : component.getReferences()) { + + // Wire the component reference to the actual + // non-composite component services + List<ComponentService> targets = componentReference.getTargets(); + for (int i = 0, n = targets.size(); i < n; i++) { + ComponentService target = targets.get(i); + if (target.getService() instanceof CompositeService) { + + // Wire to the actual component service + // promoted by a composite service + CompositeService compositeService = (CompositeService)target.getService(); + ComponentService componentService = compositeService.getPromotedService(); + if (componentService != null) { + targets.set(i, componentService); + } + } + } + } + */ + } + } + } + +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentServiceBindingBuilderImpl.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentServiceBindingBuilderImpl.java new file mode 100644 index 0000000000..9a74023504 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentServiceBindingBuilderImpl.java @@ -0,0 +1,80 @@ +/* + * 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 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.Composite; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.assembly.builder.BindingBuilder; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; + +/** + * A composite builder that performs any additional building steps that + * component service bindings may need. Used for WSDL generation. + * + * @version $Rev$ $Date$ + */ +public class ComponentServiceBindingBuilderImpl implements CompositeBuilder { + private BuilderExtensionPoint builders; + + public ComponentServiceBindingBuilderImpl(ExtensionPointRegistry registry) { + this.builders = registry.getExtensionPoint(BuilderExtensionPoint.class); + } + + public String getID() { + return "org.apache.tuscany.sca.assembly.builder.ComponentServiceBindingBuilder"; + } + + public Composite build(Composite composite, BuilderContext context) + throws CompositeBuilderException { + buildServiceBindings(composite, context); + return composite; + } + + private void buildServiceBindings(Composite composite, BuilderContext context) { + + // build bindings recursively + for (Component component : composite.getComponents()) { + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + buildServiceBindings((Composite)implementation, context); + } + } + + // find all the component service bindings + for (Component component : composite.getComponents()) { + for (ComponentService componentService : component.getServices()) { + for (Binding binding : componentService.getBindings()) { + BindingBuilder builder = builders.getBindingBuilder(binding.getType()); + if (builder != null) { + builder.build(component, componentService, binding, context); + } + } + } + } + } + +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositeCloneBuilderImpl.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositeCloneBuilderImpl.java new file mode 100644 index 0000000000..148a3b5368 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositeCloneBuilderImpl.java @@ -0,0 +1,100 @@ +/* + * 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.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; + +/** + * A composite builder that clones nested composites. + * + * @version $Rev$ $Date$ + */ +public class CompositeCloneBuilderImpl implements CompositeBuilder { + + public CompositeCloneBuilderImpl() { + } + + public Composite build(Composite composite, BuilderContext context) + throws CompositeBuilderException { + + if (Composite.DOMAIN_COMPOSITE.equals(composite.getName())) { + // Try to avoid clone for top-level composites that are added to the domain composite + for (Composite included : composite.getIncludes()) { + cloneIncludes(included); + cloneCompositeImplementations(included); + } + } else { + // Clone the includes + cloneIncludes(composite); + cloneCompositeImplementations(composite); + } + + return composite; + } + + private void cloneIncludes(Composite composite) { + List<Composite> includes = new ArrayList<Composite>(); + for (Composite included : composite.getIncludes()) { + try { + includes.add((Composite)included.clone()); + } catch (CloneNotSupportedException e) { + throw new UnsupportedOperationException(e); + } + } + composite.getIncludes().clear(); + composite.getIncludes().addAll(includes); + } + + public String getID() { + return "org.apache.tuscany.sca.assembly.builder.CompositeCloneBuilder"; + } + + /** + * Clone composite component implementations + * + * @param composite + * @param problems + */ + private void cloneCompositeImplementations(Composite composite) { + for (Component component : composite.getComponents()) { + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + + Composite compositeImplementation = (Composite)implementation; + try { + // Please note the clone method is recursive + Composite clone = (Composite)compositeImplementation.clone(); + component.setImplementation(clone); + } catch (CloneNotSupportedException e) { + throw new UnsupportedOperationException(e); + } + } + } + } + +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositeComponentTypeBuilderImpl.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositeComponentTypeBuilderImpl.java new file mode 100644 index 0000000000..75fda5880a --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositeComponentTypeBuilderImpl.java @@ -0,0 +1,528 @@ +/* + * 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.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.logging.Logger; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.ComponentType; +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.Contract; +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.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint; +import org.apache.tuscany.sca.assembly.builder.Messages; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.definitions.Definitions; +import org.apache.tuscany.sca.interfacedef.IncompatibleInterfaceContractException; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.policy.ExtensionType; +import org.apache.tuscany.sca.policy.PolicySubject; + +/** + * @version $Rev$ $Date$ + */ + +// TODO - really implementation.composite component type builder - CompositeComponentTypeBuilder? + +public class CompositeComponentTypeBuilderImpl { + private static final Logger logger = Logger.getLogger(CompositeComponentTypeBuilderImpl.class.getName()); + + protected static final String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200903"; + protected static final String BINDING_SCA = "binding.sca"; + protected static final QName BINDING_SCA_QNAME = new QName(SCA11_NS, BINDING_SCA); + + private ComponentBuilderImpl componentBuilder; + private AssemblyFactory assemblyFactory; + private SCABindingFactory scaBindingFactory; + private InterfaceContractMapper interfaceContractMapper; + private BuilderExtensionPoint builders; + + public CompositeComponentTypeBuilderImpl(ExtensionPointRegistry registry) { + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + + FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); + assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); + scaBindingFactory = modelFactories.getFactory(SCABindingFactory.class); + + interfaceContractMapper = utilities.getUtility(InterfaceContractMapper.class); + builders = registry.getExtensionPoint(BuilderExtensionPoint.class); + } + + public void setComponentBuilder(ComponentBuilderImpl componentBuilder) { + this.componentBuilder = componentBuilder; + } + + /** + * Calculate the component type for the provided implementation + * + * @param implementation + * @return component type + */ + public void createComponentType(Component outerComponent, Composite composite, BuilderContext context) { + + Monitor monitor = context.getMonitor(); + monitor.pushContext("Composite: " + composite.getName().toString()); + + try { + // first make sure that each child component has been properly configured based + // on its own component type + for (Component component : composite.getComponents()) { + + // Check for duplicate component names + if (component != composite.getComponent(component.getName())) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "DuplicateComponentName", + composite.getName().toString(), + component.getName()); + } + + // do any work we need to do before we configure the component + // Anything that needs to be pushed down the promotion + // hierarchy must be done before we configure the component + + // Push down the autowire flag from the composite to components + if (component.getAutowire() == null) { + component.setAutowire(composite.getAutowire()); + } + + // configure the component from its component type + componentBuilder.configureComponentFromComponentType(outerComponent, composite, component, context); + } + + // create the composite component type based on the promoted artifacts + // from the components that it contains + + // index all the components, services and references in the + // component type so that they are easy to find + 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); + + // services + calculateServices(composite, components, componentServices, monitor); + + // references + calculateReferences(composite, components, componentReferences, monitor); + + // properties + // Properties on the composite component are unaffected by properties + // on child components. Instead child component properties might take their + // values from composite properties. Hence there is nothing to do here. + //calculateProperties(composite, components); + + } finally { + monitor.popContext(); + } + } + + /** + * Index components, services and references inside a composite. + * + * @param composite + * @param components + * @param componentServices + * @param componentReferences + */ + private void indexComponentsServicesAndReferences(Composite composite, + Map<String, Component> components, + Map<String, ComponentService> componentServices, + Map<String, ComponentReference> componentReferences) { + + for (Component component : composite.getComponents()) { + + // Index components by name + components.put(component.getName(), component); + + ComponentService nonCallbackService = null; + int nonCallbackServices = 0; + for (ComponentService componentService : component.getServices()) { + + // Index component services by component name / service name + String uri = component.getName() + '/' + componentService.getName(); + componentServices.put(uri, componentService); + + // count how many non-callback services there are + // if there is only one the component name also acts as the service name + if (!componentService.isForCallback()) { + + // Check how many non callback non-promoted services we have + if (nonCallbackServices == 0) { + nonCallbackService = componentService; + } + nonCallbackServices++; + } + } + + if (nonCallbackServices == 1) { + // If we have a single non callback service, index it by + // component name as well + componentServices.put(component.getName(), nonCallbackService); + } + + // Index references by component name / reference name + for (ComponentReference componentReference : component.getReferences()) { + String uri = component.getName() + '/' + componentReference.getName(); + componentReferences.put(uri, componentReference); + } + } + } + + /** + * Connect the services in the component type to the component services that + * they promote + * + * @param componentType + * @param component + */ + private void calculateServices(ComponentType componentType, + Map<String, Component> components, + Map<String, ComponentService> componentServices, + Monitor monitor) { + + // Connect this component type's services to the + // services from child components which it promotes + connectPromotedServices(componentType, components, componentServices, monitor); + + // look at each component type service in turn and + // calculate its configuration based on OASIS rules + for (Service service : componentType.getServices()) { + CompositeService compositeService = (CompositeService)service; + ComponentService promotedComponentService = compositeService.getPromotedService(); + + // promote interface contracts + calculatePromotedServiceInterfaceContract(compositeService, promotedComponentService, monitor); + + // promote bindings + calculatePromotedBindings(compositeService, promotedComponentService); + + // promote intents - done later in CompositePolicyBuilder - discuss with RF + // calculatePromotedIntents(compositeService, promotedComponentService); + + // promote policy sets - done later in CompositePolicyBuilder - discuss with RF + // calculatePromotedPolicySets(compositeService, promotedComponentService); + } + } + + /** + * Connect the references in the component type to the component references that + * they promote + * + * @param componentType + * @param component + */ + private void calculateReferences(ComponentType componentType, + Map<String, Component> components, + Map<String, ComponentReference> componentReferences, Monitor monitor) { + + // Connect this component type's references to the + // references from child components which it promotes + connectPromotedReferences(componentType, components, componentReferences, monitor); + + // look at each component type reference in turn and + // calculate its configuration based on OASIS rules + for (Reference reference : componentType.getReferences()) { + CompositeReference compositeReference = (CompositeReference)reference; + List<ComponentReference> promotedReferences = compositeReference.getPromotedReferences(); + + for (ComponentReference promotedComponentReference : promotedReferences) { + + // promote interface contracts + calculatePromotedReferenceInterfaceContract(compositeReference, promotedComponentReference, monitor); + + // promote bindings + // Don't need to promote reference bindings as any lower level binding will + // already be targeting the correct service without need for promotion + //calculatePromotedBindings(compositeReference, promotedComponentReference); + + // promote intents - done later in CompositePolicyBuilder - discuss with RF + // calculatePromotedIntents(compositeService, promotedComponentService); + + // promote policy sets - done later in CompositePolicyBuilder - discuss with RF + // calculatePromotedPolicySets(compositeService, promotedComponentService); + } + } + } + + /** + * Connect the services in the component type to the component services that + * they promote + * + * @param componentType + * @param component + */ + private void connectPromotedServices(ComponentType componentType, + Map<String, Component> components, + Map<String, ComponentService> componentServices, + Monitor monitor) { + + for (Service service : componentType.getServices()) { + // Connect composite (component type) services to the component services + // that they promote + CompositeService compositeService = (CompositeService)service; + ComponentService componentService = compositeService.getPromotedService(); + if (componentService != null && componentService.isUnresolved()) { + // get the name of the promoted component/service + String promotedComponentName = compositeService.getPromotedComponent().getName(); + String promotedServiceName; + if (componentService.getName() != null) { + if (compositeService.isForCallback()) { + // For callbacks the name already has the form "componentName/servicename" + promotedServiceName = componentService.getName(); + } else { + promotedServiceName = promotedComponentName + '/' + componentService.getName(); + } + } else { + promotedServiceName = promotedComponentName; + } + + // find the promoted service + 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); + } else { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "PromotedServiceNotFound", + ((Composite)componentType).getName().toString(), + promotedServiceName); + } + } + } + } + + /** + * Connect the references in the component type to the component references that + * they promote + * + * @param componentType + * @param component + */ + private void connectPromotedReferences(ComponentType componentType, + Map<String, Component> components, + Map<String, ComponentReference> componentReferences, + Monitor monitor) { + + // Connect composite (component type) references to the component references that they promote + for (Reference reference : componentType.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) { + // Set the promoted component + Component promotedComponent = compositeReference.getPromotedComponents().get(i); + promotedComponent = components.get(promotedComponent.getName()); + compositeReference.getPromotedComponents().set(i, promotedComponent); + + componentReference.setPromoted(true); + + // Point to the resolved component reference + promotedReferences.set(i, componentReference); + } else { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "PromotedReferenceNotFound", + ((Composite)componentType).getName().toString(), + componentReferenceName); + } + } + } + } + } + + /** + * Create a default SCA binding in the case that no binding + * is specified by the user + * + * @param contract + * @param definitions + */ + protected void createSCABinding(Contract contract, Definitions definitions) { + + SCABinding scaBinding = scaBindingFactory.createSCABinding(); + scaBinding.setName(contract.getName()); + + if (definitions != null) { + for (ExtensionType attachPointType : definitions.getBindingTypes()) { + if (attachPointType.getType().equals(BINDING_SCA_QNAME)) { + ((PolicySubject)scaBinding).setExtensionType(attachPointType); + } + } + } + + contract.getBindings().add(scaBinding); + contract.setOverridingBindings(false); + } + + /** + * The following methods implement rules that the OASIS specification defined explicitly + * to control how configuration from a component type is inherited by a component + */ + + /** + * Interface contract from higher in the implementation hierarchy takes precedence. + * When it comes to checking compatibility the top level service interface is a + * subset of the promoted service interface so treat the top level interface as + * the source + * + * @param topContract the top contract + * @param bottomContract the bottom contract + */ + private void calculatePromotedServiceInterfaceContract(Service topContract, Service bottomContract, Monitor monitor) { + // Use the interface contract from the bottom level contract if + // none is specified on the top level contract + InterfaceContract topInterfaceContract = topContract.getInterfaceContract(); + InterfaceContract bottomInterfaceContract = bottomContract.getInterfaceContract(); + + if (topInterfaceContract == null) { + topContract.setInterfaceContract(bottomInterfaceContract); + } else if (bottomInterfaceContract != null) { + // Check that the top and bottom interface contracts are compatible + boolean isCompatible = true; + String incompatibilityReason = ""; + try{ + isCompatible = interfaceContractMapper.checkCompatibility(topInterfaceContract, bottomInterfaceContract, false, false); + } catch (IncompatibleInterfaceContractException ex){ + isCompatible = false; + incompatibilityReason = ex.getMessage(); + } + if (!isCompatible) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "ServiceInterfaceNotSubSet", + topContract.getName(), + incompatibilityReason); + } + } + } + + /** + * Interface contract from higher in the implementation hierarchy takes precedence. + * When it comes to checking compatibility the top level reference interface is a + * superset of the promoted reference interface so treat the treat the promoted + * (bottom) interface as the source + * + * @param topContract the top contract + * @param bottomContract the bottom contract + */ + private void calculatePromotedReferenceInterfaceContract(Reference topContract, Reference bottomContract, Monitor monitor) { + // Use the interface contract from the bottom level contract if + // none is specified on the top level contract + InterfaceContract topInterfaceContract = topContract.getInterfaceContract(); + InterfaceContract bottomInterfaceContract = bottomContract.getInterfaceContract(); + + if (topInterfaceContract == null) { + topContract.setInterfaceContract(bottomInterfaceContract); + } else if (bottomInterfaceContract != null) { + // Check that the top and bottom interface contracts are compatible + boolean isCompatible = true; + String incompatibilityReason = ""; + try{ + isCompatible = interfaceContractMapper.checkCompatibility(bottomInterfaceContract, topInterfaceContract, false, false); + } catch (IncompatibleInterfaceContractException ex){ + isCompatible = false; + incompatibilityReason = ex.getMessage(); + } + if (!isCompatible) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "ReferenceInterfaceNotSubSet", + topContract.getName(), + incompatibilityReason); + } + } + } + + /** + * Bindings from higher in the implementation hierarchy take precedence + * + * @param compositeService + * @param promotedComponentService + */ + private void calculatePromotedBindings(CompositeService compositeService, ComponentService promotedComponentService) { + // forward bindings + if (compositeService.getBindings().isEmpty()) { + for (Binding binding : promotedComponentService.getBindings()) { + try { + compositeService.getBindings().add((Binding)binding.clone()); + } catch (CloneNotSupportedException ex) { + // this binding can't be used in the promoted service + } + } + } + + if (compositeService.getBindings().isEmpty()) { + createSCABinding(compositeService, null); + } + + // callback bindings + if (promotedComponentService.getCallback() != null) { + if (compositeService.getCallback() != null) { + compositeService.getCallback().getBindings().clear(); + } else { + compositeService.setCallback(assemblyFactory.createCallback()); + } + + for (Binding binding : promotedComponentService.getCallback().getBindings()) { + try { + compositeService.getCallback().getBindings().add((Binding)binding.clone()); + } catch (CloneNotSupportedException ex) { + // this binding can't be used in the promoted service + } + } + } + } + +} //end class diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositeIncludeBuilderImpl.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositeIncludeBuilderImpl.java new file mode 100644 index 0000000000..b4a897af54 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositeIncludeBuilderImpl.java @@ -0,0 +1,126 @@ +/* + * 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 org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Composite; +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.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.assembly.builder.Messages; +import org.apache.tuscany.sca.monitor.Monitor; + +/** + * Implementation of a CompositeBuilder. + * + * @version $Rev$ $Date$ + */ +public class CompositeIncludeBuilderImpl implements CompositeBuilder { + + public CompositeIncludeBuilderImpl() { + } + + public String getID() { + return "org.apache.tuscany.sca.assembly.builder.CompositeIncludeBuilder"; + } + + public Composite build(Composite composite, BuilderContext context) + throws CompositeBuilderException { + return processIncludes(composite, context.getMonitor()); + } + + /** + * Merge the elements from the included composites into the including composite + * @param composite + * @param monitor + * @return + */ + private Composite processIncludes(Composite composite, Monitor monitor) { + monitor.pushContext("Composite: " + composite.getName().toString()); + + try { + // process any composites referenced through implementation.composite + for (Component component : composite.getComponents()) { + + // recurse for composite implementations + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + processIncludes((Composite)implementation, monitor); + } + } + + // get the components etc. from any included composites + for (Composite included : composite.getIncludes()) { + if (included.isLocal() && !composite.isLocal()) { + // ASM60041 + Monitor.error(monitor, this, Messages.ASSEMBLY_VALIDATION, "IllegalCompositeIncusion", composite + .getName().toString(), included.getName().toString()); + return null; + } + + // The included has been cloned during composite.clone() + Composite merged = processIncludes(included, monitor); + if (merged != null) { + for (Component component : merged.getComponents()) { + // apply the autowire flag on this composite to any inline + // components - Assembly 5.6 point 4 + if (component.getAutowire() == null && merged.getAutowire() == Boolean.TRUE) { + component.setAutowire(Boolean.TRUE); + } + // Merge the intents and policySets from the included composite into + // component/service/reference elements under the composite + component.getRequiredIntents().addAll(merged.getRequiredIntents()); + component.getPolicySets().addAll(merged.getPolicySets()); + } + + for (Service service : merged.getServices()) { + service.getRequiredIntents().addAll(merged.getRequiredIntents()); + service.getPolicySets().addAll(merged.getPolicySets()); + } + + for (Reference reference : merged.getReferences()) { + reference.getRequiredIntents().addAll(merged.getRequiredIntents()); + reference.getPolicySets().addAll(merged.getPolicySets()); + } + composite.getComponents().addAll(merged.getComponents()); + composite.getServices().addAll(merged.getServices()); + composite.getReferences().addAll(merged.getReferences()); + composite.getProperties().addAll(merged.getProperties()); + composite.getWires().addAll(merged.getWires()); + // FIXME: What should we do for the extensions + /* + clone.getExtensions().addAll(fusedComposite.getExtensions()); + clone.getAttributeExtensions().addAll(fusedComposite.getAttributeExtensions()); + */ + } + } + + composite.getIncludes().clear(); + + // return the fused composite we have built up so far + return composite; + } finally { + monitor.popContext(); + } + } +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositePolicyBuilderImpl.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositePolicyBuilderImpl.java new file mode 100644 index 0000000000..f36a702752 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositePolicyBuilderImpl.java @@ -0,0 +1,534 @@ +/* + * 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.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +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.CompositeReference; +import org.apache.tuscany.sca.assembly.CompositeService; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.EndpointReference; +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.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.assembly.builder.PolicyBuilder; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.definitions.Definitions; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.IntentMap; +import org.apache.tuscany.sca.policy.PolicyExpression; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySubject; +import org.apache.tuscany.sca.policy.Qualifier; + +/** + * A composite builder that computes policy sets based on attached intents and policy sets. + * Useful if you want to build the model without making any runtime decisions such as + * reference/services matching + * + * @version $Rev$ $Date$ + */ +public class CompositePolicyBuilderImpl extends BaseBuilderImpl implements CompositeBuilder { + public CompositePolicyBuilderImpl(ExtensionPointRegistry registry) { + super(registry); + } + + public String getID() { + return "org.apache.tuscany.sca.assembly.builder.CompositePolicyBuilder"; + } + + public Composite build(Composite composite, BuilderContext context) throws CompositeBuilderException { + computePolicies(composite, context); + buildPolicies(composite, context); + return composite; + } + + /** + * Inherit the intents and policySets from the list of models + * @param intents + * @param policySets + * @param models + */ + private void inherit(PolicySubject policySubject, Object... models) { + for (Object model : models) { + if (model instanceof PolicySubject) { + PolicySubject subject = (PolicySubject)model; + // FIXME: We should ignore the mutually exclusive intents from different levels + policySubject.getRequiredIntents().addAll(subject.getRequiredIntents()); + policySubject.getPolicySets().addAll(subject.getPolicySets()); + } + } + } + + /** + * Check if two policy subjects requires multually exclusive intents + * @param subject1 + * @param subject2 + * @param monitor + * @return + */ + private boolean checkMutualExclusion(PolicySubject subject1, PolicySubject subject2, BuilderContext context) { + if (subject1 == subject2 || subject1 == null || subject2 == null) { + return false; + } + for (Intent i1 : subject1.getRequiredIntents()) { + for (Intent i2 : subject2.getRequiredIntents()) { + if (i1.getExcludedIntents().contains(i2) || i2.getExcludedIntents().contains(i1)) { + error(context.getMonitor(), "MutuallyExclusiveIntents", new Object[] {subject1, subject2}, i1, i2); + return true; + } + } + } + return false; + } + + private boolean resolveAndCheck(PolicySubject subject, BuilderContext context) { + if (subject == null) { + return false; + } + // FIXME: [rfeng] Should we resolve the intents during the "build" phase? + resolveAndNormalize(subject, context); + List<Intent> intents = subject.getRequiredIntents(); + int size = intents.size(); + for (int i = 0; i < size; i++) { + for (int j = i + 1; j < size; j++) { + Intent i1 = intents.get(i); + Intent i2 = intents.get(j); + if (i1 != i2 && i1.getExcludedIntents().contains(i2) || i2.getExcludedIntents().contains(i1)) { + error(context.getMonitor(), "MutuallyExclusiveIntents", subject, i1, i2); + return true; + } + } + } + return false; + } + + /** + * Inherit the policySets and intents from the implementation hierarchy + * @param subject + * @param composite + * @param component + * @param service + */ + private void inheritFromService(PolicySubject subject, Composite composite, Component component, Service service) { + if (service == null) { + return; + } + if (service instanceof ComponentService) { + // component!=null + if (component.getImplementation() instanceof Composite) { + composite = (Composite)component.getImplementation(); + } + inheritFromService(subject, composite, component, ((ComponentService)service).getService()); + // Component service also inherits the intents/policySets from composite/component + inherit(subject, composite, component); + } else if (service instanceof CompositeService) { + // composite!=null, component is not used + CompositeService compositeService = (CompositeService)service; + // Handle the promoted component service + inheritFromService(subject, composite, compositeService.getPromotedComponent(), compositeService + .getPromotedService()); + } + // For atomic service, the composite is not used + inherit(subject, component.getImplementation(), service); + } + + /** + * Inherit the policySets and intents from the implementation hierarchy + * @param subject + * @param composite + * @param component + * @param reference + */ + private void inheritFromReference(PolicySubject subject, + Composite composite, + Component component, + Reference reference) { + if (reference == null) { + return; + } + if (reference instanceof ComponentReference) { + // component!=null + if (component.getImplementation() instanceof Composite) { + composite = (Composite)component.getImplementation(); + } + inheritFromReference(subject, composite, component, ((ComponentReference)reference).getReference()); + } else if (reference instanceof CompositeReference) { + CompositeReference compositeReference = (CompositeReference)reference; + for (int i = 0, n = compositeReference.getPromotedReferences().size(); i < n; i++) { + inheritFromReference(subject, + composite, + compositeReference.getPromotedComponents().get(i), + compositeReference.getPromotedReferences().get(i)); + } + } + // Inherit from the componentType/reference + inherit(subject, component.getImplementation(), reference); + } + + /** + * Check if two names are equal + * @param name1 + * @param name2 + * @return + */ + private boolean isEqual(String name1, String name2) { + if (name1 == name2) { + return true; + } + if (name1 != null) { + return name1.equals(name2); + } else { + return name2.equals(name1); + } + } + + private Intent resolve(Definitions definitions, Intent proxy) { + for (Intent i : definitions.getIntents()) { + if (i.equals(proxy)) { + return i; + } + for (Intent qi : i.getQualifiedIntents()) { + if (qi.equals(proxy)) { + return qi; + } + } + } + return null; + } + + private void resolveAndNormalize(PolicySubject subject, BuilderContext context) { + Definitions definitions = context.getDefinitions(); + Set<Intent> intents = new HashSet<Intent>(); + if (definitions != null) { + for (Intent i : subject.getRequiredIntents()) { + Intent resolved = resolve(definitions, i); + if (resolved != null) { + intents.add(resolved); + } else { + warning(context.getMonitor(), "IntentNotFound", subject, i); + // Intent cannot be resolved + } + } + } + + // Replace profile intents with their required intents + while (!intents.isEmpty()) { + boolean profileIntentsFound = false; + Set<Intent> copy = new HashSet<Intent>(intents); + for (Intent i : copy) { + if (!i.getRequiredIntents().isEmpty()) { + intents.remove(i); + intents.addAll(i.getRequiredIntents()); + profileIntentsFound = true; + } + } + if (!profileIntentsFound) { + // No more profileIntents + break; + } + } + + // Remove the intents whose @contraints do not include the current element + // Replace unqualified intents if there is a qualified intent in the list + Set<Intent> copy = new HashSet<Intent>(intents); + for (Intent i : copy) { + if (i.getQualifiableIntent() != null) { + intents.remove(i.getQualifiableIntent()); + } + } + + // Replace qualifiable intents with the default qualified intent + copy = new HashSet<Intent>(intents); + for (Intent i : copy) { + if (i.getDefaultQualifiedIntent() != null) { + intents.remove(i); + intents.add(i.getDefaultQualifiedIntent()); + } + } + + subject.getRequiredIntents().clear(); + subject.getRequiredIntents().addAll(intents); + + Set<PolicySet> policySets = new HashSet<PolicySet>(); + if (definitions != null) { + for (PolicySet policySet : subject.getPolicySets()) { + int index = definitions.getPolicySets().indexOf(policySet); + if (index != -1) { + policySets.add(definitions.getPolicySets().get(index)); + } else { + // PolicySet cannot be resolved + warning(context.getMonitor(), "PolicySetNotFound", subject, policySet); + } + } + } + + for (Intent intent : subject.getRequiredIntents()) { + loop: for (PolicySet ps : definitions.getPolicySets()) { + // FIXME: We will have to check the policy references and intentMap too + // as well as the appliesTo + if (ps.getProvidedIntents().contains(intent)) { + policySets.add(ps); + break; + } + for (IntentMap map : ps.getIntentMaps()) { + for (Qualifier q : map.getQualifiers()) { + if (intent.equals(q.getIntent())) { + policySets.add(ps); + break loop; + } + } + } + } + } + + subject.getPolicySets().clear(); + subject.getPolicySets().addAll(policySets); + + } + + protected void computePolicies(Composite composite, BuilderContext context) { + resolveAndCheck(composite, context); + + for (Service service : composite.getServices()) { + CompositeService compositeService = (CompositeService)service; + checkMutualExclusion(compositeService, compositeService.getPromotedService(), context); + } + + for (Reference reference : composite.getReferences()) { + CompositeReference compositeReference = (CompositeReference)reference; + for (Reference promoted : compositeReference.getPromotedReferences()) { + checkMutualExclusion(compositeReference, promoted, context); + } + } + + // compute policies recursively + for (Component component : composite.getComponents()) { + Implementation implementation = component.getImplementation(); + resolveAndCheck(component, context); + + // Check component against implementation + checkMutualExclusion(component, component.getImplementation(), context); + + for (ComponentService componentService : component.getServices()) { + resolveAndCheck(componentService, context); + resolveAndCheck(componentService.getService(), context); + + // Check component/service against componentType/service + checkMutualExclusion(componentService, componentService.getService(), context); + + if (componentService.getInterfaceContract() != null && componentService.getService() != null) { + resolveAndCheck(componentService.getInterfaceContract().getInterface(), context); + resolveAndCheck(componentService.getService().getInterfaceContract().getInterface(), context); + + checkMutualExclusion(componentService.getInterfaceContract().getInterface(), componentService + .getService().getInterfaceContract().getInterface(), context); + + resolveAndCheck(componentService.getInterfaceContract().getCallbackInterface(), context); + resolveAndCheck(componentService.getService().getInterfaceContract().getCallbackInterface(), + context); + + checkMutualExclusion(componentService.getInterfaceContract().getCallbackInterface(), + componentService.getService().getInterfaceContract().getCallbackInterface(), + context); + } + + for (Endpoint ep : componentService.getEndpoints()) { + // Inherit from the componentType.service.interface + if (componentService.getService() != null && componentService.getService().getInterfaceContract() != null) { + inherit(ep, componentService.getService().getInterfaceContract().getInterface()); + } + if (componentService.getInterfaceContract() != null) { + // Inherit from the component.service.interface + inherit(ep, componentService.getInterfaceContract().getInterface()); + } + // Inherit from the componentType/service + inheritFromService(ep, composite, component, componentService.getService()); + // Find the corresponding binding in the componentType and inherit the intents/policySets + if (componentService.getService() != null) { + for (Binding binding : componentService.getService().getBindings()) { + resolveAndCheck((PolicySubject)binding, context); + if (isEqual(ep.getBinding().getName(), binding.getName()) && (binding instanceof PolicySubject)) { + checkMutualExclusion((PolicySubject)ep.getBinding(), (PolicySubject)binding, context); + // Inherit from componentType.service.binding + inherit(ep, binding); + break; + } + } + } + // Inherit from composite/component/service + inheritFromService(ep, composite, ep.getComponent(), ep.getService()); + // Inherit from binding + inherit(ep, ep.getBinding()); + + // Replace profile intents with their required intents + // Remove the intents whose @contraints do not include the current element + // Replace unqualified intents if there is a qualified intent in the list + // Replace qualifiable intents with the default qualied intent + resolveAndNormalize(ep, context); + } + } + + for (ComponentReference componentReference : component.getReferences()) { + resolveAndCheck(componentReference, context); + resolveAndCheck(componentReference.getReference(), context); + + // Check component/reference against componentType/reference + checkMutualExclusion(componentReference, componentReference.getReference(), context); + + if (componentReference.getInterfaceContract() != null && componentReference.getReference() != null) { + resolveAndCheck(componentReference.getInterfaceContract().getInterface(), context); + resolveAndCheck(componentReference.getReference().getInterfaceContract().getInterface(), context); + + checkMutualExclusion(componentReference.getInterfaceContract().getInterface(), componentReference + .getReference().getInterfaceContract().getInterface(), context); + + resolveAndCheck(componentReference.getInterfaceContract().getCallbackInterface(), context); + resolveAndCheck(componentReference.getReference().getInterfaceContract().getCallbackInterface(), + context); + + checkMutualExclusion(componentReference.getInterfaceContract().getCallbackInterface(), + componentReference.getReference().getInterfaceContract() + .getCallbackInterface(), + context); + } + + for (EndpointReference epr : componentReference.getEndpointReferences()) { + // Inherit from the componentType.reference.interface + if (componentReference.getReference() != null && componentReference.getReference() + .getInterfaceContract() != null) { + inherit(epr, componentReference.getReference().getInterfaceContract().getInterface()); + } + // Inherit from the component.reference.interface + if (componentReference.getInterfaceContract() != null) { + inherit(epr, componentReference.getInterfaceContract().getInterface()); + } + // Inherit from the componentType/reference + inheritFromReference(epr, composite, component, componentReference.getReference()); + // Find the corresponding binding in the componentType and inherit the intents/policySets + if (componentReference.getReference() != null) { + for (Binding binding : componentReference.getReference().getBindings()) { + if (epr.getBinding() != null && isEqual(epr.getBinding().getName(), binding.getName()) + && (binding instanceof PolicySubject)) { + resolveAndCheck((PolicySubject)binding, context); + checkMutualExclusion((PolicySubject)epr.getBinding(), (PolicySubject)binding, context); + // Inherit from componentType.reference.binding + inherit(epr, binding); + break; + } + } + } + // Inherit from composite/component/reference/binding + inheritFromReference(epr, composite, epr.getComponent(), epr.getReference()); + inherit(epr, epr.getBinding()); + + // Replace profile intents with their required intents + // Remove the intents whose @contraints do not include the current element + // Replace unqualified intents if there is a qualified intent in the list + // Replace qualifiable intents with the default qualied intent + resolveAndNormalize(epr, context); + } + } + + if (implementation instanceof Composite) { + inherit(implementation, component, composite); + computePolicies((Composite)implementation, context); + } else { + resolveAndCheck(implementation, context); + if (implementation != null) { + inherit(implementation, component, composite); + } + } + } + + } + + private Set<QName> getPolicyNames(PolicySubject subject) { + if (subject == null) { + return Collections.emptySet(); + } + Set<QName> names = new HashSet<QName>(); + for (PolicySet ps : subject.getPolicySets()) { + for (PolicyExpression exp : ps.getPolicies()) { + names.add(exp.getName()); + } + } + return names; + } + + protected void buildPolicies(Composite composite, BuilderContext context) { + + // compute policies recursively + for (Component component : composite.getComponents()) { + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + buildPolicies((Composite)implementation, context); + } + } + + for (Component component : composite.getComponents()) { + + for (ComponentService componentService : component.getServices()) { + for (Endpoint ep : componentService.getEndpoints()) { + for (QName policyType : getPolicyNames(ep)) { + PolicyBuilder builder = builders.getPolicyBuilder(policyType); + if (builder != null) { + builder.build(ep, context); + } + } + } + } + + for (ComponentReference componentReference : component.getReferences()) { + for (EndpointReference epr : componentReference.getEndpointReferences()) { + for (QName policyType : getPolicyNames(epr)) { + PolicyBuilder builder = builders.getPolicyBuilder(policyType); + if (builder != null) { + builder.build(epr, context); + } + } + } + } + + Implementation implementation = component.getImplementation(); + if (implementation != null) { + for (QName policyType : getPolicyNames(implementation)) { + PolicyBuilder builder = builders.getPolicyBuilder(policyType); + if (builder != null) { + builder.build(component, implementation, context); + } + } + } + } + } +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/EndpointBuilderImpl.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/EndpointBuilderImpl.java new file mode 100644 index 0000000000..6b181db0c1 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/EndpointBuilderImpl.java @@ -0,0 +1,147 @@ +/* + * 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.util.List; + +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.ComponentService; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.monitor.Monitor; + +/** + * creates endpoint models for component services. + */ +public class EndpointBuilderImpl implements CompositeBuilder { + private AssemblyFactory assemblyFactory; + + public EndpointBuilderImpl(ExtensionPointRegistry registry) { + FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); + } + + /** + * Create endpoint models for all component services. + * + * @param composite - the top-level composite to build the models for + * @param definitions + * @param monitor - a Monitor for logging errors + */ + public Composite build(Composite composite, BuilderContext context) throws CompositeBuilderException { + + processComponentServices(composite, context); + return composite; + + } + + private void processComponentServices(Composite composite, BuilderContext context) { + + Monitor monitor = context.getMonitor(); + + for (Component component : composite.getComponents()) { + + try { + monitor.pushContext("Component: " + component.getName().toString()); + + // recurse for composite implementations + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + processComponentServices((Composite)implementation, context); + } + + // create an endpoint for each component service binding + for (ComponentService service : component.getServices()) { + try { + monitor.pushContext("Service: " + service.getName()); + + //verify JAX-WS async assertions as in JavaCAA section 11.1 + List<Operation> asyncOperations = null; + try { + asyncOperations = (List<Operation>) service.getInterfaceContract().getInterface().getAttributes().get("JAXWS-ASYNC-OPERATIONS"); + }catch(Exception e) { + //ignore + } + + if(asyncOperations != null) { + if( ! asyncOperations.isEmpty()) { + + //error JCA100006 + + //FIXME create a java validation message resource bundle + Monitor.error(monitor, + this, + null, + "[JCA100006] JAX-WS client-side asynchronous pooling and callback methods are not allowed in service interfaces", + service, + service.getName()); + } + } + + + + + /* change to finding the promoted component and service + * when the wire is created as storing them here leads to + * the wrong URI being calculated + Component endpointComponent = component; + ComponentService endpointService = service; + + // TODO - EPR - We maintain all endpoints at the right level now + // but endpoints for promoting services must point down + // to the services they promote. + if (service.getService() instanceof CompositeService) { + CompositeService compositeService = (CompositeService)service.getService(); + endpointService = ServiceConfigurationUtil.getPromotedComponentService(compositeService); + endpointComponent = ServiceConfigurationUtil.getPromotedComponent(compositeService); + } // end if + */ + + for (Binding binding : service.getBindings()) { + Endpoint endpoint = assemblyFactory.createEndpoint(); + endpoint.setComponent(component); + endpoint.setService(service); + endpoint.setBinding(binding); + endpoint.setUnresolved(false); + service.getEndpoints().add(endpoint); + } // end for + } finally { + monitor.popContext(); + } + } + } finally { + monitor.popContext(); + } + } + } + + public String getID() { + return "org.apache.tuscany.sca.assembly.builder.EndpointBuilder"; + } +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/EndpointReferenceBuilderImpl.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/EndpointReferenceBuilderImpl.java new file mode 100644 index 0000000000..b1cc873914 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/EndpointReferenceBuilderImpl.java @@ -0,0 +1,839 @@ +/* + * 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.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.CompositeReference; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.assembly.Multiplicity; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.SCABinding; +import org.apache.tuscany.sca.assembly.Wire; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.assembly.builder.Messages; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.monitor.Monitor; + +/** + * Creates endpoint reference models. + */ +public class EndpointReferenceBuilderImpl { + + private AssemblyFactory assemblyFactory; + private InterfaceContractMapper interfaceContractMapper; + + public EndpointReferenceBuilderImpl(ExtensionPointRegistry registry) { + UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); + interfaceContractMapper = utilities.getUtility(InterfaceContractMapper.class); + + FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); + assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); + } + + /** + * Create endpoint references for all component references. + * + * @param composite + */ + public Composite build(Composite composite, BuilderContext context) + throws CompositeBuilderException { + Monitor monitor = context.getMonitor(); + + // process component services + processComponentReferences(composite, monitor); + return composite; + } + + private void processComponentReferences(Composite composite, Monitor monitor) { + + monitor.pushContext("Composite: " + composite.getName().toString()); + try { + // 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 component references as described in wires + connectWires(composite, componentServices, componentReferences, monitor); + + // create endpoint references for each component's references + for (Component component : composite.getComponents()) { + monitor.pushContext("Component: " + component.getName()); + + try { + + // recurse for composite implementations + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + processComponentReferences((Composite)implementation, monitor); + } + + // create endpoint references to represent the component reference + for (ComponentReference reference : component.getReferences()) { + createReferenceEndpointReferences(composite, + component, + reference, + components, + componentServices, + monitor); + + // fix up links between endpoints and endpoint references that represent callbacks + for (ComponentService service : component.getServices()) { + if ((service.getInterfaceContract() != null) && (service.getInterfaceContract() + .getCallbackInterface() != null)) { + if (reference.getName().equals(service.getName())) { + for (Endpoint endpoint : service.getEndpoints()) { + endpoint.getCallbackEndpointReferences().addAll(reference + .getEndpointReferences()); + } + break; + } // end if + } // end if + } // end for + } // end for + + // Validate that references are wired or promoted, according + // to their multiplicity + validateReferenceMultiplicity(composite, component, monitor); + + } finally { + monitor.popContext(); + } + } // end for + + } finally { + monitor.popContext(); + } + + } // end method processCompoenntReferences + + protected void indexComponentsServicesAndReferences(Composite composite, + Map<String, Component> components, + Map<String, ComponentService> componentServices, + Map<String, ComponentReference> componentReferences) { + + for (Component component : composite.getComponents()) { + + // Index components by name + components.put(component.getName(), component); + + ComponentService nonCallbackService = null; + int nonCallbackServices = 0; + for (ComponentService componentService : component.getServices()) { + + // Index component services by component name / service name + String uri = component.getName() + '/' + componentService.getName(); + componentServices.put(uri, componentService); + + // TODO - EPR - $promoted$ no longer used but it doesn't do any + // harm here + boolean promotedService = false; + if (componentService.getName() != null && componentService.getName().indexOf("$promoted$") > -1) { + promotedService = true; + } + + // count how many non-callback, non-promoted services there are + // if there is only one the component name also acts as the + // service name + if ((!componentService.isForCallback()) && (!promotedService)) { + + // Check how many non callback non-promoted services we have + if (nonCallbackServices == 0) { + nonCallbackService = componentService; + } + nonCallbackServices++; + } + + } + + if (nonCallbackServices == 1) { + // If we have a single non callback service, index it by + // component name as well + componentServices.put(component.getName(), nonCallbackService); + } + + // Index references by component name / reference name + for (ComponentReference componentReference : component.getReferences()) { + String uri = component.getName() + '/' + componentReference.getName(); + componentReferences.put(uri, componentReference); + } + } + } + + /** + * Resolve wires and connect the sources to their targets + * + * @param composite + * @param componentServices + * @param componentReferences + * @param problems + */ + private void connectWires(Composite composite, + Map<String, ComponentService> componentServices, + Map<String, ComponentReference> componentReferences, + Monitor monitor) { + + // For each wire, resolve the source reference, the target service, and + // add it to the list of targets of the reference + List<Wire> wires = composite.getWires(); + for (int i = 0, n = wires.size(); i < n; i++) { + Wire wire = wires.get(i); + + ComponentReference resolvedReference; + ComponentService resolvedService; + + // Resolve the source reference + ComponentReference source = wire.getSource(); + if (source != null && source.isUnresolved()) { + resolvedReference = componentReferences.get(source.getName()); + if (resolvedReference != null) { + wire.setSource(resolvedReference); + } else { + Monitor.warning(monitor, this, Messages.ASSEMBLY_VALIDATION, "WireSourceNotFound", source + .getName()); + } + } else { + resolvedReference = wire.getSource(); + } + + // Resolve the target service + ComponentService target = wire.getTarget(); + if (target != null && target.isUnresolved()) { + resolvedService = componentServices.get(target.getName()); + if (resolvedService != null) { + wire.setTarget(target); + } else { + Monitor.warning(monitor, this, Messages.ASSEMBLY_VALIDATION, "WireTargetNotFound", target + .getName()); + } + } else { + resolvedService = wire.getTarget(); + } + + // Add the target service to the list of targets of the + // reference + if (resolvedReference != null && resolvedService != null) { + // Check that the target component service provides + // a superset of + // the component reference interface + if (resolvedReference.getInterfaceContract() == null || interfaceContractMapper + .isCompatible(resolvedReference.getInterfaceContract(), resolvedService.getInterfaceContract())) { + + //resolvedReference.getTargets().add(resolvedService); + if (wire.isReplace()) { + resolvedReference.getTargets().clear(); + } + resolvedReference.getTargets().add(wire.getTarget()); + } else { + Monitor.warning(monitor, this, Messages.ASSEMBLY_VALIDATION, "WireIncompatibleInterface", source + .getName(), target.getName()); + } + } + } + + // Clear the list of wires + composite.getWires().clear(); + } + + private void createReferenceEndpointReferences(Composite composite, + Component component, + ComponentReference reference, + Map<String, Component> components, + Map<String, ComponentService> componentServices, + Monitor monitor) { + + monitor.pushContext("Reference: " + reference.getName()); + + // Get reference targets + List<ComponentService> refTargets = getReferenceTargets(reference); + if (reference.getAutowire() == Boolean.TRUE && reference.getTargets().isEmpty()) { + + // Find suitable targets in the current composite for an + // autowired reference + Multiplicity multiplicity = reference.getMultiplicity(); + for (Component targetComponent : composite.getComponents()) { + + // Tuscany specific selection of the first autowire reference + // when there are more than one (ASM_60025) + if ((multiplicity == Multiplicity.ZERO_ONE || multiplicity == Multiplicity.ONE_ONE) && (reference + .getEndpointReferences().size() != 0)) { + break; + } + + // Prevent autowire connecting to self + if (targetComponent == component) + continue; + + for (ComponentService targetComponentService : targetComponent.getServices()) { + if (reference.getInterfaceContract() == null || interfaceContractMapper.isCompatible(reference + .getInterfaceContract(), targetComponentService.getInterfaceContract())) { + + EndpointReference endpointRef = createEndpointRef(component, reference, false); + endpointRef.setTargetEndpoint(createEndpoint(targetComponent, targetComponentService, true)); + endpointRef.setStatus(EndpointReference.WIRED_TARGET_FOUND_READY_FOR_MATCHING); + reference.getEndpointReferences().add(endpointRef); + + // Stop with the first match for 0..1 and 1..1 references + if (multiplicity == Multiplicity.ZERO_ONE || multiplicity == Multiplicity.ONE_ONE) { + break; + } // end if + } // end if + } // end for + } // end for + + if (multiplicity == Multiplicity.ONE_N || multiplicity == Multiplicity.ONE_ONE) { + if (reference.getEndpointReferences().size() == 0) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "NoComponentReferenceTarget", + reference.getName()); + } + } + + setSingleAutoWireTarget(reference); + + } else if (!refTargets.isEmpty()) { + // Check that the component reference does not mix the use of endpoint references + // specified via the target attribute with the presence of binding elements + if (bindingsIdentifyTargets(reference)) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "ReferenceEndPointMixWithTarget", + composite.getName().toString(), + component.getName(), + reference.getName()); + } + + // Resolve targets specified on the component reference + for (ComponentService target : refTargets) { + + String targetName = getComponentServiceName(target.getName()); + String bindingName = getBindingName(target.getName()); + ComponentService targetComponentService = componentServices.get(targetName); + + Component targetComponent = getComponentFromTargetName(components, targetName); + + if (targetComponentService != null) { + // Check that target component service provides a superset of the component reference interface + if (reference.getInterfaceContract() == null || interfaceContractMapper.isCompatible(reference + .getInterfaceContract(), targetComponentService.getInterfaceContract())) { + + if (bindingName != null) { + // the user has selected a binding as part of the target name + Binding targetBinding = null; + + for (Binding tmp : targetComponentService.getBindings()) { + if (tmp.getName().equals(bindingName)) { + targetBinding = tmp; + continue; + } + } + + if (targetBinding != null) { + EndpointReference endpointRef = createEndpointRef(component, reference, false); + endpointRef.setTargetEndpoint(createEndpoint(targetComponent, + targetComponentService, + targetBinding, + true)); + endpointRef.setStatus(EndpointReference.WIRED_TARGET_NOT_FOUND); + // relying on the registry here to resolve the real endpoint + reference.getEndpointReferences().add(endpointRef); + + } else { + EndpointReference endpointRef = createEndpointRef(component, reference, true); + endpointRef.setTargetEndpoint(createEndpoint(component, targetName)); + endpointRef.setRemote(true); + endpointRef.setStatus(EndpointReference.WIRED_TARGET_NOT_FOUND); + reference.getEndpointReferences().add(endpointRef); + Monitor.warning(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "ComponentReferenceTargetNotFound", + composite.getName().toString(), + targetName); + } + + } else { + // the user hasn't selected a binding as part of the target name + + EndpointReference endpointRef = createEndpointRef(component, reference, false); + endpointRef + .setTargetEndpoint(createEndpoint(targetComponent, targetComponentService, true)); + endpointRef.setStatus(EndpointReference.WIRED_TARGET_FOUND_READY_FOR_MATCHING); + reference.getEndpointReferences().add(endpointRef); + } + } else { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "ReferenceIncompatibleInterface", + composite.getName().toString(), + component.getName() + "." + reference.getName(), + targetName); + } + } else { + // add an unresolved endpoint reference with an unresolved endpoint to go with it + EndpointReference endpointRef = createEndpointRef(component, reference, true); + endpointRef.setTargetEndpoint(createEndpoint(component, targetName)); + endpointRef.setRemote(true); + endpointRef.setStatus(EndpointReference.WIRED_TARGET_NOT_FOUND); + reference.getEndpointReferences().add(endpointRef); + Monitor.warning(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "ComponentReferenceTargetNotFound", + composite.getName().toString(), + targetName); + } // end if + } // end for + } // end if + + // if no endpoints have found so far the bindings hold the targets. + if (reference.getEndpointReferences().isEmpty()) { + for (Binding binding : reference.getBindings()) { + + String uri = binding.getURI(); + + // user hasn't put a uri on the binding so it's not a target name and the assumption is that + // the target is established via configuration of the binding element itself + if (uri == null) { + // Regular forward references are UNWIRED with no endpoint if they have an SCABinding with NO targets + // and NO URI set - but Callbacks with an SCABinding are wired and need an endpoint + if (!reference.isForCallback() && (binding instanceof SCABinding)) + continue; + + // create endpoint reference for manually configured bindings with a resolved endpoint to + // signify that this reference is pointing at some unwired endpoint + EndpointReference endpointRef = createEndpointRef(component, reference, binding, null, false); + if (binding instanceof SCABinding) { + // Assume that the system needs to resolve this binding later as + // it's the SCA binding + endpointRef.setTargetEndpoint(createEndpoint(true)); + endpointRef.setStatus(EndpointReference.NOT_CONFIGURED); + } else { + // The user has configured a binding so assume they know what + // they are doing and mark in as already resolved. + endpointRef.setTargetEndpoint(createEndpoint(false)); + endpointRef.setStatus(EndpointReference.RESOLVED_BINDING); + } + endpointRef.setRemote(true); + reference.getEndpointReferences().add(endpointRef); + continue; + } // end if + + // user might have put a local target name in the uri - see if it refers to a target we know about + // - if it does the reference binding will be matched with a service binding + // - if it doesn't it is assumed to be an external reference + if (uri.startsWith("/")) { + uri = uri.substring(1); + } + + String targetName = getComponentServiceName(uri); + String bindingName = getBindingName(uri); + + // Resolve the target component and service + ComponentService targetComponentService = componentServices.get(targetName); + Component targetComponent = getComponentFromTargetName(components, targetName); + + // If the binding URI matches a component in the composite, configure an endpoint reference with + // this component as the target. + // If not, the binding URI is assumed to reference an external service + if (targetComponentService != null) { + + // Check that the target component service provides + // a superset of the component reference interface + if (reference.getInterfaceContract() == null || interfaceContractMapper.isCompatible(reference + .getInterfaceContract(), targetComponentService.getInterfaceContract())) { + if (bindingName != null) { + // the user has selected a binding as part of the target name + Binding targetBinding = null; + + for (Binding tmp : targetComponentService.getBindings()) { + if (tmp.getName().equals(bindingName)) { + targetBinding = tmp; + continue; + } + } + + if (targetBinding != null) { + EndpointReference endpointRef = createEndpointRef(component, reference, false); + endpointRef.setTargetEndpoint(createEndpoint(targetComponent, + targetComponentService, + targetBinding, + true)); + endpointRef.setStatus(EndpointReference.WIRED_TARGET_NOT_FOUND); + // relying on the registry here to resolve the real endpoint + reference.getEndpointReferences().add(endpointRef); + + } else { + EndpointReference endpointRef = createEndpointRef(component, reference, true); + endpointRef.setTargetEndpoint(createEndpoint(component, targetName)); + endpointRef.setRemote(true); + endpointRef.setStatus(EndpointReference.WIRED_TARGET_NOT_FOUND); + reference.getEndpointReferences().add(endpointRef); + Monitor.warning(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "ComponentReferenceTargetNotFound", + composite.getName().toString(), + targetName); + } + + } else { + // create endpoint reference with dummy endpoint which will be replaced when policies + // are matched and bindings are configured later + EndpointReference endpointRef = + createEndpointRef(component, reference, binding, null, false); + endpointRef + .setTargetEndpoint(createEndpoint(targetComponent, targetComponentService, true)); + endpointRef.setStatus(EndpointReference.WIRED_TARGET_FOUND_READY_FOR_MATCHING); + reference.getEndpointReferences().add(endpointRef); + } + } else { + Monitor.warning(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "ReferenceIncompatibleInterface", + composite.getName().toString(), + reference.getName(), + uri); + } + } else { + // create endpoint reference for manually configured bindings with resolved endpoint + // to signify that this reference is pointing at some unwired endpoint. The endpoint + // is given the configured binding as a representation of the endpoint configuration. + EndpointReference endpointRef = createEndpointRef(component, reference, binding, null, false); + Endpoint endpoint = createEndpoint(false); + endpoint.setBinding(binding); + endpointRef.setTargetEndpoint(endpoint); + endpointRef.setRemote(true); + endpointRef.setStatus(EndpointReference.RESOLVED_BINDING); + reference.getEndpointReferences().add(endpointRef); + } // end if + } + } + + monitor.popContext(); + + } // end method + + private void validateReferenceMultiplicity(Composite composite, Component component, Monitor monitor) { + for (ComponentReference componentReference : component.getReferences()) { + if (!ReferenceConfigurationUtil.validateMultiplicityAndTargets(componentReference.getMultiplicity(), + componentReference.getEndpointReferences())) { + if (componentReference.getEndpointReferences().isEmpty()) { + + // No error if the reference is promoted out of the current composite + boolean promoted = false; + for (Reference reference : composite.getReferences()) { + CompositeReference compositeReference = (CompositeReference)reference; + if (compositeReference.getPromotedReferences().contains(componentReference)) { + promoted = true; + break; + } + } + if (!promoted && !componentReference.isForCallback() && !componentReference.isWiredByImpl()) { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "ReferenceWithoutTargets", + composite.getName().toString(), + componentReference.getName()); + } + } else { + // no error if reference is autowire and more targets + // than multiplicity have been found + if (componentReference.getAutowire() == Boolean.TRUE) { + break; + } + + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "TooManyReferenceTargets", + componentReference.getName()); + } + } + } + + } + + /** + * Evaluates whether the bindings attached to a reference identify one or more target services. + * @param reference - the reference + * @return true if the bindings identify a target, false otherwise + */ + private boolean bindingsIdentifyTargets(ComponentReference reference) { + for (Binding binding : reference.getBindings()) { + // <binding.sca without a URI does not identify a target + if ((binding instanceof SCABinding) && (binding.getURI() == null)) + continue; + // any other binding implies a target + // TODO Processing for other binding types + return true; + } // end for + return false; + } // end bindingsIdentifyTargets + + /** + * Helper method which obtains a list of targets for a reference + * @param reference - Component reference + * @return - the list of targets, which will be empty if there are no targets + */ + private List<ComponentService> getReferenceTargets(ComponentReference reference) { + List<ComponentService> theTargets = reference.getTargets(); + if (theTargets.isEmpty()) { + // Component reference list of targets is empty, try the implementation reference + if (reference.getReference() != null) { + theTargets = reference.getReference().getTargets(); + } // end if + } // end if + return theTargets; + } // end method getReferenceTargets + + /** + * Target names can take the form + * component/service/binding + * This extracts the component/service part + * + * @param targetName + * @return String the component/service name + */ + private String getComponentServiceName(String targetName) { + String[] parts = targetName.split("/"); + + if (parts.length > 1) { + return parts[0] + "/" + parts[1]; + } else { + return parts[0]; + } + } + + /** + * Target names can take the form + * component/service/binding + * This extracts the binding part and returns + * it. If there is no binding part it returns null + * + * @param targetName + * @return String the binding name or null if there is no binding name + */ + private String getBindingName(String targetName) { + String[] parts = targetName.split("/"); + + if (parts.length == 3) { + return parts[2]; + } else { + return null; + } + } + + /** + * Helper method that finds the Component given a target name + * @param components + * @param targetName + * @return the component + */ + private Component getComponentFromTargetName(Map<String, Component> components, String targetName) { + Component theComponent; + int s = targetName.indexOf('/'); + if (s == -1) { + theComponent = components.get(targetName); + } else { + theComponent = components.get(targetName.substring(0, s)); + } + return theComponent; + } // end method getComponentFromTargetName + + /** + * Helper method to create an Endpoint Reference + * @param component + * @param reference + * @param binding + * @param endpoint + * @param unresolved + * @return the endpoint reference + */ + private EndpointReference createEndpointRef(Component component, + ComponentReference reference, + Binding binding, + Endpoint endpoint, + boolean unresolved) { + EndpointReference endpointRef = createEndpointRef(component, reference, unresolved); + endpointRef.setBinding(binding); + endpointRef.setTargetEndpoint(endpoint); + return endpointRef; + } // end method + + /** + * Helper method to create an Endpoint Reference + * @param component + * @param reference + * @param unresolved + * @return the endpoint reference + */ + private EndpointReference createEndpointRef(Component component, ComponentReference reference, boolean unresolved) { + EndpointReference endpointRef = assemblyFactory.createEndpointReference(); + endpointRef.setComponent(component); + endpointRef.setReference(reference); + endpointRef.setUnresolved(unresolved); + return endpointRef; + } // end method createEndpointRef + + /** + * Helper method to create an endpoint + * @param component + * @param service + * @param unresolved + * @return the endpoint + */ + private Endpoint createEndpoint(Component component, ComponentService service, boolean unresolved) { + Endpoint endpoint = createEndpoint(unresolved); + endpoint.setComponent(component); + endpoint.setService(service); + endpoint.setUnresolved(unresolved); + return endpoint; + } // end method createEndpoint + + /** + * Helper method to create an endpoint + * @param component + * @param service + * @param binding + * @param unresolved + * @return the endpoint + */ + private Endpoint createEndpoint(Component component, ComponentService service, Binding binding, boolean unresolved) { + Endpoint endpoint = createEndpoint(unresolved); + endpoint.setComponent(component); + endpoint.setService(service); + endpoint.setBinding(binding); + endpoint.setUnresolved(unresolved); + return endpoint; + } // end method createEndpoint + + /** + * Helper method to create an Endpoint + * @param unresolved + * @return the endpoint + */ + private Endpoint createEndpoint(boolean unresolved) { + Endpoint endpoint = assemblyFactory.createEndpoint(); + endpoint.setUnresolved(unresolved); + return endpoint; + } // end method createEndpoint + + /** + * Helper method to create an Endpoint + * + * @param component The component that owns the reference + * @param targetName It can be one of the following formats + * <ul> + * <li>componentName + * <li>componentName/serviceName + * <li>componentName/serviceName/bindingName + * </ul> + * @return the endpoint + */ + private Endpoint createEndpoint(Component component, String targetName) { + String[] parts = targetName.split("/"); + if (parts.length < 1 || parts.length > 3) { + throw new IllegalArgumentException("Invalid target URI: " + targetName); + } + + // Find the parent uri + String uri = component.getURI(); + int index = uri.lastIndexOf('/'); + if (index == -1) { + uri = ""; + } else { + uri = uri.substring(0, index); + } + + if (parts.length >= 1) { + // Append the target component name + if (uri.length() == 0) { + uri = parts[0]; + } else { + uri = uri + "/" + parts[0]; + } + } + if (parts.length == 3) { + // <componentURI>#service-binding(serviceName/bindingName) + uri = uri + "#service-binding(" + parts[1] + "/" + parts[2] + ")"; + } else if (parts.length == 2) { + // <componentURI>#service(serviceName) + uri = uri + "#service(" + parts[1] + ")"; + } + + Endpoint endpoint = assemblyFactory.createEndpoint(); + endpoint.setUnresolved(true); + endpoint.setURI(uri); + return endpoint; + } // end method createEndpoint + + /** + * ASM_5021: where a <reference/> of a <component/> has @autowire=true + * and where the <reference/> has a <binding/> child element which + * declares a single target service, the reference is wired only to + * the single service identified by the <wire/> element + */ + private void setSingleAutoWireTarget(ComponentReference reference) { + if (reference.getEndpointReferences().size() > 1 && reference.getBindings() != null + && reference.getBindings().size() == 1) { + String uri = reference.getBindings().get(0).getURI(); + if (uri != null) { + if (uri.indexOf('/') > -1) { + // TODO: must be a way to avoid this fiddling + int i = uri.indexOf('/'); + String c = uri.substring(0, i); + String s = uri.substring(i + 1); + uri = c + "#service(" + s + ")"; + } + for (EndpointReference er : reference.getEndpointReferences()) { + if (er.getTargetEndpoint() != null && uri.equals(er.getTargetEndpoint().getURI())) { + reference.getEndpointReferences().clear(); + reference.getEndpointReferences().add(er); + return; + } + } + } + } + } + +} // end class diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ModelBuilderImpl.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ModelBuilderImpl.java new file mode 100644 index 0000000000..206afb98da --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ModelBuilderImpl.java @@ -0,0 +1,184 @@ +/* + * 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.io.ByteArrayOutputStream; + +import javax.xml.stream.XMLOutputFactory; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.monitor.Monitor; + +public class ModelBuilderImpl implements CompositeBuilder { + private ExtensionPointRegistry registry; + + private CompositeBuilder compositeIncludeBuilder; + private CompositeBuilder compositeCloneBuilder; + private CompositeComponentTypeBuilderImpl compositeComponentTypeBuilder; + private ComponentBuilderImpl componentBuilder; + + private CompositeBuilder structuralURIBuilder; + private BindingURIBuilderImpl bindingURIBuilder; + private ComponentServiceBindingBuilderImpl componentServiceBindingBuilder; + private ComponentReferenceBindingBuilderImpl componentReferenceBindingBuilder; + private EndpointBuilderImpl endpointBuilder; + private EndpointReferenceBuilderImpl endpointReferenceBuilder; + private ComponentReferencePromotionBuilderImpl componentReferencePromotionBuilder; + + private CompositeBuilder policyAttachmentBuilder; + private CompositePolicyBuilderImpl compositePolicyBuilder; + + /** + * Constructs a new composite builder. + * + * @param registry the extension point registry + */ + public ModelBuilderImpl(ExtensionPointRegistry registry) { + this.registry = registry; + + compositeIncludeBuilder = new CompositeIncludeBuilderImpl(); + compositeCloneBuilder = new CompositeCloneBuilderImpl(); + + compositeComponentTypeBuilder = new CompositeComponentTypeBuilderImpl(registry); + componentBuilder = new ComponentBuilderImpl(registry); + + compositeComponentTypeBuilder.setComponentBuilder(componentBuilder); + componentBuilder.setComponentTypeBuilder(compositeComponentTypeBuilder); + + structuralURIBuilder = new StructuralURIBuilderImpl(registry); + bindingURIBuilder = new BindingURIBuilderImpl(registry); + componentServiceBindingBuilder = new ComponentServiceBindingBuilderImpl(registry); + componentReferenceBindingBuilder = new ComponentReferenceBindingBuilderImpl(registry); + endpointBuilder = new EndpointBuilderImpl(registry); + endpointReferenceBuilder = new EndpointReferenceBuilderImpl(registry); + componentReferencePromotionBuilder = new ComponentReferencePromotionBuilderImpl(registry); + + policyAttachmentBuilder = new PolicyAttachmentBuilderImpl(registry); + compositePolicyBuilder = new CompositePolicyBuilderImpl(registry); + } + + public String getID() { + return "org.apache.tuscany.sca.assembly.builder.CompositeBuilder"; + } + + public Composite build(Composite composite, BuilderContext context) + throws CompositeBuilderException { + Monitor monitor = context.getMonitor(); + try { + // Clone the composites that are included or referenced in implementation.composite + composite = compositeCloneBuilder.build(composite, context); + + // Collect and fuse includes. Copy all of the components + // out of the included composite into the including composite + // and discards the included composite + composite = compositeIncludeBuilder.build(composite, context); + + // Set up the structural URIs for components (services/references/bindings?) + composite = structuralURIBuilder.build(composite, context); + + // need to apply policy external attachment + composite = policyAttachmentBuilder.build(composite, context); + + // Process the implementation hierarchy by calculating the component type + // for the top level implementation (composite). This has the effect of + // recursively calculating component types and configuring the + // components that depend on them + compositeComponentTypeBuilder.createComponentType(null, composite, context); + + // create the runtime model by updating the static model we have just + // created. This involves things like creating + // component URIs + // binding URIs + // binding specific build processing + // callback references - currently done in static pass + // callback services - currently done in static pass + // Endpoints + // Endoint References + // Policies + // TODO - called here at the moment but we could have a separate build phase + // to call these. Also we need to re-org these builders + composite = bindingURIBuilder.build(composite, context); + composite = componentServiceBindingBuilder.build(composite, context); // binding specific build + composite = componentReferenceBindingBuilder.build(composite, context); // binding specific build + endpointBuilder.build(composite, context); + endpointReferenceBuilder.build(composite, context); + composite = componentReferencePromotionBuilder.build(composite, context); // move into the static build? + composite = compositePolicyBuilder.build(composite, context); // the rest of the policy processing? + + // For debugging - in success cases + //System.out.println(dumpBuiltComposite(composite)); + + return composite; + } catch (Exception e) { + // For debugging - in failure cases + //System.out.println(dumpBuiltComposite(composite)); + throw new CompositeBuilderException("Exception while building model " + composite.getName(), e); + } + } + + /** + * For debugging the build process + * + * @return a tring version of the built model + */ + public String dumpBuiltComposite(Composite composite) { + + StAXArtifactProcessorExtensionPoint xmlProcessors = + registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + StAXArtifactProcessor<Composite> compositeProcessor = + xmlProcessors.getProcessor(Composite.class); + + return writeComposite(composite, compositeProcessor); + } + + private String writeComposite(Composite composite, StAXArtifactProcessor<Composite> compositeProcessor){ + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + XMLOutputFactory outputFactory = + registry.getExtensionPoint(FactoryExtensionPoint.class) + .getFactory(XMLOutputFactory.class); + + try { + compositeProcessor.write(composite, outputFactory.createXMLStreamWriter(bos), new ProcessorContext(registry)); + } catch(Exception ex) { + return ex.toString(); + } + + String result = bos.toString(); + + // write out and nested composites + for (Component component : composite.getComponents()) { + if (component.getImplementation() instanceof Composite) { + result += "\n<!-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -->\n" + + writeComposite((Composite)component.getImplementation(), + compositeProcessor); + } + } + + return result; + } +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java new file mode 100644 index 0000000000..cbbf54b330 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java @@ -0,0 +1,379 @@ +/* + * 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 static javax.xml.XMLConstants.DEFAULT_NS_PREFIX; +import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE; +import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.Set; +import java.util.StringTokenizer; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; + +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.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.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper; +import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.definitions.Definitions; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySubject; +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +/** + * A builder that attaches policy sets to the domain composite using the xpath defined by + * the attachTo attribute. It first creates a DOM model for the composite so that the xpath + * expression can be evaluated. For the nodes selected by the xpath, add the policySets attribute + * to the subject element. Then reload the patched DOM into a Composite model again. + * + * @version $Rev$ $Date$ + */ +public class PolicyAttachmentBuilderImpl implements CompositeBuilder { + private StAXHelper staxHelper; + private DOMHelper domHelper; + private ExtensionPointRegistry registry; + private StAXArtifactProcessor<Composite> processor; + + public PolicyAttachmentBuilderImpl(ExtensionPointRegistry registry) { + this.registry = registry; + domHelper = DOMHelper.getInstance(registry); + staxHelper = StAXHelper.getInstance(registry); + StAXArtifactProcessorExtensionPoint processors = + registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + processor = processors.getProcessor(Composite.class); + } + + public String getID() { + return "org.apache.tuscany.sca.policy.builder.PolicyAttachmentBuilder"; + } + + public Composite build(Composite composite, BuilderContext context) + throws CompositeBuilderException { + try { + Composite patched = applyXPath(composite, context.getDefinitions(), context.getMonitor()); + return patched; + } catch (Exception e) { + throw new CompositeBuilderException(e); + } + } + + /** + * Apply the attachTo XPath against the composite model + * @param composite The orginal composite + * @param definitions SCA definitions that contain the policy sets + * @param monitor The monitor + * @return A reloaded composite + * @throws Exception + */ + private Composite applyXPath(Composite composite, Definitions definitions, Monitor monitor) throws Exception { + if (definitions == null || definitions.getPolicySets().isEmpty()) { + return composite; + } + // Recursively apply the xpath against the composites referenced by <implementation.composite> + for (Component component : composite.getComponents()) { + Implementation impl = component.getImplementation(); + if (impl instanceof Composite) { + Composite patched = applyXPath((Composite)impl, definitions, monitor); + if (patched != impl) { + component.setImplementation(patched); + } + } + } + Document document = null; + + for (PolicySet ps : definitions.getPolicySets()) { + // First calculate the applicable nodes + Set<Node> applicableNodes = null; + /* + XPathExpression appliesTo = ps.getAppliesToXPathExpression(); + if (appliesTo != null) { + applicableNodes = new HashSet<Node>(); + NodeList nodes = (NodeList)appliesTo.evaluate(document, XPathConstants.NODESET); + for (int i = 0; i < nodes.getLength(); i++) { + applicableNodes.add(nodes.item(i)); + } + } + */ + XPathExpression exp = ps.getAttachToXPathExpression(); + if (exp != null) { + if (document == null) { + document = saveAsDOM(composite); + } + NodeList nodes = (NodeList)exp.evaluate(document, XPathConstants.NODESET); + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + if (applicableNodes == null || applicableNodes.contains(node)) { + // The node can be a component, service, reference or binding + String index = getStructuralURI(node); + PolicySubject subject = lookup(composite, index); + if (subject != null) { + subject.getPolicySets().add(ps); + } + } + } + } + } + + return composite; + } + + private Document saveAsDOM(Composite composite) throws XMLStreamException, ContributionWriteException, IOException, + SAXException { + // First write the composite into a DOM document so that we can apply the xpath + StringWriter sw = new StringWriter(); + XMLStreamWriter writer = staxHelper.createXMLStreamWriter(sw); + // Write the composite into a DOM document + processor.write(composite, writer, new ProcessorContext(registry)); + writer.close(); + + Document document = domHelper.load(sw.toString()); + return document; + } + + private static final QName COMPONENT = new QName(Base.SCA11_NS, "component"); + private static final QName SERVICE = new QName(Base.SCA11_NS, "service"); + private static final QName REFERENCE = new QName(Base.SCA11_NS, "reference"); + + private static String getStructuralURI(Node node) { + if (node != null) { + QName name = new QName(node.getNamespaceURI(), node.getLocalName()); + if (COMPONENT.equals(name)) { + Element element = (Element)node; + return element.getAttributeNS(null, "uri"); + } else if (SERVICE.equals(name)) { + Element component = (Element)node.getParentNode(); + String uri = component.getAttributeNS(null, "uri"); + String service = ((Element)node).getAttributeNS(null, "name"); + return uri + "#service(" + service + ")"; + } else if (REFERENCE.equals(name)) { + Element component = (Element)node.getParentNode(); + String uri = component.getAttributeNS(null, "uri"); + String reference = ((Element)node).getAttributeNS(null, "name"); + return uri + "#reference(" + reference + ")"; + } else { + String localName = node.getLocalName(); + if (localName.startsWith("binding.")) { + String bindingName = ((Element)node).getAttributeNS(null, "name"); + Element contract = (Element)node.getParentNode(); + String contractName = contract.getAttributeNS(null, "name"); + Element component = (Element)node.getParentNode().getParentNode(); + String uri = component.getAttributeNS(null, "uri"); + return uri + "#" + contract.getLocalName() + "(" + contractName + "/" + bindingName + ")"; + } else if (localName.startsWith("implementation.")) { + Element component = (Element)node.getParentNode(); + String uri = component.getAttributeNS(null, "uri"); + return uri + "#implementation()"; + } + } + } + return null; + } + + private Binding getBinding(Contract contract, String name) { + for (Binding binding : contract.getBindings()) { + if (name.equals(binding.getName())) { + return binding; + } + } + return null; + } + + private PolicySubject lookup(Composite composite, String structuralURI) { + if (structuralURI == null) { + return null; + } + int index = structuralURI.indexOf('#'); + String componentURI = structuralURI; + String service = null; + String reference = null; + String binding = null; + boolean impl = false; + + if (index != -1) { + componentURI = structuralURI.substring(0, index); + String fragment = structuralURI.substring(index + 1); + int begin = fragment.indexOf('('); + int end = fragment.indexOf(')'); + if (begin != -1 && end != -1) { + String path = fragment.substring(begin + 1, end).trim(); + String prefix = fragment.substring(0, begin).trim(); + if (prefix.equals("implementation")) { + impl = true; + } else { + int pos = path.indexOf('/'); + if (pos != -1) { + binding = path.substring(pos + 1); + path = path.substring(0, index); + if ("service-binding".equals(prefix)) { + service = path; + } else if ("reference-binding".equals(prefix)) { + reference = path; + } + } + if ("service".equals(prefix)) { + service = path; + } else if ("reference".equals(prefix)) { + reference = path; + } + } + } + } + for (Component component : composite.getComponents()) { + if (component.getURI().equals(componentURI)) { + if (service != null) { + ComponentService componentService = component.getService(service); + if (binding != null) { + Binding b = getBinding(componentService, binding); + if (b instanceof PolicySubject) { + return (PolicySubject)b; + } + } else { + return componentService; + } + } else if (reference != null) { + ComponentReference componentReference = component.getReference(reference); + if (binding != null) { + Binding b = getBinding(componentReference, binding); + if (b instanceof PolicySubject) { + return (PolicySubject)b; + } + } else { + return componentReference; + } + } else if (impl) { + return component.getImplementation(); + } + return component; + } else if (structuralURI.startsWith(component.getURI() + "/")) { + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + return lookup((Composite)implementation, structuralURI); + } else { + return null; + } + } + } + return null; + } + + /** + * Attach the policySet to the given DOM node + * @param node The DOM node (should be an element) + * @param policySet The policy set to be attached + * @return true if the element is changed, false if the element already contains the same policy set + * and no change is made + */ + private boolean attach(Node node, PolicySet policySet) { + Element element = (Element)node; + Document document = element.getOwnerDocument(); + + QName qname = policySet.getName(); + String prefix = DOMHelper.getPrefix(element, qname.getNamespaceURI()); + if (prefix == null) { + // Find the a non-conflicting prefix + int i = 0; + while (true) { + prefix = "ns" + i; + String ns = DOMHelper.getNamespaceURI(element, prefix); + if (ns == null) { + break; + } + } + // Declare the namespace + Attr nsAttr = document.createAttributeNS(XMLNS_ATTRIBUTE_NS_URI, XMLNS_ATTRIBUTE + ":" + prefix); + nsAttr.setValue(qname.getNamespaceURI()); + element.setAttributeNodeNS(nsAttr); + } + // Form the value as a qualified name + String qvalue = null; + if (DEFAULT_NS_PREFIX.equals(prefix)) { + qvalue = qname.getLocalPart(); + } else { + qvalue = prefix + ":" + qname.getLocalPart(); + } + + // Check if the attribute exists + Attr attr = element.getAttributeNode("policySets"); + if (attr == null) { + // Create the policySets attr + attr = document.createAttributeNS(null, "policySets"); + attr.setValue(qvalue); + element.setAttributeNodeNS(attr); + return true; + } else { + // Append to the existing value + boolean duplicate = false; + String value = attr.getValue(); + StringTokenizer tokenizer = new StringTokenizer(value); + while (tokenizer.hasMoreTokens()) { + String ps = tokenizer.nextToken(); + int index = ps.indexOf(':'); + String ns = null; + String localName = null; + if (index == -1) { + ns = DOMHelper.getNamespaceURI(element, DEFAULT_NS_PREFIX); + localName = ps; + } else { + ns = DOMHelper.getNamespaceURI(element, ps.substring(0, index)); + localName = ps.substring(index + 1); + } + QName psName = new QName(ns, localName); + if (qname.equals(psName)) { + duplicate = true; + break; + } + } + if (!duplicate) { + // REVIEW: [rfeng] How to comply to POL40012? + value = value + " " + qvalue; + attr.setValue(value.trim()); + return true; + } + return false; + } + } + +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ReferenceConfigurationUtil.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ReferenceConfigurationUtil.java new file mode 100644 index 0000000000..67b143e95d --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ReferenceConfigurationUtil.java @@ -0,0 +1,216 @@ +/* + * 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.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.ComponentReference; +import org.apache.tuscany.sca.assembly.CompositeReference; +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.assembly.Multiplicity; +import org.apache.tuscany.sca.assembly.OptimizableBinding; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.builder.Messages; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; + +/** + * This class encapsulates utility methods to deal with reference definitions + * + * @version $Rev$ $Date$ + */ +abstract class ReferenceConfigurationUtil { + + /** + * Report a warning. + * + * @param problems + * @param message + * @param model + */ + private static void warning(Monitor monitor, String message, Object model, String... messageParameters) { + if (monitor != null) { + Problem problem = + monitor.createProblem(ReferenceConfigurationUtil.class.getName(), + Messages.ASSEMBLY_VALIDATION, + Severity.WARNING, + model, + message, + (Object[])messageParameters); + monitor.problem(problem); + } + } + + static boolean isValidMultiplicityOverride(Multiplicity definedMul, Multiplicity overridenMul) { + if (definedMul != overridenMul) { + switch (definedMul) { + case ZERO_N: + return overridenMul == Multiplicity.ZERO_ONE || overridenMul == Multiplicity.ONE_ONE + || overridenMul == Multiplicity.ONE_N; + case ONE_N: + return overridenMul == Multiplicity.ONE_ONE; + case ZERO_ONE: + return overridenMul == Multiplicity.ONE_ONE; + default: + return false; + } + } else { + return true; + } + } + + static boolean validateMultiplicityAndTargets(Multiplicity multiplicity, List<EndpointReference> endpointReferences) { + + // In some tests multiplicity is not set + if (multiplicity == null) { + return true; + } + + // Count targets + int count = endpointReferences.size(); + + switch (multiplicity) { + case ZERO_N: + break; + case ZERO_ONE: + if (count > 1) { + return false; + } + break; + case ONE_ONE: + if (count != 1) { + return false; + } + break; + case ONE_N: + if (count < 1) { + return false; + } + break; + } + return true; + } + + /** + * Follow a reference promotion chain down to the innermost (non composite) + * component references. + * + * @param compositeReference + * @return + */ + static List<ComponentReference> getPromotedComponentReferences(CompositeReference compositeReference) { + List<ComponentReference> componentReferences = new ArrayList<ComponentReference>(); + collectPromotedComponentReferences(compositeReference, componentReferences); + return componentReferences; + } + + /** + * Follow a reference promotion chain down to the innermost (non composite) + * component references. + * + * @param compositeReference + * @param componentReferences + * @return + */ + private static void collectPromotedComponentReferences(CompositeReference compositeReference, + List<ComponentReference> componentReferences) { + for (ComponentReference componentReference : compositeReference.getPromotedReferences()) { + Reference reference = componentReference.getReference(); + if (reference instanceof CompositeReference) { + + // Continue to follow the reference promotion chain + collectPromotedComponentReferences((CompositeReference)reference, componentReferences); + + } else if (reference != null) { + + // Found a non-composite reference + componentReferences.add(componentReference); + } + } + } + + /** + * Override the bindings for a promoted reference from an outer component reference + * + * @param reference - the outer level reference + * @param promotedReference - the inner level promoted reference + */ + static void reconcileReferenceBindings(Reference reference, + ComponentReference promotedReference, + AssemblyFactory assemblyFactory, + Monitor monitor) { + + if (reference.getEndpointReferences().size() > 0) { + if (promotedReference.getMultiplicity() == Multiplicity.ONE_ONE || promotedReference.getMultiplicity() == Multiplicity.ZERO_ONE) { + // Override any existing wires for 0..1 and 1..1 multiplicity + promotedReference.getEndpointReferences().clear(); + // For 0..1 and 1..1, there should not be more than 1 endpoint reference + if (reference.getEndpointReferences().size() > 1) { + warning(monitor, "ComponentReferenceMoreWire", promotedReference, promotedReference.getName()); + } // end if + } // end if + // Clone the EndpointReferences from the outer level and add to the promoted reference + for (EndpointReference epRef : reference.getEndpointReferences()) { + EndpointReference epRefClone = copyHigherReference(epRef, promotedReference); + promotedReference.getEndpointReferences().add(epRefClone); + } // end for + } // end if + + Set<Binding> callbackBindings = new HashSet<Binding>(); + if (promotedReference.getCallback() != null) { + callbackBindings.addAll(promotedReference.getCallback().getBindings()); + } + if (reference.getCallback() != null) { + callbackBindings.addAll(reference.getCallback().getBindings()); + } + promotedReference.setCallback(assemblyFactory.createCallback()); + for (Binding binding : callbackBindings) { + if ((!(binding instanceof OptimizableBinding)) || binding.getURI() != null) { + promotedReference.getCallback().getBindings().add(binding); + } // end if + } // end for + } // end method reconcileReferenceBindings + + /** + * Copy a higher level EndpointReference down to a lower level reference which it promotes + * @param epRef - the endpoint reference + * @param promotedReference - the promoted reference + * @return - a copy of the EndpointReference with data merged from the promoted reference + */ + private static EndpointReference copyHigherReference(EndpointReference epRef, ComponentReference promotedReference) { + EndpointReference epRefClone = null; + try { + epRefClone = (EndpointReference)epRef.clone(); + } catch (Exception e) { + // Ignore (we know that EndpointReference2 can be cloned) + } // end try + // Copy across details of the inner reference + ComponentReference ref = epRefClone.getReference(); + //FIXME + epRefClone.setReference(promotedReference); + return epRefClone; + } // end copyHigherReference + +} // end class diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ServiceConfigurationUtil.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ServiceConfigurationUtil.java new file mode 100644 index 0000000000..ef03344131 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ServiceConfigurationUtil.java @@ -0,0 +1,88 @@ +/* + * 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 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.Service; + +/** + * This class encapsulates utility methods to deal with service definitions. + * + * @version $Rev$ $Date$ + */ +abstract class ServiceConfigurationUtil { + + /** + * Follow a service promotion chain down to the inner most (non composite) + * component service. + * + * @param topCompositeService + * @return + */ + static ComponentService getPromotedComponentService(CompositeService compositeService) { + ComponentService componentService = compositeService.getPromotedService(); + if (componentService != null) { + Service service = componentService.getService(); + if (componentService.getName() != null && service instanceof CompositeService) { + + // Continue to follow the service promotion chain + return getPromotedComponentService((CompositeService)service); + + } else { + + // Found a non-composite service + return componentService; + } + } else { + + // No promoted service + return null; + } + } + + /** + * Follow a service promotion chain down to the innermost (non-composite) component. + * + * @param compositeService + * @return + */ + static Component getPromotedComponent(CompositeService compositeService) { + ComponentService componentService = compositeService.getPromotedService(); + if (componentService != null) { + Service service = componentService.getService(); + if (componentService.getName() != null && service instanceof CompositeService) { + + // Continue to follow the service promotion chain + return getPromotedComponent((CompositeService)service); + + } else { + + // Found a non-composite service + return compositeService.getPromotedComponent(); + } + } else { + + // No promoted service + return null; + } + } + +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/StructuralURIBuilderImpl.java b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/StructuralURIBuilderImpl.java new file mode 100644 index 0000000000..b543e3347d --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/StructuralURIBuilderImpl.java @@ -0,0 +1,276 @@ +/* + * 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.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.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.assembly.builder.Messages; +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 { + + public StructuralURIBuilderImpl(ExtensionPointRegistry registry) { + } + + /** + * 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, Messages.ASSEMBLY_VALIDATION, "MultipleBindingsForService", contract + .getName(), binding.getName()); + } else { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "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, + Messages.ASSEMBLY_VALIDATION, + "MultipleBindingsForServiceCallback", + contract.getName(), + binding.getName()); + } else { + Monitor.error(monitor, + this, + Messages.ASSEMBLY_VALIDATION, + "MultipleBindingsForReferenceCallback", + contract.getName(), + binding.getName()); + } + } + } + } + } + + /** + * 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, BuilderContext context) + throws CompositeBuilderException { + configureStructuralURIs(composite, null, context.getDefinitions(), context.getBindingBaseURIs(), context.getMonitor()); + return composite; + } + + public String getID() { + return "org.apache.tuscany.sca.assembly.builder.StructualURIBuilder"; + } + + 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(); + } + } + +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/main/resources/META-INF/services/org.apache.tuscany.sca.assembly.builder.CompositeBuilder b/branches/sca-java-2.0-M4/modules/builder/src/main/resources/META-INF/services/org.apache.tuscany.sca.assembly.builder.CompositeBuilder new file mode 100644 index 0000000000..5de5512c82 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/main/resources/META-INF/services/org.apache.tuscany.sca.assembly.builder.CompositeBuilder @@ -0,0 +1,17 @@ +# 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.
+org.apache.tuscany.sca.builder.impl.ModelBuilderImpl;id=org.apache.tuscany.sca.assembly.builder.CompositeBuilder
diff --git a/branches/sca-java-2.0-M4/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/BuildPolicyTestCase.java b/branches/sca-java-2.0-M4/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/BuildPolicyTestCase.java new file mode 100644 index 0000000000..0c7105bd9f --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/BuildPolicyTestCase.java @@ -0,0 +1,134 @@ +/* + * 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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.net.URI; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.contribution.processor.DefaultURLArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.ExtensibleURLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.resolver.DefaultModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.definitions.Definitions; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.MonitorFactory; +import org.apache.tuscany.sca.policy.PolicySubject; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +/** + * Test reading SCA XML assembly documents. + * + * @version $Rev$ $Date$ + */ +public class BuildPolicyTestCase { + private static URLArtifactProcessor<Object> documentProcessor; + private static URLArtifactProcessor<Definitions> policyDefinitionsProcessor; + private static ModelResolver resolver; + private static CompositeBuilder compositeBuilder; + private static Composite composite; + private static Monitor monitor; + private static ProcessorContext context; + + @BeforeClass + public static void setUp() throws Exception { + DefaultExtensionPointRegistry extensionPoints = new DefaultExtensionPointRegistry(); + context = new ProcessorContext(extensionPoints); + FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + + compositeBuilder = + extensionPoints.getExtensionPoint(BuilderExtensionPoint.class) + .getCompositeBuilder("org.apache.tuscany.sca.assembly.builder.CompositeBuilder"); + + List<Definitions> policyDefinitions = new ArrayList<Definitions>(); + resolver = new DefaultModelResolver(); + + UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); + MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class); + monitor = monitorFactory.createMonitor(); + + URLArtifactProcessorExtensionPoint documentProcessors = + new DefaultURLArtifactProcessorExtensionPoint(extensionPoints); + documentProcessor = new ExtensibleURLArtifactProcessor(documentProcessors); + policyDefinitionsProcessor = documentProcessors.getProcessor(Definitions.class); + + StAXArtifactProcessorExtensionPoint staxProcessors = + extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessors.addArtifactProcessor(new TestPolicyProcessor()); + + URL url = BuildPolicyTestCase.class.getResource("Calculator.composite"); + URI uri = URI.create("TestAllCalculator.composite"); + composite = (Composite)documentProcessor.read(null, uri, url, context); + assertNotNull(composite); + + url = BuildPolicyTestCase.class.getResource("test_definitions.xml"); + uri = URI.create("test_definitions.xml"); + Definitions definitions = (Definitions)policyDefinitionsProcessor.read(null, uri, url, context); + assertNotNull(definitions); + policyDefinitions.add(definitions); + + documentProcessor.resolve(definitions, resolver, context); + documentProcessor.resolve(composite, resolver, context); + + BuilderContext builderContext = new BuilderContext(definitions, null, monitor); + compositeBuilder.build(composite, builderContext); + } + + @Test + @Ignore("There are so many errors with this composite the builder doesn't have enough to go on") + public void testPolicyIntentInheritance() throws Exception { + String namespaceUri = "http://test"; + + PolicySubject policiedComposite = composite; + assertEquals(policiedComposite.getRequiredIntents().size(), 1); + assertEquals(policiedComposite.getRequiredIntents().get(0).getName(), + new QName(namespaceUri, "tuscanyIntent_1")); + + Component component = composite.getComponents().get(0); + Endpoint ep = component.getServices().get(0).getEndpoints().get(0); + EndpointReference epr = component.getReferences().get(0).getEndpointReferences().get(0); + + System.out.println(ep.getRequiredIntents()); + System.out.println(epr.getRequiredIntents()); + } + +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/CompositeBuilderTestCase.java b/branches/sca-java-2.0-M4/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/CompositeBuilderTestCase.java new file mode 100644 index 0000000000..a99d0df91c --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/CompositeBuilderTestCase.java @@ -0,0 +1,139 @@ +/* + * 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 static org.junit.Assert.assertTrue; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Component; +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.DefaultAssemblyFactory; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.monitor.DefaultMonitorFactory; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.MonitorFactory; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test the CompositeBuilder. + * + * @version $Rev$ $Date$ + */ +public class CompositeBuilderTestCase { + + private static AssemblyFactory assemblyFactory; + private static Monitor monitor; + + @BeforeClass + public static void setUp() throws Exception { + assemblyFactory = new DefaultAssemblyFactory(); + MonitorFactory mf = new DefaultMonitorFactory(); + monitor = mf.createMonitor(); + } + + @Test + public void testFuseIncludes() throws Exception { + Composite c1 = assemblyFactory.createComposite(); + c1.setName(new QName("http://foo", "C1")); + Component a = assemblyFactory.createComponent(); + a.setName("a"); + c1.getComponents().add(a); + CompositeService s = assemblyFactory.createCompositeService(); + s.setName("s"); + c1.getServices().add(s); + CompositeReference r = assemblyFactory.createCompositeReference(); + r.setName("r"); + c1.getReferences().add(r); + + Composite c2 = assemblyFactory.createComposite(); + c2.setName(new QName("http://foo", "C2")); + c1.getIncludes().add(c2); + Component b = assemblyFactory.createComponent(); + b.setName("b"); + c2.getComponents().add(b); + + Composite c = assemblyFactory.createComposite(); + c.setName(new QName("http://foo", "C")); + c.getIncludes().add(c1); + + new CompositeIncludeBuilderImpl().build(c, new BuilderContext(monitor)); + + assertTrue(c.getComponents().get(0).getName().equals("a")); + assertTrue(c.getComponents().get(1).getName().equals("b")); + assertTrue(c.getServices().get(0).getName().equals("s")); + assertTrue(c.getReferences().get(0).getName().equals("r")); + } + + @Test + public void testExpandComposites() throws Exception { + Composite c1 = assemblyFactory.createComposite(); + c1.setName(new QName("http://foo", "C1")); + Component a = assemblyFactory.createComponent(); + a.setName("a"); + c1.getComponents().add(a); + CompositeService s = assemblyFactory.createCompositeService(); + s.setName("s"); + c1.getServices().add(s); + CompositeReference r = assemblyFactory.createCompositeReference(); + r.setName("r"); + c1.getReferences().add(r); + + Composite c2 = assemblyFactory.createComposite(); + c2.setName(new QName("http://foo", "C2")); + Component b = assemblyFactory.createComponent(); + b.setName("b"); + c2.getComponents().add(b); + + Composite c = assemblyFactory.createComposite(); + c.setName(new QName("http://foo", "C")); + Component x = assemblyFactory.createComponent(); + x.setName("x"); + x.setImplementation(c1); + c.getComponents().add(x); + Component y = assemblyFactory.createComponent(); + y.setName("y"); + y.setImplementation(c2); + c.getComponents().add(y); + Component z = assemblyFactory.createComponent(); + z.setName("z"); + z.setImplementation(c1); + c.getComponents().add(z); + + new CompositeCloneBuilderImpl().build(c, new BuilderContext(monitor)); + + assertTrue(c.getComponents().get(0).getImplementation() != c1); + assertTrue(c.getComponents().get(1).getImplementation() != c2); + assertTrue(c.getComponents().get(2).getImplementation() != c1); + + Composite i = (Composite)c.getComponents().get(0).getImplementation(); + assertTrue(i.getComponents().get(0) != a); + assertTrue(i.getComponents().get(0).getName().equals("a")); + assertTrue(i.getServices().get(0).getName().equals("s")); + assertTrue(i.getServices().get(0) != s); + assertTrue(i.getReferences().get(0).getName().equals("r")); + assertTrue(i.getReferences().get(0) != r); + } + +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/MockPolicy.java b/branches/sca-java-2.0-M4/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/MockPolicy.java new file mode 100644 index 0000000000..dac50ec227 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/MockPolicy.java @@ -0,0 +1,39 @@ +/* + * 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 javax.xml.namespace.QName; + +/** + * Mocked Policy + */ +public class MockPolicy { + public QName getName() { + return new QName("http://schemas.xmlsoap.org/ws/2004/09/policy", "PolicyAttachment"); + } + + public boolean isUnresolved() { + return false; + } + + public void setUnresolved(boolean unresolved) { + } + +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentTestCase.java b/branches/sca-java-2.0-M4/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentTestCase.java new file mode 100644 index 0000000000..9102257b77 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentTestCase.java @@ -0,0 +1,151 @@ +/* + * 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.io.IOException; +import java.io.InputStream; +import java.net.URL; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Base; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.builder.BuilderContext; +import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.core.UtilityExtensionPoint; +import org.apache.tuscany.sca.definitions.Definitions; +import org.apache.tuscany.sca.monitor.DefaultMonitorFactory; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.MonitorFactory; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Test reading SCA XML assembly documents. + * + * @version $Rev$ $Date$ + */ +public class PolicyAttachmentTestCase { + + private static StAXArtifactProcessor<Object> staxProcessor; + private static Monitor monitor; + + private static ExtensionPointRegistry extensionPoints; + private static XMLInputFactory inputFactory; + private static AssemblyFactory assemblyFactory; + private static BuilderExtensionPoint builders; + private static ProcessorContext context; + + @BeforeClass + public static void init() throws Exception { + extensionPoints = new DefaultExtensionPointRegistry(); + context = new ProcessorContext(extensionPoints); + + FactoryExtensionPoint factories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + assemblyFactory = factories.getFactory(AssemblyFactory.class); + inputFactory = factories.getFactory(XMLInputFactory.class); + // Create a monitor + UtilityExtensionPoint utilities = extensionPoints.getExtensionPoint(UtilityExtensionPoint.class); + MonitorFactory monitorFactory = new DefaultMonitorFactory(); + if (monitorFactory != null) { + monitor = monitorFactory.createMonitor(); + utilities.addUtility(monitorFactory); + } + StAXArtifactProcessorExtensionPoint staxProcessors = + extensionPoints.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, inputFactory, null); + staxProcessors.addArtifactProcessor(new TestPolicyProcessor()); + + builders = extensionPoints.getExtensionPoint(BuilderExtensionPoint.class); + } + + @Test + public void testBuild() throws Exception { + Definitions definitions = load("test_definitions.xml"); + Composite composite = load("Calculator.composite"); + + CompositeBuilder uriBuilder = new StructuralURIBuilderImpl(extensionPoints); + + BuilderContext builderContext = new BuilderContext(extensionPoints); + builderContext.setDefinitions(definitions); + composite = uriBuilder.build(composite, builderContext); + PolicyAttachmentBuilderImpl builder = new PolicyAttachmentBuilderImpl(extensionPoints); + builder.build(composite, builderContext); + } + + private <T> T load(String file) throws IOException, XMLStreamException, ContributionReadException { + URL url = getClass().getResource(file); + InputStream urlStream = url.openStream(); + XMLStreamReader reader = inputFactory.createXMLStreamReader(urlStream); + reader.nextTag(); + + T model = (T)staxProcessor.read(reader, context); + reader.close(); + return model; + } + + @Test + public void testComplexBuild() throws Exception { + Definitions definitions = load("definitions.xml"); + Composite composite1 = load("Composite1.composite"); + Composite composite2 = load("Composite2.composite"); + Composite composite3 = load("Composite3.composite"); + Composite composite4 = load("Composite4.composite"); + composite1.getIncludes().clear(); + composite1.getIncludes().add(composite3); + + composite1.getComponent("Component1B").setImplementation(composite4); + composite2.getComponent("Component2B").setImplementation(composite4); + + Composite domainComposite = assemblyFactory.createComposite(); + domainComposite.setName(new QName(Base.SCA11_NS, "")); + domainComposite.setLocal(false); + domainComposite.getIncludes().add(composite1); + domainComposite.getIncludes().add(composite2); + + CompositeBuilder includeBuilder = new CompositeIncludeBuilderImpl(); + CompositeBuilder cloneBuilder = new CompositeCloneBuilderImpl(); + CompositeBuilder uriBuilder = new StructuralURIBuilderImpl(extensionPoints); + + BuilderContext context = new BuilderContext(extensionPoints); + context.setDefinitions(definitions); + domainComposite = cloneBuilder.build(domainComposite, context); + domainComposite = includeBuilder.build(domainComposite, context); + domainComposite = uriBuilder.build(domainComposite, context); + + PolicyAttachmentBuilderImpl builder = new PolicyAttachmentBuilderImpl(extensionPoints); + domainComposite = builder.build(domainComposite, context); + + } + +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/TestPolicyProcessor.java b/branches/sca-java-2.0-M4/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/TestPolicyProcessor.java new file mode 100644 index 0000000000..1201b6e36f --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/TestPolicyProcessor.java @@ -0,0 +1,58 @@ +/* + * 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 javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; + +/** + * A PolicyProcessor used for testing. + * + * @version $Rev$ $Date$ + */ +public class TestPolicyProcessor implements StAXArtifactProcessor<MockPolicy> { + public QName getArtifactType() { + return new QName("http://schemas.xmlsoap.org/ws/2004/09/policy", "PolicyAttachment"); + } + + public MockPolicy read(XMLStreamReader arg0, ProcessorContext context) throws ContributionReadException, XMLStreamException { + return new MockPolicy(); + } + + public void write(MockPolicy arg0, XMLStreamWriter arg1, ProcessorContext context) throws ContributionWriteException, XMLStreamException { + } + + public Class<MockPolicy> getModelType() { + return MockPolicy.class; + } + + public void resolve(MockPolicy arg0, ModelResolver arg1, ProcessorContext context) throws ContributionResolveException { + + } + +} diff --git a/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/Calculator.composite b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/Calculator.composite new file mode 100644 index 0000000000..d6082a936d --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/Calculator.composite @@ -0,0 +1,49 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * 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. +--> +<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" + targetNamespace="http://sample" + xmlns:sample="http://sample" + name="Calculator"> + + <component name="CalculatorServiceComponent" requires="confidentiality"> + <implementation.java class="calculator.CalculatorServiceImpl" xmlns:test="http://test" requiers="test:TestIntentOne"/> + <reference name="addService" target="AddServiceComponent" /> + <reference name="subtractService" target="SubtractServiceComponent" /> + <reference name="multiplyService" target="MultiplyServiceComponent" /> + <reference name="divideService" target="DivideServiceComponent" /> + </component> + + <component name="AddServiceComponent"> + <implementation.java class="calculator.AddServiceImpl"/> + </component> + + <component name="SubtractServiceComponent"> + <implementation.java class="calculator.SubtractServiceImpl"/> + </component> + + <component name="MultiplyServiceComponent"> + <implementation.java class="calculator.MultiplyServiceImpl"/> + </component> + + <component name="DivideServiceComponent"> + <implementation.java class="calculator.DivideServiceImpl"/> + </component> + +</composite> diff --git a/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/Composite1.composite b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/Composite1.composite new file mode 100644 index 0000000000..105a478842 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/Composite1.composite @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * 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. +--> +<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" + targetNamespace="http://sample" + xmlns:sample="http://sample" + name="Composite1"> + <include composite="sample:Composite3"/> + + <component name="Component1A"> + <implementation.java class="sample.Component1AImpl"/> + </component> + + <component name="Component1B"> + <implementation.composite name="sample:Composite4"/> + </component> + +</composite> diff --git a/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/Composite2.composite b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/Composite2.composite new file mode 100644 index 0000000000..d3d5bafdfc --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/Composite2.composite @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * 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. +--> +<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" + targetNamespace="http://sample" + xmlns:sample="http://sample" + name="Composite2"> + + <component name="Component2A"> + <implementation.java class="sample.Component2AImpl"/> + </component> + + <component name="Component2B"> + <implementation.composite name="sample:Composite4"/> + </component> + +</composite> diff --git a/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/Composite3.composite b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/Composite3.composite new file mode 100644 index 0000000000..12aa35b694 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/Composite3.composite @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * 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. +--> +<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" + targetNamespace="http://sample" + xmlns:sample="http://sample" + name="Composite3"> + + <component name="Component3A"> + <implementation.java class="sample.Component3AImpl"/> + </component> + +</composite> diff --git a/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/Composite4.composite b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/Composite4.composite new file mode 100644 index 0000000000..141ff2a4ea --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/Composite4.composite @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * 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. +--> +<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" + targetNamespace="http://sample" + xmlns:sample="http://sample" + name="Composite4"> + + <service name="Service1" promote="Component4A/Service1"/> + <reference name="reference1" promote="Component4A/reference1"/> + + <component name="Component4A"> + <implementation.java class="sample.Component4AImpl"/> + </component> + +</composite> diff --git a/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/definitions.xml b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/definitions.xml new file mode 100644 index 0000000000..fe76d0038a --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/definitions.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="ASCII"?> +<!-- + * 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. +--> +<definitions + xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" + targetNamespace="http://sample" + xmlns:sample="http://sample" + xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200903"> + + <policySet name="PolicySet1" appliesTo="sca:implementation.java" + attachTo = "//composite[@name='']"> + </policySet> + + <policySet name="PolicySet2" appliesTo="sca:implementation.java" + attachTo = "//component[@name='Component3A']"> + </policySet> + + <policySet name="PolicySet3" appliesTo="sca:binding.ws" + attachTo = "//sca:component[sca:URIRef('Component2B/Component4A')]"> + </policySet> + +</definitions>
\ No newline at end of file diff --git a/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/scenario.odg b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/scenario.odg Binary files differnew file mode 100644 index 0000000000..a010bb1bf1 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/scenario.odg diff --git a/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/scenario.png b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/scenario.png Binary files differnew file mode 100644 index 0000000000..e1f78f6423 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/scenario.png diff --git a/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/test_definitions.xml b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/test_definitions.xml new file mode 100644 index 0000000000..dac8e384e3 --- /dev/null +++ b/branches/sca-java-2.0-M4/modules/builder/src/test/resources/org/apache/tuscany/sca/builder/impl/test_definitions.xml @@ -0,0 +1,207 @@ +<?xml version="1.0" encoding="ASCII"?> +<!-- + * 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. +--> +<definitions xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" targetNamespace="http://test" + xmlns:test="http://test" xmlns:sca="http://docs.oasis-open.org/ns/opencsa/sca/200903"> + + <!-- Extension Types Metadata --> + <implementationType type="sca:implementation.java" alwaysProvides="test:logging" mayProvide="test:tracing" /> + <bindingType type="sca:binding.ws" alwaysProvides="test:confidentiality" mayProvide="test:integrity" /> + + <!-- Intents and Policysets to assume targetnamespace --> + <intent name="TestIntentOne" constrains="sca:binding"> + <description> + Test Intent + </description> + </intent> + + <intent name="TestIntentTwo" constrains="sca:binding" requires="test:TestIntentOne"> + <description> + Protect messages from unauthorized reading or modification + </description> + </intent> + + <policySet name="TestPolicySetOne" provides="test:TestIntentOne" appliesTo="sca:binding.ws" + attachTo = "//sca:component[@name='CalculatorServiceComponent']/sca:reference[@name='addService']" + xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> + <wsp:PolicyAttachment> + <!-- policy expression and policy subject for + "basic authentication" --> + </wsp:PolicyAttachment> + <wsp:PolicyAttachment> + <!-- policy expression and policy subject for + "reliability" --> + </wsp:PolicyAttachment> + </policySet> + + <!-- POLICY SETS --> + <policySet name="SecureReliablePolicy" provides="test:confidentiality.transport test:integrity" appliesTo="sca:binding.ws" + xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> + <wsp:PolicyAttachment> + <!-- policy expression and policy subject for + "basic authentication" --> + </wsp:PolicyAttachment> + <wsp:PolicyAttachment> + <!-- policy expression and policy subject for + "reliability" --> + </wsp:PolicyAttachment> + </policySet> + + <policySet name="SecureMessagingPolicies" provides="test:confidentiality" appliesTo="binding.ws" + xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> + <intentMap provides="test:confidentiality"> + <qualifier name="transport"> + <wsp:PolicyAttachment> + <!-- policy expression and policy subject for "transport" alternative --> + </wsp:PolicyAttachment> + <wsp:PolicyAttachment>...</wsp:PolicyAttachment> + </qualifier> + <qualifier name="message"> + <wsp:PolicyAttachment> + <!-- policy expression and policy subject for "message" alternative" --> + </wsp:PolicyAttachment> + </qualifier> + </intentMap> + </policySet> + + <policySet name="SecurityPolicy" provides="test:confidentiality" appliesTo="binding.ws" + xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> + <intentMap provides="test:confidentiality"> + <qualifier name="message"> + <wsp:PolicyAttachment> + <!-- policy attachment for body encryption --> + </wsp:PolicyAttachment> + <wsp:PolicyAttachment> + <!-- policy attachment for whole message encryption --> + </wsp:PolicyAttachment> + </qualifier> + <qualifier name="transport"> + <wsp:PolicyAttachment> + <!-- policy attachment for transport encryption --> + </wsp:PolicyAttachment> + </qualifier> + </intentMap> + </policySet> + + <policySet name="BasicAuthMsgProtSecurity" provides="test:authentication test:confidentiality" appliesTo="binding.ws" + xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903"> + <policySetReference name="test:AuthenticationPolicies" /> + <policySetReference name="test:ConfidentialityPolicies" /> + </policySet> + + <policySet name="AuthenticationPolicies" provides="test:authentication" appliesTo="binding.ws" + xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> + <wsp:PolicyAttachment> + <!-- policy expression and policy subject for "basic + authentication" --> + </wsp:PolicyAttachment> + </policySet> + + <policySet name="ConfidentialityPolicies" provides="test:confidentiality" appliesTo="binding.ws" + xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> + <intentMap provides="test:confidentiality"> + <qualifier name="transport"> + <wsp:PolicyAttachment> + <!-- policy expression and policy subject for "transport" + alternative --> + </wsp:PolicyAttachment> + <wsp:PolicyAttachment>...</wsp:PolicyAttachment> + </qualifier> + <qualifier name="message"> + <wsp:PolicyAttachment> + <!-- policy expression and policy subject for "message" + alternative" --> + ... + </wsp:PolicyAttachment> + </qualifier> + </intentMap> + </policySet> + + <policySet name="SecureWSPolicy" provides="test:confidentiality" appliesTo="sca:binding.ws" + xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200903" xmlns:sp="http://schemas.xmlsoap.org/ws/2002/12/secext" + xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"> + <wsp:Policy> + <wsp:ExactlyOne> + <wsp:All> + <sp:SecurityToken> + <sp:TokenType>sp:X509v3</sp:TokenType> + </sp:SecurityToken> + <sp:UsernameToken /> + <sp:SignedParts /> + <sp:EncryptedParts> + <sp:Body /> + </sp:EncryptedParts> + <sp:TransportBinding> + <sp:IncludeTimeStamp /> + </sp:TransportBinding> + </wsp:All> + </wsp:ExactlyOne> + </wsp:Policy> + </policySet> + + <!-- profile intent --> + <intent name="reliableMessageProtection" constrains="sca:binding" requires="test:messageProtection"> + <description> + Protect messages from unauthorized reading or modification + </description> + </intent> + + <intent name="messageProtection" constrains="sca:binding" requires="test:confidentiality test:integrity"> + <description> + Protect messages from unauthorized reading or modification + </description> + </intent> + + <!-- simple intent --> + <intent name="confidentiality" constrains="sca:binding"> + <description> + Communitcation thro this binding must prevent + unauthorized users from reading the messages. + </description> + <qualifier name="transport" /> + <qualifier name="message" default="true" /> + </intent> + + <intent name="integrity" constrains="sca:binding"> + <description> + Communitcation thro this binding must prevent + unauthorized modification of the messages. + </description> + </intent> + + <intent name="authentication" constrains="sca:binding"> + <description> + Communitcation thro this binding required + Authentication. + </description> + </intent> + + <intent name="logging" constrains="sca:implementation"> + <description> + All messages to and from this implementation must be logged + </description> + </intent> + + <intent name="tracing" constrains="sca:implementation.java"> + <description> + Need to figure out some description for this + </description> + </intent> + +</definitions>
\ No newline at end of file |