From b7fb8ba1b979ed77a8d9ad29acd15d9ae9a78d8f Mon Sep 17 00:00:00 2001 From: rfeng Date: Tue, 29 Sep 2009 23:41:25 +0000 Subject: Start to integrate the policy attachment builder git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@820125 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tuscany/sca/assembly/Component.java | 6 +- .../tuscany/sca/assembly/impl/ComponentImpl.java | 49 ++---- .../tuscany/sca/builder/impl/BaseBuilderImpl.java | 1 + .../sca/builder/impl/BindingURIBuilderImpl.java | 144 ++-------------- .../sca/builder/impl/ComponentBuilderImpl.java | 1 + .../impl/CompositeComponentTypeBuilderImpl.java | 1 + .../tuscany/sca/builder/impl/ModelBuilderImpl.java | 14 +- .../builder/impl/PolicyAttachmentBuilderImpl.java | 179 +++++++++++++++++--- .../sca/builder/impl/StructuralURIBuilderImpl.java | 181 +-------------------- .../sca/builder/impl/PolicyAttachmentTestCase.java | 14 +- .../common/xml/stax/impl/XMLStreamSerializer.java | 21 ++- .../tuscany/sca/common/xml/xpath/XPathHelper.java | 1 + .../processor/BaseStAXArtifactProcessor.java | 2 +- 13 files changed, 229 insertions(+), 385 deletions(-) (limited to 'java/sca/modules') diff --git a/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/Component.java b/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/Component.java index cb213e9f11..2779457937 100644 --- a/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/Component.java +++ b/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/Component.java @@ -85,7 +85,7 @@ public interface Component extends Base, Extensible, PolicySubject, Cloneable { * @param name the reference name * @return reference the reference */ - Reference getReference(String name); + ComponentReference getReference(String name); /** * Returns a list of services exposed by the component. @@ -100,7 +100,7 @@ public interface Component extends Base, Extensible, PolicySubject, Cloneable { * @param name the service name * @return service the service */ - Service getService(String name); + ComponentService getService(String name); /** * Returns a list of properties for the component. @@ -115,7 +115,7 @@ public interface Component extends Base, Extensible, PolicySubject, Cloneable { * @param name the property name * @return property the property */ - Property getProperty(String name); + ComponentProperty getProperty(String name); /** * Returns a constraining type defining the shape of the component. diff --git a/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/impl/ComponentImpl.java b/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/impl/ComponentImpl.java index 0227ab3c65..66e0c9cb59 100644 --- a/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/impl/ComponentImpl.java +++ b/java/sca/modules/assembly/src/main/java/org/apache/tuscany/sca/assembly/impl/ComponentImpl.java @@ -28,9 +28,6 @@ import org.apache.tuscany.sca.assembly.ComponentService; import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.assembly.ConstrainingType; 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.Service; import org.apache.tuscany.sca.policy.ExtensionType; import org.apache.tuscany.sca.policy.Intent; import org.apache.tuscany.sca.policy.PolicySet; @@ -112,52 +109,40 @@ public class ComponentImpl extends ExtensibleImpl implements Component, Cloneabl return properties; } - public Property getProperty(String name){ - Property property = null; - - for (Property tmp : getProperties()){ - if (tmp.getName().equals(name)){ - property = tmp; - break; + public ComponentProperty getProperty(String name) { + for (ComponentProperty property : getProperties()) { + if (property.getName().equals(name)) { + return property; } } - - return property; - } + return null; + } public List getReferences() { return references; } - public Reference getReference(String name){ - Reference reference = null; - - for (Reference tmp : getReferences()){ - if (tmp.getName().equals(name)){ - reference = tmp; - break; + public ComponentReference getReference(String name){ + for (ComponentReference ref : getReferences()){ + if (ref.getName().equals(name)){ + return ref; } } - - return reference; + return null; } public List getServices() { return services; } - public Service getService(String name){ - Service service = null; - - for (Service tmp : getServices()){ - if (tmp.getName().equals(name)){ - service = tmp; - break; + public ComponentService getService(String name) { + for (ComponentService service : getServices()) { + if (service.getName().equals(name)) { + return service; } } - - return service; - } + return null; + } public void setConstrainingType(ConstrainingType constrainingType) { this.constrainingType = constrainingType; diff --git a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/BaseBuilderImpl.java b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/BaseBuilderImpl.java index e78c0c46d5..ad2267a635 100644 --- a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/BaseBuilderImpl.java +++ b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/BaseBuilderImpl.java @@ -691,6 +691,7 @@ public abstract class BaseBuilderImpl implements CompositeBuilder { } */ SCABinding scaBinding = scaBindingFactory.createSCABinding(); + scaBinding.setName(contract.getName()); if (definitions != null) { for (ExtensionType attachPointType : definitions.getBindingTypes()) { diff --git a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/BindingURIBuilderImpl.java b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/BindingURIBuilderImpl.java index 88b5c126fc..57d0a7f639 100644 --- a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/BindingURIBuilderImpl.java +++ b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/BindingURIBuilderImpl.java @@ -21,7 +21,6 @@ package org.apache.tuscany.sca.builder.impl; import java.net.URI; import java.net.URISyntaxException; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -29,33 +28,25 @@ import javax.xml.namespace.QName; import org.apache.tuscany.sca.assembly.Binding; import org.apache.tuscany.sca.assembly.Component; -import org.apache.tuscany.sca.assembly.ComponentReference; import org.apache.tuscany.sca.assembly.ComponentService; import org.apache.tuscany.sca.assembly.Composite; -import org.apache.tuscany.sca.assembly.Contract; import org.apache.tuscany.sca.assembly.Implementation; -import org.apache.tuscany.sca.assembly.Reference; import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; import org.apache.tuscany.sca.assembly.builder.CompositeBuilderException; +import org.apache.tuscany.sca.assembly.builder.DeployedCompositeBuilder; import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.apache.tuscany.sca.definitions.Definitions; import org.apache.tuscany.sca.monitor.Monitor; -import org.apache.tuscany.sca.monitor.MonitorFactory; /** * Configuration of binding URIs. * * @version $Rev$ $Date$ */ -public class BindingURIBuilderImpl { - - private Monitor monitor; +public class BindingURIBuilderImpl implements CompositeBuilder, DeployedCompositeBuilder { public BindingURIBuilderImpl(ExtensionPointRegistry registry) { - UtilityExtensionPoint utilities = registry.getExtensionPoint(UtilityExtensionPoint.class); - MonitorFactory monitorFactory = utilities.getUtility(MonitorFactory.class); - monitor = monitorFactory.createMonitor(); } /** @@ -63,10 +54,10 @@ public class BindingURIBuilderImpl { * * @param composite the composite to be configured */ - protected void configureBindingURIsAndNames(Composite composite, Definitions definitions, Monitor monitor) + public Composite build(Composite composite, Definitions definitions, Monitor monitor) throws CompositeBuilderException { configureBindingURIs(composite, null, definitions, null, monitor); - configureBindingNames(composite, monitor); + return composite; } /** @@ -77,11 +68,12 @@ public class BindingURIBuilderImpl { * @param composite the composite to be configured * @param defaultBindings list of default binding configurations */ - protected void configureBindingURIs(Composite composite, - Definitions definitions, - Map> defaultBindings, - Monitor monitor) throws CompositeBuilderException { + public Composite build(Composite composite, + Definitions definitions, + Map> defaultBindings, + Monitor monitor) throws CompositeBuilderException { configureBindingURIs(composite, null, definitions, defaultBindings, monitor); + return composite; } /** @@ -137,8 +129,6 @@ public class BindingURIBuilderImpl { List compositeServices = composite.getServices(); for (Service service : compositeServices) { - constructBindingNames(service, monitor); - // Initialize binding names and URIs for (Binding binding : service.getBindings()) { constructBindingURI(parentComponentURI, composite, service, binding, defaultBindings, monitor); @@ -154,8 +144,6 @@ public class BindingURIBuilderImpl { for (ComponentService service : component.getServices()) { - constructBindingNames(service, monitor); - // Initialize binding names and URIs for (Binding binding : service.getBindings()) { constructBindingURI(component, service, binding, defaultBindings, monitor); @@ -170,114 +158,6 @@ public class BindingURIBuilderImpl { } } - /** - * Add default names for callback bindings and reference bindings. Needs to be - * separate from configureBindingURIs() because configureBindingURIs() is called - * by NodeConfigurationServiceImpl as well as by CompositeBuilderImpl. - */ - private void configureBindingNames(Composite composite, Monitor monitor) { - - // Process nested composites recursively - for (Component component : composite.getComponents()) { - - Implementation implementation = component.getImplementation(); - if (implementation instanceof Composite) { - - // Process nested composite - configureBindingNames((Composite)implementation, monitor); - } - } - - // Initialize composite service callback binding names - for (Service service : composite.getServices()) { - constructBindingNames(service, monitor); - } - - // Initialize composite reference binding names - for (Reference reference : composite.getReferences()) { - constructBindingNames(reference, monitor); - } - - // Initialize component service and reference binding names - for (Component component : composite.getComponents()) { - - // Initialize component service callback binding names - for (ComponentService service : component.getServices()) { - constructBindingNames(service, monitor); - } - - // Initialize component reference binding names - for (ComponentReference reference : component.getReferences()) { - // Initialize binding names - constructBindingNames(reference, monitor); - } - } - } - - /** - * If a binding name is not provided by the user, construct it based on the service - * or reference name - * - * @param contract the service or reference - */ - private void constructBindingNames(Contract contract, Monitor monitor) { - List bindings = contract.getBindings(); - Map bindingMap = new HashMap(); - for (Binding binding : bindings) { - // set the default binding name if one is required - // if there is no name on the binding then set it to the service or reference name - if (binding.getName() == null) { - binding.setName(contract.getName()); - } - Binding existed = bindingMap.put(binding.getName(), binding); - // Check that multiple bindings do not have the same name - if (existed != null && existed != binding) { - if (contract instanceof Service) { - Monitor.error(monitor, this, "assembly-validation-messages", "MultipleBindingsForService", contract - .getName(), binding.getName()); - } else { - Monitor.error(monitor, - this, - "assembly-validation-messages", - "MultipleBindingsForReference", - contract.getName(), - binding.getName()); - } - } - } - - if (contract.getCallback() != null) { - bindings = contract.getCallback().getBindings(); - bindingMap.clear(); - for (Binding binding : bindings) { - // set the default binding name if one is required - // if there is no name on the binding then set it to the service or reference name - if (binding.getName() == null) { - binding.setName(contract.getName()); - } - Binding existed = bindingMap.put(binding.getName(), binding); - // Check that multiple bindings do not have the same name - if (existed != null && existed != binding) { - if (contract instanceof Service) { - Monitor.error(monitor, - this, - "assembly-validation-messages", - "MultipleBindingsForServiceCallback", - contract.getName(), - binding.getName()); - } else { - Monitor.error(monitor, - this, - "assembly-validation-messages", - "MultipleBindingsForReferenceCallback", - contract.getName(), - binding.getName()); - } - } - } - } - } - /** * URI construction for composite bindings based on Assembly Specification section 1.7.2, This method * assumes that the component URI part of the binding URI is formed from the part to the @@ -506,4 +386,8 @@ public class BindingURIBuilderImpl { return URI.create(baseURI.toString() + str).normalize(); } + public String getID() { + return "org.apache.tuscany.sca.assembly.builder.BindingURIBuilder"; + } + } diff --git a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentBuilderImpl.java b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentBuilderImpl.java index eac88b88b5..1603c80eb5 100644 --- a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentBuilderImpl.java +++ b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ComponentBuilderImpl.java @@ -872,6 +872,7 @@ public class ComponentBuilderImpl { protected void createSCABinding(Contract contract, Definitions definitions) { SCABinding scaBinding = scaBindingFactory.createSCABinding(); + scaBinding.setName(contract.getName()); if (definitions != null) { for (ExtensionType attachPointType : definitions.getBindingTypes()) { diff --git a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositeComponentTypeBuilderImpl.java b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositeComponentTypeBuilderImpl.java index 69428285ce..a05a90afa2 100644 --- a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositeComponentTypeBuilderImpl.java +++ b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/CompositeComponentTypeBuilderImpl.java @@ -386,6 +386,7 @@ public class CompositeComponentTypeBuilderImpl { protected void createSCABinding(Contract contract, Definitions definitions) { SCABinding scaBinding = scaBindingFactory.createSCABinding(); + scaBinding.setName(contract.getName()); if (definitions != null) { for (ExtensionType attachPointType : definitions.getBindingTypes()) { diff --git a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ModelBuilderImpl.java b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ModelBuilderImpl.java index 43e5d76e9e..45df386aae 100644 --- a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ModelBuilderImpl.java +++ b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/ModelBuilderImpl.java @@ -37,12 +37,15 @@ public class ModelBuilderImpl implements CompositeBuilder, DeployedCompositeBuil 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; /** @@ -61,14 +64,16 @@ public class ModelBuilderImpl implements CompositeBuilder, DeployedCompositeBuil 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); - compositePolicyBuilder = new CompositePolicyBuilderImpl(registry); + policyAttachmentBuilder = new PolicyAttachmentBuilderImpl(registry); + compositePolicyBuilder = new CompositePolicyBuilderImpl(registry); } public String getID() { @@ -94,8 +99,11 @@ public class ModelBuilderImpl implements CompositeBuilder, DeployedCompositeBuil // and discards the included composite composite = compositeIncludeBuilder.build(composite, definitions, monitor); + // Set up the structural URIs for components (services/references/bindings?) + composite = structuralURIBuilder.build(composite, definitions, monitor); + // need to apply policy external attachment - + composite = policyAttachmentBuilder.build(composite, definitions, monitor); // Process the implementation hierarchy by calculating the component type // for the top level implementation (composite). This has the effect of @@ -115,7 +123,7 @@ public class ModelBuilderImpl implements CompositeBuilder, DeployedCompositeBuil // 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 - bindingURIBuilder.configureBindingURIsAndNames(composite, definitions, monitor); + composite = bindingURIBuilder.build(composite, definitions, monitor); composite = componentServiceBindingBuilder.build(composite, definitions, monitor); // binding specific build composite = componentReferenceBindingBuilder.build(composite, definitions, monitor); // binding specific build endpointBuilder.build(composite, definitions, monitor); diff --git a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java index 19dff30051..df98ec89b7 100644 --- a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java +++ b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentBuilderImpl.java @@ -23,34 +23,43 @@ 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.XMLStreamReader; +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.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.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 @@ -77,7 +86,8 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { return "org.apache.tuscany.sca.policy.builder.PolicyAttachmentBuilder"; } - public Composite build(Composite composite, Definitions definitions, Monitor monitor) throws CompositeBuilderException { + public Composite build(Composite composite, Definitions definitions, Monitor monitor) + throws CompositeBuilderException { try { Composite patched = applyXPath(composite, definitions, monitor); return patched; @@ -95,6 +105,9 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { * @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 for (Component component : composite.getComponents()) { Implementation impl = component.getImplementation(); @@ -105,16 +118,8 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { } } } - // 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); - writer.close(); - - Document document = domHelper.load(sw.toString()); + Document document = saveAsDOM(composite); - boolean changed = false; for (PolicySet ps : definitions.getPolicySets()) { // First calculate the applicable nodes Set applicableNodes = null; @@ -135,22 +140,156 @@ public class PolicyAttachmentBuilderImpl implements CompositeBuilder { Node node = nodes.item(i); if (applicableNodes == null || applicableNodes.contains(node)) { // The node can be a component, service, reference or binding - if (attach(node, ps) && !changed) { - changed = true; + String index = getStructuralURI(node); + PolicySubject subject = lookup(composite, index); + if (subject != null) { + subject.getPolicySets().add(ps); } } } } } - if (changed) { - XMLStreamReader reader = staxHelper.createXMLStreamReader(document); - reader.nextTag(); - Composite patchedComposite = (Composite)processor.read(reader); - return patchedComposite; - } else { - return composite; + 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); + 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; } /** diff --git a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/StructuralURIBuilderImpl.java b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/StructuralURIBuilderImpl.java index 04f7759493..d1942a3e4e 100644 --- a/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/StructuralURIBuilderImpl.java +++ b/java/sca/modules/builder/src/main/java/org/apache/tuscany/sca/builder/impl/StructuralURIBuilderImpl.java @@ -53,160 +53,6 @@ public class StructuralURIBuilderImpl implements CompositeBuilder, DeployedCompo public StructuralURIBuilderImpl(ExtensionPointRegistry registry) { } - /** - * Called by CompositeBindingURIBuilderImpl - * - * @param composite the composite to be configured - */ - protected void configureBindingURIsAndNames(Composite composite, Definitions definitions, Monitor monitor) - throws CompositeBuilderException { - configureBindingURIs(composite, null, definitions, null, monitor); - configureBindingNames(composite, monitor); - } - - /** - * Fully resolve the binding URIs based on available information. This includes information - * from the ".composite" files, from resources associated with the binding, e.g. WSDL files, - * from any associated policies and from the default information for each binding type. - * - * @param composite the composite to be configured - * @param bindingBaseURIs list of default binding configurations - */ - private void configureBindingURIs(Composite composite, - Definitions definitions, - Map> bindingBaseURIs, - Monitor monitor) throws CompositeBuilderException { - configureBindingURIs(composite, null, definitions, bindingBaseURIs, monitor); - } - - /** - * Fully resolve the binding URIs based on available information. This includes information - * from the ".composite" files, from resources associated with the binding, e.g. WSDL files, - * from any associated policies and from the default information for each binding type. - * - * NOTE: This method repeats some of the processing performed by the configureComponents() - * method above. The duplication is needed because NodeConfigurationServiceImpl - * calls this method without previously calling configureComponents(). In the - * normal builder sequence used by CompositeBuilderImpl, both of these methods - * are called. - * - * TODO: Share the URL calculation algorithm with the configureComponents() method above - * although keeping the configureComponents() methods signature as is because when - * a composite is actually build in a node the node default information is currently - * available - * - * @param composite the composite to be configured - * @param uri the path to the composite provided through any nested composite component implementations - * @param defaultBindings list of default binding configurations - */ - private void configureBindingURIs(Composite composite, - String parentComponentURI, - Definitions definitions, - Map> defaultBindings, - Monitor monitor) throws CompositeBuilderException { - - monitor.pushContext("Composite: " + composite.getName().toString()); - try { - // Process nested composites recursively - for (Component component : composite.getComponents()) { - - // Initialize component URI - String componentURI; - if (parentComponentURI == null) { - componentURI = component.getName(); - } else { - componentURI = parentComponentURI + '/' + component.getName(); - } - component.setURI(componentURI); - - Implementation implementation = component.getImplementation(); - if (implementation instanceof Composite) { - // Process nested composite - configureBindingURIs((Composite)implementation, componentURI, definitions, defaultBindings, monitor); - } - } - - // Initialize composite service binding URIs - List compositeServices = composite.getServices(); - for (Service service : compositeServices) { - - constructBindingNames(service, monitor); - - // Initialize binding names and URIs - for (Binding binding : service.getBindings()) { - constructBindingURI(parentComponentURI, composite, service, binding, defaultBindings, monitor); - } - } - - // Initialize component service binding URIs - for (Component component : composite.getComponents()) { - - monitor.pushContext("Component: " + component.getName()); - - try { - - for (ComponentService service : component.getServices()) { - - constructBindingNames(service, monitor); - - // Initialize binding names and URIs - for (Binding binding : service.getBindings()) { - constructBindingURI(component.getURI(), service, binding, defaultBindings, monitor); - } - } - } finally { - monitor.popContext(); - } - } - } finally { - monitor.popContext(); - } - } - - /** - * Add default names for callback bindings and reference bindings. Needs to be - * separate from configureBindingURIs() because configureBindingURIs() is called - * by NodeConfigurationServiceImpl as well as by CompositeBuilderImpl. - */ - private void configureBindingNames(Composite composite, Monitor monitor) { - - // Process nested composites recursively - for (Component component : composite.getComponents()) { - - Implementation implementation = component.getImplementation(); - if (implementation instanceof Composite) { - - // Process nested composite - configureBindingNames((Composite)implementation, monitor); - } - } - - // Initialize composite service callback binding names - for (Service service : composite.getServices()) { - constructBindingNames(service, monitor); - } - - // Initialize composite reference binding names - for (Reference reference : composite.getReferences()) { - constructBindingNames(reference, monitor); - } - - // Initialize component service and reference binding names - for (Component component : composite.getComponents()) { - - // Initialize component service callback binding names - for (ComponentService service : component.getServices()) { - constructBindingNames(service, monitor); - } - - // Initialize component reference binding names - for (ComponentReference reference : component.getReferences()) { - // Initialize binding names - constructBindingNames(reference, monitor); - } - } - } - /** * If a binding name is not provided by the user, construct it based on the service * or reference name @@ -271,29 +117,6 @@ public class StructuralURIBuilderImpl implements CompositeBuilder, DeployedCompo } } - /** - * 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> defaultBindings, - Monitor monitor) throws CompositeBuilderException { - // This is a composite service so there is no component to provide a component URI - // The path to this composite (through nested composites) is used. - constructBindingURI(parentComponentURI, service, binding, defaultBindings, monitor); - } - /** * Generic URI construction for bindings based on Assembly Specification section 1.7.2 * @@ -494,7 +317,7 @@ public class StructuralURIBuilderImpl implements CompositeBuilder, DeployedCompo Definitions definitions, Map> bindingBaseURIs, Monitor monitor) throws CompositeBuilderException { - configureBindingURIs(composite, definitions, bindingBaseURIs, monitor); + configureStructuralURIs(composite, null, definitions, bindingBaseURIs, monitor); return composite; } @@ -531,10 +354,12 @@ public class StructuralURIBuilderImpl implements CompositeBuilder, DeployedCompo 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); diff --git a/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentTestCase.java b/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentTestCase.java index 075dac51a1..76b6c8b1f1 100644 --- a/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentTestCase.java +++ b/java/sca/modules/builder/src/test/java/org/apache/tuscany/sca/builder/impl/PolicyAttachmentTestCase.java @@ -33,10 +33,6 @@ import org.apache.tuscany.sca.assembly.Base; import org.apache.tuscany.sca.assembly.Composite; import org.apache.tuscany.sca.assembly.builder.BuilderExtensionPoint; import org.apache.tuscany.sca.assembly.builder.CompositeBuilder; -import org.apache.tuscany.sca.builder.impl.CompositeCloneBuilderImpl; -import org.apache.tuscany.sca.builder.impl.CompositeIncludeBuilderImpl; -import org.apache.tuscany.sca.builder.impl.PolicyAttachmentBuilderImpl; -import org.apache.tuscany.sca.builder.impl.StructuralURIBuilderImpl; import org.apache.tuscany.sca.contribution.processor.ContributionReadException; import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; @@ -92,7 +88,10 @@ public class PolicyAttachmentTestCase { public void testBuild() throws Exception { Definitions definitions = load("test_definitions.xml"); Composite composite = load("Calculator.composite"); + + CompositeBuilder uriBuilder = new StructuralURIBuilderImpl(extensionPoints); + composite = uriBuilder.build(composite, definitions, monitor); PolicyAttachmentBuilderImpl builder = new PolicyAttachmentBuilderImpl(extensionPoints); builder.build(composite, definitions, monitor); } @@ -131,13 +130,6 @@ public class PolicyAttachmentTestCase { CompositeBuilder cloneBuilder = new CompositeCloneBuilderImpl(); CompositeBuilder uriBuilder = new StructuralURIBuilderImpl(extensionPoints); - /* - CompositeBuilder includeBuilder = - builders.getCompositeBuilder("org.apache.tuscany.sca.assembly.builder.CompositeIncludeBuilder"); - CompositeBuilder cloneBuilder = - builders.getCompositeBuilder("org.apache.tuscany.sca.assembly.builder.CompositeCloneBuilder"); - */ - domainComposite = cloneBuilder.build(domainComposite, definitions, monitor); domainComposite = includeBuilder.build(domainComposite, definitions, monitor); domainComposite = uriBuilder.build(domainComposite, definitions, monitor); diff --git a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/stax/impl/XMLStreamSerializer.java b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/stax/impl/XMLStreamSerializer.java index 5ef5a03d9f..883199e4dd 100644 --- a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/stax/impl/XMLStreamSerializer.java +++ b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/stax/impl/XMLStreamSerializer.java @@ -22,6 +22,7 @@ package org.apache.tuscany.sca.common.xml.stax.impl; import static javax.xml.XMLConstants.DEFAULT_NS_PREFIX; import static javax.xml.XMLConstants.NULL_NS_URI; +import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamConstants; @@ -120,10 +121,10 @@ public class XMLStreamSerializer implements XMLStreamConstants { } public String writeAttribute(XMLStreamWriter writer, - String prefix, - String localName, - String namespaceURI, - String value) throws XMLStreamException { + String prefix, + String localName, + String namespaceURI, + String value) throws XMLStreamException { String writerPrefix; /* * Due to parser implementations returning null as the namespace URI (for the empty namespace) we need to @@ -231,17 +232,17 @@ public class XMLStreamSerializer implements XMLStreamConstants { public void writeStartElement(XMLStreamWriter writer, QName name) throws XMLStreamException { writeStartElement(writer, name.getPrefix(), name.getLocalPart(), name.getNamespaceURI()); } - + public void writeStartElement(XMLStreamWriter writer, String prefix, String localName, String namespaceURI) throws XMLStreamException { - + if (namespaceURI == null) { namespaceURI = NULL_NS_URI; } if (prefix == null) { prefix = DEFAULT_NS_PREFIX; } - + if (isRepairingNamespaces) { writer.writeStartElement(prefix, localName, namespaceURI); return; @@ -298,6 +299,12 @@ public class XMLStreamSerializer implements XMLStreamConstants { if (prefix == null) { prefix = DEFAULT_NS_PREFIX; } + if (DEFAULT_NS_PREFIX.equals(prefix) && !XMLConstants.NULL_NS_URI.equals(uri)) { + String ns = writer.getNamespaceContext().getNamespaceURI(prefix); + if (ns != null) { + prefix = generateUniquePrefix(writer.getNamespaceContext()); + } + } writer.writeNamespace(prefix, uri); return prefix; } else { diff --git a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/xpath/XPathHelper.java b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/xpath/XPathHelper.java index 85306622b2..a65bd5fe15 100644 --- a/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/xpath/XPathHelper.java +++ b/java/sca/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/xpath/XPathHelper.java @@ -102,6 +102,7 @@ public class XPathHelper { */ private Collection getPrefixes(String expression) { Collection prefixes = new HashSet(); + prefixes.add(XMLConstants.DEFAULT_NS_PREFIX); Pattern pattern = Pattern.compile("([^:]+):([^:]+)"); Matcher matcher = pattern.matcher(expression); while (matcher.find()) { diff --git a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/BaseStAXArtifactProcessor.java b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/BaseStAXArtifactProcessor.java index a11a69867b..bae566639a 100644 --- a/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/BaseStAXArtifactProcessor.java +++ b/java/sca/modules/contribution/src/main/java/org/apache/tuscany/sca/contribution/processor/BaseStAXArtifactProcessor.java @@ -479,7 +479,7 @@ public abstract class BaseStAXArtifactProcessor { } str = String.valueOf(value); } - if (str.length() == 0) { + if (str.length() == 0 && (value instanceof Collection)) { return; } -- cgit v1.2.3