From 132aa8a77685ec92bc90c03f987650d275a7b639 Mon Sep 17 00:00:00 2001 From: lresende Date: Mon, 30 Sep 2013 06:59:11 +0000 Subject: 2.0.1 RC1 release tag git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1527464 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/assembly/xml/BaseAssemblyProcessor.java | 742 +++++++++++ .../xml/ComponentTypeDocumentProcessor.java | 129 ++ .../assembly/xml/ComponentTypeModelResolver.java | 96 ++ .../sca/assembly/xml/ComponentTypeProcessor.java | 470 +++++++ .../assembly/xml/CompositeDocumentProcessor.java | 198 +++ .../sca/assembly/xml/CompositeModelResolver.java | 96 ++ .../sca/assembly/xml/CompositeProcessor.java | 1289 ++++++++++++++++++++ .../assembly/xml/ConfiguredOperationProcessor.java | 108 ++ .../apache/tuscany/sca/assembly/xml/Constants.java | 181 +++ .../assembly/xml/DefaultBeanModelProcessor.java | 257 ++++ .../sca/assembly/xml/EndpointProcessor.java | 185 +++ .../assembly/xml/EndpointReferenceProcessor.java | 151 +++ .../assembly/xml/InterfaceContractProcessor.java | 373 ++++++ .../apache/tuscany/sca/assembly/xml/Messages.java | 28 + .../sca/assembly/xml/PolicySubjectProcessor.java | 242 ++++ .../sca/assembly/xml/SCABindingProcessor.java | 178 +++ .../org/apache/tuscany/sca/assembly/xml/Utils.java | 86 ++ .../xml/DefaultDefinitionsExtensionPoint.java | 135 ++ .../xml/DefinitionsDocumentProcessor.java | 171 +++ .../definitions/xml/DefinitionsExtensionPoint.java | 64 + .../sca/definitions/xml/DefinitionsProcessor.java | 299 +++++ .../tuscany/sca/definitions/xml/Messages.java | 27 + .../sca/policy/xml/BindingTypeProcessor.java | 65 + .../sca/policy/xml/ExtensionTypeProcessor.java | 250 ++++ .../policy/xml/ExternalAttachmentProcessor.java | 294 +++++ .../policy/xml/ImplementationTypeProcessor.java | 65 + .../tuscany/sca/policy/xml/IntentProcessor.java | 446 +++++++ .../apache/tuscany/sca/policy/xml/Messages.java | 27 + .../tuscany/sca/policy/xml/PolicyConstants.java | 76 ++ .../tuscany/sca/policy/xml/PolicySetProcessor.java | 577 +++++++++ .../sca/policy/xml/PolicyXPathFunction.java | 295 +++++ .../policy/xml/PolicyXPathFunctionResolver.java | 72 ++ 32 files changed, 7672 insertions(+) create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeDocumentProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeModelResolver.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeDocumentProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeModelResolver.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConfiguredOperationProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Constants.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/DefaultBeanModelProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/EndpointProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/EndpointReferenceProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/InterfaceContractProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Messages.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/PolicySubjectProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/SCABindingProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Utils.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefaultDefinitionsExtensionPoint.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsDocumentProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsExtensionPoint.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/Messages.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/BindingTypeProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/ExtensionTypeProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/ExternalAttachmentProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/ImplementationTypeProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/IntentProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/Messages.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyConstants.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicySetProcessor.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java (limited to 'sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache') diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java new file mode 100644 index 0000000000..a1e24f1494 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseAssemblyProcessor.java @@ -0,0 +1,742 @@ +/* + * 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.assembly.xml; + +import static javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI; +import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI; +import static javax.xml.stream.XMLStreamConstants.CDATA; +import static javax.xml.stream.XMLStreamConstants.CHARACTERS; +import static javax.xml.stream.XMLStreamConstants.COMMENT; +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; +import static org.apache.tuscany.sca.assembly.xml.Constants.ELEMENT; +import static org.apache.tuscany.sca.assembly.xml.Constants.MANY; +import static org.apache.tuscany.sca.assembly.xml.Constants.MULTIPLICITY; +import static org.apache.tuscany.sca.assembly.xml.Constants.MUST_SUPPLY; +import static org.apache.tuscany.sca.assembly.xml.Constants.NAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.ONE_N; +import static org.apache.tuscany.sca.assembly.xml.Constants.ONE_ONE; +import static org.apache.tuscany.sca.assembly.xml.Constants.PROPERTY; +import static org.apache.tuscany.sca.assembly.xml.Constants.PROPERTY_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.SCA11_NS; +import static org.apache.tuscany.sca.assembly.xml.Constants.TARGET; +import static org.apache.tuscany.sca.assembly.xml.Constants.TYPE; +import static org.apache.tuscany.sca.assembly.xml.Constants.VALUE; +import static org.apache.tuscany.sca.assembly.xml.Constants.VALUE_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.ZERO_N; +import static org.apache.tuscany.sca.assembly.xml.Constants.ZERO_ONE; + +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +import javax.xml.namespace.QName; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.assembly.AbstractContract; +import org.apache.tuscany.sca.assembly.AbstractProperty; +import org.apache.tuscany.sca.assembly.AbstractReference; +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Base; +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.assembly.Contract; +import org.apache.tuscany.sca.assembly.Extensible; +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.Service; +import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.StAXAttributeProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +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.PolicyFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +/** + * A base class with utility methods for the other artifact processors in this module. + * + * @version $Rev$ $Date$ + */ +abstract class BaseAssemblyProcessor extends BaseStAXArtifactProcessor { + + protected AssemblyFactory assemblyFactory; + protected PolicyFactory policyFactory; + protected StAXArtifactProcessor extensionProcessor; + protected PolicySubjectProcessor policyProcessor; + private DocumentBuilderFactory documentBuilderFactory; + + + /** + * Constructs a new BaseArtifactProcessor. + * @param assemblyFactory + * @param policyFactory + */ + @SuppressWarnings("unchecked") + protected BaseAssemblyProcessor(AssemblyFactory assemblyFactory, + PolicyFactory policyFactory, + DocumentBuilderFactory documentBuilderFactory, + StAXArtifactProcessor extensionProcessor) { + this.assemblyFactory = assemblyFactory; + this.policyFactory = policyFactory; + this.documentBuilderFactory = documentBuilderFactory; + this.extensionProcessor = (StAXArtifactProcessor)extensionProcessor; + this.policyProcessor = new PolicySubjectProcessor(policyFactory); + } + + /** + * @param modelFactories + * @param staxProcessor + * @param monitor + */ + protected BaseAssemblyProcessor(FactoryExtensionPoint modelFactories, + StAXArtifactProcessor staxProcessor) { + this.assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); + this.policyFactory = modelFactories.getFactory(PolicyFactory.class); + this.documentBuilderFactory = modelFactories.getFactory(DocumentBuilderFactory.class); + this.extensionProcessor = (StAXArtifactProcessor)staxProcessor; + this.policyProcessor = new PolicySubjectProcessor(policyFactory); + } + + /** + * Marshals warnings into the monitor + * + * @param message + * @param model + * @param messageParameters + */ + protected void warning(Monitor monitor, String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = + monitor.createProblem(this.getClass().getName(), + Messages.RESOURCE_BUNDLE, + Severity.WARNING, + model, + message, + (Object[])messageParameters); + monitor.problem(problem); + } + } + + /** + * Marshals errors into the 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.RESOURCE_BUNDLE, + Severity.ERROR, + model, + message, + (Object[])messageParameters); + monitor.problem(problem); + } + } + + /** + * Marshals exceptions into the monitor + * + * @param problems + * @param message + * @param model + */ + protected void error(Monitor monitor, String message, Object model, Exception ex) { + if (monitor != null) { + Problem problem = + monitor.createProblem(this.getClass().getName(), + Messages.RESOURCE_BUNDLE, + Severity.ERROR, + model, + message, + ex); + monitor.problem(problem); + } + } + + /** + * Start an element. + * @param writer + * @param name + * @param attrs + * @throws XMLStreamException + */ + protected void writeStart(XMLStreamWriter writer, String name, XAttr... attrs) throws XMLStreamException { + super.writeStart(writer, SCA11_NS, name, attrs); + } + + /** + * Start a document. + * @param writer + * @throws XMLStreamException + */ + protected void writeStartDocument(XMLStreamWriter writer, String name, XAttr... attrs) throws XMLStreamException { + super.writeStartDocument(writer, SCA11_NS, name, attrs); + } + + /** + * Read list of reference targets + * @param reference + * @param reader + */ + protected void readTargets(Reference reference, XMLStreamReader reader) { + String value = getString(reader, TARGET); + ComponentService target = null; + if (value != null) { + for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) { + target = assemblyFactory.createComponentService(); + target.setUnresolved(true); + target.setName(tokens.nextToken()); + reference.getTargets().add(target); + } + } + } + + /** + * Write a list of targets into an attribute + * @param reference + * @return + */ + protected XAttr writeTargets(Reference reference) { + List targets = new ArrayList(); + for (Service target : reference.getTargets()) { + targets.add(target.getName()); + } + return new XAttr(TARGET, targets); + } + + /** + * Read a multiplicity attribute. + * @param reference + * @param reader + */ + protected void readMultiplicity(AbstractReference reference, XMLStreamReader reader) { + String value = reader.getAttributeValue(null, MULTIPLICITY); + if (ZERO_ONE.equals(value)) { + reference.setMultiplicity(Multiplicity.ZERO_ONE); + } else if (ONE_N.equals(value)) { + reference.setMultiplicity(Multiplicity.ONE_N); + } else if (ZERO_N.equals(value)) { + reference.setMultiplicity(Multiplicity.ZERO_N); + } else if (ONE_ONE.equals(value)) { + reference.setMultiplicity(Multiplicity.ONE_ONE); + } + } + + protected XAttr writeMultiplicity(AbstractReference reference) { + Multiplicity multiplicity = reference.getMultiplicity(); + if (multiplicity != null) { + String value = null; + if (Multiplicity.ZERO_ONE.equals(multiplicity)) { + value = ZERO_ONE; + } else if (Multiplicity.ONE_N.equals(multiplicity)) { + value = ONE_N; + } else if (Multiplicity.ZERO_N.equals(multiplicity)) { + value = ZERO_N; + } else if (Multiplicity.ONE_ONE.equals(multiplicity)) { + value = ONE_ONE; + return null; + } + return new XAttr(MULTIPLICITY, value); + } + return null; + } + + + /** + * Reads an abstract property element. + * @param property + * @param reader + * @param context TODO + * @throws XMLStreamException + * @throws ContributionReadException + */ + protected void readAbstractProperty(AbstractProperty property, XMLStreamReader reader, ProcessorContext context) throws XMLStreamException, + ContributionReadException { + + property.setName(getString(reader, NAME)); + property.setMany(getBoolean(reader, MANY)); + property.setMustSupply(getBoolean(reader, MUST_SUPPLY)); + property.setXSDElement(getQName(reader, ELEMENT)); + property.setXSDType(getQName(reader, TYPE)); + // MJE added 14/05/2009 - check for both @element and @type being present - disallowed by OASIS Assembly spec + if( property.getXSDElement() != null && property.getXSDType() != null ) { + // TUSCANY-4020 - should get rid of the exception but that would mean getting OASIS to change the + // expected strings again + ContributionReadException ce = new ContributionReadException(context.getMonitor().getMessageString(BaseAssemblyProcessor.class.getName(), + Messages.RESOURCE_BUNDLE, + "BothTypeAndElementAttributeFound") + + " - " + + property.getName()); + error(context.getMonitor(), "ContributionReadException", property, ce); + } // end if + + } + + /** + * Resolve an implementation. + * @param implementation + * @param resolver + * @param context + * @return + * @throws ContributionResolveException + */ + protected Implementation resolveImplementation(Implementation implementation, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + if (implementation != null) { + if (implementation.isUnresolved()) { + implementation = resolver.resolveModel(Implementation.class, implementation, context); + + // Lazily resolve implementations + if (implementation.isUnresolved()) { + extensionProcessor.resolve(implementation, resolver, context); + if (!implementation.isUnresolved()) { + resolver.addModel(implementation, context); + } + } + } + } + return implementation; + } + + /** + * Resolve interface, callback interface and bindings on a list of contracts. + * @param contracts the list of contracts + * @param resolver the resolver to use to resolve models + * @param context TODO + */ + protected void resolveContracts(List contracts, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + resolveContracts(null, contracts, resolver, context); + } + + /** + * Resolve interface, callback interface and bindings on a list of contracts. + * @param parent element for the contracts + * @param contracts the list of contracts + * @param resolver the resolver to use to resolve models + * @param context TODO + */ + protected void resolveContracts(Base parent, List contracts, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + + String parentName = + (parent instanceof Composite) ? ((Composite)parent).getName().toString() : (parent instanceof Component) + ? ((Component)parent).getName() : "UNKNOWN"; + + for (Contract contract : contracts) { + // Resolve the interface contract + InterfaceContract interfaceContract = contract.getInterfaceContract(); + if (interfaceContract != null) { + extensionProcessor.resolve(interfaceContract, resolver, context); + } + + // Resolve bindings + for (int i = 0, n = contract.getBindings().size(); i < n; i++) { + Binding binding = contract.getBindings().get(i); + extensionProcessor.resolve(binding, resolver, context); + + } + + // Resolve callback bindings + if (contract.getCallback() != null) { + + for (int i = 0, n = contract.getCallback().getBindings().size(); i < n; i++) { + Binding binding = contract.getCallback().getBindings().get(i); + extensionProcessor.resolve(binding, resolver, context); + } + } + } + } + + /* + * Resolve policy that's been attached to interface operations + */ + protected void resolveContractOperationPolicy(List contracts, ModelResolver resolver, ProcessorContext context){ + if (contracts != null){ + for (Contract contract : contracts) { + InterfaceContract interfaceContract = contract.getInterfaceContract(); + if (interfaceContract != null) { + + // Resolve any policy on the interface operations + if (interfaceContract.getInterface() != null){ + for (Operation op : interfaceContract.getInterface().getOperations()){ + policyProcessor.resolvePolicies(op, resolver, context); + } + } + + if (interfaceContract.getCallbackInterface() != null){ + for (Operation op : interfaceContract.getCallbackInterface().getOperations()){ + policyProcessor.resolvePolicies(op, resolver, context); + } + } + } + } + } + } + + /** + * Resolve interface and callback interface on a list of abstract contracts. + * @param contracts the list of contracts + * @param resolver the resolver to use to resolve models + * @param context TODO + */ + protected void resolveAbstractContracts(List contracts, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + for (AbstractContract contract : contracts) { + + // Resolve the interface contract + InterfaceContract interfaceContract = contract.getInterfaceContract(); + if (interfaceContract != null) { + extensionProcessor.resolve(interfaceContract, resolver, context); + } + } + } + + /** + * Read a property value into a DOM document. + * @param element + * @param type + * @param reader + * @param context + * @return + * @throws XMLStreamException + * @throws ContributionReadException + * @throws ParserConfigurationException + */ + protected Document readPropertyValue(QName element, QName type, boolean isMany, XMLStreamReader reader, ProcessorContext context) throws XMLStreamException, + ContributionReadException { + Document document; + try { + if (documentBuilderFactory == null) { + documentBuilderFactory = DocumentBuilderFactory.newInstance(); + documentBuilderFactory.setNamespaceAware(true); + } + document = documentBuilderFactory.newDocumentBuilder().newDocument(); + } catch (ParserConfigurationException e) { + ContributionReadException ce = new ContributionReadException(e); + error(context.getMonitor(), "ContributionReadException", documentBuilderFactory, ce); + throw ce; + } + + // Collect the property values as elements under the + Element root = document.createElementNS(SCA11_NS, "sca:" + PROPERTY); + String nameAttr = getString(reader, NAME); + if (nameAttr != null) { + root.setAttributeNS(SCA11_NS, "sca:" + NAME, nameAttr); + } + declareNamespace(root, "sca", SCA11_NS); + if (type != null) { + org.w3c.dom.Attr xsi = document.createAttributeNS(XMLNS_ATTRIBUTE_NS_URI, "xmlns:xsi"); + xsi.setValue(W3C_XML_SCHEMA_INSTANCE_NS_URI); + root.setAttributeNodeNS(xsi); + + String prefix = type.getPrefix(); + if (prefix == null || prefix.length() == 0) { + prefix = "ns"; + } + + declareNamespace(root, prefix, type.getNamespaceURI()); + + org.w3c.dom.Attr xsiType = document.createAttributeNS(W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi:type"); + xsiType.setValue(prefix + ":" + type.getLocalPart()); + root.setAttributeNodeNS(xsiType); + } + document.appendChild(root); + + // Start to parse the property + QName name = reader.getName(); // Should be sca:property + + // SCA 1.1 supports the @value for simple types + String valueAttr = getString(reader, VALUE); + if (valueAttr != null) { + Element valueElement = document.createElementNS(SCA11_NS, VALUE); + root.appendChild(valueElement); + valueElement.setTextContent(valueAttr); + } + + boolean gotOneValue = false; + boolean isTextForProperty = true; + StringBuffer text = new StringBuffer(); + + int event = reader.getEventType(); + while (true) { + switch (event) { + case START_ELEMENT: + name = reader.getName(); + if (PROPERTY_QNAME.equals(name)) { + isTextForProperty = true; + break; + } + isTextForProperty = false; + + // CONFORMANCE: ASM50033 + // A property subelement MUST NOT be used when the @value attribute is used + // to specify the value for that property. + if (valueAttr != null) { + error(context.getMonitor(), "ValueAttributeForPropertyElement", element, nameAttr); + } + // Read + if (VALUE_QNAME.equals(name)) { + if (gotOneValue && !isMany) { + // TODO: TUSCANY-3231 this should be error not warning but that breaks OASIS tests + // [rfeng] We should not issue warning here as the component property many inherit @many from the componentType property + // warning(context.getMonitor(), "ASM50032: multiple value elements for single-valued property", name, name); + } + loadElement(reader, root); + gotOneValue = true; + } else { + // Global elements + loadElement(reader, root); + } + break; + case XMLStreamConstants.CHARACTERS: + case XMLStreamConstants.CDATA: + if (isTextForProperty) { + text.append(reader.getText()); + } + break; + case END_ELEMENT: + name = reader.getName(); + if (PROPERTY_QNAME.equals(name)) { + + if (root.getChildNodes().getLength() == 0) { + // Add an text as an + if (isTextForProperty){ + if (text.length() > 0) { + Element valueElement = document.createElementNS(SCA11_NS, VALUE); + root.appendChild(valueElement); + valueElement.setTextContent(text.toString()); + } + } + } + return document; + } + break; + } + if (reader.hasNext()) { + event = reader.next(); + } else { + break; + } + } + return document; + } + + /** + * Create a DOM element + * @param document + * @param name + * @return + */ + private Element createElement(Document document, QName name) { + String prefix = name.getPrefix(); + String qname = + (prefix != null && prefix.length() > 0) ? prefix + ":" + name.getLocalPart() : name.getLocalPart(); + return document.createElementNS(name.getNamespaceURI(), qname); + } + + /** + * Declare a namespace. + * @param element + * @param prefix + * @param ns + */ + private void declareNamespace(Element element, String prefix, String ns) { + if (ns == null) { + ns = ""; + } + if (prefix == null) { + prefix = ""; + } + String qname = null; + if ("".equals(prefix)) { + qname = "xmlns"; + } else { + qname = "xmlns:" + prefix; + } + Node node = element; + boolean declared = false; + while (node != null && node.getNodeType() == Node.ELEMENT_NODE) { + NamedNodeMap attrs = node.getAttributes(); + if (attrs == null) { + break; + } + Node attr = attrs.getNamedItem(qname); + if (attr != null) { + declared = ns.equals(attr.getNodeValue()); + break; + } + node = node.getParentNode(); + } + if (!declared) { + org.w3c.dom.Attr attr = element.getOwnerDocument().createAttributeNS(XMLNS_ATTRIBUTE_NS_URI, qname); + attr.setValue(ns); + element.setAttributeNodeNS(attr); + } + } + + /** + * Load a property value specification from an StAX stream into a DOM + * Document. Only elements, text and attributes are processed; all comments + * and other whitespace are ignored. + * + * @param reader the stream to read from + * @param root the DOM node to load + * @throws javax.xml.stream.XMLStreamException + */ + private void loadElement(XMLStreamReader reader, Element root) throws XMLStreamException { + Document document = root.getOwnerDocument(); + Node current = root; + while (true) { + switch (reader.getEventType()) { + case START_ELEMENT: + QName name = reader.getName(); + Element child = createElement(document, name); + + // push the new element and make it the current one + current.appendChild(child); + current = child; + + int count = reader.getNamespaceCount(); + for (int i = 0; i < count; i++) { + String prefix = reader.getNamespacePrefix(i); + String ns = reader.getNamespaceURI(i); + declareNamespace(child, prefix, ns); + } + + if (!"".equals(name.getNamespaceURI())) { + declareNamespace(child, name.getPrefix(), name.getNamespaceURI()); + } + + // add the attributes for this element + count = reader.getAttributeCount(); + for (int i = 0; i < count; i++) { + String ns = reader.getAttributeNamespace(i); + String prefix = reader.getAttributePrefix(i); + String qname = reader.getAttributeLocalName(i); + String value = reader.getAttributeValue(i); + if (prefix != null && prefix.length() != 0) { + qname = prefix + ":" + qname; + } + child.setAttributeNS(ns, qname, value); + if (ns != null) { + declareNamespace(child, prefix, ns); + } + } + + break; + case CDATA: + current.appendChild(document.createCDATASection(reader.getText())); + break; + case CHARACTERS: + current.appendChild(document.createTextNode(reader.getText())); + break; + case COMMENT: + current.appendChild(document.createComment(reader.getText())); + break; + case END_ELEMENT: + // pop the element off the stack + current = current.getParentNode(); + // if we are back at the root then we are done + if (current == root) { + return; + } + + } + if (reader.hasNext()) { + reader.next(); + } else { + return; + } + } + } + + /** + * + * @param reader + * @param elementName + * @param extensionAttributeProcessor + * @param context TODO + * @param estensibleElement + * @throws ContributionReadException + * @throws XMLStreamException + */ + protected void readExtendedAttributes(XMLStreamReader reader, + QName elementName, + Extensible extensible, + StAXAttributeProcessor extensionAttributeProcessor, + ProcessorContext context) throws ContributionReadException, + XMLStreamException { + super.readExtendedAttributes(reader, extensible, extensionAttributeProcessor, assemblyFactory, context); + } + + + /*protected void validatePolicySets(PolicySubject policySetAttachPoint) + throws ContributionResolveException { + validatePolicySets(policySetAttachPoint, policySetAttachPoint.getApplicablePolicySets()); + } + + + protected void validatePolicySets(PolicySubject policySetAttachPoint, + List applicablePolicySets) throws ContributionResolveException { + //Since the applicablePolicySets in a policySetAttachPoint will already have the + //list of policysets that might ever be applicable to this attachPoint, just check + //if the defined policysets feature in the list of applicable policysets + ExtensionType attachPointType = policySetAttachPoint.getType(); + for ( PolicySet definedPolicySet : policySetAttachPoint.getPolicySets() ) { + if ( !definedPolicySet.isUnresolved() ) { + if ( !applicablePolicySets.contains(definedPolicySet)) { + throw new ContributionResolveException("Policy Set '" + definedPolicySet.getName() + + "' does not apply to binding type " + + attachPointType.getName()); + } + } else { + throw new ContributionResolveException("Policy Set '" + definedPolicySet.getName() + + "' is not defined in this domain "); + + + } + } + }*/ +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeDocumentProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeDocumentProcessor.java new file mode 100644 index 0000000000..b5fe005812 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeDocumentProcessor.java @@ -0,0 +1,129 @@ +/* + * 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.assembly.xml; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URL; + +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.assembly.ComponentType; +import org.apache.tuscany.sca.common.java.io.IOHelper; +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ValidatingXMLInputFactory; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.monitor.Monitor; + +/** + * A componentType processor. + * + * @version $Rev$ $Date$ + */ +public class ComponentTypeDocumentProcessor extends BaseAssemblyProcessor implements URLArtifactProcessor { + private ValidatingXMLInputFactory inputFactory; + + /** + * Constructs a new componentType processor. + * @param modelFactories + * @param staxProcessor + */ + public ComponentTypeDocumentProcessor(FactoryExtensionPoint modelFactories, + StAXArtifactProcessor staxProcessor) { + super(modelFactories, staxProcessor); + this.inputFactory = modelFactories.getFactory(ValidatingXMLInputFactory.class); + } + + public ComponentType read(URL contributionURL, URI uri, URL url, ProcessorContext context) throws ContributionReadException { + InputStream urlStream = null; + Monitor monitor = context.getMonitor(); + try { + + // Create a stream reader + urlStream = IOHelper.openStream(url); + XMLStreamReader reader = inputFactory.createXMLStreamReader(url.toString(), urlStream); + ValidatingXMLInputFactory.setMonitor(reader, monitor); + reader.nextTag(); + + // Reader the componentType model + ComponentType componentType = (ComponentType)extensionProcessor.read(reader, context); + if (componentType != null) { + componentType.setURI(uri.toString()); + } + + // For debugging purposes, write it back to XML +// if (componentType != null) { +// try { +// ByteArrayOutputStream bos = new ByteArrayOutputStream(); +// XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); +// outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); +// extensionProcessor.write(componentType, outputFactory.createXMLStreamWriter(bos)); +// Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(bos.toByteArray())); +// OutputFormat format = new OutputFormat(); +// format.setIndenting(true); +// format.setIndent(2); +// XMLSerializer serializer = new XMLSerializer(System.out, format); +// serializer.serialize(document); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } + + return componentType; + + } catch (XMLStreamException e) { + ContributionReadException ce = new ContributionReadException(e); + error(monitor, "ContributionReadException", inputFactory, ce); + throw ce; + } catch (IOException e) { + ContributionReadException ce = new ContributionReadException(e); + error(monitor, "ContributionReadException", inputFactory, ce); + throw ce; + } finally { + try { + if (urlStream != null) { + urlStream.close(); + urlStream = null; + } + } catch (IOException ioe) { + //ignore + } + } + } + + public void resolve(ComponentType componentType, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + extensionProcessor.resolve(componentType, resolver, context); + } + + public String getArtifactType() { + return ".componentType"; + } + + public Class getModelType() { + return ComponentType.class; + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeModelResolver.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeModelResolver.java new file mode 100644 index 0000000000..9a4a0dc035 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeModelResolver.java @@ -0,0 +1,96 @@ +/* + * 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.assembly.xml; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.sca.assembly.ComponentType; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.Import; +import org.apache.tuscany.sca.contribution.java.JavaImport; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; + +/** + * A Model Resolver for ComponentType models. + * + * @version $Rev$ $Date$ + */ +public class ComponentTypeModelResolver implements ModelResolver { + private Contribution contribution; + private Map map = new HashMap(); + + public ComponentTypeModelResolver(Contribution contribution, FactoryExtensionPoint modelFactories) { + this.contribution = contribution; + } + + public void addModel(Object resolved, ProcessorContext context) { + ComponentType componentType = (ComponentType)resolved; + map.put(componentType.getURI(), componentType); + } + + public Object removeModel(Object resolved, ProcessorContext context) { + return map.remove(((ComponentType)resolved).getURI()); + } + + public T resolveModel(Class modelClass, T unresolved, ProcessorContext context) { + + //get componentType artifact URI + String uri = ((ComponentType)unresolved).getURI(); + if (uri == null) { + return (T)unresolved; + } + + //lookup the componentType + ComponentType resolved = (ComponentType) map.get(uri); + if (resolved != null) { + return modelClass.cast(resolved); + } + + //If not found, delegate the resolution to the imports (in this case based on the java imports) + //compute the package name from the componentType URI + if (unresolved instanceof ComponentType) { + //FIXME The core assembly model now depends on java imports to + // resolve componentTypes of all kinds, this is not right at all!!! + int s = uri.lastIndexOf('/'); + if (s != -1) { + String packageName = uri.substring(0, uri.lastIndexOf("/")); + for (Import import_ : this.contribution.getImports()) { + if (import_ instanceof JavaImport) { + JavaImport javaImport = (JavaImport)import_; + //check the import location against the computed package name from the componentType URI + if (javaImport.getPackage().equals(packageName)) { + // Delegate the resolution to the import resolver + resolved = javaImport.getModelResolver().resolveModel(ComponentType.class, (ComponentType)unresolved, context); + if (!resolved.isUnresolved()) { + return modelClass.cast(resolved); + } + } + } + } + } + } + + return (T)unresolved; + } + +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java new file mode 100644 index 0000000000..646b5b273b --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java @@ -0,0 +1,470 @@ +/* + * 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.assembly.xml; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; +import static org.apache.tuscany.sca.assembly.xml.Constants.CALLBACK; +import static org.apache.tuscany.sca.assembly.xml.Constants.CALLBACK_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.COMPONENT_TYPE; +import static org.apache.tuscany.sca.assembly.xml.Constants.COMPONENT_TYPE_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.ELEMENT; +import static org.apache.tuscany.sca.assembly.xml.Constants.EXTENSION_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.IMPLEMENTATION; +import static org.apache.tuscany.sca.assembly.xml.Constants.MANY; +import static org.apache.tuscany.sca.assembly.xml.Constants.MUST_SUPPLY; +import static org.apache.tuscany.sca.assembly.xml.Constants.NAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.OPERATION_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.PROPERTY; +import static org.apache.tuscany.sca.assembly.xml.Constants.PROPERTY_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.REFERENCE; +import static org.apache.tuscany.sca.assembly.xml.Constants.REFERENCE_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.SERVICE; +import static org.apache.tuscany.sca.assembly.xml.Constants.SERVICE_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.TYPE; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Callback; +import org.apache.tuscany.sca.assembly.ComponentType; +import org.apache.tuscany.sca.assembly.Contract; +import org.apache.tuscany.sca.assembly.Extensible; +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.common.xml.stax.StAXHelper; +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.processor.StAXAttributeProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.impl.OperationImpl; +import org.apache.tuscany.sca.policy.PolicySubject; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * A componentType processor. + * + * @version $Rev$ $Date$ + */ +public class ComponentTypeProcessor extends BaseAssemblyProcessor implements StAXArtifactProcessor { + + private StAXHelper staxHelper; + + /** + * Constructs a new componentType processor. + * + * @param modelFactories + * @param extensionProcessor + * @param extensionAttributeProcessor + * @param monitor + */ + public ComponentTypeProcessor(ExtensionPointRegistry extensionPoints, + //public ComponentTypeProcessor(FactoryExtensionPoint modelFactories, + StAXArtifactProcessor extensionProcessor, + StAXAttributeProcessor extensionAttributeProcessor) { + super(modelFactories(extensionPoints), extensionProcessor); + + // + staxHelper = StAXHelper.getInstance(extensionPoints); + } + + public ComponentType read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException { + ComponentType componentType = null; + Service service = null; + Reference reference = null; + Contract contract = null; + Property property = null; + Callback callback = null; + QName name = null; + + try { + // Read the componentType document + while (reader.hasNext()) { + int event = reader.getEventType(); + switch (event) { + case START_ELEMENT: + name = reader.getName(); + + if (Constants.COMPONENT_TYPE_QNAME.equals(name)) { + + // Read a + componentType = assemblyFactory.createComponentType(); + + } else if (Constants.SERVICE_QNAME.equals(name)) { + + // Read a + service = assemblyFactory.createService(); + contract = service; + service.setName(getString(reader, Constants.NAME)); + componentType.getServices().add(service); + policyProcessor.readPolicies(service, reader); + + } else if (Constants.REFERENCE_QNAME.equals(name)) { + + // Read a + reference = assemblyFactory.createReference(); + contract = reference; + reference.setName(getString(reader, Constants.NAME)); + reference.setWiredByImpl(getBoolean(reader, Constants.WIRED_BY_IMPL)); + readMultiplicity(reference, reader); + readTargets(reference, reader); + componentType.getReferences().add(reference); + policyProcessor.readPolicies(reference, reader); + + } else if (Constants.PROPERTY_QNAME.equals(name)) { + + // Read a + property = assemblyFactory.createProperty(); + readAbstractProperty(property, reader, context); + policyProcessor.readPolicies(property, reader); + + // Read the property value + Document value = readPropertyValue(property.getXSDElement(), property.getXSDType(), property.isMany(), reader, context); + property.setValue(value); + + componentType.getProperties().add(property); + + } else if (Constants.IMPLEMENTATION_QNAME.equals(name)) { + + // Read an element + policyProcessor.readPolicies(componentType, reader); + + } else if (Constants.CALLBACK_QNAME.equals(name)) { + + // Read a + callback = assemblyFactory.createCallback(); + contract.setCallback(callback); + policyProcessor.readPolicies(callback, reader); + + } else if (OPERATION_QNAME.equals(name)) { + + // Read an + Operation operation = new OperationImpl(); + operation.setName(getString(reader, NAME)); + operation.setUnresolved(true); + if (callback != null) { + policyProcessor.readPolicies(callback, operation, reader); + } else { + policyProcessor.readPolicies(contract, operation, reader); + } + } else if(EXTENSION_QNAME.equals(name)) { + // Handle + //ignore element as this is a wrapper for extensibility + break; + } else { + + + // Read an extension element + Object extension = extensionProcessor.read(reader, context); + if (extension != null) { + if (extension instanceof InterfaceContract) { + + // and + contract.setInterfaceContract((InterfaceContract)extension); + + } else if (extension instanceof Binding) { + + // and + if (callback != null) { + callback.getBindings().add((Binding)extension); + } else { + contract.getBindings().add((Binding)extension); + } + } else { + + // Add the extension element to the current element + if (callback != null) { + callback.getExtensions().add(extension); + } else if (contract != null) { + contract.getExtensions().add(extension); + } else if (property != null) { + property.getExtensions().add(extension); + } else { + if (componentType instanceof Extensible) { + ((Extensible)componentType).getExtensions().add(extension); + } + } + } + } + } + break; + + case END_ELEMENT: + name = reader.getName(); + + // Clear current state when reading reaching end element + if (SERVICE_QNAME.equals(name)) { + service = null; + contract = null; + } else if (REFERENCE_QNAME.equals(name)) { + reference = null; + contract = null; + } else if (PROPERTY_QNAME.equals(name)) { + property = null; + } else if (CALLBACK_QNAME.equals(name)) { + callback = null; + } + break; + } + + // Read the next element + if (reader.hasNext()) { + reader.next(); + } + } + } + catch (XMLStreamException e) { + ContributionReadException ex = new ContributionReadException(e); + error(context.getMonitor(), "XMLStreamException", reader, ex); + } + + return componentType; + } + + public void write(ComponentType componentType, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, XMLStreamException { + + // Write element + writeStartDocument(writer, COMPONENT_TYPE); + + // Write elements + for (Service service : componentType.getServices()) { + writeStart(writer, SERVICE, new XAttr(NAME, service.getName()), + policyProcessor.writePolicies(service)); + + if (service.getInterfaceContract() != null) { + extensionProcessor.write(service.getInterfaceContract(), writer, context); + } + + for (Binding binding: service.getBindings()) { + extensionProcessor.write(binding, writer, context); + } + + if (service.getCallback() != null) { + Callback callback = service.getCallback(); + writeStart(writer, CALLBACK, policyProcessor.writePolicies(callback)); + + for (Binding binding: callback.getBindings()) { + extensionProcessor.write(binding, writer, context); + } + for (Object extension: callback.getExtensions()) { + extensionProcessor.write(extension, writer, context); + } + + writeEnd(writer); + } + + this.writeExtendedElements(writer, service, extensionProcessor, context); + + writeEnd(writer); + } + + // Write elements + for (Reference reference : componentType.getReferences()) { + + writeStart(writer, REFERENCE, + new XAttr(NAME, reference.getName()), + writeMultiplicity(reference), + writeTargets(reference), + policyProcessor.writePolicies(reference)); + + extensionProcessor.write(reference.getInterfaceContract(), writer, context); + + for (Binding binding: reference.getBindings()) { + extensionProcessor.write(binding, writer, context); + } + + if (reference.getCallback() != null) { + Callback callback = reference.getCallback(); + writeStart(writer, CALLBACK, + policyProcessor.writePolicies(callback)); + + for (Binding binding: callback.getBindings()) { + extensionProcessor.write(binding, writer, context); + } + for (Object extension: callback.getExtensions()) { + extensionProcessor.write(extension, writer, context); + } + + writeEnd(writer); + } + + this.writeExtendedElements(writer, reference, extensionProcessor, context); + + writeEnd(writer); + } + + // Write elements + for (Property property : componentType.getProperties()) { + writeStart(writer, + PROPERTY, + new XAttr(NAME, property.getName()), + new XAttr(MUST_SUPPLY, property.isMustSupply()), + new XAttr(MANY, property.isMany()), + new XAttr(TYPE, property.getXSDType()), + new XAttr(ELEMENT, property.getXSDElement()), + policyProcessor.writePolicies(property)); + + // Write property value + writePropertyValue(property.getValue(), property.getXSDElement(), property.getXSDType(), writer); + + // Write extensions + for (Object extension : property.getExtensions()) { + extensionProcessor.write(extension, writer, context); + } + + writeEnd(writer); + } + + // Write extension elements + if (componentType instanceof Extensible) { + for (Object extension: ((Extensible)componentType).getExtensions()) { + extensionProcessor.write(extension, writer, context); + } + } + + // Write elements if the componentType has + // any intents or policySets + boolean writeImplementation = false; + if (componentType instanceof PolicySubject) { + if (!((PolicySubject)componentType).getRequiredIntents().isEmpty()) { + writeImplementation = true; + } + } + if (componentType instanceof PolicySubject) { + if (!((PolicySubject)componentType).getPolicySets().isEmpty()) { + writeImplementation = true; + } + } + if (writeImplementation) { + writeStart(writer, IMPLEMENTATION, + policyProcessor.writePolicies(componentType)); + } + + writeEndDocument(writer); + } + + /** + * Write the value of a property - override to use correct method of creating an XMLStreamReader + * @param document + * @param element + * @param type + * @param writer + * @throws XMLStreamException + */ + protected void writePropertyValue(Object propertyValue, QName element, QName type, XMLStreamWriter writer) + throws XMLStreamException { + + if (propertyValue instanceof Document) { + Document document = (Document)propertyValue; + NodeList nodeList = document.getDocumentElement().getChildNodes(); + + for (int item = 0; item < nodeList.getLength(); ++item) { + Node node = nodeList.item(item); + int nodeType = node.getNodeType(); + if (nodeType == Node.ELEMENT_NODE) { + // Correct way to create a reader for a node object... + XMLStreamReader reader = staxHelper.createXMLStreamReader(node); + + while (reader.hasNext()) { + switch (reader.next()) { + case XMLStreamConstants.START_ELEMENT: + QName name = reader.getName(); + writer.writeStartElement(name.getPrefix(), name.getLocalPart(), name.getNamespaceURI()); + + int namespaces = reader.getNamespaceCount(); + for (int i = 0; i < namespaces; i++) { + String prefix = reader.getNamespacePrefix(i); + String ns = reader.getNamespaceURI(i); + writer.writeNamespace(prefix, ns); + } + + if (!"".equals(name.getNamespaceURI())) { + writer.writeNamespace(name.getPrefix(), name.getNamespaceURI()); + } + + // add the attributes for this element + namespaces = reader.getAttributeCount(); + for (int i = 0; i < namespaces; i++) { + String ns = reader.getAttributeNamespace(i); + String prefix = reader.getAttributePrefix(i); + String qname = reader.getAttributeLocalName(i); + String value = reader.getAttributeValue(i); + + writer.writeAttribute(prefix, ns, qname, value); + } + + break; + case XMLStreamConstants.CDATA: + writer.writeCData(reader.getText()); + break; + case XMLStreamConstants.CHARACTERS: + writer.writeCharacters(reader.getText()); + break; + case XMLStreamConstants.END_ELEMENT: + writer.writeEndElement(); + break; + } + } + } else { + writer.writeCharacters(node.getTextContent()); + } + } + } + } // end method writePropertyValue + + public void resolve(ComponentType componentType, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + + // Resolve component type services and references + resolveContracts(componentType.getServices(), resolver, context); + resolveContracts(componentType.getReferences(), resolver, context); + } + + public QName getArtifactType() { + return COMPONENT_TYPE_QNAME; + } + + public Class getModelType() { + return ComponentType.class; + } + + /** + * Returns the model factory extension point to use. + * + * @param extensionPoints + * @return + */ + private static FactoryExtensionPoint modelFactories(ExtensionPointRegistry extensionPoints) { + return extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeDocumentProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeDocumentProcessor.java new file mode 100644 index 0000000000..2e8daf7f67 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeDocumentProcessor.java @@ -0,0 +1,198 @@ +/* + * 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.assembly.xml; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URL; + +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.transform.stream.StreamSource; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.common.java.io.IOHelper; +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ValidatingXMLInputFactory; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.monitor.Monitor; + +/** + * A composite processor. + * + * @version $Rev$ $Date$ + */ +public class CompositeDocumentProcessor extends BaseAssemblyProcessor implements URLArtifactProcessor, + XMLStreamConstants { + private ValidatingXMLInputFactory inputFactory; + + + /** + * Constructs a composite document processor + * @param modelFactories + * @param staxProcessor + * @param monitor + */ + public CompositeDocumentProcessor(FactoryExtensionPoint modelFactories, + StAXArtifactProcessor staxProcessor) { + super(modelFactories, staxProcessor); + this.inputFactory = modelFactories.getFactory(ValidatingXMLInputFactory.class); + } + + /** + * Reads the contents of a Composite document and returns a Composite object + * @param contributionURL - the URL of the contribution containing the Composite - can be null + * @param uri - the URI of the composite document + * @param url - the URL of the composite document + * @return a Composite object built from the supplied Composite document + */ + public Composite read(URL contributionURL, URI uri, URL url, ProcessorContext context) throws ContributionReadException { + if( uri == null || url == null ) { + throw new ContributionReadException(context.getMonitor().getMessageString(CompositeDocumentProcessor.class.getName(), + Messages.RESOURCE_BUNDLE, + "NullURL")); + } // end if + InputStream scdlStream = null; + + try { + scdlStream = IOHelper.openStream(url); + } catch (IOException e) { + ContributionReadException ce = new ContributionReadException(context.getMonitor().getMessageString(CompositeDocumentProcessor.class.getName(), + Messages.RESOURCE_BUNDLE, + "ReadException") + + " " + + uri, e); + error(context.getMonitor(), "ContributionReadException", url, ce); + throw ce; + } + return read(uri, url, scdlStream, context); + } + + public Composite read(URI uri, URL url, InputStream scdlStream, ProcessorContext context) throws ContributionReadException { + try { + + Composite composite = null; + Monitor monitor = context.getMonitor(); + // Tag the monitor with the name of the composite artifact + if( monitor != null ) { + monitor.setArtifactName(uri.toString()); + } //end if + + // Set up a StreamSource for the composite file, since this has an associated URL that + // can be used by the parser to find references to other files such as DTDs + StreamSource scdlSource = new StreamSource( scdlStream, url.toString() ); + XMLStreamReader reader = inputFactory.createXMLStreamReader(scdlSource); + + // set the monitor on the input factory as the standard XMLInputFactory + // methods used for creating readers don't allow for the context to + // be passed in + ValidatingXMLInputFactory.setMonitor(reader, context.getMonitor()); + + // Read the header (i.e. text before the element, if any + readCompositeFileHeader( reader ); + + // Read the composite model + composite = (Composite)extensionProcessor.read(reader, context); + if (composite != null) { + composite.setURI(uri.toString()); + } + + return composite; + + } catch (XMLStreamException e) { + ContributionReadException ce = new ContributionReadException(context.getMonitor().getMessageString(CompositeDocumentProcessor.class.getName(), + Messages.RESOURCE_BUNDLE, + "ReadException") + + " " + + uri, e); + error(context.getMonitor(), "ContributionReadException", inputFactory, ce); + throw ce; + } finally { + try { + if (scdlStream != null) { + scdlStream.close(); + scdlStream = null; + } + } catch (IOException ioe) { + //ignore + } + } + } + + /** + * Reads the header portion of a composite file - i.e. the section of the file before the + * start tag + * In particular handle any DTD declarations + * @param reader - an XMLStreamReader which is reading the composite file + * @throws XMLStreamException + */ + private void readCompositeFileHeader( XMLStreamReader reader ) throws XMLStreamException { + + while (true) { + int event = reader.next(); + + if ( event == CHARACTERS + || event == CDATA + || event == SPACE + || event == PROCESSING_INSTRUCTION + || event == COMMENT + || event == DTD + || event == ENTITY_DECLARATION ) { + continue; + } // end if + + // The first start (or end) element terminates the header scan + if (event == START_ELEMENT || event == END_ELEMENT) { + return; + } // end if + } // end while + } // end method readCompositeFileHeader + + public void resolve(Composite composite, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + try { + if (composite != null) + extensionProcessor.resolve(composite, resolver, context); + } catch (Throwable e ) { + // Add information about which composite was being processed when the exception occurred + String newMessage = context.getMonitor().getMessageString(CompositeDocumentProcessor.class.getName(), + Messages.RESOURCE_BUNDLE, + "ProcessingComposite") + + " " + + composite.getName() + ": " + e.getMessage(); + throw new ContributionResolveException( newMessage, e ); + } // end try + } + + public String getArtifactType() { + return ".composite"; + } + + public Class getModelType() { + return Composite.class; + } + +} // end class diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeModelResolver.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeModelResolver.java new file mode 100644 index 0000000000..7a396fe100 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeModelResolver.java @@ -0,0 +1,96 @@ +/* + * 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.assembly.xml; + +import java.util.HashMap; +import java.util.Map; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.Import; +import org.apache.tuscany.sca.contribution.namespace.NamespaceImport; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.monitor.Monitor; + +/** + * A Model Resolver for Composite models. + * + * @version $Rev$ $Date$ + */ +public class CompositeModelResolver implements ModelResolver { + + private Contribution contribution; + private Map map = new HashMap(); + + public CompositeModelResolver(Contribution contribution, FactoryExtensionPoint modelFactories) { + this.contribution = contribution; + } + + public void addModel(Object resolved, ProcessorContext context) { + Composite composite = (Composite)resolved; + Composite old = map.put(composite.getName(), composite); + if (old != null) { + Monitor.error(context.getMonitor(), + this, + Messages.RESOURCE_BUNDLE, + "DuplicateCompositeName", + composite.getName().toString(), + contribution.getLocation()); + } + } + + public Object removeModel(Object resolved, ProcessorContext context) { + return map.remove(((Composite)resolved).getName()); + } + + public T resolveModel(Class modelClass, T unresolved, ProcessorContext context) { + + // Lookup a definition for the given namespace + QName qname = ((Composite)unresolved).getName(); + Composite resolved = null; + + // Delegate the resolution to the imports + for (Import import_ : this.contribution.getImports()) { + if (import_ instanceof NamespaceImport) { + NamespaceImport namespaceImport = (NamespaceImport)import_; + if (namespaceImport.getNamespace().equals(qname.getNamespaceURI())) { + + // Delegate the resolution to the import resolver + resolved = namespaceImport.getModelResolver().resolveModel(Composite.class, (Composite)unresolved, context); + if (!resolved.isUnresolved()) { + return modelClass.cast(resolved); + } + } + } + } + + // No definition found, search within the current contribution + resolved = (Composite) map.get(qname); + if (resolved != null) { + return modelClass.cast(resolved); + } + + return (T)unresolved; + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java new file mode 100644 index 0000000000..4b2f32c4c2 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java @@ -0,0 +1,1289 @@ +/* + * 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.assembly.xml; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; +import static org.apache.tuscany.sca.assembly.xml.Constants.AUTOWIRE; +import static org.apache.tuscany.sca.assembly.xml.Constants.CALLBACK; +import static org.apache.tuscany.sca.assembly.xml.Constants.CALLBACK_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.COMPONENT; +import static org.apache.tuscany.sca.assembly.xml.Constants.COMPONENT_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.COMPOSITE; +import static org.apache.tuscany.sca.assembly.xml.Constants.COMPOSITE_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.ELEMENT; +import static org.apache.tuscany.sca.assembly.xml.Constants.EXTENSION_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.FILE; +import static org.apache.tuscany.sca.assembly.xml.Constants.IMPLEMENTATION_COMPOSITE; +import static org.apache.tuscany.sca.assembly.xml.Constants.IMPLEMENTATION_COMPOSITE_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.INCLUDE; +import static org.apache.tuscany.sca.assembly.xml.Constants.INCLUDE_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.INTENTS; +import static org.apache.tuscany.sca.assembly.xml.Constants.LOCAL; +import static org.apache.tuscany.sca.assembly.xml.Constants.MANY; +import static org.apache.tuscany.sca.assembly.xml.Constants.MUST_SUPPLY; +import static org.apache.tuscany.sca.assembly.xml.Constants.NAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.NONOVERRIDABLE; +import static org.apache.tuscany.sca.assembly.xml.Constants.POLICY_SET_ATTACHMENT_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.PROMOTE; +import static org.apache.tuscany.sca.assembly.xml.Constants.PROPERTY; +import static org.apache.tuscany.sca.assembly.xml.Constants.PROPERTY_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.REFERENCE; +import static org.apache.tuscany.sca.assembly.xml.Constants.REFERENCE_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.REPLACE; +import static org.apache.tuscany.sca.assembly.xml.Constants.REQUIRES_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.SCA11_NS; +import static org.apache.tuscany.sca.assembly.xml.Constants.SERVICE; +import static org.apache.tuscany.sca.assembly.xml.Constants.SERVICE_QNAME; +import static org.apache.tuscany.sca.assembly.xml.Constants.SOURCE; +import static org.apache.tuscany.sca.assembly.xml.Constants.TARGET; +import static org.apache.tuscany.sca.assembly.xml.Constants.TARGET_NAMESPACE; +import static org.apache.tuscany.sca.assembly.xml.Constants.TYPE; +import static org.apache.tuscany.sca.assembly.xml.Constants.URI; +import static org.apache.tuscany.sca.assembly.xml.Constants.WIRE; +import static org.apache.tuscany.sca.assembly.xml.Constants.WIRED_BY_IMPL; +import static org.apache.tuscany.sca.assembly.xml.Constants.WIRE_QNAME; + +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; +import javax.xml.xpath.XPathExpressionException; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Callback; +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.Property; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.assembly.Wire; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper; +import org.apache.tuscany.sca.common.xml.xpath.XPathHelper; +import org.apache.tuscany.sca.contribution.Artifact; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.ContributionFactory; +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.processor.StAXAttributeProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ResolverExtension; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.policy.ExtensionType; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.PolicyFactory; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySubject; +import org.apache.tuscany.sca.xsd.XSDFactory; +import org.apache.tuscany.sca.xsd.XSDefinition; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * A composite processor. + * + * @version $Rev$ $Date$ + */ +public class CompositeProcessor extends BaseAssemblyProcessor implements StAXArtifactProcessor { + private XPathHelper xpathHelper; + private PolicyFactory intentAttachPointTypeFactory; + private StAXAttributeProcessor extensionAttributeProcessor; + private ContributionFactory contributionFactory; + private XSDFactory xsdFactory; + + private StAXHelper staxHelper; + + /** + * Construct a new composite processor + * + * @param extensionPoints + * @param extensionProcessor + */ + public CompositeProcessor(ExtensionPointRegistry extensionPoints, + StAXArtifactProcessor extensionProcessor, + StAXAttributeProcessor extensionAttributeProcessor) { + + this(modelFactories(extensionPoints), extensionProcessor, extensionAttributeProcessor); + + this.xpathHelper = XPathHelper.getInstance(extensionPoints); + this.extensionAttributeProcessor = extensionAttributeProcessor; + + this.xsdFactory = extensionPoints.getExtensionPoint(XSDFactory.class); + + // + staxHelper = StAXHelper.getInstance(extensionPoints); + } + + /** + * Constructs a new composite processor + * + * @param modelFactories + * @param extensionProcessor + * @param monitor + */ + private CompositeProcessor(FactoryExtensionPoint modelFactories, + StAXArtifactProcessor extensionProcessor, + StAXAttributeProcessor extensionAttributeProcessor) { + + super(modelFactories, extensionProcessor); + this.intentAttachPointTypeFactory = modelFactories.getFactory(PolicyFactory.class); + this.contributionFactory = modelFactories.getFactory(ContributionFactory.class); + this.extensionAttributeProcessor = extensionAttributeProcessor; + + this.xsdFactory = modelFactories.getFactory(XSDFactory.class); + } + + public Composite read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException { + Composite composite = null; + Composite include = null; + Component component = null; + Property property = null; + ComponentService componentService = null; + ComponentReference componentReference = null; + ComponentProperty componentProperty = null; + CompositeService compositeService = null; + CompositeReference compositeReference = null; + Contract contract = null; + Wire wire = null; + Callback callback = null; + QName name = null; + Monitor monitor = context.getMonitor(); + Contribution contribution = context.getContribution(); + try { + // Read the composite document + while (reader.hasNext()) { + int event = reader.getEventType(); + switch (event) { + case START_ELEMENT: + name = reader.getName(); + + if (COMPOSITE_QNAME.equals(name)) { + + // Read a + composite = assemblyFactory.createComposite(); + composite.setSpecVersion(Constants.SCA11_NS); + if (contribution != null) { + composite.setContributionURI(contribution.getURI()); + } + + composite.setName(new QName(getURIString(reader, TARGET_NAMESPACE), getString(reader, NAME))); + + if (!isSet(reader, TARGET_NAMESPACE)) { + // spec says that a composite must have a namespace + warning(monitor, "NoCompositeNamespace", composite, composite.getName().toString()); + } + + if (isSet(reader, AUTOWIRE)) { + composite.setAutowire(getBoolean(reader, AUTOWIRE)); + } + + //handle extension attributes + this.readExtendedAttributes(reader, name, composite, extensionAttributeProcessor, context); + + composite.setLocal(getBoolean(reader, LOCAL)); + policyProcessor.readPolicies(composite, reader); + + } else if (INCLUDE_QNAME.equals(name)) { + + // Read an + include = assemblyFactory.createComposite(); + include.setName(getQName(reader, NAME)); + include.setURI(getURIString(reader, URI)); + include.setUnresolved(true); + composite.getIncludes().add(include); + + } else if (SERVICE_QNAME.equals(name)) { + if (component != null) { + + // Read a + componentService = assemblyFactory.createComponentService(); + contract = componentService; + componentService.setName(getString(reader, NAME)); + + //handle extension attributes + this.readExtendedAttributes(reader, name, componentService, extensionAttributeProcessor, context); + + component.getServices().add(componentService); + policyProcessor.readPolicies(contract, reader); + } else { + + // Read a + compositeService = assemblyFactory.createCompositeService(); + contract = compositeService; + compositeService.setName(getString(reader, NAME)); + + String promoted = getURIString(reader, PROMOTE); + if (promoted != null) { + String promotedComponentName; + String promotedServiceName; + int s = promoted.indexOf('/'); + if (s == -1) { + promotedComponentName = promoted; + promotedServiceName = null; + } else { + promotedComponentName = promoted.substring(0, s); + promotedServiceName = promoted.substring(s + 1); + } + + Component promotedComponent = assemblyFactory.createComponent(); + promotedComponent.setUnresolved(true); + promotedComponent.setName(promotedComponentName); + compositeService.setPromotedComponent(promotedComponent); + + ComponentService promotedService = assemblyFactory.createComponentService(); + promotedService.setUnresolved(true); + promotedService.setName(promotedServiceName); + compositeService.setPromotedService(promotedService); + } + + //handle extension attributes + this.readExtendedAttributes(reader, name, compositeService, extensionAttributeProcessor, context); + + composite.getServices().add(compositeService); + policyProcessor.readPolicies(contract, reader); + } + + // set the parent model so that binding processing can + // detect it they're being read as part of a reference + // or a service + context.setParentModel(contract); + + } else if (REFERENCE_QNAME.equals(name)) { + if (component != null) { + // Read a + componentReference = assemblyFactory.createComponentReference(); + contract = componentReference; + componentReference.setName(getString(reader, NAME)); + readMultiplicity(componentReference, reader); + if (isSet(reader, AUTOWIRE)) { + componentReference.setAutowire(getBoolean(reader, AUTOWIRE)); + } + // Read @nonOverridable + String nonOverridable = reader.getAttributeValue(null, NONOVERRIDABLE); + if (nonOverridable != null) { + componentReference.setNonOverridable(Boolean.parseBoolean(nonOverridable)); + } + readTargets(componentReference, reader); + componentReference.setWiredByImpl(getBoolean(reader, WIRED_BY_IMPL)); + + //handle extension attributes + this.readExtendedAttributes(reader, + name, + componentReference, + extensionAttributeProcessor, context); + + component.getReferences().add(componentReference); + policyProcessor.readPolicies(contract, reader); + } else { + // Read a + compositeReference = assemblyFactory.createCompositeReference(); + contract = compositeReference; + compositeReference.setName(getString(reader, NAME)); + readMultiplicity(compositeReference, reader); + readTargets(compositeReference, reader); + String promote = getString(reader, Constants.PROMOTE); + if (promote != null) { + for (StringTokenizer tokens = new StringTokenizer(promote); tokens.hasMoreTokens();) { + String refName = tokens.nextToken(); + Component promotedComponent = assemblyFactory.createComponent(); + int index = refName.indexOf('/'); + if (index == -1) { + error(monitor, "Invalid reference name", compositeReference, refName); + } + String promotedComponentName = refName.substring(0, index); + promotedComponent.setName(promotedComponentName); + promotedComponent.setUnresolved(true); + compositeReference.getPromotedComponents().add(promotedComponent); + ComponentReference promotedReference = + assemblyFactory.createComponentReference(); + promotedReference.setUnresolved(true); + promotedReference.setName(refName); + compositeReference.getPromotedReferences().add(promotedReference); + } + } + compositeReference.setWiredByImpl(getBoolean(reader, WIRED_BY_IMPL)); + + //handle extension attributes + this.readExtendedAttributes(reader, + name, + compositeReference, + extensionAttributeProcessor, context); + + composite.getReferences().add(compositeReference); + policyProcessor.readPolicies(contract, reader); + } + + // set the parent model so that binding processing can + // detect it they're being read as part of a reference + // or a service + context.setParentModel(contract); + + } else if (PROPERTY_QNAME.equals(name)) { + if (component != null) { + + // Read a + componentProperty = assemblyFactory.createComponentProperty(); + property = componentProperty; + String source = getURIString(reader, SOURCE); + if (source != null) { + source = source.trim(); + } + componentProperty.setSource(source); + if (source != null) { + String xPath = prepareSourceXPathString( source ); + + try { + componentProperty.setSourceXPathExpression(xpathHelper.compile(reader + .getNamespaceContext(), xPath)); + } catch (XPathExpressionException e) { + ContributionReadException ce = new ContributionReadException(e); + error(monitor, "ContributionReadException", source, ce); + //throw ce; + } + } + componentProperty.setFile(getURIString(reader, FILE)); + + //handle extension attributes + this.readExtendedAttributes(reader, + name, + componentProperty, + extensionAttributeProcessor, context); + + policyProcessor.readPolicies(property, reader); + readAbstractProperty(componentProperty, reader, context); + + // Read the property value + Document value = + readPropertyValue(property.getXSDElement(), property.getXSDType(), property + .isMany(), reader, context); + property.setValue(value); + + component.getProperties().add(componentProperty); + } else { + + // Read a + property = assemblyFactory.createProperty(); + policyProcessor.readPolicies(property, reader); + readAbstractProperty(property, reader, context); + + // Read the property value + Document value = + readPropertyValue(property.getXSDElement(), property.getXSDType(), property + .isMany(), reader, context); + property.setValue(value); + + composite.getProperties().add(property); + } + + // TUSCANY-1949 + // If the property doesn't have a value, the END_ELEMENT event is read by the readPropertyValue + if (reader.getEventType() == END_ELEMENT && PROPERTY_QNAME.equals(reader.getName())) { + property = null; + componentProperty = null; + } + + } else if (COMPONENT_QNAME.equals(name)) { + + // Read a + component = assemblyFactory.createComponent(); + component.setName(getString(reader, NAME)); + if (isSet(reader, AUTOWIRE)) { + component.setAutowire(getBoolean(reader, AUTOWIRE)); + } + if (isSet(reader, URI)) { + component.setURI(getURIString(reader, URI)); + } + + //handle extension attributes + this.readExtendedAttributes(reader, name, component, extensionAttributeProcessor, context); + + composite.getComponents().add(component); + policyProcessor.readPolicies(component, reader); + + } else if (WIRE_QNAME.equals(name)) { + + // Read a + wire = assemblyFactory.createWire(); + ComponentReference source = assemblyFactory.createComponentReference(); + source.setUnresolved(true); + source.setName(getURIString(reader, SOURCE)); + wire.setSource(source); + + ComponentService target = assemblyFactory.createComponentService(); + target.setUnresolved(true); + target.setName(getURIString(reader, TARGET)); + wire.setTarget(target); + + // Read @replace + String replace = reader.getAttributeValue(null, REPLACE); + if (replace != null) { + wire.setReplace(Boolean.parseBoolean(replace)); + } + + //handle extension attributes + this.readExtendedAttributes(reader, name, wire, extensionAttributeProcessor, context); + + composite.getWires().add(wire); + policyProcessor.readPolicies(wire, reader); + + } else if (CALLBACK_QNAME.equals(name)) { + + // Read a + callback = assemblyFactory.createCallback(); + contract.setCallback(callback); + callback.setParentContract(contract); + + //handle extension attributes + this.readExtendedAttributes(reader, name, callback, extensionAttributeProcessor, context); + + policyProcessor.readPolicies(callback, reader); + + // set the parent model so that binding processing can + // detect it they're being read as part of a callback + context.setParentModel(callback); + + } else if (IMPLEMENTATION_COMPOSITE_QNAME.equals(name)) { + + // Read an implementation.composite + Composite implementation = assemblyFactory.createComposite(); + implementation.setName(getQName(reader, NAME)); + implementation.setUnresolved(true); + + //handle extension attributes + this.readExtendedAttributes(reader, name, implementation, extensionAttributeProcessor, context); + + component.setImplementation(implementation); + policyProcessor.readPolicies(implementation, reader); + } else if (REQUIRES_QNAME.equals(name)) { + List intents = getQNames(reader, INTENTS); + for (QName i : intents) { + Intent intent = policyFactory.createIntent(); + intent.setName(i); + if (composite != null) { + composite.getRequiredIntents().add(intent); + } else if (component != null) { + component.getRequiredIntents().add(intent); + } else if (contract != null) { + contract.getRequiredIntents().add(intent); + } else if (callback != null) { + callback.getRequiredIntents().add(intent); + } + } + } else if (POLICY_SET_ATTACHMENT_QNAME.equals(name)) { + QName ps = getQName(reader, NAME); + if (ps != null) { + PolicySet policySet = policyFactory.createPolicySet(); + policySet.setName(ps); + if (composite != null) { + composite.getPolicySets().add(policySet); + } else if (component != null) { + component.getPolicySets().add(policySet); + } else if (contract != null) { + contract.getPolicySets().add(policySet); + } else if (callback != null) { + callback.getPolicySets().add(policySet); + } + } + } else if(EXTENSION_QNAME.equals(name)) { + // Handle + //ignore element as this is a wrapper for extensibility + break; + } else { + + // Read an extension element + Object extension = extensionProcessor.read(reader, context); + if (extension != null) { + if (extension instanceof InterfaceContract) { + + // and + // + if (contract != null) { + contract.setInterfaceContract((InterfaceContract)extension); + } else { + if (name.getNamespaceURI().equals(SCA11_NS)) { + error(monitor, "UnexpectedInterfaceElement", extension); + //throw new ContributionReadException("Unexpected element found. It should appear inside a or element"); + } else { + composite.getExtensions().add(extension); + } + } + } else if (extension instanceof Binding) { + if (extension instanceof PolicySubject) { + ExtensionType bindingType = intentAttachPointTypeFactory.createBindingType(); + bindingType.setType(name); + bindingType.setUnresolved(true); + ((PolicySubject)extension).setExtensionType(bindingType); + } + // and + // + if (callback != null) { + callback.getBindings().add((Binding)extension); + } else { + if (contract != null) { + contract.getBindings().add((Binding)extension); + } else { + if (name.getNamespaceURI().equals(SCA11_NS)) { + error(monitor, "UnexpectedBindingElement", extension); + //throw new ContributionReadException("Unexpected element found. It should appear inside a or element"); + } else { + composite.getExtensions().add(extension); + } + } + } + + } else if (extension instanceof Implementation) { + if (extension instanceof PolicySubject) { + ExtensionType implType = + intentAttachPointTypeFactory.createImplementationType(); + implType.setType(name); + implType.setUnresolved(true); + ((PolicySubject)extension).setExtensionType(implType); + } + // + if (component != null) { + component.setImplementation((Implementation)extension); + } else { + if (name.getNamespaceURI().equals(SCA11_NS)) { + error(monitor, "UnexpectedImplementationElement", extension); + //throw new ContributionReadException("Unexpected element found. It should appear inside a element"); + } else { + composite.getExtensions().add(extension); + } + } + } else { + + // Add the extension element to the current + // element + if (callback != null) { + callback.getExtensions().add(extension); + } else if (contract != null) { + contract.getExtensions().add(extension); + } else if (property != null) { + property.getExtensions().add(extension); + } else if (component != null) { + component.getExtensions().add(extension); + } else { + composite.getExtensions().add(extension); + } + } + } + } + break; + + case XMLStreamConstants.CHARACTERS: + break; + + case END_ELEMENT: + name = reader.getName(); + + // Clear current state when reading reaching end element + if (SERVICE_QNAME.equals(name)) { + componentService = null; + compositeService = null; + contract = null; + } else if (INCLUDE_QNAME.equals(name)) { + include = null; + } else if (REFERENCE_QNAME.equals(name)) { + componentReference = null; + compositeReference = null; + contract = null; + } else if (PROPERTY_QNAME.equals(name)) { + componentProperty = null; + property = null; + } else if (COMPONENT_QNAME.equals(name)) { + component = null; + } else if (WIRE_QNAME.equals(name)) { + wire = null; + } else if (CALLBACK_QNAME.equals(name)) { + callback = null; + } + break; + } + + // Read the next element + if (reader.hasNext()) { + reader.next(); + } + } + } catch (XMLStreamException e) { + ContributionReadException ex = new ContributionReadException(e); + error(monitor, "XMLStreamException", reader, ex); + } + + return composite; + } + + /** + * Prepares the property @source XPath expression + * + * The form of the @source attribute in the composite file must take one of the forms + * $propertyName + * $propertyName/expression + * $propertyName[n] + * $propertyName[n]/expression + * Property values are stored as elements with one or more subelements or one or more + * global element subelements. The XPath constructed is designed to work against this XML structure and aims to + * retrieve one or more of the subelements or subportions of those subelements (eg some text content). + * Thus the XPath: + * - starts with "*", which means "all the child elements of the root" where root = the element + * - may then be followed by [xxx] (typically [n] to select one of the child elements) if the source string has [xxx] + * following the propertyName + * - may then be followed by /expression, if the source contains an expression, which will typically select some subportion + * of the child element(s) + * + * @param source - the @source attribute string from a element + * @return the XPath string to use for the source property + */ + private String prepareSourceXPathString( String source ) { + String output = null; + // Expression must begin with '$' + if( source.charAt(0) != '$' ) return output; + + int slash = source.indexOf('/'); + int bracket = source.indexOf('['); + if (slash == -1) { + // Form is $propertyName or $propertyName[n] + output = "*"; + if( bracket != -1 ) { + output = "*" + source.substring(bracket); + } + } else { + // Form is $propertyName/exp or $propertyName[n]/exp + output = "*/" + source.substring(slash + 1); + if( bracket != -1 && bracket < slash ) { + output = "*" + source.substring(bracket); + } + } // end if + + return output; + } // end method prepareSourceXPathString( source ) + + public void write(Composite composite, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, + XMLStreamException { + + // Write element + writeStartDocument(writer, + COMPOSITE, + new XAttr(TARGET_NAMESPACE, composite.getName().getNamespaceURI()), + new XAttr(NAME, composite.getName().getLocalPart()), + new XAttr(LOCAL, composite.isLocal() ? Boolean.TRUE : null), + new XAttr(AUTOWIRE, composite.getAutowire()), + policyProcessor.writePolicies(composite)); + + //write extended attributes + this.writeExtendedAttributes(writer, composite, extensionAttributeProcessor, context); + + // Write elements + for (Composite include : composite.getIncludes()) { + String uri = include.isUnresolved() ? include.getURI() : null; + writeStart(writer, INCLUDE, new XAttr(NAME, include.getName()), new XAttr(URI, uri)); + + //write extended attributes + this.writeExtendedAttributes(writer, include, extensionAttributeProcessor, context); + + writeEnd(writer); + } + + // Write elements + for (Service service : composite.getServices()) { + CompositeService compositeService = (CompositeService)service; + Component promotedComponent = compositeService.getPromotedComponent(); + ComponentService promotedService = compositeService.getPromotedService(); + String promote; + if (promotedService != null) { + if (promotedService.getName() != null) { + promote = promotedComponent.getName() + '/' + promotedService.getName(); + } else { + promote = promotedComponent.getName(); + } + } else { + promote = null; + } + writeStart(writer, + SERVICE, + new XAttr(NAME, service.getName()), + new XAttr(PROMOTE, promote), + policyProcessor.writePolicies(service)); + + //write extended attributes + this.writeExtendedAttributes(writer, service, extensionAttributeProcessor, context); + + // Write service interface + extensionProcessor.write(service.getInterfaceContract(), writer, context); + + // Write bindings + for (Binding binding : service.getBindings()) { + extensionProcessor.write(binding, writer, context); + } + + // Write element + if (service.getCallback() != null) { + Callback callback = service.getCallback(); + writeStart(writer, CALLBACK, policyProcessor.writePolicies(callback)); + + //write extended attributes + this.writeExtendedAttributes(writer, callback, extensionAttributeProcessor, context); + + // Write callback bindings + for (Binding binding : callback.getBindings()) { + extensionProcessor.write(binding, writer, context); + } + + // Write extensions + this.writeExtendedElements(writer, service, extensionProcessor, context); + + writeEnd(writer); + } + + // Write extensions + this.writeExtendedElements(writer, service, extensionProcessor, context); + + writeEnd(writer); + } + + // Write elements + for (Component component : composite.getComponents()) { + writeStart(writer, + COMPONENT, + new XAttr(NAME, component.getName()), + new XAttr(URI, component.getURI()), + new XAttr(AUTOWIRE, component.getAutowire()), + policyProcessor.writePolicies(component)); + + //write extended attributes + this.writeExtendedAttributes(writer, component, extensionAttributeProcessor, context); + + // Write the component implementation + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + writeStart(writer, IMPLEMENTATION_COMPOSITE, new XAttr(NAME, ((Composite)implementation).getName()), policyProcessor.writePolicies(implementation)); + + //write extended attributes + this.writeExtendedAttributes(writer, (Composite)implementation, extensionAttributeProcessor, context); + + writeEnd(writer); + } else { + extensionProcessor.write(component.getImplementation(), writer, context); + } + + for (Object extension : component.getExtensions()) { + extensionProcessor.write(extension, writer, context); + } + + // Write elements + for (ComponentService service : component.getServices()) { + writeStart(writer, SERVICE, new XAttr(NAME, service.getName()), policyProcessor.writePolicies(service)); + + //write extended attributes + this.writeExtendedAttributes(writer, service, extensionAttributeProcessor, context); + + // Write service interface + extensionProcessor.write(service.getInterfaceContract(), writer, context); + + // Write bindings + for (Binding binding : service.getBindings()) { + extensionProcessor.write(binding, writer, context); + } + + // Write element + if (service.getCallback() != null) { + Callback callback = service.getCallback(); + writeStart(writer, CALLBACK, policyProcessor.writePolicies(callback)); + + //write extended attributes + this.writeExtendedAttributes(writer, callback, extensionAttributeProcessor, context); + + // Write bindings + for (Binding binding : callback.getBindings()) { + extensionProcessor.write(binding, writer, context); + } + + // Write extensions + this.writeExtendedElements(writer, callback, extensionProcessor, context); + + writeEnd(writer); + } + + // Write extensions + this.writeExtendedElements(writer, service, extensionProcessor, context); + + writeEnd(writer); + } + + // Write elements + for (ComponentReference reference : component.getReferences()) { + writeStart(writer, + REFERENCE, + new XAttr(NAME, reference.getName()), + new XAttr(AUTOWIRE, reference.getAutowire()), + (reference.isNonOverridable() ? new XAttr(NONOVERRIDABLE, true) : null), + writeMultiplicity(reference), + writeTargets(reference), + policyProcessor.writePolicies(reference)); + + //write extended attributes + this.writeExtendedAttributes(writer, reference, extensionAttributeProcessor, context); + + // Write reference interface + extensionProcessor.write(reference.getInterfaceContract(), writer, context); + + // Write bindings + for (Binding binding : reference.getBindings()) { + extensionProcessor.write(binding, writer, context); + } + + // Write callback + if (reference.getCallback() != null) { + Callback callback = reference.getCallback(); + writeStart(writer, CALLBACK, policyProcessor.writePolicies(callback)); + + //write extended attributes + this.writeExtendedAttributes(writer, callback, extensionAttributeProcessor, context); + + // Write callback bindings + for (Binding binding : callback.getBindings()) { + extensionProcessor.write(binding, writer, context); + } + + // Write extensions + this.writeExtendedElements(writer, callback, extensionProcessor, context); + + writeEnd(writer); + } + + // Write extensions + this.writeExtendedElements(writer, reference, extensionProcessor, context); + + writeEnd(writer); + } + + // Write elements + for (ComponentProperty property : component.getProperties()) { + writeStart(writer, + PROPERTY, + new XAttr(NAME, property.getName()), + new XAttr(MUST_SUPPLY, property.isMustSupply()), + new XAttr(MANY, property.isMany()), + new XAttr(TYPE, property.getXSDType()), + new XAttr(ELEMENT, property.getXSDElement()), + new XAttr(SOURCE, property.getSource()), + new XAttr(FILE, property.getFile()), + policyProcessor.writePolicies(property)); + + //write extended attributes + this.writeExtendedAttributes(writer, property, extensionAttributeProcessor, context); + + // Write property value + writePropertyValue(property.getValue(), property.getXSDElement(), property.getXSDType(), writer); + + // Write extensions + for (Object extension : property.getExtensions()) { + extensionProcessor.write(extension, writer, context); + } + + writeEnd(writer); + } + + writeEnd(writer); + } + + // Write elements + for (Reference reference : composite.getReferences()) { + CompositeReference compositeReference = (CompositeReference)reference; + + // Write list of promoted references + List promote = new ArrayList(); + for (ComponentReference promoted : compositeReference.getPromotedReferences()) { + promote.add(promoted.getName()); + } + + // Write element + writeStart(writer, + REFERENCE, + new XAttr(NAME, reference.getName()), + new XAttr(PROMOTE, promote), + writeMultiplicity(reference), + policyProcessor.writePolicies(reference)); + + //write extended attributes + this.writeExtendedAttributes(writer, reference, extensionAttributeProcessor, context); + + // Write reference interface + extensionProcessor.write(reference.getInterfaceContract(), writer, context); + + // Write bindings + for (Binding binding : reference.getBindings()) { + extensionProcessor.write(binding, writer, context); + } + + // Write element + if (reference.getCallback() != null) { + Callback callback = reference.getCallback(); + writeStart(writer, CALLBACK); + + //write extended attributes + this.writeExtendedAttributes(writer, callback, extensionAttributeProcessor, context); + + // Write callback bindings + for (Binding binding : callback.getBindings()) { + extensionProcessor.write(binding, writer, context); + } + + // Write extensions + this.writeExtendedElements(writer, callback, extensionProcessor, context); + + writeEnd(writer); + } + + // Write extensions + this.writeExtendedElements(writer, reference, extensionProcessor, context); + + writeEnd(writer); + } + + // Write elements + for (Property property : composite.getProperties()) { + writeStart(writer, + PROPERTY, + new XAttr(NAME, property.getName()), + new XAttr(MUST_SUPPLY, property.isMustSupply()), + new XAttr(MANY, property.isMany()), + new XAttr(TYPE, property.getXSDType()), + new XAttr(ELEMENT, property.getXSDElement()), + policyProcessor.writePolicies(property)); + + //write extended attributes + this.writeExtendedAttributes(writer, property, extensionAttributeProcessor, context); + + // Write property value + writePropertyValue(property.getValue(), property.getXSDElement(), property.getXSDType(), writer); + + // Write extensions + for (Object extension : property.getExtensions()) { + extensionProcessor.write(extension, writer, context); + } + + writeEnd(writer); + } + + // Write elements + for (Wire wire : composite.getWires()) { + writeStart(writer, WIRE, new XAttr(SOURCE, wire.getSource().getName()), new XAttr(TARGET, wire.getTarget() + .getName()), wire.isReplace() ? new XAttr(Constants.REPLACE, true) : null); + + //write extended attributes + this.writeExtendedAttributes(writer, wire, extensionAttributeProcessor, context); + + // Write extensions + for (Object extension : wire.getExtensions()) { + extensionProcessor.write(extension, writer, context); + } + writeEnd(writer); + } + + for (Object extension : composite.getExtensions()) { + extensionProcessor.write(extension, writer, context); + } + + writeEndDocument(writer); + } + + public void resolve(Composite composite, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + + Monitor monitor = context.getMonitor(); + try { + monitor.pushContext("Composite: " + composite.getName()); + + // Resolve includes in the composite + for (int i = 0, n = composite.getIncludes().size(); i < n; i++) { + Composite include = composite.getIncludes().get(i); + if (include != null) { + Composite resolved = resolver.resolveModel(Composite.class, include, context); + if (!resolved.isUnresolved()) { + if ((composite.isLocal() && resolved.isLocal()) || (!composite.isLocal() && !resolved.isLocal())) { + composite.getIncludes().set(i, resolved); + } else { + String message = context.getMonitor().getMessageString(CompositeProcessor.class.getName(), + Messages.RESOURCE_BUNDLE, + "LocalAttibuteMissmatch"); + message = message.replace("{0}", composite.getName().toString()); + ContributionResolveException ce = new ContributionResolveException(message); + error(monitor, "ContributionResolveException", include, ce); + } + } else { + String message = context.getMonitor().getMessageString(CompositeProcessor.class.getName(), + Messages.RESOURCE_BUNDLE, + "CompositeNotFound"); + message = message.replace("{0}", include.getName().toString()); + ContributionResolveException ce = new ContributionResolveException(message); + error(monitor, "ContributionResolveException", include, ce); + } + } + } + + // Resolve extensions + for (Object extension : composite.getExtensions()) { + if (extension != null) { + extensionProcessor.resolve(extension, resolver, context); + } + } + + //Resolve composite services and references + resolveContracts(composite, composite.getServices(), resolver, context); + resolveContracts(composite, composite.getReferences(), resolver, context); + + for (Property property : composite.getProperties()){ + resolvePropertyType("composite " + composite.getName().toString(), + property, + context.getContribution(), context); + } + + // Resolve component implementations, services and references + for (Component component : composite.getComponents()) { + + //resolve component services and references + resolveContracts(component, component.getServices(), resolver, context); + resolveContracts(component, component.getReferences(), resolver, context); + + for (ComponentProperty componentProperty : component.getProperties()) { + // resolve a reference to a property file + if (componentProperty.getFile() != null) { + Artifact artifact = contributionFactory.createArtifact(); + artifact.setURI(componentProperty.getFile()); + artifact = resolver.resolveModel(Artifact.class, artifact, context); + if (artifact.getLocation() != null) { + componentProperty.setFile(artifact.getLocation()); + } + } + + // resolve the reference to a complex property + resolvePropertyType("component " + component.getName(), + componentProperty, + context.getContribution(), context); + } + + //resolve component implementation + Implementation implementation = component.getImplementation(); + if (implementation != null) { + //now resolve the implementation so that even if there is a shared instance + //for this that is resolved, the specified intents and policysets are safe in the + //component and not lost + + List policySets = new ArrayList(implementation.getPolicySets()); + List intents = new ArrayList(implementation.getRequiredIntents()); + implementation = resolveImplementation(implementation, resolver, context); + + // If there are any policy sets on the implementation or component we have to + // ignore policy sets from the component type (policy spec 4.9) + if ( !policySets.isEmpty() || !component.getPolicySets().isEmpty() ) { + implementation.getPolicySets().clear(); + implementation.getPolicySets().addAll(policySets); + } + + //implementation.getRequiredIntents().addAll(intents); + + // Make sure we don't repeat any intents that are already on the + // resolved implementation + for (Intent intent : intents){ + if (!implementation.getRequiredIntents().contains(intent)){ + implementation.getRequiredIntents().add(intent); + } + } + + // resolve any policy on implementation operations + for (Operation op : implementation.getOperations()){ + policyProcessor.resolvePolicies(op, resolver, context); + } + + // resolve any policy on interface operations + resolveContractOperationPolicy(implementation.getServices(), resolver, context); + resolveContractOperationPolicy(implementation.getReferences(), resolver, context); + + component.setImplementation(implementation); + } + + //add model resolver to component + if (component instanceof ResolverExtension) { + ((ResolverExtension)component).setModelResolver(resolver); + } + } + + // Add model resolver to promoted components + for (Service service : composite.getServices()) { + CompositeService compositeService = (CompositeService)service; + Component promotedComponent = compositeService.getPromotedComponent(); + if (promotedComponent instanceof ResolverExtension) { + ((ResolverExtension)promotedComponent).setModelResolver(resolver); + } + } // end for + + } finally { + // Pop context + monitor.popContext(); + } // end try + } + + public QName getArtifactType() { + return COMPOSITE_QNAME; + } + + public Class getModelType() { + return Composite.class; + } + + /** + * Write the value of a property - override to use correct method of creating an XMLStreamReader + * @param document + * @param element + * @param type + * @param writer + * @throws XMLStreamException + */ + protected void writePropertyValue(Object propertyValue, QName element, QName type, XMLStreamWriter writer) + throws XMLStreamException { + + if (propertyValue instanceof Document) { + Document document = (Document)propertyValue; + NodeList nodeList = document.getDocumentElement().getChildNodes(); + + for (int item = 0; item < nodeList.getLength(); ++item) { + Node node = nodeList.item(item); + int nodeType = node.getNodeType(); + if (nodeType == Node.ELEMENT_NODE) { + // Correct way to create a reader for a node object... + XMLStreamReader reader = staxHelper.createXMLStreamReader(node); + + while (reader.hasNext()) { + switch (reader.next()) { + case XMLStreamConstants.START_ELEMENT: + QName name = reader.getName(); + writer.writeStartElement(name.getPrefix(), name.getLocalPart(), name.getNamespaceURI()); + + int namespaces = reader.getNamespaceCount(); + boolean elementNamespaceAdded = false; + for (int i = 0; i < namespaces; i++) { + String prefix = reader.getNamespacePrefix(i); + String ns = reader.getNamespaceURI(i); + writer.writeNamespace(prefix, ns); + if(ns.equals(name.getNamespaceURI())){ + elementNamespaceAdded = true; + } + } + + if (!"".equals(name.getNamespaceURI()) && + !elementNamespaceAdded) { + writer.writeNamespace(name.getPrefix(), name.getNamespaceURI()); + } + + + // add the attributes for this element + namespaces = reader.getAttributeCount(); + for (int i = 0; i < namespaces; i++) { + String ns = reader.getAttributeNamespace(i); + String prefix = reader.getAttributePrefix(i); + String qname = reader.getAttributeLocalName(i); + String value = reader.getAttributeValue(i); + + writer.writeAttribute(prefix, ns, qname, value); + } + + break; + case XMLStreamConstants.CDATA: + writer.writeCData(reader.getText()); + break; + case XMLStreamConstants.CHARACTERS: + writer.writeCharacters(reader.getText()); + break; + case XMLStreamConstants.END_ELEMENT: + writer.writeEndElement(); + break; + } + } + } else { + writer.writeCharacters(node.getTextContent()); + } + } + } + } // end method writePropertyValue + + + /** + * Returns the model factory extension point to use. + * + * @param extensionPoints + * @return + */ + private static FactoryExtensionPoint modelFactories(ExtensionPointRegistry extensionPoints) { + return extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + } + + /** + * Property elements can have XSD types attributes so, in the case of a complex type, we need to find + * the XSD definition that defines that type in the contribution while we still have access to the + * contribution. Later, in the builder, we use this XSD definition to ensure that the property value + * is of the correct type + * + * @param property + * @param contribution + */ + private void resolvePropertyType(String parentName, Property property, Contribution contribution, ProcessorContext context){ + // resolve the reference to a complex property + // we ignore any types in the schema namespace + if (property.getXSDType() != null && + property.getXSDType().getNamespaceURI().equals("http://www.w3.org/2001/XMLSchema") != true){ + XSDefinition xsdDefinition = xsdFactory.createXSDefinition(); + xsdDefinition.setUnresolved(true); + xsdDefinition.setNamespace(property.getXSDType().getNamespaceURI()); + // some unit tests don't set up contribution and model resolvers properly + if (contribution != null && contribution.getModelResolver() != null) { + XSDefinition resolved = contribution.getModelResolver().resolveModel(XSDefinition.class, xsdDefinition, context); + if (resolved == null || resolved.isUnresolved()){ + // raise an error + // [rfeng] The XSD might be not available if we use JAXB annotated classes, report it as a warning for now + warning(context.getMonitor(), "PropertyTypeNotFound", property, property.getXSDType().toString(), property.getName(), parentName); + } else { + // store the schema in the property + property.setXSDDefinition(resolved); + } + } + } + } + +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConfiguredOperationProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConfiguredOperationProcessor.java new file mode 100644 index 0000000000..9930096a26 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConfiguredOperationProcessor.java @@ -0,0 +1,108 @@ +/* + * 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.assembly.xml; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; + +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.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.ConfiguredOperation; +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; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.policy.PolicyFactory; + +/** + * Processor for dealing with 'operation' elements from composite definitions + * + * @version $Rev$ $Date$ + * @tuscany.spi.extension.asclient + */ +public class ConfiguredOperationProcessor implements StAXArtifactProcessor, Constants{ + + private AssemblyFactory assemblyFactory; + private PolicySubjectProcessor policyProcessor; + private PolicyFactory policyFactory; + + + public ConfiguredOperationProcessor(FactoryExtensionPoint modelFactories) { + this.assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); + this.policyFactory = modelFactories.getFactory(PolicyFactory.class); + this.policyProcessor = new PolicySubjectProcessor(policyFactory); + } + + public ConfiguredOperation read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException { + ConfiguredOperation configuredOp = assemblyFactory.createConfiguredOperation(); + + //Read an + configuredOp.setName(reader.getAttributeValue(null, NAME)); + configuredOp.setContractName(reader.getAttributeValue(null, SERVICE)); + configuredOp.setUnresolved(true); + + // Read policies + policyProcessor.readPolicies(configuredOp, reader); + + //Skip to end element + while (reader.hasNext()) { + if (reader.next() == END_ELEMENT && OPERATION_QNAME.equals(reader.getName())) { + break; + } + } + + return configuredOp; + } + + public void write(ConfiguredOperation configuredOperation, XMLStreamWriter writer, ProcessorContext context) + throws ContributionWriteException, XMLStreamException { + + // Write an + writer.writeStartElement(Constants.SCA11_NS, OPERATION); + policyProcessor.writePolicyAttributes(configuredOperation, writer); + + writer.writeAttribute(NAME, configuredOperation.getName()); + if ( configuredOperation.getContractName() != null ) { + writer.writeAttribute(SERVICE, configuredOperation.getContractName()); + } + writer.writeEndElement(); + } + + public void resolve(ConfiguredOperation configuredOperation, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + } + + public QName getArtifactType() { + return OPERATION_QNAME; + } + + public Class getModelType() { + return ConfiguredOperation.class; + } + +} + + diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Constants.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Constants.java new file mode 100644 index 0000000000..b716ff44d9 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Constants.java @@ -0,0 +1,181 @@ +/* + * 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.assembly.xml; + +import javax.xml.namespace.QName; + +/** + * Constants used in SCA assembly XML files. + * + * @version $Rev$ $Date$ + * @tuscany.spi.extension + */ +public interface Constants { + String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200912"; + String SCA11_TUSCANY_NS = "http://tuscany.apache.org/xmlns/sca/1.1"; + + String COMPONENT_TYPE = "componentType"; + QName COMPONENT_TYPE_QNAME = new QName(SCA11_NS, COMPONENT_TYPE); + + String SERVICE = "service"; + QName SERVICE_QNAME = new QName(SCA11_NS, SERVICE); + + String REFERENCE = "reference"; + QName REFERENCE_QNAME = new QName(SCA11_NS, REFERENCE); + + String PROPERTY = "property"; + QName PROPERTY_QNAME = new QName(SCA11_NS, PROPERTY); + + String COMPOSITE = "composite"; + QName COMPOSITE_QNAME = new QName(SCA11_NS, COMPOSITE); + + String INCLUDE = "include"; + QName INCLUDE_QNAME = new QName(SCA11_NS, INCLUDE); + + String COMPONENT = "component"; + QName COMPONENT_QNAME = new QName(SCA11_NS, COMPONENT); + + String WIRE = "wire"; + QName WIRE_QNAME = new QName(SCA11_NS, WIRE); + + String OPERATION = "operation"; + QName OPERATION_QNAME = new QName(SCA11_NS, OPERATION); + + String CALLBACK = "callback"; + QName CALLBACK_QNAME = new QName(SCA11_NS, CALLBACK); + + String IMPLEMENTATION_COMPOSITE = "implementation.composite"; + QName IMPLEMENTATION_COMPOSITE_QNAME = new QName(SCA11_NS, IMPLEMENTATION_COMPOSITE); + + String IMPLEMENTATION = "implementation"; + QName IMPLEMENTATION_QNAME = new QName(SCA11_NS, IMPLEMENTATION); + + String BINDING_SCA = "binding.sca"; + QName BINDING_SCA_QNAME = new QName(Constants.SCA11_NS, BINDING_SCA); + + String EXTENSION = "extensions"; + QName EXTENSION_QNAME = new QName(Constants.SCA11_NS, EXTENSION); + + String NAME = "name"; + String VALUE = "value"; + QName VALUE_QNAME = new QName(SCA11_NS, VALUE); + + String POLICY_SET_ATTACHMENT = "policySetAttachment"; + QName POLICY_SET_ATTACHMENT_QNAME = new QName(SCA11_NS, POLICY_SET_ATTACHMENT); + + String TARGET_NAMESPACE = "targetNamespace"; + String LOCAL = "local"; + String AUTOWIRE = "autowire"; + String NONOVERRIDABLE = "nonOverridable"; + String REPLACE = "replace"; + String REQUIRES = "requires"; + QName REQUIRES_QNAME = new QName(SCA11_NS, REQUIRES); + String INTENTS = "intents"; + + String POLICY_SETS = "policySets"; + String PROMOTE = "promote"; + String TARGET = "target"; + String WIRED_BY_IMPL = "wiredByImpl"; + String MULTIPLICITY = "multiplicity"; + String TYPE = "type"; + String ELEMENT = "element"; + String MANY = "many"; + String MUST_SUPPLY = "mustSupply"; + String SOURCE = "source"; + String FILE = "file"; + String URI = "uri"; + String ZERO_ONE = "0..1"; + String ZERO_N = "0..n"; + String ONE_ONE = "1..1"; + String ONE_N = "1..n"; + + String SERVER_AUTHENTICATION = "serverAuthentication"; + QName SERVER_AUTHENTICATION_INTENT = new QName(SCA11_NS, SERVER_AUTHENTICATION); + String SERVER_AUTHENTICATION_TRANSPORT = "serverAuthentication.transport"; + QName SERVER_AUTHENTICATION_TRANSPORT_INTENT = new QName(SCA11_NS, SERVER_AUTHENTICATION_TRANSPORT); + String SERVER_AUTHENTICATION_MESSAGE = "serverAuthentication.message"; + QName SERVER_AUTHENTICATION_MESSAGE_INTENT = new QName(SCA11_NS, SERVER_AUTHENTICATION_MESSAGE); + + String CLIENT_AUTHENTICATION = "clientAuthentication"; + QName CLIENT_AUTHENTICATION_INTENT = new QName(SCA11_NS, CLIENT_AUTHENTICATION); + String CLIENT_AUTHENTICATION_TRANSPORT = "clientAuthentication.transport"; + QName CLIENT_AUTHENTICATION_TRANSPORT_INTENT = new QName(SCA11_NS, CLIENT_AUTHENTICATION_TRANSPORT); + String CLIENT_AUTHENTICATION_MESSAGE = "clientAuthentication.message"; + QName CLIENT_AUTHENTICATION_MESSAGE_INTENT = new QName(SCA11_NS, CLIENT_AUTHENTICATION_MESSAGE); + + String AUTHENTICATION = "authentication"; + QName AUTHENTICATION_INTENT = new QName(SCA11_NS, AUTHENTICATION); + + String MUTUAL_AUTHENTICATION = "mutualAuthentication"; + QName MUTUAL_AUTHENTICATION_INTENT = new QName(SCA11_NS, MUTUAL_AUTHENTICATION); + + String CONFIDENTIALITY = "confidentiality"; + QName CONFIDENTIALITY_INTENT = new QName(SCA11_NS, CONFIDENTIALITY); + String CONFIDENTIALITY_TRANSPORT = "confidentiality.transport"; + QName CONFIDENTIALITY_TRANSPORT_INTENT = new QName(SCA11_NS, CONFIDENTIALITY_TRANSPORT); + String CONFIDENTIALITY_MESSAGE = "confidentiality.message"; + QName CONFIDENTIALITY_MESSAGE_INTENT = new QName(SCA11_NS, CONFIDENTIALITY_MESSAGE); + + String INTEGRITY = "integrity"; + QName INTEGRITY_INTENT = new QName(SCA11_NS, INTEGRITY); + String INTEGRITY_TRANSPORT = "integrity.transport"; + QName INTEGRITY_TRANSPORT_INTENT = new QName(SCA11_NS, INTEGRITY_TRANSPORT); + String INTEGRITY_MESSAGE = "integrity.message"; + QName INTEGRITY_MESSAGE_INTENT = new QName(SCA11_NS, INTEGRITY_MESSAGE); + + String AUTHORIZATION = "authorization"; + QName AUTHORIZATION_INTENT = new QName(SCA11_NS, AUTHORIZATION); + String AUTHORIZATION_FINE_GRAIN = "authorization.fineGrain"; + QName AUTHORIZATION_FINE_GRAIN_INTENT = new QName(SCA11_NS, AUTHORIZATION_FINE_GRAIN); + + String MANAGED_TRANSACTION = "managedTransaction"; + QName MANAGED_TRANSACTION_INTENT = new QName(SCA11_NS, MANAGED_TRANSACTION); + + String NO_MANAGED_TRANSACTION = "noManagedTransaction"; + QName NO_MANAGED_TRANSACTION_INTENT = new QName(SCA11_NS, NO_MANAGED_TRANSACTION); + + String MANAGED_TRANSACTION_LOCAL = "managedTransaction.local"; + QName MANAGED_TRANSACTION_LOCAL_INTENT = new QName(SCA11_NS, MANAGED_TRANSACTION_LOCAL); + + String MANAGED_TRANSACTION_GLOBAL = "managedTransaction.global"; + QName MANAGED_TRANSACTION_GLOBAL_INTENT = new QName(SCA11_NS, MANAGED_TRANSACTION_GLOBAL); + + String PROPAGATES_TRANSACTION = "propagatesTransaction"; + QName PROPAGATES_TRANSACTION_INTENT = new QName(SCA11_NS, PROPAGATES_TRANSACTION); + + String SUSPENDS_TRANSACTION = "suspendsTransaction"; + QName SUSPENDS_TRANSACTION_INTENT = new QName(SCA11_NS, SUSPENDS_TRANSACTION); + + String TRANSACTED_ONE_WAY = "transactedOneWay"; + QName TRANSACTED_ONE_WAY_INTENT = new QName(SCA11_NS, TRANSACTED_ONE_WAY); + + String IMMEDIATE_ONE_WAY = "immediateOneWay"; + QName IMMEDIATE_ONE_WAY_INTENT = new QName(SCA11_NS, IMMEDIATE_ONE_WAY); + + String NOLISTENER = "noListener"; + QName NOLISTENER_INTENT = new QName(SCA11_NS, NOLISTENER); + + String SOAP = "SOAP"; + QName SOAP_INTENT = new QName(SCA11_NS, SOAP); + String SOAP11 = "SOAP.v1_1"; + QName SOAP11_INTENT = new QName(SCA11_NS, SOAP11); + String SOAP12 = "SOAP.v1_2"; + QName SOAP12_INTENT = new QName(SCA11_NS, SOAP12); +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/DefaultBeanModelProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/DefaultBeanModelProcessor.java new file mode 100644 index 0000000000..e6e172a44b --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/DefaultBeanModelProcessor.java @@ -0,0 +1,257 @@ +/* + * 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.assembly.xml; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +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.assembly.Base; +import org.apache.tuscany.sca.assembly.ComponentType; +import org.apache.tuscany.sca.assembly.Implementation; +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; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.policy.PolicySubject; + +/** + * Default Model Processor for beans. + * + * @version $Rev$ $Date$ + */ +public class DefaultBeanModelProcessor extends BaseAssemblyProcessor implements StAXArtifactProcessor { + + private QName artifactType; + private Class modelClass; + private Object modelFactory; + private Method factoryMethod; + private Map setterMethods = new HashMap(); + private Map getterMethods = new HashMap(); + + public DefaultBeanModelProcessor(FactoryExtensionPoint modeFactories, + QName artifactType, + Class modelClass, + Object modelFactory) { + super(modeFactories, null); + this.artifactType = artifactType; + this.modelClass = modelClass; + this.modelFactory = modelFactory; + + // Introspect the factory class and bean model class + if (modelFactory != null) { + + // Find the model create method + for (Method method: modelFactory.getClass().getMethods()) { + if (method.getName().startsWith("create") && method.getReturnType() == modelClass) { + factoryMethod = method; + break; + } + } + } + + // Index the bean's setter methods + for (Method method: modelClass.getMethods()) { + Method getter; + String name = method.getName(); + if (name.startsWith("set") && name.length() > 3) { + + // Get the corresponding getter method + try { + getter = modelClass.getMethod("get" + name.substring(3)); + } catch (Exception e) { + getter = null; + continue; + } + + // Get the property name + name = name.substring(3); + if (name.length() > 1) { + if (!name.toUpperCase().equals(name)) { + name = name.substring(0, 1).toLowerCase() + name.substring(1); + } + } + } else { + continue; + } + + // Map an uppercase property name to a lowercase attribute name + if (name.toUpperCase().equals(name)) { + name = name.toLowerCase(); + } + + // Trim trailing _ from property names + if (name.endsWith("_")) { + name = name.substring(0, name.length()-1); + } + setterMethods.put(name, method); + getterMethods.put(name, getter); + } + } + + public T read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException { + + // Read an element + try { + + // Create a new instance of the model + Object model; + if (modelFactory != null) { + // Invoke the factory create method + model = factoryMethod.invoke(modelFactory); + } else { + // Invoke the model bean class default constructor + model = modelClass.newInstance(); + } + + // Initialize the bean properties with the attributes found in the + // XML element + for (int i = 0, n = reader.getAttributeCount(); i < n; i++) { + String attributeName = reader.getAttributeLocalName(i); + Method setter = setterMethods.get(attributeName); + if (setter != null) { + String value = null; + if (attributeName.equals("uri")){ + value = getURIString(reader, "uri"); + } else { + value = reader.getAttributeValue(i); + } + setter.invoke(model, value); + } + } + + // Read policies + policyProcessor.readPolicies(model, reader); + + // FIXME read extension elements + + // By default mark the model object unresolved + if (model instanceof Base) { + ((Base)model).setUnresolved(true); + } + + // Skip to end element + while (reader.hasNext()) { + if (reader.next() == END_ELEMENT && artifactType.equals(reader.getName())) { + break; + } + } + return (T) model; + + } catch (Exception e) { + ContributionReadException ce = new ContributionReadException(e); + error(context.getMonitor(), "ContributionReadException", reader, ce); + throw ce; + } + } + + public void write(T bean, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, XMLStreamException { + try { + // Write the bean properties as attributes + List attrs = new ArrayList(); + for (Map.Entry entry: getterMethods.entrySet()) { + if (entry.getValue().getReturnType() == String.class) { + String value = (String)entry.getValue().invoke(bean); + attrs.add(new XAttr(entry.getKey(), value)); + } + } + + // Write element + writeStart(writer, artifactType.getNamespaceURI(), artifactType.getLocalPart(), + policyProcessor.writePolicies(bean), new XAttr(null, attrs)); + + writeEnd(writer); + + } catch (Exception e) { + ContributionWriteException ce = new ContributionWriteException(e); + error(context.getMonitor(), "ContributionWriteException", writer, ce); + throw ce; + } + } + + public void resolve(T bean, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + + // Resolve and merge the component type associated with an + // implementation model + if (bean instanceof Implementation) { + Implementation implementation = (Implementation)bean; + String uri = implementation.getURI(); + if (uri != null) { + int d = uri.lastIndexOf('.'); + if (d != -1) { + uri = uri.substring(0, d) + ".componentType"; + + // Resolve the component type + ComponentType componentType = assemblyFactory.createComponentType(); + componentType.setURI(uri); + componentType.setUnresolved(true); + + componentType = resolver.resolveModel(ComponentType.class, componentType, context); + if (componentType != null && !componentType.isUnresolved()) { + + // We found a component type, merge it into the implementation model + implementation.getServices().addAll(componentType.getServices()); + implementation.getReferences().addAll(componentType.getReferences()); + implementation.getProperties().addAll(componentType.getProperties()); + + if (implementation instanceof PolicySubject && + componentType instanceof PolicySubject ) { + PolicySubject policiedImpl = (PolicySubject)implementation; + PolicySubject policiedCompType = (PolicySubject)componentType; + + if ( policiedImpl.getPolicySets() != null) { + policiedImpl.getPolicySets().addAll(policiedCompType.getPolicySets()); + } + if (policiedImpl.getRequiredIntents() != null) { + policiedImpl.getRequiredIntents().addAll(policiedCompType.getRequiredIntents()); + } + } + } + } + } + } + + // Mark the model resolved + if (bean instanceof Base) { + ((Base)bean).setUnresolved(false); + } + } + + public QName getArtifactType() { + return artifactType; + } + + public Class getModelType() { + return modelClass; + } + +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/EndpointProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/EndpointProcessor.java new file mode 100644 index 0000000000..6bfc775117 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/EndpointProcessor.java @@ -0,0 +1,185 @@ +/* + * 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.assembly.xml; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.assembly.Callback; +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.EndpointReference; +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.processor.StAXAttributeProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.policy.PolicySet; + +/** + * + */ +public class EndpointProcessor extends BaseAssemblyProcessor implements StAXArtifactProcessor { + private final static String ENDPOINT = "endpoint"; + private final static QName ENDPOINT_QNAME = new QName(Constants.SCA11_TUSCANY_NS, ENDPOINT); + private final static String CALLBACK_ENDPOINT = "callbackEndpoint"; + private final static QName CALLBACK_ENDPOINT_QNAME = new QName(Constants.SCA11_TUSCANY_NS, CALLBACK_ENDPOINT); + + private ExtensionPointRegistry registry; + + public EndpointProcessor(ExtensionPointRegistry registry, + StAXArtifactProcessor extensionProcessor, + StAXAttributeProcessor extensionAttributeProcessor) { + + super(modelFactories(registry), extensionProcessor); + this.registry = registry; + } + + /** + * Returns the model factory extension point to use. + * + * @param extensionPoints + * @return + */ + private static FactoryExtensionPoint modelFactories(ExtensionPointRegistry extensionPoints) { + return extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + } + + public QName getArtifactType() { + return ENDPOINT_QNAME; + } + + public Endpoint read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException { + Endpoint endpoint = assemblyFactory.createEndpoint(); + if (reader.getEventType() == XMLStreamConstants.START_DOCUMENT) { + reader.nextTag(); + } + if (reader.getEventType() == XMLStreamConstants.START_ELEMENT && ENDPOINT_QNAME.equals(reader.getName())) { + // Skip the "endpoint" element wrapper + reader.nextTag(); + } + Object model = extensionProcessor.read(reader, context); + if (model instanceof Composite) { + Composite composite = (Composite)model; + Component component = composite.getComponents().get(0); + ComponentService service = component.getServices().get(0); + Binding binding = service.getBindings().isEmpty() ? null : service.getBindings().get(0); + endpoint.setComponent(component); + endpoint.setService(service); + endpoint.setBinding(binding); + + // We use the name of the composite to indicate if this is a callback endpoint + // saves passing other extension attributes + if (composite.getName().equals(CALLBACK_ENDPOINT_QNAME)){ + service.setForCallback(true); + } + + // retrieve the stash of intents and policy sets from the component + endpoint.getRequiredIntents().addAll(component.getRequiredIntents()); + endpoint.getPolicySets().addAll(component.getPolicySets()); + } + return endpoint; + } + + public void write(Endpoint model, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, XMLStreamException { + // writeStart(writer, ENDPOINT_QNAME); + extensionProcessor.write(wrap(model), writer, context); + // writeEnd(writer); + } + + private Composite wrap(Endpoint endpoint) { + try { + Composite composite = assemblyFactory.createComposite(); + // We use the name of the composite (which just wraps the single endpoint + // to indicate whether this endpoint represents a callback or not + if (endpoint.getService() != null && + endpoint.getService().isForCallback()){ + composite.setName(CALLBACK_ENDPOINT_QNAME); + } else { + composite.setName(ENDPOINT_QNAME); + } + composite.setLocal(false); + if (endpoint.getComponent() != null) { + Component component = (Component)endpoint.getComponent().clone(); + component.setImplementation(null); + composite.getComponents().add(component); + component.getReferences().clear(); + component.getServices().clear(); + + // stash endpoint intents and policy sets on the component so that they are all + // in one place + component.getRequiredIntents().clear(); + component.getRequiredIntents().addAll(endpoint.getRequiredIntents()); + component.getPolicySets().clear(); + component.getPolicySets().addAll(endpoint.getPolicySets()); + + if (endpoint.getService() != null) { + ComponentService service = (ComponentService)endpoint.getService().clone(); + component.getServices().add(service); + service.getBindings().clear(); + service.setInterfaceContract(endpoint.getComponentServiceInterfaceContract()); + if (endpoint.getBinding() != null) { + Binding binding = (Binding)endpoint.getBinding().clone(); + service.getBindings().add(binding); + } + // put both manually configured AND automatically generated callback bindings + // into the wrapping model so that we can pass callback configuarion via + // the registry + if (service.getCallbackReference() != null) { + Callback callback = service.getCallback(); + if(callback == null){ + callback = assemblyFactory.createCallback(); + } + for (EndpointReference epr : service.getCallbackReference().getEndpointReferences()){ + callback.getBindings().add(epr.getBinding()); + } + service.setCallback(callback); + } + } + } + return composite; + } catch (CloneNotSupportedException e) { + return null; + } + } + + public Class getModelType() { + return Endpoint.class; + } + + public void resolve(Endpoint model, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + // the only thing we'll resolve here is the policy model as the endpoint + // matching algorithm needs to look inside the policy model + + for (PolicySet policySet : model.getPolicySets()){ + extensionProcessor.resolve(policySet, resolver, context); + } + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/EndpointReferenceProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/EndpointReferenceProcessor.java new file mode 100644 index 0000000000..eda90c9d44 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/EndpointReferenceProcessor.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.assembly.xml; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +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.Endpoint; +import org.apache.tuscany.sca.assembly.EndpointReference; +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.processor.StAXAttributeProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; + +/** + * + */ +public class EndpointReferenceProcessor extends BaseAssemblyProcessor implements StAXArtifactProcessor { + private final static String ENDPOINT_REFERENCE = "endpointReference"; + private final static QName ENDPOINT_REFERENCE_QNAME = new QName(Constants.SCA11_TUSCANY_NS, ENDPOINT_REFERENCE); + + private ExtensionPointRegistry registry; + + public EndpointReferenceProcessor(ExtensionPointRegistry registry, + StAXArtifactProcessor extensionProcessor, + StAXAttributeProcessor extensionAttributeProcessor) { + + super(modelFactories(registry), extensionProcessor); + this.registry = registry; + } + + /** + * Returns the model factory extension point to use. + * + * @param extensionPoints + * @return + */ + private static FactoryExtensionPoint modelFactories(ExtensionPointRegistry extensionPoints) { + return extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + } + + public QName getArtifactType() { + return ENDPOINT_REFERENCE_QNAME; + } + + public EndpointReference read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException { + EndpointReference endpointReference = assemblyFactory.createEndpointReference(); + if (reader.getEventType() == XMLStreamConstants.START_DOCUMENT) { + reader.nextTag(); + } + Object model = extensionProcessor.read(reader, context); + if (model instanceof Composite) { + Composite composite = (Composite)model; + Component component = composite.getComponents().get(0); + ComponentReference reference = component.getReferences().get(0); + Binding binding = reference.getBindings().get(0); + endpointReference.setComponent(component); + endpointReference.setReference(reference); + reference.getEndpointReferences().add(endpointReference); + endpointReference.setBinding(binding); + + // set up the EPR so that resolution will happen + // at wire creation time if needs be + Endpoint endpoint = assemblyFactory.createEndpoint(); + endpointReference.setTargetEndpoint(endpoint); + + if (reference.getTargets().size() > 0){ + // create a dummy endpoint with the URI set so that + // the endpoint registry will be consulted + endpoint.setUnresolved(true); + endpoint.setURI(reference.getTargets().get(0).getName()); + endpointReference.setStatus(EndpointReference.Status.WIRED_TARGET_NOT_FOUND); + endpointReference.setUnresolved(true); + } else { + endpoint.setUnresolved(false); + endpoint.setBinding(reference.getBindings().get(0)); + endpointReference.setStatus(EndpointReference.Status.RESOLVED_BINDING); + endpointReference.setUnresolved(false); + } + } + return endpointReference; + } + + public void write(EndpointReference model, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, XMLStreamException { + extensionProcessor.write(wrap(model), writer, context); + } + + private Composite wrap(EndpointReference endpointReference) { + try { + Composite composite = assemblyFactory.createComposite(); + composite.setName(ENDPOINT_REFERENCE_QNAME); + composite.setLocal(false); + Component component = (Component)endpointReference.getComponent().clone(); + composite.getComponents().add(component); + component.getReferences().clear(); + component.getServices().clear(); + ComponentReference reference = (ComponentReference)endpointReference.getReference().clone(); + component.getReferences().add(reference); + reference.getBindings().clear(); + Binding binding = (Binding)endpointReference.getBinding().clone(); + reference.getBindings().add(binding); + //reference.setInterfaceContract(endpointReference.getInterfaceContract()); + if (endpointReference.getStatus() != EndpointReference.Status.RESOLVED_BINDING){ + ComponentService service = assemblyFactory.createComponentService(); + service.setName(endpointReference.getTargetEndpoint().getURI()); + reference.getTargets().clear(); + reference.getTargets().add(service); + } + return composite; + } catch (CloneNotSupportedException e) { + return null; + } + } + + public Class getModelType() { + return EndpointReference.class; + } + + public void resolve(EndpointReference model, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/InterfaceContractProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/InterfaceContractProcessor.java new file mode 100644 index 0000000000..3cdc956cfd --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/InterfaceContractProcessor.java @@ -0,0 +1,373 @@ +/* + * 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.assembly.xml; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; +import static org.apache.tuscany.sca.assembly.xml.Constants.SCA11_TUSCANY_NS; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamConstants; +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; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; +import org.apache.tuscany.sca.interfacedef.impl.InterfaceImpl; +import org.apache.tuscany.sca.interfacedef.impl.OperationImpl; +import org.apache.tuscany.sca.interfacedef.impl.TuscanyInterfaceContractImpl; +import org.apache.tuscany.sca.interfacedef.util.XMLType; + +/** + * Processor for reading/writing the Tuscany interface model in order to + * support distributed interface matching + * + */ +public class InterfaceContractProcessor extends BaseAssemblyProcessor implements StAXArtifactProcessor, Constants{ + + String INTERFACE_CONTRACT = "interfaceContract"; + QName INTERFACE_CONTRACT_QNAME = new QName(SCA11_TUSCANY_NS, INTERFACE_CONTRACT); + String INTERFACE = "interface"; + QName INTERFACE_QNAME = new QName(SCA11_TUSCANY_NS, INTERFACE); + String CALLBACK_INTERFACE = "callbackInterface"; + QName CALLBACK_INTERFACE_QNAME = new QName(SCA11_TUSCANY_NS, CALLBACK_INTERFACE); + String OPERATION = "operation"; + QName OPERATION_QNAME = new QName(SCA11_TUSCANY_NS, OPERATION); + String INPUT = "input"; + QName INPUT_QNAME = new QName(SCA11_TUSCANY_NS, INPUT); + String OUTPUT = "output"; + QName OUTPUT_QNAME = new QName(SCA11_TUSCANY_NS, OUTPUT); + String FAULT = "fault"; + QName FAULT_QNAME = new QName(SCA11_TUSCANY_NS, FAULT); + String DATATYPE = "dataType"; + QName DATATYPE_QNAME = new QName(SCA11_TUSCANY_NS, DATATYPE); + String GENERIC = "generic"; + QName GENERIC_QNAME = new QName(SCA11_TUSCANY_NS, GENERIC); + String LOGICAL_COLLECTION = "logicalCollection"; + QName LOGICAL_COLLECTION_QNAME = new QName(SCA11_TUSCANY_NS, LOGICAL_COLLECTION); + String LOGICAL_XMLTYPE = "logicalXMLType"; + QName LOGICAL_XMLTYPE_QNAME = new QName(SCA11_TUSCANY_NS, LOGICAL_XMLTYPE); + String LOGICAL_TYPE = "logicalType"; + QName LOGICAL_TYPE_QNAME = new QName(SCA11_TUSCANY_NS, LOGICAL_TYPE); + String PHYSICAL = "physical"; + QName PHYSICAL_QNAME = new QName(SCA11_TUSCANY_NS, PHYSICAL); + String XMLTYPE = "xmlType"; + QName XMLTYPE_QNAME = new QName(SCA11_TUSCANY_NS, XMLTYPE); + + String NO_TYPE = "NoType"; + + enum Iof { + UNSET, + INPUT, + OUTPUT, + FAULT + } + + enum CharacterTarget { + UNSET, + GENERIC, + PHYSICAL, + XMLTYPE + } + + public InterfaceContractProcessor(ExtensionPointRegistry registry) { + super(registry.getExtensionPoint(FactoryExtensionPoint.class), + null /*registry.getExtensionPoint(StAXArtifactProcessor.class)*/); + } + + public InterfaceContract read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException { + + TuscanyInterfaceContractImpl interfaceContract = new TuscanyInterfaceContractImpl(); + + try { + InterfaceImpl iface = null; + QName name = null; + Operation operation = null; + List inputs = null; + List outputs = null; + List faults = null; + XMLType logicalXMLType = null; + + Iof iof = Iof.UNSET; + CharacterTarget characterTarget = CharacterTarget.UNSET; + + boolean logicalCollection = false; + + DataType dataType = null; + + while (reader.hasNext()) { + int event = reader.getEventType(); + + switch (event) { + case START_ELEMENT: + name = reader.getName(); + if (INTERFACE_CONTRACT_QNAME.equals(name)){ + } else if (INTERFACE_QNAME.equals(name)){ + iface = new InterfaceImpl(); + interfaceContract.setInterface(iface); + iface.setRemotable(getBoolean(reader, "isRemotable")); + } else if (CALLBACK_INTERFACE_QNAME.equals(name)){ + iface = new InterfaceImpl(); + interfaceContract.setCallbackInterface(iface); + iface.setRemotable(getBoolean(reader, "isRemotable")); + } else if (OPERATION_QNAME.equals(name)) { + operation = new OperationImpl(); + iface.getOperations().add(operation); + + operation.setName(getString(reader, "name")); + operation.setDynamic(getBoolean(reader, "isDynamic")); + operation.setNonBlocking(getBoolean(reader, "isNonBlocking")); + operation.setInputWrapperStyle(getBoolean(reader, "isInputWrapperStyle")); + operation.setOutputWrapperStyle(getBoolean(reader, "isOutputWrapperStyle")); + + inputs = new ArrayList(); + DataType inputType = new DataTypeImpl>(null, null); + inputType.setLogical(inputs); + operation.setInputType(inputType); + + outputs = new ArrayList(); + DataType outputType = new DataTypeImpl>(null, null); + outputType.setLogical(outputs); + operation.setOutputType(outputType); + + faults = new ArrayList(); + operation.setFaultTypes(faults); + } else if (INPUT_QNAME.equals(name)) { + iof = Iof.INPUT; + } else if (OUTPUT_QNAME.equals(name)) { + iof = Iof.OUTPUT; + } else if (FAULT_QNAME.equals(name)) { + iof = Iof.FAULT; + } else if (DATATYPE_QNAME.equals(name)){ + DataType newDataType = new DataTypeImpl(null, null); + newDataType.setDataBinding(getString(reader, "dataBinding")); + if (logicalCollection) { + dataType.setLogical(newDataType); + dataType = newDataType; + } else if (iof == Iof.INPUT) { + inputs.add(newDataType); + dataType = newDataType; + } else if (iof == Iof.OUTPUT){ + outputs.add(newDataType); + dataType = newDataType; + } else if (iof == Iof.FAULT){ + faults.add(newDataType); + dataType = newDataType; + } + } else if (GENERIC_QNAME.equals(name)){ + characterTarget = CharacterTarget.GENERIC; + } else if (PHYSICAL_QNAME.equals(name)){ + characterTarget = CharacterTarget.PHYSICAL; + } else if (LOGICAL_COLLECTION_QNAME.equals(name)){ + logicalCollection = true; + } else if (LOGICAL_XMLTYPE_QNAME.equals(name)){ + characterTarget = CharacterTarget.XMLTYPE; + logicalXMLType = new XMLType(null, null); + dataType.setLogical(logicalXMLType); + } else if (LOGICAL_TYPE_QNAME.equals(name)){ + // is this ever used? + } else if (XMLTYPE_QNAME.equals(name)){ + // is this ever used? + } else { + System.out.println("Unexpected element " + name); + } + break; + case XMLStreamConstants.CHARACTERS: + if (characterTarget == CharacterTarget.GENERIC){ + String generic = reader.getText(); + // Not sure what to do with this as we may not have the actual type + } else if (characterTarget == CharacterTarget.PHYSICAL){ + String physical = reader.getText(); + // Not sure what to do with this as we may not have the actual type + } else if (characterTarget == CharacterTarget.XMLTYPE) { + String xmlType = reader.getText(); + if (!xmlType.equals(NO_TYPE)){ + int splitPoint = xmlType.indexOf("}"); + String namespace = xmlType.substring(1, splitPoint); + String localname = xmlType.substring(splitPoint + 1); + QName typeName = new QName(namespace, localname); + logicalXMLType.setTypeName(typeName); + } + } + characterTarget = CharacterTarget.UNSET; + break; + case END_ELEMENT: + name = reader.getName(); + if (INPUT_QNAME.equals(name) || + OUTPUT_QNAME.equals(name) || + FAULT_QNAME.equals(name)) { + iof = Iof.UNSET; + } else if (LOGICAL_COLLECTION_QNAME.equals(name)) { + logicalCollection = false; + } + } + + // Read the next element + if (reader.hasNext()) { + reader.next(); + } + } + } catch (XMLStreamException e) { + ContributionReadException ex = new ContributionReadException(e); + //error(monitor, "XMLStreamException", reader, ex); + } + + return interfaceContract; + } + + public void write(InterfaceContract interfaceContract, XMLStreamWriter writer, ProcessorContext context) + throws ContributionWriteException, XMLStreamException { + + if (interfaceContract == null || interfaceContract.getInterface() == null) { + return; + } + + writer.writeStartElement(Constants.SCA11_TUSCANY_NS, INTERFACE_CONTRACT); + writer.writeStartElement(Constants.SCA11_TUSCANY_NS, INTERFACE); + writeInterface(interfaceContract.getInterface(), writer, context); + + if (interfaceContract.getCallbackInterface() != null){ + writer.writeStartElement(Constants.SCA11_TUSCANY_NS, CALLBACK_INTERFACE); + writeInterface(interfaceContract.getCallbackInterface(), writer, context); + } + writer.writeEndElement(); + + } + + private void writeInterface(Interface iface, XMLStreamWriter writer, ProcessorContext context) throws XMLStreamException { + + + writer.writeAttribute("isRemotable", String.valueOf(iface.isRemotable())); + + for (Operation operation : iface.getOperations()){ + writer.writeStartElement(Constants.SCA11_TUSCANY_NS, OPERATION); + writer.writeAttribute("name", operation.getName()); + writer.writeAttribute("isDynamic", String.valueOf(operation.isDynamic())); + writer.writeAttribute("isNonBlocking", String.valueOf(operation.isNonBlocking())); + writer.writeAttribute("isInputWrapperStyle", String.valueOf(operation.isInputWrapperStyle())); + writer.writeAttribute("isOutputWrapperStyle", String.valueOf(operation.isOutputWrapperStyle())); + + List outputTypes = operation.getOutputType().getLogical(); + List inputTypes = operation.getInputType().getLogical(); + List faultTypes = operation.getFaultTypes(); + + if (operation.isInputWrapperStyle() && operation.getInputWrapper() != null) { + inputTypes = operation.getInputWrapper().getUnwrappedType().getLogical(); + } + if (operation.isOutputWrapperStyle() && operation.getOutputWrapper() != null) { + outputTypes = operation.getOutputWrapper().getUnwrappedType().getLogical(); + } + + writer.writeStartElement(Constants.SCA11_TUSCANY_NS, INPUT); + writeDataTypes(inputTypes, writer); + writer.writeEndElement(); + + writer.writeStartElement(Constants.SCA11_TUSCANY_NS, OUTPUT); + writeDataTypes(outputTypes, writer); + writer.writeEndElement(); + + writer.writeStartElement(Constants.SCA11_TUSCANY_NS, FAULT); + writeDataTypes(faultTypes, writer); + writer.writeEndElement(); + + writer.writeEndElement(); + } + writer.writeEndElement(); + } + + private void writeDataTypes(List dataTypes, XMLStreamWriter writer) throws XMLStreamException { + for(DataType dataType : dataTypes){ + writeDataType(dataType, writer); + } + } + + private void writeDataType(DataType dataType, XMLStreamWriter writer) throws XMLStreamException { + writer.writeStartElement(Constants.SCA11_TUSCANY_NS, DATATYPE); + if (dataType.getDataBinding() != null){ + writer.writeAttribute("dataBinding", dataType.getDataBinding()); + } + + if (dataType.getGenericType() != null){ + writer.writeStartElement(Constants.SCA11_TUSCANY_NS, GENERIC); + writer.writeCharacters(dataType.getGenericType().toString()); + writer.writeEndElement(); + } + + if (dataType.getLogical() instanceof DataType){ + writer.writeStartElement(Constants.SCA11_TUSCANY_NS, LOGICAL_COLLECTION); + writeDataType((DataType)dataType.getLogical(), writer); + } else if (dataType.getLogical() instanceof XMLType){ + writer.writeStartElement(Constants.SCA11_TUSCANY_NS, LOGICAL_XMLTYPE); + writer.writeStartElement(Constants.SCA11_TUSCANY_NS, XMLTYPE); + XMLType xmlType = (XMLType)dataType.getLogical(); + if (xmlType.getTypeName() != null){ + writer.writeCharacters(xmlType.getTypeName().toString()); + } else { + writer.writeCharacters("NoType"); + } + writer.writeEndElement(); + } else { + writer.writeStartElement(Constants.SCA11_TUSCANY_NS, LOGICAL_TYPE); + writer.writeCharacters(dataType.getLogical().toString()); + } + writer.writeEndElement(); + + if (dataType.getPhysical() != null){ + writer.writeStartElement(Constants.SCA11_TUSCANY_NS, PHYSICAL); + writer.writeCharacters(dataType.getPhysical().getName()); + writer.writeEndElement(); + } + + writer.writeEndElement(); + + } + + public void resolve(InterfaceContract interfaceContract, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + // do nothing + } + + public QName getArtifactType() { + // these internal interface contracts aren't found in the composite file + return null; + } + + public Class getModelType() { + return InterfaceContract.class; + } + +} + + diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Messages.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Messages.java new file mode 100644 index 0000000000..0391acc676 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Messages.java @@ -0,0 +1,28 @@ +/* + * 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.assembly.xml; + +/** + * Constants for resource bundle names + * @tuscany.spi.extension + */ +public interface Messages { + String RESOURCE_BUNDLE = "org.apache.tuscany.sca.assembly.xml.assembly-xml-validation-messages"; +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/PolicySubjectProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/PolicySubjectProcessor.java new file mode 100644 index 0000000000..286f109f44 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/PolicySubjectProcessor.java @@ -0,0 +1,242 @@ +/* + * 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.assembly.xml; + +import static org.apache.tuscany.sca.assembly.xml.Constants.POLICY_SETS; +import static org.apache.tuscany.sca.assembly.xml.Constants.REQUIRES; + +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +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.BaseStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +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.policy.Intent; +import org.apache.tuscany.sca.policy.PolicyFactory; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySubject; + +/** + * A Policy Attach Point processor. + * + * @version $Rev$ $Date$ + * @tuscany.spi.extension.asclient + */ +public class PolicySubjectProcessor extends BaseStAXArtifactProcessor { + + private PolicyFactory policyFactory; + + public PolicySubjectProcessor(PolicyFactory policyFactory) { + this.policyFactory = policyFactory; + } + + public PolicySubjectProcessor(ExtensionPointRegistry registry) { + FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.policyFactory = factories.getFactory(PolicyFactory.class); + } + + /** + * Read policy intents associated with an operation. + * @param subject + * @param operation + * @param reader + */ + private void readIntents(Object subject, Operation operation, XMLStreamReader reader) { + if (!(subject instanceof PolicySubject)) + return; + PolicySubject intentAttachPoint = (PolicySubject)subject; + String value = reader.getAttributeValue(null, REQUIRES); + if (value != null) { + List requiredIntents = intentAttachPoint.getRequiredIntents(); + for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) { + QName qname = getQNameValue(reader, tokens.nextToken()); + Intent intent = policyFactory.createIntent(); + intent.setName(qname); + if (operation != null) { + //FIXME Don't we need to handle intent specification + // on an operation basis? + //intent.getOperations().add(operation); + } + requiredIntents.add(intent); + } + } + } + + /** + * Reads policy intents and policy sets associated with an operation. + * @param subject + * @param operation + * @param reader + */ + public void readPolicies(Object subject, Operation operation, XMLStreamReader reader) { + readIntents(subject, operation, reader); + readPolicySets(subject, operation, reader); + } + + /** + * Reads policy intents and policy sets. + * @param subject + * @param reader + */ + public void readPolicies(Object subject, XMLStreamReader reader) { + readPolicies(subject, null, reader); + } + + /** + * Reads policy sets associated with an operation. + * @param subject + * @param operation + * @param reader + */ + private void readPolicySets(Object subject, Operation operation, XMLStreamReader reader) { + if (!(subject instanceof PolicySubject)) { + return; + } + PolicySubject policySubject = (PolicySubject)subject; + String value = reader.getAttributeValue(null, POLICY_SETS); + if (value != null) { + List policySets = policySubject.getPolicySets(); + for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) { + QName qname = getQNameValue(reader, tokens.nextToken()); + PolicySet policySet = policyFactory.createPolicySet(); + policySet.setName(qname); + if (operation != null) { + //FIXME Don't we need to handle policySet specification + // on an operation basis? + //policySet.getOperations().add(operation); + } + policySets.add(policySet); + } + } + } + + /** + * Write policies + * @param subject + * @return + */ + XAttr writePolicies(Object subject) throws XMLStreamException { + return writePolicies(subject, (Operation)null); + } + + /** + * Write policies + * @param subject + * @return + */ + public void writePolicyAttributes(Object subject, XMLStreamWriter writer) throws XMLStreamException { + writePolicyAttributes(subject, (Operation)null, writer); + } + + /** + * Write policies associated with an operation + * @param subject + * @param operation + * @return + */ + XAttr writePolicies(Object subject, Operation operation) { + List attrs =new ArrayList(); + attrs.add(writeIntents(subject, operation)); + attrs.add(writePolicySets(subject, operation)); + return new XAttr(null, attrs); + } + + /** + * Write policies + * @param subject + * @return + */ + public void writePolicyAttributes(Object subject, Operation operation, XMLStreamWriter writer) throws XMLStreamException { + XAttr attr = writePolicies(subject, operation); + attr.write(writer); + } + + /** + * Write policy intents associated with an operation. + * @param subject + * @param operation + */ + private XAttr writeIntents(Object subject, Operation operation) { + if (!(subject instanceof PolicySubject)) { + return null; + } + PolicySubject intentAttachPoint = (PolicySubject)subject; + List qnames = new ArrayList(); + for (Intent intent: intentAttachPoint.getRequiredIntents()) { + qnames.add(intent.getName()); + } + return new XAttr(Constants.REQUIRES, qnames); + } + + /** + * Write policy sets associated with an operation. + * @param subject + * @param operation + */ + private XAttr writePolicySets(Object subject, Operation operation) { + if (!(subject instanceof PolicySubject)) { + return null; + } + PolicySubject policySetAttachPoint = (PolicySubject)subject; + List qnames = new ArrayList(); + for (PolicySet policySet: policySetAttachPoint.getPolicySets()) { + qnames.add(policySet.getName()); + } + return new XAttr(Constants.POLICY_SETS, qnames); + } + + public void resolvePolicies(Object subject, ModelResolver resolver, ProcessorContext context) { + if ( subject instanceof PolicySubject ) { + PolicySubject policySetAttachPoint = (PolicySubject)subject; + + List requiredIntents = new ArrayList(); + Intent resolvedIntent = null; + + if ( policySetAttachPoint.getRequiredIntents() != null && policySetAttachPoint.getRequiredIntents().size() > 0 ) { + for ( Intent intent : policySetAttachPoint.getRequiredIntents() ) { + resolvedIntent = resolver.resolveModel(Intent.class, intent, context); + requiredIntents.add(resolvedIntent); + } + policySetAttachPoint.getRequiredIntents().clear(); + policySetAttachPoint.getRequiredIntents().addAll(requiredIntents); + } + + if ( policySetAttachPoint.getPolicySets() != null && policySetAttachPoint.getPolicySets().size() > 0 ) { + List resolvedPolicySets = new ArrayList(); + PolicySet resolvedPolicySet = null; + for ( PolicySet policySet : policySetAttachPoint.getPolicySets() ) { + resolvedPolicySet = resolver.resolveModel(PolicySet.class, policySet, context); + resolvedPolicySets.add(resolvedPolicySet); + } + policySetAttachPoint.getPolicySets().clear(); + policySetAttachPoint.getPolicySets().addAll(resolvedPolicySets); + } + } + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/SCABindingProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/SCABindingProcessor.java new file mode 100644 index 0000000000..f0cbcc669f --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/SCABindingProcessor.java @@ -0,0 +1,178 @@ +/* + * 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.assembly.xml; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; + +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.assembly.SCABinding; +import org.apache.tuscany.sca.assembly.SCABindingFactory; +import org.apache.tuscany.sca.assembly.WireFormat; +import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor; +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; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.policy.ExtensionType; +import org.apache.tuscany.sca.policy.PolicyFactory; +import org.apache.tuscany.sca.policy.PolicySubject; + +/** + * A processor to read the XML that describes the SCA binding. + * + * @version $Rev$ $Date$ + */ + +public class SCABindingProcessor extends BaseStAXArtifactProcessor implements StAXArtifactProcessor { + private static final String NAME = "name"; + private static final String URI = "uri"; + private static final String DELEGATE_BINDING_TYPE = "delegateBindingType"; + private static final String DELEGATE_BINDING_URI = "delegateBindingURI"; + + private static final String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200912"; + private static final String BINDING_SCA = "binding.sca"; + private static final QName BINDING_SCA_QNAME = new QName(SCA11_NS, BINDING_SCA); + + private PolicyFactory policyFactory; + private SCABindingFactory scaBindingFactory; + private PolicySubjectProcessor policyProcessor; + private PolicyFactory intentAttachPointTypeFactory; + private StAXArtifactProcessor extensionProcessor; + + + public SCABindingProcessor(FactoryExtensionPoint modelFactories, StAXArtifactProcessor extensionProcessor) { + this.policyFactory = modelFactories.getFactory(PolicyFactory.class); + this.scaBindingFactory = modelFactories.getFactory(SCABindingFactory.class); + policyProcessor = new PolicySubjectProcessor(policyFactory); + this.intentAttachPointTypeFactory = modelFactories.getFactory(PolicyFactory.class); + this.extensionProcessor = extensionProcessor; + } + + public QName getArtifactType() { + return BINDING_SCA_QNAME; + } + + public Class getModelType() { + return SCABinding.class; + } + + public SCABinding read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException { + SCABinding scaBinding = scaBindingFactory.createSCABinding(); + ExtensionType bindingType = intentAttachPointTypeFactory.createBindingType(); + bindingType.setType(getArtifactType()); + bindingType.setUnresolved(true); + ((PolicySubject)scaBinding).setExtensionType(bindingType); + + // Read policies + policyProcessor.readPolicies(scaBinding, reader); + + // Read binding name + String name = reader.getAttributeValue(null, NAME); + if (name != null) { + scaBinding.setName(name); + } + + // Read binding URI + String uri = getURIString(reader, URI); + if (uri != null) { + scaBinding.setURI(uri); + } + + // Read delegate binding type + String delegateBindingType = getString(reader, DELEGATE_BINDING_TYPE); + if (delegateBindingType != null) { + scaBinding.setDelegateBindingType(delegateBindingType); + } + + // Read delegate binding URI + String delegateBindingURI = getURIString(reader, DELEGATE_BINDING_URI); + if (delegateBindingURI != null) { + scaBinding.setDelegateBindingURI(delegateBindingURI); + } + + // Read any sub-elements + boolean endFound = false; + while (reader.hasNext() && endFound == false) { + int nextElementType = reader.next(); + switch (nextElementType) { + case START_ELEMENT: + Object extension = extensionProcessor.read(reader, context); + if (extension != null) { + if (extension instanceof WireFormat) { + scaBinding.setRequestWireFormat((WireFormat)extension); + } + } + break; + case END_ELEMENT: + QName endElementName = reader.getName(); + if(endElementName.equals(endElementName)){ + endFound = true; + } + break; + } + } + return scaBinding; + } + + public void resolve(SCABinding model, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + policyProcessor.resolvePolicies(model, resolver, context); + } + + public void write(SCABinding scaBinding, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, XMLStreamException { + + // Write + writer.writeStartElement(SCA11_NS, BINDING_SCA); + policyProcessor.writePolicyAttributes(scaBinding, writer); + + // Write binding name + if (scaBinding.getName() != null) { + writer.writeAttribute(NAME, scaBinding.getName()); + } + + // Write binding URI + if (scaBinding.getURI() != null) { + writer.writeAttribute(URI, scaBinding.getURI()); + } + + // Write delegate binding type + if (scaBinding.getDelegateBindingType() != null) { + writer.writeAttribute(DELEGATE_BINDING_TYPE, scaBinding.getDelegateBindingType()); + } + + // Write delegate binding URI + if (scaBinding.getDelegateBindingURI() != null) { + writer.writeAttribute(DELEGATE_BINDING_URI, scaBinding.getDelegateBindingURI()); + } + + // write wireFormat + extensionProcessor.write(scaBinding.getRequestWireFormat(), writer, context); + + writer.writeEndElement(); + } + +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Utils.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Utils.java new file mode 100644 index 0000000000..694621fa24 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Utils.java @@ -0,0 +1,86 @@ +/* + * 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.assembly.xml; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.StringWriter; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.tuscany.sca.assembly.Base; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper; +import org.apache.tuscany.sca.contribution.processor.ContributionWriteException; +import org.apache.tuscany.sca.contribution.processor.ExtensibleStAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.w3c.dom.Document; +import org.w3c.dom.ls.DOMImplementationLS; +import org.w3c.dom.ls.LSOutput; +import org.w3c.dom.ls.LSSerializer; +import org.xml.sax.SAXException; + +public class Utils { + + /** + * Helper method to get the XML string for a model object. + */ + public static String modelToXML(Base model, boolean pretty, ExtensionPointRegistry extensionPointRegistry) { + try { + StAXHelper stAXHelper = StAXHelper.getInstance(extensionPointRegistry); + StAXArtifactProcessorExtensionPoint staxProcessors = extensionPointRegistry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + ExtensibleStAXArtifactProcessor staxProcessor = new ExtensibleStAXArtifactProcessor(staxProcessors, null, stAXHelper.getOutputFactory()); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + staxProcessor.write(model, bos, new ProcessorContext(extensionPointRegistry)); + bos.close(); + + if (!pretty) { + return bos.toString(); + } + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder parser = factory.newDocumentBuilder(); + Document document = parser.parse(new ByteArrayInputStream(bos.toByteArray())); + + DOMImplementationLS domImplementationLS = (DOMImplementationLS) document.getImplementation().getFeature("LS", "3.0"); + LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); + lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); + LSOutput lsOutput = domImplementationLS.createLSOutput(); + lsOutput.setEncoding("UTF-8"); + StringWriter stringWriter = new StringWriter(); + lsOutput.setCharacterStream(stringWriter); + lsSerializer.write(document, lsOutput); + return stringWriter.toString(); + + } catch (ContributionWriteException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } catch (SAXException e) { + throw new RuntimeException(e); + } catch (ParserConfigurationException e) { + throw new RuntimeException(e); + } + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefaultDefinitionsExtensionPoint.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefaultDefinitionsExtensionPoint.java new file mode 100644 index 0000000000..dfbc2e2cbf --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefaultDefinitionsExtensionPoint.java @@ -0,0 +1,135 @@ +/* + * 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.definitions.xml; + +import java.io.IOException; +import java.net.URI; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint; +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.extensibility.ServiceDeclaration; +import org.apache.tuscany.sca.monitor.MonitorFactory; + +/** + * Default implementation of an extension point for XML definitionss. + * + * @version $Rev$ $Date$ + */ +public class DefaultDefinitionsExtensionPoint implements DefinitionsExtensionPoint { + private static final Logger logger = Logger.getLogger(DefaultDefinitionsExtensionPoint.class.getName()); + private static final URI DEFINITIONS_URI = URI.create("META-INF/definitions.xml"); + private ExtensionPointRegistry registry; + private MonitorFactory monitorFactory; + private Set documents = new HashSet(); + private List definitions = new ArrayList(); + private boolean documentsLoaded; + private boolean loaded; + + public DefaultDefinitionsExtensionPoint(ExtensionPointRegistry registry) { + this.registry = registry; + this.monitorFactory = registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(MonitorFactory.class); + } + + public void addDefinitionsDocument(URL url) { + documents.add(url); + } + + public void removeDefinitionsDocument(URL url) { + documents.remove(url); + } + + /** + * Load definitions declarations from META-INF/services/ + * org.apache.tuscany.sca.contribution.processor.Definitions files + */ + private synchronized void loadDefinitionsDocuments() { + if (documentsLoaded) + return; + + // Get the definitions declarations + Collection definitionsDeclarations; + try { + // Get definitions declarations in ranking order. + definitionsDeclarations = registry.getServiceDiscovery().getServiceDeclarations(DEFINITIONS_FILE, true); + } catch (IOException e) { + throw new IllegalStateException(e); + } + + // Find each definitions + Set definitionResources = new HashSet(); + for (ServiceDeclaration definitionsDeclaration : definitionsDeclarations) { + // Only process the first instance of a given resource name. + // This allows definitions files to be overridden by an embedder. + String resourceName = definitionsDeclaration.getClassName(); + if (!definitionResources.contains(resourceName)) { + definitionResources.add(resourceName); + URL url = definitionsDeclaration.getResource(resourceName); + if (url == null) { + throw new IllegalArgumentException(definitionsDeclaration.getClassName() + " cannot be found"); + } + documents.add(url); + } + } + + documentsLoaded = true; + } + + public synchronized List getDefinitions() { + if (!loaded) { + loadDefinitionsDocuments(); + URLArtifactProcessorExtensionPoint processors = + registry.getExtensionPoint(URLArtifactProcessorExtensionPoint.class); + URLArtifactProcessor processor = processors.getProcessor(Definitions.class); + ProcessorContext context = new ProcessorContext(monitorFactory.createMonitor()); + for (URL url : documents) { + Definitions def; + try { + def = processor.read(null, DEFINITIONS_URI, url, context); + definitions.add(def); + } catch (ContributionReadException e) { + logger.log(Level.SEVERE, e.getMessage(), e); + } + } + loaded = true; + } + return definitions; + } + + public void addDefinitions(Definitions def) { + this.definitions.add(def); + } + + public void removeDefinitions(Definitions def) { + this.definitions.remove(def); + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsDocumentProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsDocumentProcessor.java new file mode 100644 index 0000000000..d0a42be34e --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsDocumentProcessor.java @@ -0,0 +1,171 @@ +/* + * 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.definitions.xml; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URL; +import java.security.AccessController; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.common.java.io.IOHelper; +import org.apache.tuscany.sca.contribution.processor.ContributionReadException; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ValidatingXMLInputFactory; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.definitions.Definitions; +import org.apache.tuscany.sca.definitions.DefinitionsFactory; +import org.apache.tuscany.sca.definitions.util.DefinitionsUtil; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.monitor.Problem; +import org.apache.tuscany.sca.monitor.Problem.Severity; + +/** + * A SCA Definitions Document processor. + * + * @version $Rev$ $Date$ + */ +public class DefinitionsDocumentProcessor implements URLArtifactProcessor { + private StAXArtifactProcessor extensionProcessor; + private XMLInputFactory inputFactory; + private DefinitionsFactory definitionsFactory; + + + + /** + * Constructs a new SCADefinitions processor. + * + * @param modelFactories + * @param staxProcessor + */ + public DefinitionsDocumentProcessor(FactoryExtensionPoint modelFactories, + StAXArtifactProcessor staxProcessor) { + this.extensionProcessor = (StAXArtifactProcessor)staxProcessor; + this.inputFactory = modelFactories.getFactory(ValidatingXMLInputFactory.class); + this.definitionsFactory = modelFactories.getFactory(DefinitionsFactory.class); + } + + /** + * Report a exception. + * + * @param problems + * @param message + * @param model + */ + private void error(Monitor monitor, String message, Object model, Exception ex) { + if (monitor != null) { + Problem problem = + monitor.createProblem(this.getClass().getName(), + Messages.RESOURCE_BUNDLE, + Severity.ERROR, + model, + message, + ex); + monitor.problem(problem); + } + } + + public Definitions read(URL contributionURL, final URI uri, final URL url, ProcessorContext context) throws ContributionReadException { + InputStream urlStream = null; + Monitor monitor = context.getMonitor(); + monitor.pushContext("Definitions: " + url); + try { + // Allow privileged access to open URL stream. Add FilePermission to added to security + // policy file. + try { + urlStream = AccessController.doPrivileged(new PrivilegedExceptionAction() { + public InputStream run() throws IOException { + return IOHelper.openStream(url); + } + }); + } catch (PrivilegedActionException e) { + error(monitor, "PrivilegedActionException", url, (IOException)e.getException()); + throw (IOException)e.getException(); + } + + //urlStream = createInputStream(url); + XMLStreamReader reader = inputFactory.createXMLStreamReader(url.toString(), urlStream); + ValidatingXMLInputFactory.setMonitor(reader, context.getMonitor()); + Definitions definitions = definitionsFactory.createDefinitions(); + int event = reader.getEventType(); + while (reader.hasNext()) { + event = reader.next(); + + // We only deal with the root element + if (event == XMLStreamConstants.START_ELEMENT) { + // QName name = reader.getName(); + Object model = extensionProcessor.read(reader, context); + if (model instanceof Definitions) { + DefinitionsUtil.aggregate((Definitions)model, definitions, monitor); + return definitions; + } else { + error(monitor, "ContributionReadException", model, null); + } + } + } + + return definitions; + } catch (XMLStreamException e) { + ContributionReadException ce = new ContributionReadException(e); + error(monitor, "ContributionReadException", inputFactory, ce); + throw ce; + } catch (IOException e) { + ContributionReadException ce = new ContributionReadException(e); + error(monitor, "ContributionReadException", inputFactory, ce); + throw ce; + } finally { + + try { + if (urlStream != null) { + urlStream.close(); + urlStream = null; + } + } catch (IOException ioe) { + //ignore + } + + monitor.popContext(); + } + } + + public void resolve(Definitions scaDefinitions, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + extensionProcessor.resolve(scaDefinitions, resolver, context); + } + + public String getArtifactType() { + return "/META-INF/definitions.xml"; + } + + public Class getModelType() { + return Definitions.class; + } + +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsExtensionPoint.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsExtensionPoint.java new file mode 100644 index 0000000000..3058b1f5b6 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsExtensionPoint.java @@ -0,0 +1,64 @@ +/* + * 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.definitions.xml; + +import java.net.URL; +import java.util.List; + +import org.apache.tuscany.sca.definitions.Definitions; + +/** + * An extension point for built-in SCA intent/policySet definition files + * + * @version $Rev$ $Date$ + */ +public interface DefinitionsExtensionPoint { + String DEFINITIONS_FILE = "org.apache.tuscany.sca.definitions.xml.Definitions"; + + /** + * Add a definitions document + * + * @param url the URL of the definitions + */ + void addDefinitionsDocument(URL url); + + /** + * Remove a definitions document + * + * @param url the URL of the definitions + */ + void removeDefinitionsDocument(URL url); + + /** + * @param definitions + */ + void addDefinitions(Definitions definitions); + /** + * @param definitions + */ + void removeDefinitions(Definitions definitions); + + /** + * Get the list of definitions + * @return A list of definitions + */ + List getDefinitions(); + +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsProcessor.java new file mode 100644 index 0000000000..0213713ced --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/DefinitionsProcessor.java @@ -0,0 +1,299 @@ +/* + * 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.definitions.xml; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor; +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.processor.StAXArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.definitions.Definitions; +import org.apache.tuscany.sca.definitions.DefinitionsFactory; +import org.apache.tuscany.sca.monitor.Monitor; +import org.apache.tuscany.sca.policy.BindingType; +import org.apache.tuscany.sca.policy.ExternalAttachment; +import org.apache.tuscany.sca.policy.ImplementationType; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.IntentMap; +import org.apache.tuscany.sca.policy.PolicyFactory; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.Qualifier; + +/** + * Processor for SCA Definitions + * + * @version $Rev$ $Date$ + */ +public class DefinitionsProcessor extends BaseStAXArtifactProcessor implements StAXArtifactProcessor { + private ExtensionPointRegistry registry; + private StAXArtifactProcessorExtensionPoint processors; + private StAXArtifactProcessor extensionProcessor; + private DefinitionsFactory definitionsFactory; + private PolicyFactory policyFactory; + + public static final String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200912"; + public static final String BINDING = "binding"; + public static final String IMPLEMENTATION = "implementation"; + public static final String DEFINITIONS = "definitions"; + public static final QName DEFINITIONS_QNAME = new QName(SCA11_NS, DEFINITIONS); + public static final String TARGET_NAMESPACE = "targetNamespace"; + public static final String NAME = "name"; + + public DefinitionsProcessor(ExtensionPointRegistry registry, + StAXArtifactProcessor extensionProcessor) { + this.registry = registry; + this.extensionProcessor = extensionProcessor; + this.processors = registry.getExtensionPoint(StAXArtifactProcessorExtensionPoint.class); + FactoryExtensionPoint factoryExtensionPoint = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.definitionsFactory = factoryExtensionPoint.getFactory(DefinitionsFactory.class); + this.policyFactory = factoryExtensionPoint.getFactory(PolicyFactory.class); + } + + public Definitions read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException { + QName name = null; + Definitions definitions = null; + String targetNamespace = null; + + while (reader.hasNext()) { + int event = reader.getEventType(); + switch (event) { + case START_ELEMENT: { + name = reader.getName(); + if (DEFINITIONS_QNAME.equals(name)) { + definitions = definitionsFactory.createDefinitions(); + targetNamespace = getURIString(reader, TARGET_NAMESPACE); + definitions.setTargetNamespace(targetNamespace); + } else { + Object extension = extensionProcessor.read(reader, context); + if (extension != null) { + if (extension instanceof Intent) { + Intent intent = (Intent)extension; + intent.setName(new QName(targetNamespace, intent.getName().getLocalPart())); + definitions.getIntents().add(intent); + for (Intent i : intent.getQualifiedIntents()) { + i.setName(new QName(targetNamespace, i.getName().getLocalPart())); + } + } else if (extension instanceof PolicySet) { + PolicySet policySet = (PolicySet)extension; + policySet.setName(new QName(targetNamespace, policySet.getName().getLocalPart())); + definitions.getPolicySets().add(policySet); + } else if (extension instanceof Binding) { + Binding binding = (Binding)extension; + definitions.getBindings().add(binding); + } else if (extension instanceof BindingType) { + definitions.getBindingTypes().add((BindingType)extension); + } else if (extension instanceof ImplementationType) { + definitions.getImplementationTypes().add((ImplementationType)extension); + } else if (extension instanceof ExternalAttachment) { + definitions.getExternalAttachments().add((ExternalAttachment)extension); + } + } + break; + } + } + + case XMLStreamConstants.CHARACTERS: + break; + + case END_ELEMENT: + name = reader.getName(); + if (DEFINITIONS_QNAME.equals(name)) { + return definitions; + } + break; + } + + //Read the next element + if (reader.hasNext()) { + reader.next(); + } + } + return definitions; + } + + public void write(Definitions definitions, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, + XMLStreamException { + + writeStartDocument(writer, SCA11_NS, DEFINITIONS, new XAttr(TARGET_NAMESPACE, definitions.getTargetNamespace())); + + for (Intent policyIntent : definitions.getIntents()) { + extensionProcessor.write(policyIntent, writer, context); + } + + for (PolicySet policySet : definitions.getPolicySets()) { + extensionProcessor.write(policySet, writer, context); + } + + for (Binding binding : definitions.getBindings()) { + extensionProcessor.write(binding, writer, context); + } + + for (BindingType bindingType : definitions.getBindingTypes()) { + extensionProcessor.write(bindingType, writer, context); + } + + for (ImplementationType implType : definitions.getImplementationTypes()) { + extensionProcessor.write(implType, writer, context); + } + + writeEndDocument(writer); + } + + public void resolve(Definitions scaDefns, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + + // start by adding all of the top level artifacts into the resolver as there + // are many cross artifact references in a definitions file and we don't want + // to be dependent on the order things appear + + List intents = new ArrayList(); + List policySets = new ArrayList(); + List referredPolicySets = new ArrayList(); + + for (Intent intent : scaDefns.getIntents()) { + intents.add(intent); + resolver.addModel(intent, context); + for (Intent i : intent.getQualifiedIntents()) { + intents.add(i); + resolver.addModel(i, context); + } + } + + for (PolicySet policySet : scaDefns.getPolicySets()) { + if (policySet.getReferencedPolicySets().isEmpty()) { + policySets.add(policySet); + } else { + referredPolicySets.add(policySet); + } + + resolver.addModel(policySet, context); + } + + for (BindingType bindingType : scaDefns.getBindingTypes()) { + resolver.addModel(bindingType, context); + } + + for (ImplementationType implType : scaDefns.getImplementationTypes()) { + resolver.addModel(implType, context); + } + + // now resolve everything to ensure that any references between + // artifacts are satisfied + + for (Intent policyIntent : intents) + extensionProcessor.resolve(policyIntent, resolver, context); + + for (PolicySet policySet : policySets) + extensionProcessor.resolve(policySet, resolver, context); + + for (PolicySet policySet : referredPolicySets) + extensionProcessor.resolve(policySet, resolver, context); + + for (Binding binding : scaDefns.getBindings()) { + extensionProcessor.resolve(binding, resolver, context); + } + + for (BindingType bindingType : scaDefns.getBindingTypes()) { + extensionProcessor.resolve(bindingType, resolver, context); + if (processors.getProcessor(bindingType.getType()) == null){ + Monitor.error(context.getMonitor(), + this, + "org.apache.tuscany.sca.definitions.xml.definitions-xml-validation-messages", + "BindingTypeNotFound", + bindingType.getType().toString()); + } + } + + for (ImplementationType implementationType : scaDefns.getImplementationTypes()) { + extensionProcessor.resolve(implementationType, resolver, context); + if (processors.getProcessor(implementationType.getType()) == null){ + Monitor.error(context.getMonitor(), + this, + "org.apache.tuscany.sca.definitions.xml.definitions-xml-validation-messages", + "ImplementationTypeNotFound", + implementationType.getType().toString()); + } + } + + + // Flat intentMap structure by creating a policySet for each one + List copy = new ArrayList(scaDefns.getPolicySets()); + for (PolicySet policySet : copy) { + // Add PolicySets to model based on qualified intents. The policy builder will be responsible for assigning + // the correct policy set. + // For example, ManagedTransactionPolicySet will result in: + // ManagedTransactionPolicySet (original PolicySet, must exist for matching at build time) + // ManagedTransactionPolicySet.managedTransaction.global + // ManagedTransactionPolicySet.managedTransaction.local + + //process intent maps + for(IntentMap intentMap : policySet.getIntentMaps()) { + for(Qualifier qualifier : intentMap.getQualifiers()) { + // remove = true; + + PolicySet qualifiedPolicySet = policyFactory.createPolicySet(); + qualifiedPolicySet.setAppliesTo(policySet.getAppliesTo()); + qualifiedPolicySet.setAppliesToXPathExpression(policySet.getAttachToXPathExpression()); + qualifiedPolicySet.setAttachTo(policySet.getAttachTo()); + qualifiedPolicySet.setAttachToXPathExpression(policySet.getAttachToXPathExpression()); + + String qualifiedLocalName = policySet.getName().getLocalPart() + "." + qualifier.getIntent().getName().getLocalPart(); + qualifiedPolicySet.setName(new QName(policySet.getName().getNamespaceURI(), qualifiedLocalName)); + qualifiedPolicySet.getProvidedIntents().clear(); + qualifiedPolicySet.getProvidedIntents().add(qualifier.getIntent()); + qualifiedPolicySet.getPolicies().clear(); + qualifiedPolicySet.getPolicies().addAll(qualifier.getPolicies()); + + scaDefns.getPolicySets().add(qualifiedPolicySet); + } + } + +// if(remove) { +// scaDefns.getPolicySets().remove(policySet); +// } + } + } + + public QName getArtifactType() { + return DEFINITIONS_QNAME; + } + + public Class getModelType() { + return Definitions.class; + } + +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/Messages.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/Messages.java new file mode 100644 index 0000000000..38cc4a9de2 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/definitions/xml/Messages.java @@ -0,0 +1,27 @@ +/* + * 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.definitions.xml; + +/** + * Constants for resource bundle names + */ +public interface Messages { + String RESOURCE_BUNDLE = "org.apache.tuscany.sca.definitions.xml.definitions-xml-validation-messages"; +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/BindingTypeProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/BindingTypeProcessor.java new file mode 100644 index 0000000000..70c71c5f1f --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/BindingTypeProcessor.java @@ -0,0 +1,65 @@ +/* + * 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.policy.xml; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.policy.BindingType; +import org.apache.tuscany.sca.policy.ExtensionType; +import org.apache.tuscany.sca.policy.PolicyFactory; + +/** + * Processor for handling XML models of BindingType meta data definitions + * + * @version $Rev$ $Date$ + */ +public class BindingTypeProcessor extends ExtensionTypeProcessor { + + public BindingTypeProcessor(PolicyFactory policyFactory, + StAXArtifactProcessor extensionProcessor) { + super(policyFactory, extensionProcessor); + } + + public BindingTypeProcessor(FactoryExtensionPoint modelFactories, + StAXArtifactProcessor extensionProcessor) { + super(modelFactories.getFactory(PolicyFactory.class), extensionProcessor); + } + + public QName getArtifactType() { + return BINDING_TYPE_QNAME; + } + + @Override + protected ExtensionType resolveExtensionType(ExtensionType extnType, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + if (extnType instanceof BindingType) { + BindingType bindingType = (BindingType)extnType; + return resolver.resolveModel(BindingType.class, bindingType, context); + } else { + return extnType; + } + + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/ExtensionTypeProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/ExtensionTypeProcessor.java new file mode 100644 index 0000000000..95d8e67bf7 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/ExtensionTypeProcessor.java @@ -0,0 +1,250 @@ +/* + * 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.policy.xml; + +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; + +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.BaseStAXArtifactProcessor; +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; +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.BindingType; +import org.apache.tuscany.sca.policy.ExtensionType; +import org.apache.tuscany.sca.policy.ImplementationType; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.PolicyFactory; + +/** + * Processor for handling XML models of ExtensionType meta data definitions + * + * @version $Rev$ $Date$ + */ +abstract class ExtensionTypeProcessor extends BaseStAXArtifactProcessor implements + StAXArtifactProcessor, PolicyConstants { + + private PolicyFactory policyFactory; + + + protected abstract ExtensionType resolveExtensionType(ExtensionType extnType, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException; + + public ExtensionTypeProcessor(PolicyFactory policyFactory, + StAXArtifactProcessor extensionProcessor) { + this.policyFactory = policyFactory; + } + + /** + * Report a error. + * + * @param problems + * @param message + * @param model + */ + private void error(Monitor monitor, String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = + monitor.createProblem(this.getClass().getName(), + Messages.RESOURCE_BUNDLE, + Severity.ERROR, + model, + message, + (Object[])messageParameters); + monitor.problem(problem); + } + } + + public ExtensionType read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException { + QName extType = getArtifactType(); + QName type = getQName(reader, "type"); + + if (type != null) { + ExtensionType extensionType = null; + if (BINDING_TYPE_QNAME.equals(extType)) { + extensionType = policyFactory.createBindingType(); + } else if (IMPLEMENTATION_TYPE_QNAME.equals(extType)) { + extensionType = policyFactory.createImplementationType(); + } else { + error(context.getMonitor(), "UnrecognizedExtensionType", reader, type); + return null; + //throw new ContributionReadException("Unrecognized ExtensionType - " + type); + } + extensionType.setType(type); + extensionType.setUnresolved(true); + + readAlwaysProvidedIntents(extensionType, reader); + readMayProvideIntents(extensionType, reader); + return extensionType; + + } else { + error(context.getMonitor(), "RequiredAttributeMissing", reader, extType); + //throw new ContributionReadException("Required attribute '" + TYPE + + //"' missing from BindingType Definition"); + } + return null; + } + + private void readAlwaysProvidedIntents(ExtensionType extnType, XMLStreamReader reader) { + String value = reader.getAttributeValue(null, ALWAYS_PROVIDES); + if (value != null) { + List alwaysProvided = extnType.getAlwaysProvidedIntents(); + for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) { + QName qname = getQNameValue(reader, tokens.nextToken()); + Intent intent = policyFactory.createIntent(); + intent.setName(qname); + alwaysProvided.add(intent); + } + } + } + + private void readMayProvideIntents(ExtensionType extnType, XMLStreamReader reader) { + String value = reader.getAttributeValue(null, MAY_PROVIDE); + if (value != null) { + List mayProvide = extnType.getMayProvidedIntents(); + for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) { + QName qname = getQNameValue(reader, tokens.nextToken()); + Intent intent = policyFactory.createIntent(); + intent.setName(qname); + mayProvide.add(intent); + } + } + } + + public void write(ExtensionType extnType, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, + XMLStreamException { + + // Write an + if (extnType instanceof BindingType) { + writer.writeStartElement(SCA11_NS, BINDING_TYPE); + } else if (extnType instanceof ImplementationType) { + writer.writeStartElement(SCA11_NS, IMPLEMENTATION_TYPE); + } + + writeAlwaysProvidesIntentsAttribute(extnType, writer); + writeMayProvideIntentsAttribute(extnType, writer); + + writer.writeEndElement(); + } + + private void writeMayProvideIntentsAttribute(ExtensionType extnType, XMLStreamWriter writer) + throws XMLStreamException { + StringBuffer sb = new StringBuffer(); + for (Intent intent : extnType.getMayProvidedIntents()) { + writer.writeNamespace(intent.getName().getPrefix(), intent.getName().getNamespaceURI()); + sb.append(intent.getName().getPrefix() + COLON + intent.getName().getLocalPart()); + sb.append(WHITE_SPACE); + } + + if (sb.length() > 0) { + writer.writeAttribute(MAY_PROVIDE, sb.toString()); + } + } + + private void writeAlwaysProvidesIntentsAttribute(ExtensionType extnType, XMLStreamWriter writer) + throws XMLStreamException { + StringBuffer sb = new StringBuffer(); + for (Intent intent : extnType.getAlwaysProvidedIntents()) { + writer.writeNamespace(intent.getName().getPrefix(), intent.getName().getNamespaceURI()); + sb.append(intent.getName().getPrefix() + COLON + intent.getName().getLocalPart()); + sb.append(WHITE_SPACE); + } + + if (sb.length() > 0) { + writer.writeAttribute(ALWAYS_PROVIDES, sb.toString()); + + } + } + + public void resolve(ExtensionType extnType, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + + if (extnType != null && extnType.isUnresolved()) { + resolveAlwaysProvidedIntents(extnType, resolver, context); + resolveMayProvideIntents(extnType, resolver, context); + extnType.setUnresolved(false); + //resolveExtensionType(extnType, resolver); + } + } + + private void resolveAlwaysProvidedIntents(ExtensionType extensionType, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + if (extensionType != null) { + // resolve all provided intents + List alwaysProvided = new ArrayList(); + for (Intent providedIntent : extensionType.getAlwaysProvidedIntents()) { + if (providedIntent.isUnresolved()) { + providedIntent = resolver.resolveModel(Intent.class, providedIntent, context); + if (!providedIntent.isUnresolved()) { + alwaysProvided.add(providedIntent); + } else { + error(context.getMonitor(), "AlwaysProvidedIntentNotFound", resolver, providedIntent, extensionType); + //throw new ContributionResolveException("Always Provided Intent - " + providedIntent + //+ " not found for ExtensionType " + //+ extensionType); + } + } else { + alwaysProvided.add(providedIntent); + } + } + extensionType.getAlwaysProvidedIntents().clear(); + extensionType.getAlwaysProvidedIntents().addAll(alwaysProvided); + } + } + + private void resolveMayProvideIntents(ExtensionType extensionType, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + if (extensionType != null) { + // resolve all provided intents + List mayProvide = new ArrayList(); + for (Intent providedIntent : extensionType.getMayProvidedIntents()) { + if (providedIntent.isUnresolved()) { + providedIntent = resolver.resolveModel(Intent.class, providedIntent, context); + if (!providedIntent.isUnresolved()) { + mayProvide.add(providedIntent); + } else { + error(context.getMonitor(), "MayProvideIntentNotFound", resolver, providedIntent, extensionType); + //throw new ContributionResolveException("May Provide Intent - " + providedIntent + //+ " not found for ExtensionType " + //+ extensionType); + } + } else { + mayProvide.add(providedIntent); + } + } + extensionType.getMayProvidedIntents().clear(); + extensionType.getMayProvidedIntents().addAll(mayProvide); + } + } + + public Class getModelType() { + return ExtensionType.class; + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/ExternalAttachmentProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/ExternalAttachmentProcessor.java new file mode 100644 index 0000000000..7b7855bd7a --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/ExternalAttachmentProcessor.java @@ -0,0 +1,294 @@ +/* + * 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.policy.xml; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.StringTokenizer; + +import javax.xml.XMLConstants; +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; + +import org.apache.tuscany.sca.common.xml.xpath.XPathHelper; +import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor; +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; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +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.ExternalAttachment; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.PolicyFactory; +import org.apache.tuscany.sca.policy.PolicySet; + +/** + * Processor for handling XML models of ExternalAttachment definitions + * + * @version $Rev: 961010 $ $Date: 2010-07-06 13:34:54 -0700 (Tue, 06 Jul 2010) $ + */ +public class ExternalAttachmentProcessor extends BaseStAXArtifactProcessor + implements StAXArtifactProcessor, PolicyConstants { + + + private PolicyFactory policyFactory; + private XPathHelper xpathHelper; + + public ExternalAttachmentProcessor(ExtensionPointRegistry registry) { + FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.policyFactory = factories.getFactory(PolicyFactory.class); + this.xpathHelper = XPathHelper.getInstance(registry); + } + + public Class getModelType() { + return ExternalAttachment.class; + } + + public void resolve(ExternalAttachment attachment, ModelResolver resolver, + ProcessorContext context) throws ContributionResolveException { + if (attachment != null && attachment.isUnresolved()) { + resolveIntents(attachment, resolver, context); + resolvePolicySets(attachment, resolver, context); + + attachment.setUnresolved(false); + } + + } + + private void resolvePolicySets(ExternalAttachment attachment, + ModelResolver resolver, ProcessorContext context) { + List referredPolicySets = new ArrayList(); + for (PolicySet referredPolicySet : attachment.getPolicySets()) { + if (referredPolicySet.isUnresolved()) { + PolicySet resolved = resolver.resolveModel(PolicySet.class, referredPolicySet, context); + if (!resolved.isUnresolved() || resolved != referredPolicySet) { + referredPolicySets.add(resolved); + } else { + error(context.getMonitor(), "ReferredPolicySetNotFound", attachment, referredPolicySet, attachment); + return; + } + } else { + referredPolicySets.add(referredPolicySet); + } + } + attachment.getPolicySets().clear(); + attachment.getPolicySets().addAll(referredPolicySets); + + } + + private void resolveIntents(ExternalAttachment attachment, + ModelResolver resolver, ProcessorContext context) { + if (attachment != null) { + //resolve all provided intents + List providedIntents = new ArrayList(); + for (Intent providedIntent : attachment.getIntents()) { + if (providedIntent.isUnresolved()) { + Intent resolved = resolver.resolveModel(Intent.class, providedIntent, context); + if (!resolved.isUnresolved() || resolved != providedIntent) { + providedIntents.add(resolved); + } else { + error(context.getMonitor(), "ProvidedIntentNotFound", attachment, providedIntent, attachment); + return; + } + } else { + providedIntents.add(providedIntent); + } + } + attachment.getIntents().clear(); + attachment.getIntents().addAll(providedIntents); + } + + } + + public QName getArtifactType() { + return EXTERNAL_ATTACHMENT_QNAME; + } + + public ExternalAttachment read(XMLStreamReader reader, ProcessorContext context) + throws ContributionReadException, XMLStreamException { + ExternalAttachment attachment = policyFactory.createExternalAttachment(); + + readPolicySets(attachment, reader); + readIntents(attachment, reader, context); + readAttachTo(attachment, reader, context); + + return attachment; + } + + private void readIntents(ExternalAttachment attachment, + XMLStreamReader reader, ProcessorContext context) { + String value = reader.getAttributeValue(null, INTENTS); + if (value != null) { + List intents = attachment.getIntents(); + for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) { + QName qname = getQNameValue(reader, tokens.nextToken()); + Intent intent = policyFactory.createIntent(); + intent.setName(qname); + intents.add(intent); + } + } + + } + + private void readAttachTo(ExternalAttachment attachment, + XMLStreamReader reader, ProcessorContext context) { + Monitor monitor = context.getMonitor(); + + String attachTo = reader.getAttributeValue(null, ATTACH_TO); + if ( attachTo != null ) { + try { + XPath path = xpathHelper.newXPath(); + NamespaceContext nsContext = xpathHelper.getNamespaceContext(attachTo, reader.getNamespaceContext()); + path.setXPathFunctionResolver(new PolicyXPathFunctionResolver(nsContext)); + + attachTo = PolicyXPathFunction.normalize(attachTo,getSCAPrefix(nsContext)); + XPathExpression expression = xpathHelper.compile(path, nsContext, attachTo); + attachment.setAttachTo(attachTo); + attachment.setAttachToXPathExpression(expression); + } catch (XPathExpressionException e) { + ContributionReadException ce = new ContributionReadException(e); + error(monitor, "ContributionReadException", attachment, ce); + } + } + + } + + private String getSCAPrefix(NamespaceContext nsContext) { + + Iterator iter = nsContext.getPrefixes(SCA11_NS); + while (iter.hasNext()) { + String prefix = iter.next(); + if (!prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) { + return prefix; + } + } + + return "__sca"; + } + /** + * Reads policy sets associated with an external attachment element. + * @param subject + * @param operation + * @param reader + */ + private void readPolicySets(ExternalAttachment attachment, XMLStreamReader reader) { + + + String value = reader.getAttributeValue(null, POLICY_SETS); + if (value != null) { + List policySets = attachment.getPolicySets(); + for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) { + QName qname = getQNameValue(reader, tokens.nextToken()); + PolicySet policySet = policyFactory.createPolicySet(); + policySet.setName(qname); + policySets.add(policySet); + } + } + } + public void write(ExternalAttachment attachment, XMLStreamWriter writer, + ProcessorContext context) throws ContributionWriteException, + XMLStreamException { + writePolicySets(attachment, writer, context); + writeIntents(attachment, writer, context); + writeAttachTo(attachment, writer, context); + + } + + private void writeAttachTo(ExternalAttachment attachment, + XMLStreamWriter writer, ProcessorContext context) throws XMLStreamException { + if ( attachment.getAttachTo() != null ) { + writer.writeAttribute(PolicyConstants.ATTACH_TO, attachment.getAttachTo()); + } + + } + + private void writeIntents(ExternalAttachment attachment, + XMLStreamWriter writer, ProcessorContext context) throws XMLStreamException { + if (!attachment.getIntents().isEmpty()) { + StringBuffer sb = new StringBuffer(); + for (Intent intent : attachment.getIntents()) { + sb.append(getQualifiedName(intent.getName(), writer)); + sb.append(" "); + } + // Remove the last space + sb.deleteCharAt(sb.length() - 1); + writer.writeAttribute(PolicyConstants.INTENTS, sb.toString()); + } + + } + + private void writePolicySets(ExternalAttachment attachment, + XMLStreamWriter writer, ProcessorContext context) throws XMLStreamException { + if ( !attachment.getPolicySets().isEmpty()) { + StringBuffer sb = new StringBuffer(); + for (PolicySet ps : attachment.getPolicySets()) { + sb.append(getQualifiedName(ps.getName(), writer)); + sb.append(" "); + } + // Remove the last space + sb.deleteCharAt(sb.length() - 1); + writer.writeAttribute(PolicyConstants.POLICY_SETS, sb.toString()); + } + + } + + private String getQualifiedName(QName name, XMLStreamWriter writer) throws XMLStreamException { + String local = name.getLocalPart(); + String prefix = writer.getPrefix(name.getNamespaceURI()); + if (prefix != null && prefix.length() > 0) { + return prefix + ':' + local; + } else { + return local; + } + } + + /** + * Report a error. + * + * @param problems + * @param message + * @param model + */ + private void error(Monitor monitor, String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = + monitor.createProblem(this.getClass().getName(), + Messages.RESOURCE_BUNDLE, + Severity.ERROR, + model, + message, + (Object[])messageParameters); + monitor.problem(problem); + } + } + +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/ImplementationTypeProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/ImplementationTypeProcessor.java new file mode 100644 index 0000000000..ec4e17385f --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/ImplementationTypeProcessor.java @@ -0,0 +1,65 @@ +/* + * 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.policy.xml; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.policy.ExtensionType; +import org.apache.tuscany.sca.policy.ImplementationType; +import org.apache.tuscany.sca.policy.PolicyFactory; + +/** + * Processor for handling XML models of ImplementationType meta data definitions + * + * @version $Rev$ $Date$ + */ +public class ImplementationTypeProcessor extends ExtensionTypeProcessor { + + public ImplementationTypeProcessor(PolicyFactory policyFactory, + StAXArtifactProcessor extensionProcessor) { + super(policyFactory, extensionProcessor); + } + + public ImplementationTypeProcessor(FactoryExtensionPoint modelFactories, + StAXArtifactProcessor extensionProcessor) { + super(modelFactories.getFactory(PolicyFactory.class), extensionProcessor); + } + + public QName getArtifactType() { + return IMPLEMENTATION_TYPE_QNAME; + } + + @Override + protected ExtensionType resolveExtensionType(ExtensionType extnType, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + if (extnType instanceof ImplementationType) { + ImplementationType implType = (ImplementationType)extnType; + return resolver.resolveModel(ImplementationType.class, implType, context); + } else { + return extnType; + } + + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/IntentProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/IntentProcessor.java new file mode 100644 index 0000000000..c7eb7f49d8 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/IntentProcessor.java @@ -0,0 +1,446 @@ +/* + * 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.policy.xml; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.StringTokenizer; + +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.BaseStAXArtifactProcessor; +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; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +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.Intent; +import org.apache.tuscany.sca.policy.PolicyFactory; +import org.apache.tuscany.sca.policy.Intent.Type; + +/** + * Processor for handling XML models of PolicyIntent definitions + * + * @version $Rev$ $Date$ + */ +public class IntentProcessor extends BaseStAXArtifactProcessor implements StAXArtifactProcessor, + PolicyConstants { + + private PolicyFactory policyFactory; + + + public IntentProcessor(FactoryExtensionPoint modelFactories) { + this.policyFactory = modelFactories.getFactory(PolicyFactory.class); + } + + public IntentProcessor(PolicyFactory policyFactory) { + this.policyFactory = policyFactory; + } + + /** + * Report a error. + * + * @param problems + * @param message + * @param model + */ + private void error(Monitor monitor, String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = + monitor.createProblem(this.getClass().getName(), + Messages.RESOURCE_BUNDLE, + Severity.ERROR, + model, + message, + messageParameters); + monitor.problem(problem); + } + } + + private void warn(Monitor monitor, String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = + monitor.createProblem(this.getClass().getName(), + Messages.RESOURCE_BUNDLE, + Severity.WARNING, + model, + message, + messageParameters); + monitor.problem(problem); + } + } + + public Intent read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, XMLStreamException { + Intent intent = null; + String intentLocalName = reader.getAttributeValue(null, NAME); + if (intentLocalName == null) { + error(context.getMonitor(), "IntentNameMissing", reader); + return null; + } + + String intentType = reader.getAttributeValue(null, INTENT_TYPE); + if (intentType == null) { + intentType = Intent.Type.interaction.name(); + } + + intent = policyFactory.createIntent(); + + // [rfeng] the target namespace is not available, set the local part for now + // This will be changed in the definitions processor + intent.setName(new QName(intentLocalName)); + intent.setType(Type.valueOf(intentType)); + + readRequiredIntents(intent, reader, context); + readExcludedIntents(intent, reader); + + readConstrainedTypes(intent, reader); + + String mutuallyExclusiveString = reader.getAttributeValue(null, MUTUALLY_EXCLUSIVE); + if (mutuallyExclusiveString != null && + mutuallyExclusiveString.equals("true")){ + intent.setMutuallyExclusive(true); + } else { + intent.setMutuallyExclusive(false); + } + + Intent current = intent; + int event = reader.getEventType(); + QName name = null; + while (reader.hasNext()) { + event = reader.getEventType(); + switch (event) { + case START_ELEMENT: { + name = reader.getName(); + if (DESCRIPTION_QNAME.equals(name)) { + String text = reader.getElementText(); + if (text != null) { + text = text.trim(); + } + current.setDescription(text); + } else if (INTENT_QUALIFIER_QNAME.equals(name)) { + String qualifierName = reader.getAttributeValue(null, NAME); + String defaultQ = reader.getAttributeValue(null, DEFAULT); + boolean isDefault = defaultQ == null ? false : Boolean.parseBoolean(defaultQ); + String qualifiedIntentName = intentLocalName + QUALIFIER + qualifierName; + Intent qualified = policyFactory.createIntent(); + qualified.setUnresolved(false); + qualified.setType(intent.getType()); + qualified.setName(new QName(qualifiedIntentName)); + if (isDefault) { + if (intent.getDefaultQualifiedIntent() == null){ + intent.setDefaultQualifiedIntent(qualified); + } else { + Monitor.error(context.getMonitor(), + this, + Messages.RESOURCE_BUNDLE, + "MultipleDefaultQualifiers", + intent.getName().toString()); + } + } + + // check that the qualifier is unique + if ( !intent.getQualifiedIntents().contains(qualified)){ + intent.getQualifiedIntents().add(qualified); + } else { + Monitor.error(context.getMonitor(), + this, + Messages.RESOURCE_BUNDLE, + "QualifierIsNotUnique", + intent.getName().toString(), + qualifierName); + } + + qualified.setQualifiableIntent(intent); + current = qualified; + } + break; + } + case END_ELEMENT: { + name = reader.getName(); + if (INTENT_QUALIFIER_QNAME.equals(name)) { + current = intent; + } + break; + } + } + if (event == END_ELEMENT && POLICY_INTENT_QNAME.equals(reader.getName())) { + break; + } + + //Read the next element + if (reader.hasNext()) { + reader.next(); + } + } + // REVIEW: [rfeng] What's going to happen if there is only one qualified intent + if (intent.getQualifiedIntents().size() == 1) { + intent.setDefaultQualifiedIntent(intent.getQualifiedIntents().get(0)); + } + + // set all qualified intents as excluding one another if the qualifiable + // intent is set to be mutually exclusive + if (intent.isMutuallyExclusive()){ + for (Intent qualifiedIntent : intent.getQualifiedIntents()){ + for (Intent excludedIntent : intent.getQualifiedIntents()){ + if (qualifiedIntent != excludedIntent){ + qualifiedIntent.getExcludedIntents().add(excludedIntent); + } + } + } + } + + return intent; + } + + public void write(Intent intent, XMLStreamWriter writer, ProcessorContext context) throws ContributionWriteException, XMLStreamException { + // Write an + writer.writeStartElement(PolicyConstants.SCA11_NS, INTENT); + writer.writeNamespace(intent.getName().getPrefix(), intent.getName().getNamespaceURI()); + writer.writeAttribute(PolicyConstants.NAME, intent.getName().getPrefix() + COLON + + intent.getName().getLocalPart()); + if (intent.getRequiredIntents() != null && intent.getRequiredIntents().size() > 0) { + StringBuffer sb = new StringBuffer(); + for (Intent requiredIntents : intent.getRequiredIntents()) { + sb.append(requiredIntents.getName()); + sb.append(" "); + } + writer.writeAttribute(PolicyConstants.REQUIRES, sb.toString()); + } + + if (intent.getExcludedIntents() != null && intent.getExcludedIntents().size() > 0) { + StringBuffer sb = new StringBuffer(); + for (Intent excludedIntents : intent.getExcludedIntents()) { + sb.append(excludedIntents.getName()); + sb.append(" "); + } + writer.writeAttribute(PolicyConstants.EXCLUDES, sb.toString()); + } + + if (intent.getConstrainedTypes() != null && intent.getConstrainedTypes().size() > 0) { + StringBuffer sb = new StringBuffer(); + for (ExtensionType contrainedArtifact : intent.getConstrainedTypes()) { + sb.append(contrainedArtifact.getType().getPrefix()); + sb.append(':').append(contrainedArtifact.getType().getLocalPart()); + sb.append(" "); + } + writer.writeAttribute(CONSTRAINS, sb.toString()); + } + + if (intent.getDescription() != null && intent.getDescription().length() > 0) { + writer.writeStartElement(PolicyConstants.SCA11_NS, DESCRIPTION); + writer.writeCData(intent.getDescription()); + writer.writeEndElement(); + } + + writer.writeEndElement(); + } + + private void resolveContrainedTypes(Intent intent, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + Collection resolvedTypes = new HashSet(); + for (ExtensionType extensionType : intent.getConstrainedTypes()) { + if (ExtensionType.BINDING_BASE.equals(extensionType.getType()) || ExtensionType.IMPLEMENTATION_BASE + .equals(extensionType.getType())) { + // HACK: Mark sca:binding and sca:implementation as resolved + extensionType.setUnresolved(false); + resolvedTypes.add(extensionType); + } else { + ExtensionType resolved = resolver.resolveModel(ExtensionType.class, extensionType, context); + if (!resolved.isUnresolved() || resolved != extensionType) { + resolvedTypes.add(resolved); + } else { + warn(context.getMonitor(), "ConstrainedTypeNotFound", intent, extensionType, intent); + } + } + } + intent.getConstrainedTypes().clear(); + intent.getConstrainedTypes().addAll(resolvedTypes); + } + + private void resolveProfileIntent(Intent intent, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + Monitor monitor = context.getMonitor(); + // FIXME: Need to check for cyclic references first i.e an A requiring B + // and then B requiring A... + if (intent != null && !intent.getRequiredIntents().isEmpty()) { + // resolve all required intents + List requiredIntents = new ArrayList(); + for (Intent required : intent.getRequiredIntents()) { + if (required.isUnresolved()) { + Intent resolved = resolver.resolveModel(Intent.class, required, context); + // At this point, when the required intent is not resolved, it does not mean + // its undeclared, chances are that their dependency are not resolved yet. + // Lets try to resolve them first. + if (resolved.isUnresolved()) { + if (((resolved).getRequiredIntents()).contains(intent)) { + error(monitor, "CyclicReferenceFound", resolver, required, intent); + return; + } + } + + if (!resolved.isUnresolved() || resolved != required) { + requiredIntents.add(resolved); + } else { + error(monitor, "RequiredIntentNotFound", resolver, required, intent); + return; + //throw new ContributionResolveException("Required Intent - " + requiredIntent + //+ " not found for Intent " + policyIntent); + } + } else { + requiredIntents.add(required); + } + } + intent.getRequiredIntents().clear(); + intent.getRequiredIntents().addAll(requiredIntents); + } + } + + private void resolveQualifiedIntent(Intent qualifed, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + if (qualifed != null) { + //resolve the qualifiable intent + Intent parent = qualifed.getQualifiableIntent(); + if (parent == null) { + return; + } + if (parent.isUnresolved()) { + Intent resolved = resolver.resolveModel(Intent.class, parent, context); + // At this point, when the qualifiable intent is not resolved, it does not mean + // its undeclared, chances are that their dependency are not resolved yet. + // Lets try to resolve them first. + + if (!resolved.isUnresolved() || resolved != qualifed) { + qualifed.setQualifiableIntent(resolved); + } else { + error(context.getMonitor(), "QualifiableIntentNotFound", resolver, parent, qualifed); + //throw new ContributionResolveException("Qualifiable Intent - " + qualifiableIntent + //+ " not found for Intent " + policyIntent); + } + } + } + } + + public void resolve(Intent intent, ModelResolver resolver, ProcessorContext context) throws ContributionResolveException { + if (intent != null && intent.isUnresolved()) { + resolveProfileIntent(intent, resolver, context); + resolveExcludedIntents(intent, resolver, context); + resolveQualifiedIntent(intent, resolver, context); + resolveContrainedTypes(intent, resolver, context); + intent.setUnresolved(false); + } + } + + public QName getArtifactType() { + return POLICY_INTENT_QNAME; + } + + private void readConstrainedTypes(Intent policyIntent, XMLStreamReader reader) throws ContributionReadException { + String value = reader.getAttributeValue(null, CONSTRAINS); + if (value != null) { + List constrainedTypes = policyIntent.getConstrainedTypes(); + for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) { + QName qname = getQNameValue(reader, tokens.nextToken()); + ExtensionType extensionType = policyFactory.createExtensionType(); + extensionType.setType(qname); + constrainedTypes.add(extensionType); + } + } + } + + private void readRequiredIntents(Intent intent, XMLStreamReader reader, ProcessorContext context) { + String value = reader.getAttributeValue(null, REQUIRES); + if (value != null) { + List requiredIntents = intent.getRequiredIntents(); + for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) { + QName qname = getQNameValue(reader, tokens.nextToken()); + Intent required = policyFactory.createIntent(); + required.setName(qname); + required.setUnresolved(true); + requiredIntents.add(required); + } + + // Check that a profile intent does not have "." in its name + if (requiredIntents.size() > 0) { + if (intent.getName().getLocalPart().contains(".")){ + Monitor.error(context.getMonitor(), + this, + Messages.RESOURCE_BUNDLE, + "ProfileIntentNameWithPeriod", + intent.getName().toString()); + } + } + } + } + + private void readExcludedIntents(Intent intent, XMLStreamReader reader) { + String value = reader.getAttributeValue(null, EXCLUDES); + if (value != null) { + List excludedIntents = intent.getExcludedIntents(); + for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) { + QName qname = getQNameValue(reader, tokens.nextToken()); + Intent excluded = policyFactory.createIntent(); + excluded.setName(qname); + excluded.setUnresolved(true); + excludedIntents.add(excluded); + } + } + } + + private void resolveExcludedIntents(Intent policyIntent, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + if (policyIntent != null) { + // resolve all excluded intents + List excludedIntents = new ArrayList(); + for (Intent excludedIntent : policyIntent.getExcludedIntents()) { + if (excludedIntent.isUnresolved()) { + Intent resolvedExcludedIntent = resolver.resolveModel(Intent.class, excludedIntent, context); + if (!resolvedExcludedIntent.isUnresolved() || resolvedExcludedIntent != excludedIntent) { + excludedIntents.add(resolvedExcludedIntent); + } else { + error(context.getMonitor(), "ExcludedIntentNotFound", resolver, excludedIntent, policyIntent); + return; + //throw new ContributionResolveException("Excluded Intent " + excludedIntent + //+ " not found for intent " + policyIntent); + } + } else { + excludedIntents.add(excludedIntent); + } + } + policyIntent.getExcludedIntents().clear(); + policyIntent.getExcludedIntents().addAll(excludedIntents); + } + } + + public Class getModelType() { + return Intent.class; + } + +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/Messages.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/Messages.java new file mode 100644 index 0000000000..ee8b5274cc --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/Messages.java @@ -0,0 +1,27 @@ +/* + * 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.policy.xml; + +/** + * Constants for resource bundle names + */ +public interface Messages { + String RESOURCE_BUNDLE = "org.apache.tuscany.sca.policy.xml.policy-xml-validation-messages"; +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyConstants.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyConstants.java new file mode 100644 index 0000000000..fa56bf0503 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyConstants.java @@ -0,0 +1,76 @@ +/* + * 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.policy.xml; + +import javax.xml.namespace.QName; + +/** + * constants related to policy framework + * + * @version $Rev$ $Date$ + */ +public interface PolicyConstants { + String WHITE_SPACE = " "; + String COLON = ":"; + String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200912"; + String TUSCANY_NS = "http://tuscany.apache.org/xmlns/sca/1.1"; + String INTENT = "intent"; + String POLICY_SET = "policySet"; + String POLICY_SET_REFERENCE = "policySetReference"; + String INTENT_MAP = "intentMap"; + String NAME = "name"; + String TARGET_NAMESPACE = "targetNamespace"; + String SCA_DEFINITIONS = "definitions"; + String CONSTRAINS = "constrains"; + String DESCRIPTION = "description"; + String PROVIDES = "provides"; + String APPLIES_TO = "appliesTo"; + String ATTACH_TO = "attachTo"; + String ALWAYS_APPLIES_TO = "alwaysAppliesTo"; + String QUALIFIER = "."; + String INTENT_QUALIFIER = "qualifier"; + String INTENT_MAP_QUALIFIER = "qualifier"; + String REQUIRES = "requires"; + String EXCLUDES = "excludes"; + String DEFAULT = "default"; + String EXTERNAL_ATTACHMENT = "externalAttachment"; + String INTENTS = "intents"; + String POLICY_SETS = "policySets"; + String MUTUALLY_EXCLUSIVE = "mutuallyExclusive"; + + String ALWAYS_PROVIDES = "alwaysProvides"; + String MAY_PROVIDE = "mayProvide"; + String INTENT_TYPE = "intentType"; + String IMPLEMENTATION_TYPE = "implementationType"; + String BINDING_TYPE = "bindingType"; + QName IMPLEMENTATION_TYPE_QNAME = new QName(SCA11_NS, IMPLEMENTATION_TYPE); + QName BINDING_TYPE_QNAME = new QName(SCA11_NS, BINDING_TYPE); + String BINDING = "binding"; + String IMPLEMENTATION = "implementation"; + + QName POLICY_INTENT_QNAME = new QName(SCA11_NS, INTENT); + QName POLICY_SET_QNAME = new QName(SCA11_NS, POLICY_SET); + QName POLICY_INTENT_MAP_QNAME = new QName(SCA11_NS, INTENT_MAP); + QName SCA_DEFINITIONS_QNAME = new QName(SCA11_NS, SCA_DEFINITIONS); + QName DESCRIPTION_QNAME = new QName(SCA11_NS, DESCRIPTION); + QName POLICY_INTENT_MAP_QUALIFIER_QNAME = new QName(SCA11_NS, INTENT_MAP_QUALIFIER); + QName POLICY_SET_REFERENCE_QNAME = new QName(SCA11_NS, POLICY_SET_REFERENCE); + QName INTENT_QUALIFIER_QNAME = new QName(SCA11_NS, INTENT_QUALIFIER); + QName EXTERNAL_ATTACHMENT_QNAME = new QName(SCA11_NS, EXTERNAL_ATTACHMENT); +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicySetProcessor.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicySetProcessor.java new file mode 100644 index 0000000000..ec36de5bec --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicySetProcessor.java @@ -0,0 +1,577 @@ +/* + * 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.policy.xml; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.StringTokenizer; + +import javax.xml.XMLConstants; +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; + +import org.apache.tuscany.sca.common.xml.xpath.XPathHelper; +import org.apache.tuscany.sca.contribution.processor.BaseStAXArtifactProcessor; +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; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +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.Intent; +import org.apache.tuscany.sca.policy.IntentMap; +import org.apache.tuscany.sca.policy.PolicyExpression; +import org.apache.tuscany.sca.policy.PolicyFactory; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.Qualifier; + +/** + * Processor for handling XML models of PolicySet definitions + * + * @version $Rev$ $Date$ + */ +public class PolicySetProcessor extends BaseStAXArtifactProcessor implements StAXArtifactProcessor, + PolicyConstants { + + private PolicyFactory policyFactory; + private StAXArtifactProcessor extensionProcessor; + private XPathHelper xpathHelper; + + // private XPathFactory xpathFactory; + + public PolicySetProcessor(ExtensionPointRegistry registry, StAXArtifactProcessor extensionProcessor) { + FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class); + this.policyFactory = modelFactories.getFactory(PolicyFactory.class); + this.extensionProcessor = extensionProcessor; + this.xpathHelper = XPathHelper.getInstance(registry); + } + + /** + * Report a exception. + * + * @param problems + * @param message + * @param model + */ + private void error(Monitor monitor, String message, Object model, Exception ex) { + if (monitor != null) { + Problem problem = + monitor.createProblem(this.getClass().getName(), + Messages.RESOURCE_BUNDLE, + Severity.ERROR, + model, + message, + ex); + monitor.problem(problem); + } + } + + /** + * Report a error. + * + * @param problems + * @param message + * @param model + */ + private void error(Monitor monitor, String message, Object model, Object... messageParameters) { + if (monitor != null) { + Problem problem = + monitor.createProblem(this.getClass().getName(), + Messages.RESOURCE_BUNDLE, + Severity.ERROR, + model, + message, + (Object[])messageParameters); + monitor.problem(problem); + } + } + + public PolicySet read(XMLStreamReader reader, ProcessorContext context) throws ContributionReadException, + XMLStreamException { + PolicySet policySet = null; + Monitor monitor = context.getMonitor(); + String policySetName = reader.getAttributeValue(null, NAME); + String appliesTo = reader.getAttributeValue(null, APPLIES_TO); + if (policySetName == null || appliesTo == null) { + if (policySetName == null) + error(monitor, "PolicySetNameMissing", reader); + if (appliesTo == null) + error(monitor, "PolicySetAppliesToMissing", reader); + return policySet; + } + + policySet = policyFactory.createPolicySet(); + policySet.setName(new QName(policySetName)); + + //TODO: with 1.0 version of specs the applies to xpath is given related to the immediate + //parent whereas the runtime evaluates the xpath aginst the composite element. What the runtime + //is doing is what the future version of the specs could be tending towards. When that happens + //this 'if' must be deleted + if (appliesTo != null && !appliesTo.startsWith("/")) { + appliesTo = "//" + appliesTo; + } + + policySet.setAppliesTo(appliesTo); + + if (appliesTo != null) { + try { + XPath path = xpathHelper.newXPath(); + NamespaceContext nsContext = xpathHelper.getNamespaceContext(appliesTo, reader.getNamespaceContext()); + // path.setXPathFunctionResolver(new PolicyXPathFunctionResolver(context)); + XPathExpression expression = xpathHelper.compile(path, nsContext, appliesTo); + policySet.setAppliesToXPathExpression(expression); + } catch (XPathExpressionException e) { + ContributionReadException ce = new ContributionReadException(e); + error(monitor, "ContributionReadException", policySet, ce); + //throw ce; + } + } + + String attachTo = reader.getAttributeValue(null, ATTACH_TO); + if (attachTo != null) { + try { + XPath path = xpathHelper.newXPath(); + NamespaceContext nsContext = xpathHelper.getNamespaceContext(attachTo, reader.getNamespaceContext()); + path.setXPathFunctionResolver(new PolicyXPathFunctionResolver(nsContext)); + + attachTo = PolicyXPathFunction.normalize(attachTo,getSCAPrefix(nsContext)); + XPathExpression expression = xpathHelper.compile(path, nsContext, attachTo); + policySet.setAttachTo(attachTo); + policySet.setAttachToXPathExpression(expression); + } catch (XPathExpressionException e) { + ContributionReadException ce = new ContributionReadException(e); + error(monitor, "ContributionReadException", policySet, ce); + //throw ce; + } + + } + + readProvidedIntents(policySet, reader); + + int event = reader.getEventType(); + QName name = null; + reader.next(); + while (reader.hasNext()) { + event = reader.getEventType(); + switch (event) { + case START_ELEMENT: { + name = reader.getName(); + if (POLICY_INTENT_MAP_QNAME.equals(name)) { + Intent mappedIntent = policyFactory.createIntent(); + String provides = reader.getAttributeValue(null, PROVIDES); + if (provides != null) { + mappedIntent.setName(getQName(reader, PROVIDES)); + if (policySet.getProvidedIntents().contains(mappedIntent)) { + readIntentMap(reader, policySet, mappedIntent, context); + } else { + error(monitor, "IntentNotSpecified", policySet, policySetName); + } + } else { + error(monitor, "IntentMapProvidesMissing", reader, policySetName); + } + } else if (POLICY_SET_REFERENCE_QNAME.equals(name)) { + PolicySet referredPolicySet = policyFactory.createPolicySet(); + String referencename = reader.getAttributeValue(null, NAME); + if (referencename != null) { + referredPolicySet.setName(getQName(reader, NAME)); + policySet.getReferencedPolicySets().add(referredPolicySet); + } else { + error(monitor, "PolicySetReferenceNameMissing", reader, policySetName); + } + } /*else if ( WS_POLICY_QNAME.equals(name) ) { + OMElement policyElement = loadElement(reader); + org.apache.neethi.Policy wsPolicy = PolicyEngine.getPolicy(policyElement); + policySet.getPolicies().add(wsPolicy); + } */else { + Object extension = extensionProcessor.read(reader, context); + if (extension != null) { + PolicyExpression exp = policyFactory.createPolicyExpression(); + exp.setName(name); + exp.setPolicy(extension); + // check that all the policies in the policy set are + // expressed in the same language. Compare against the + // first expression we added + if ((policySet.getPolicies().size() > 0) && + (!policySet.getPolicies().get(0).getName().getNamespaceURI().equals(name.getNamespaceURI()))) { + error(monitor, "PolicyLanguageMissmatch", reader, policySet.getName(), policySet + .getPolicies().get(0).getName(), name); + } else { + policySet.getPolicies().add(exp); + } + } + } + break; + } + } + if (event == END_ELEMENT) { + if (POLICY_SET_QNAME.equals(reader.getName())) { + break; + } + } + + //Read the next element + if (reader.hasNext()) { + reader.next(); + } + } + return policySet; + } + + private String getSCAPrefix(NamespaceContext nsContext) { + + Iterator iter = nsContext.getPrefixes(SCA11_NS); + while (iter.hasNext()) { + String prefix = iter.next(); + if (!prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) { + return prefix; + } + } + + return "__sca"; + } + + public void readIntentMap(XMLStreamReader reader, PolicySet policySet, Intent mappedIntent, ProcessorContext context) + throws ContributionReadException { + Monitor monitor = context.getMonitor(); + QName name = reader.getName(); + if (POLICY_INTENT_MAP_QNAME.equals(name)) { + + IntentMap intentMap = policyFactory.createIntentMap(); + QName intentName = getQName(reader, INTENT_MAP); + intentMap.setProvidedIntent(mappedIntent); + + if (!policySet.getIntentMaps().contains(intentMap)) { + policySet.getIntentMaps().add(intentMap); + } else { + Monitor.error(context.getMonitor(), this, Messages.RESOURCE_BUNDLE, "IntentMapIsNotUnique", policySet + .getName().toString(), mappedIntent.getName().getLocalPart()); + } + + String qualifierName = null; + String qualfiedIntentName = null; + Intent qualifiedIntent = null; + + Qualifier qualifier = null; + + int event = reader.getEventType(); + try { + reader.next(); + while (reader.hasNext()) { + event = reader.getEventType(); + switch (event) { + case START_ELEMENT: { + name = reader.getName(); + if (POLICY_INTENT_MAP_QUALIFIER_QNAME.equals(name)) { + qualifierName = getString(reader, NAME); + if (qualifierName != null) { + qualfiedIntentName = + mappedIntent.getName().getLocalPart() + QUALIFIER + qualifierName; + qualifiedIntent = policyFactory.createIntent(); + qualifiedIntent.setName(new QName(mappedIntent.getName().getNamespaceURI(), + qualfiedIntentName)); + qualifier = policyFactory.createQualifier(); + qualifier.setIntent(qualifiedIntent); + intentMap.getQualifiers().add(qualifier); + + } else { + error(monitor, "QualifierNameMissing", reader, policySet.getName()); + } + } else if (POLICY_INTENT_MAP_QNAME.equals(name)) { + QName providedIntent = getQName(reader, PROVIDES); + if (qualifierName.equals(providedIntent.getLocalPart())) { + readIntentMap(reader, policySet, qualifiedIntent, context); + } else { + error(monitor, + "IntentMapDoesNotMatch", + providedIntent, + providedIntent, + qualifierName, + policySet); + //throw new ContributionReadException("Intent provided by IntentMap " + + //providedIntent + " does not match parent qualifier " + qualifierName + + //" in policyset - " + policySet); + } + } else { + Object extension = extensionProcessor.read(reader, context); + if (extension != null && qualifier != null) { + PolicyExpression exp = policyFactory.createPolicyExpression(); + exp.setName(name); + exp.setPolicy(extension); + qualifier.getPolicies().add(exp); + } + } + break; + } + } + if (event == END_ELEMENT && POLICY_INTENT_MAP_QNAME.equals(reader.getName())) { + break; + } + //Read the next element + if (reader.hasNext()) { + reader.next(); + } + } + } catch (XMLStreamException e) { + ContributionReadException ce = new ContributionReadException(e); + error(monitor, "ContributionReadException", reader, ce); + throw ce; + } + } + } + + public void write(PolicySet policySet, XMLStreamWriter writer, ProcessorContext context) + throws ContributionWriteException, XMLStreamException { + + // Write an + writer.writeStartElement(SCA11_NS, POLICY_SET); + writer.writeNamespace(policySet.getName().getPrefix(), policySet.getName().getNamespaceURI()); + writer.writeAttribute(NAME, policySet.getName().getPrefix() + COLON + policySet.getName().getLocalPart()); + + if (policySet.getAppliesTo() != null){ + writer.writeAttribute(APPLIES_TO, policySet.getAppliesTo()); + } + + if (policySet.getAttachTo() != null){ + writer.writeAttribute(ATTACH_TO, policySet.getAttachTo()); + } + + writeProvidedIntents(policySet, writer); + + writer.writeEndElement(); + } + + private void readProvidedIntents(PolicySet policySet, XMLStreamReader reader) { + String value = reader.getAttributeValue(null, PROVIDES); + if (value != null) { + List providedIntents = policySet.getProvidedIntents(); + for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) { + QName qname = getQNameValue(reader, tokens.nextToken()); + Intent intent = policyFactory.createIntent(); + intent.setName(qname); + providedIntents.add(intent); + } + } + } + + private void writeProvidedIntents(PolicySet policySet, XMLStreamWriter writer) throws XMLStreamException { + if (!policySet.getProvidedIntents().isEmpty()) { + StringBuffer sb = new StringBuffer(); + for (Intent providedIntents : policySet.getProvidedIntents()) { + sb.append(getQualifiedName(providedIntents.getName(), writer)); + sb.append(" "); + } + // Remove the last space + sb.deleteCharAt(sb.length() - 1); + writer.writeAttribute(PolicyConstants.PROVIDES, sb.toString()); + } + } + + private String getQualifiedName(QName name, XMLStreamWriter writer) throws XMLStreamException { + String local = name.getLocalPart(); + String prefix = writer.getPrefix(name.getNamespaceURI()); + if (prefix != null && prefix.length() > 0) { + return prefix + ':' + local; + } else { + return local; + } + } + + private void resolvePolicies(PolicySet policySet, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + boolean unresolved = false; + if (policySet != null) { + for (Object o : policySet.getPolicies()) { + extensionProcessor.resolve(o, resolver, context); + /*if ( o instanceof Policy && ((Policy)o).isUnresolved() ) { + unresolved = true; + }*/ + } + policySet.setUnresolved(unresolved); + } + } + + public QName getArtifactType() { + return POLICY_SET_QNAME; + } + + public Class getModelType() { + return PolicySet.class; + } + + private void resolveProvidedIntents(PolicySet policySet, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + if (policySet != null) { + //resolve all provided intents + List providedIntents = new ArrayList(); + for (Intent providedIntent : policySet.getProvidedIntents()) { + if (providedIntent.isUnresolved()) { + Intent resolved = resolver.resolveModel(Intent.class, providedIntent, context); + if (!resolved.isUnresolved() || resolved != providedIntent) { + providedIntents.add(resolved); + } else { + error(context.getMonitor(), "ProvidedIntentNotFound", policySet, providedIntent, policySet); + return; + //throw new ContributionResolveException("Provided Intent - " + providedIntent + //+ " not found for PolicySet " + policySet); + } + } else { + providedIntents.add(providedIntent); + } + } + policySet.getProvidedIntents().clear(); + policySet.getProvidedIntents().addAll(providedIntents); + } + } + + private void resolveIntentsInMappedPolicies(PolicySet policySet, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + Monitor monitor = context.getMonitor(); + for (IntentMap intentMap : policySet.getIntentMaps()) { + Intent intent = intentMap.getProvidedIntent(); + if (intent.isUnresolved()) { + Intent resolved = resolver.resolveModel(Intent.class, intent, context); + if (!resolved.isUnresolved() || resolved != intent) { + intentMap.setProvidedIntent(resolved); + } else { + error(monitor, "MappedIntentNotFound", policySet, intent, policySet); + return; + //throw new ContributionResolveException("Mapped Intent - " + mappedIntent + //+ " not found for PolicySet " + policySet); + } + } + for (Qualifier qualifier : intentMap.getQualifiers()) { + intent = qualifier.getIntent(); + if (intent.isUnresolved()) { + Intent resolved = resolver.resolveModel(Intent.class, intent, context); + if (!resolved.isUnresolved() || resolved != intent) { + qualifier.setIntent(resolved); + } else { + error(monitor, "MappedIntentNotFound", policySet, intent, policySet); + return; + //throw new ContributionResolveException("Mapped Intent - " + mappedIntent + //+ " not found for PolicySet " + policySet); + } + } + for (PolicyExpression exp : qualifier.getPolicies()) { + // FIXME: How to resolve the policies? + } + } + // validate that the intent map has a qualifier for each + // intent qualifier. The above code has already checked that the + // qualifiers that are there are resolved + Intent providedIntent = intentMap.getProvidedIntent(); + if (intentMap.getQualifiers().size() != providedIntent.getQualifiedIntents().size()) { + String missingQualifiers = ""; + for (Intent loopQualifiedIntent : providedIntent.getQualifiedIntents()) { + boolean found = false; + for (Qualifier loopQualifier : intentMap.getQualifiers()) { + if (loopQualifier.getIntent().getName().equals(loopQualifiedIntent.getName())) { + found = true; + break; + } + } + if (!found) { + missingQualifiers += loopQualifiedIntent.getName().getLocalPart() + " "; + } + } + if (missingQualifiers.length() > 0) { + Monitor.error(context.getMonitor(), + this, + Messages.RESOURCE_BUNDLE, + "IntentMapMissingQualifiers", + policySet.getName().toString(), + providedIntent.getName().getLocalPart(), + missingQualifiers); + } + } + } + + } + + private void resolveReferredPolicySets(PolicySet policySet, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + + List referredPolicySets = new ArrayList(); + for (PolicySet referredPolicySet : policySet.getReferencedPolicySets()) { + if (referredPolicySet.isUnresolved()) { + PolicySet resolved = resolver.resolveModel(PolicySet.class, referredPolicySet, context); + if (!resolved.isUnresolved() || resolved != referredPolicySet) { + referredPolicySets.add(resolved); + } else { + error(context.getMonitor(), "ReferredPolicySetNotFound", policySet, referredPolicySet, policySet); + return; + //throw new ContributionResolveException("Referred PolicySet - " + referredPolicySet + //+ "not found for PolicySet - " + policySet); + } + } else { + referredPolicySets.add(referredPolicySet); + } + } + policySet.getReferencedPolicySets().clear(); + policySet.getReferencedPolicySets().addAll(referredPolicySets); + } + + private void includeReferredPolicySets(PolicySet policySet, PolicySet referredPolicySet) { + for (PolicySet furtherReferredPolicySet : referredPolicySet.getReferencedPolicySets()) { + includeReferredPolicySets(referredPolicySet, furtherReferredPolicySet); + } + policySet.getPolicies().addAll(referredPolicySet.getPolicies()); + policySet.getIntentMaps().addAll(referredPolicySet.getIntentMaps()); + } + + public void resolve(PolicySet policySet, ModelResolver resolver, ProcessorContext context) + throws ContributionResolveException { + if (policySet != null && policySet.isUnresolved()) { + resolveProvidedIntents(policySet, resolver, context); + resolveIntentsInMappedPolicies(policySet, resolver, context); + resolveReferredPolicySets(policySet, resolver, context); + + for (PolicySet referredPolicySet : policySet.getReferencedPolicySets()) { + includeReferredPolicySets(policySet, referredPolicySet); + } + + if (policySet.isUnresolved()) { + //resolve the policy attachments + resolvePolicies(policySet, resolver, context); + + /*if ( !policySet.isUnresolved() ) { + resolver.addModel(policySet); + }*/ + } + + policySet.setUnresolved(false); + } + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java new file mode 100644 index 0000000000..8e2d7f9e60 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunction.java @@ -0,0 +1,295 @@ +/* + * 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.policy.xml; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.xml.XMLConstants; +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionException; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * The SCA-defined XPath function + */ +public class PolicyXPathFunction implements XPathFunction { + private static Logger logger = Logger.getLogger(PolicyXPathFunction.class.getName()); + + static final QName InterfaceRef = new QName(PolicyConstants.SCA11_NS, "InterfaceRef"); + static final QName OperationRef = new QName(PolicyConstants.SCA11_NS, "OperationRef"); + static final QName MessageRef = new QName(PolicyConstants.SCA11_NS, "MessageRef"); + static final QName IntentRefs = new QName(PolicyConstants.SCA11_NS, "IntentRefs"); + static final QName URIRef = new QName(PolicyConstants.SCA11_NS, "URIRef"); + + static final Set functions = + new HashSet(Arrays.asList(InterfaceRef, OperationRef, MessageRef, IntentRefs, URIRef)); + + private NamespaceContext namespaceContext; + private final QName functionName; + + public PolicyXPathFunction(NamespaceContext namespaceContext, QName functionName) { + super(); + this.namespaceContext = namespaceContext; + this.functionName = functionName; + } + + private Node getContextNode(List args) { + if (args.size() >= 2) { + NodeList nodeList = (NodeList)args.get(1); + if (nodeList.getLength() > 0) { + return nodeList.item(0); + } + } + return null; + } + + public Object evaluate(List args) throws XPathFunctionException { + if (logger.isLoggable(Level.FINE)) { + logger.fine(functionName + "(" + args + ")"); + } + + String arg = (String)args.get(0); + Node node = getContextNode(args); + /** + * If the xpath expression that contains the function does not select any nodes + * (eg IntentRefs('someIntent')), the context node passed in will be a Document. + * In this case we need to iterate over every Node in the Document and evaluate it. + * + * If the xpath expression does select nodes (eg //sca:component[IntentRefs('someIntent')]) + * then xpath will call evaluate for each node and we only need to return the result for that + * node. + */ + if ( node instanceof Document ) + return evaluateDocument(arg, (Document)node); + else + return evaluateNode(arg, node); + } + + public Object evaluateNode(String arg, Node node) { + if (InterfaceRef.equals(functionName)) { + return evaluateInterface(arg, node); + } else if (OperationRef.equals(functionName)) { + String[] params = arg.split("/"); + if (params.length != 2) { + throw new IllegalArgumentException("Invalid argument: " + arg); + } + String interfaceName = params[0]; + String operationName = params[1]; + return evaluateOperation(interfaceName, operationName, node); + } else if (MessageRef.equals(functionName)) { + String[] params = arg.split("/"); + if (params.length != 3) { + throw new IllegalArgumentException("Invalid argument: " + arg); + } + String interfaceName = params[0]; + String operationName = params[1]; + String messageName = params[2]; + return evaluateMessage(interfaceName, operationName, messageName, node); + } else if (URIRef.equals(functionName)) { + return evaluateURI(arg, node); + } else if (IntentRefs.equals(functionName)) { + String[] intents = arg.split("(\\s)+"); + return evaluateIntents(intents, node); + } else { + return Boolean.FALSE; + } + } + + private class NodeListImpl implements NodeList { + + private ArrayList list; + + public NodeListImpl() { + this.list = new ArrayList(); + } + public int getLength() { + return this.list.size(); + } + + public Node item(int index) { + return this.list.get(index); + } + public boolean add(Node node) { + return this.list.add(node); + + } + + } + private Object evaluateDocument(String arg, Document doc) { + NodeListImpl retList = new NodeListImpl(); + NodeList elements = doc.getElementsByTagName("*"); + for ( int i=0; i < elements.getLength(); i++) { + Object node = evaluateNode(arg, elements.item(i)); + if ( node != null ) + retList.add((Node)node); + } + return retList; + } + + private Boolean evaluateInterface(String interfaceName, Node node) { + return Boolean.FALSE; + } + + private Boolean evaluateOperation(String interfaceName, String operationName, Node node) { + return Boolean.FALSE; + } + + private Boolean evaluateMessage(String interfaceName, String operationName, String messageName, Node node) { + return Boolean.FALSE; + } + + private Boolean evaluateURI(String uri, Node node) { + return Boolean.FALSE; + } + + /** + * Evaluates a single node for the given intents. + * @param intents + * @param node + * @return + */ + private Object evaluateIntents(String[] intents, Node node) { + if ( node == null ) + return false; + + if ( node.getAttributes() != null ) { + for ( int i=0; i < node.getAttributes().getLength(); i++) { + Node attr = node.getAttributes().item(i); + + if ( "requires".equalsIgnoreCase(attr.getNodeName())) { + + for ( int j = 0; j < intents.length; j++ ) { + // Check negative intents + if ( intents[j].startsWith("!")) { + if ( matchIntent(intents[j].substring(1), attr, node.getNamespaceURI())) + return null; + } else if ( !matchIntent(intents[j], attr, node.getNamespaceURI())){ + return null; + } + } + return node; + } + + } + } + + return null; + } + + + + /** + * Determine whether the given intent is present in the "requires" attribute + * @param intent + * @param node + * @param namespaceURI + * @return + */ + private boolean matchIntent(String intent, Node node, String namespaceURI) { + String[] requires = node.getNodeValue().split("(\\s)+"); + QName intentName = getStringAsQName(intent); + + + for ( int i=0; i < requires.length; i++ ) { + QName nodeName = null; + int idx = requires[i].indexOf(':'); + + // No prefix specified + if ( idx == -1 ) { + nodeName = new QName(namespaceURI, requires[i]); + } else { + String prefix = requires[i].substring(0, idx); + String name = requires[i].substring(idx + 1); + String ns = node.lookupNamespaceURI(prefix); + nodeName = new QName(ns, name, prefix); + } + if ( intentName.equals(nodeName)) + return true; + } + return false; + } + + + private QName getStringAsQName(String intent) { + int idx = intent.indexOf(':'); + if (idx == -1) + return new QName(namespaceContext.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX), intent); + + String prefix = intent.substring(0, idx); + intent = intent.substring(idx + 1); + + return new QName(namespaceContext.getNamespaceURI(prefix), intent, prefix); + + } + + + private static Pattern FUNCTION; + static { + String functionPattern = "(URIRef|InterfaceRef|OperationRef|MessageRef|IntentRefs)\\s*\\(([^\\)]*)\\)"; + FUNCTION = Pattern.compile(functionPattern); + } + + /** Adds the node as an argument to the XPath function. + * Required in order to have access to the NodeList within the function + */ + public static String normalize(String attachTo, String scaPrefix) { + // Get rid of any whitespace + attachTo = attachTo.trim(); + + Matcher matcher = FUNCTION.matcher(attachTo); + boolean result = matcher.find(); + if (result) { + StringBuffer sb = new StringBuffer(); + do { + String function = matcher.group(1); + String args = matcher.group(2); + if ( (matcher.start() == 0) || (attachTo.charAt( matcher.start() -1) != ':' )) { + function = scaPrefix + ":" + function; + } + String replacement = null; + if (args.trim().length() > 0) { + replacement = function + "(" + args + "," + "self::node())"; + } else { + replacement = function + "(self::node())"; + } + matcher.appendReplacement(sb, replacement); + result = matcher.find(); + } while (result); + + matcher.appendTail(sb); + return sb.toString(); + } + return attachTo; + } + +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java new file mode 100644 index 0000000000..ca44667903 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/policy/xml/PolicyXPathFunctionResolver.java @@ -0,0 +1,72 @@ +/* + * 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.policy.xml; + +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.xpath.XPathFunction; +import javax.xml.xpath.XPathFunctionResolver; + +/** + * A resolver that handles SCA-defined XPath functions + * + * Interface Related Functions + *
    + *
  • InterfaceRef( InterfaceName ) + *
  • OperationRef( InterfaceName/OperationName ) + *
  • MessageRef( InterfaceName/OperationName/MessageName ) + *
+ * + * Intent Related Functions + *
    + *
  • IntentRefs( IntentList ) + *
+ * + * URI Based Function + *
    + *
  • URIRef( URI ) + *
+ */ +public class PolicyXPathFunctionResolver implements XPathFunctionResolver { + private NamespaceContext namespaceContext; + + public PolicyXPathFunctionResolver(NamespaceContext namespaceContext) { + super(); + this.namespaceContext = namespaceContext; + } + + public XPathFunction resolveFunction(QName functionName, int arity) { + if (functionName == null) { + throw new NullPointerException("Function name is null"); + } + if (PolicyXPathFunction.functions.contains(functionName)) { + if (arity >= 1) { + // We are relaxing the arity here so that we can pass in the context node + // by modifying the original xpath so that sca functions take self::node() + // as the 2nd argument + return new PolicyXPathFunction(namespaceContext, functionName); + } else { + throw new IllegalArgumentException("Invalid number of arguments: " + arity); + } + } + return null; + } + +} -- cgit v1.2.3