diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-11 23:26:33 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-11 23:26:33 +0000 |
commit | a40e527938d76ba71f211da7e327adb50384ba69 (patch) | |
tree | cb8f99f1727122b040a3f0fbb6649292b6a74302 /sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca | |
parent | 968721109881107520d7aefa91d7fcc0519d7739 (diff) |
Moving 1.x tags
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835157 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca')
9 files changed, 2136 insertions, 0 deletions
diff --git a/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseArtifactProcessor.java b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseArtifactProcessor.java new file mode 100644 index 0000000000..9223fa8947 --- /dev/null +++ b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/BaseArtifactProcessor.java @@ -0,0 +1,686 @@ +/* + * 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.END_ELEMENT; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.StringTokenizer; + +import javax.xml.XMLConstants; +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.Binding; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.ComponentType; +import org.apache.tuscany.sca.assembly.ConstrainingType; +import org.apache.tuscany.sca.assembly.Contract; +import org.apache.tuscany.sca.assembly.Implementation; +import org.apache.tuscany.sca.assembly.Multiplicity; +import org.apache.tuscany.sca.assembly.Property; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.policy.Intent; +import org.apache.tuscany.sca.policy.IntentAttachPoint; +import org.apache.tuscany.sca.policy.PolicyFactory; +import org.apache.tuscany.sca.policy.PolicySet; +import org.apache.tuscany.sca.policy.PolicySetAttachPoint; +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 BaseArtifactProcessor implements Constants { + + protected AssemblyFactory assemblyFactory; + protected PolicyFactory policyFactory; + protected StAXArtifactProcessor<Object> extensionProcessor; + + private static final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); + static { + domFactory.setNamespaceAware(true); + } + + /** + * Construcst a new BaseArtifactProcessor. + * @param factory + * @param policyFactory + */ + @SuppressWarnings("unchecked") + public BaseArtifactProcessor(AssemblyFactory factory, PolicyFactory policyFactory, StAXArtifactProcessor extensionProcessor) { + this.assemblyFactory = factory; + this.policyFactory = policyFactory; + this.extensionProcessor = (StAXArtifactProcessor<Object>)extensionProcessor; + } + + /** + * Returns the string value of an attribute. + * @param reader + * @param name + * @return + */ + protected String getString(XMLStreamReader reader, String name) { + return reader.getAttributeValue(null, name); + } + + /** + * Returns the qname value of an attribute. + * @param reader + * @param name + * @return + */ + protected QName getQName(XMLStreamReader reader, String name) { + String qname = reader.getAttributeValue(null, name); + return getQNameValue(reader, qname); + } + + /** + * Returns the value of xsi:type attribute + * @param reader The XML stream reader + * @return The QName of the type, if the attribute is not present, null is + * returned. + */ + protected QName getXSIType(XMLStreamReader reader) { + String qname = reader.getAttributeValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"); + return getQNameValue(reader, qname); + } + + /** + * Returns a qname from a string. + * @param reader + * @param value + * @return + */ + protected QName getQNameValue(XMLStreamReader reader, String value) { + if (value != null) { + int index = value.indexOf(':'); + String prefix = index == -1 ? "" : value.substring(0, index); + String localName = index == -1 ? value : value.substring(index + 1); + String ns = reader.getNamespaceContext().getNamespaceURI(prefix); + if (ns == null) { + ns = ""; + } + return new QName(ns, localName, prefix); + } else { + return null; + } + } + + /** + * Returns the boolean value of an attribute. + * @param reader + * @param name + * @return + */ + protected boolean getBoolean(XMLStreamReader reader, String name) { + String value = reader.getAttributeValue(null, name); + if (value == null) { + value = Boolean.toString(false); + } + return Boolean.valueOf(value); + } + + /** + * Returns the value of an attribute as a list of qnames. + * @param reader + * @param name + * @return + */ + protected List<QName> getQNames(XMLStreamReader reader, String name) { + String value = reader.getAttributeValue(null, name); + if (value != null) { + List<QName> qnames = new ArrayList<QName>(); + for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) { + qnames.add(getQName(reader, tokens.nextToken())); + } + return qnames; + } else { + return Collections.emptyList(); + } + } + + /** + * Read policy intents. + * @param attachPoint + * @param reader + */ + protected void readIntents(IntentAttachPoint attachPoint, XMLStreamReader reader) { + readIntents(attachPoint, null, reader); + } + + /** + * Read policy intents associated with an operation. + * @param attachPoint + * @param operation + * @param reader + */ + protected void readIntents(IntentAttachPoint attachPoint, Operation operation, XMLStreamReader reader) { + String value = reader.getAttributeValue(null, Constants.REQUIRES); + if (value != null) { + List<Intent> requiredIntents = attachPoint.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) { + intent.getOperations().add(operation); + } + requiredIntents.add(intent); + } + } + } + + /** + * Reads policy intents and policy sets. + * @param attachPoint + * @param reader + */ + protected void readPolicies(PolicySetAttachPoint attachPoint, XMLStreamReader reader) { + readPolicies(attachPoint, null, reader); + } + + /** + * Reads policy intents and policy sets associated with an operation. + * @param attachPoint + * @param operation + * @param reader + */ + protected void readPolicies(PolicySetAttachPoint attachPoint, Operation operation, XMLStreamReader reader) { + readIntents(attachPoint, operation, reader); + + String value = reader.getAttributeValue(null, Constants.POLICY_SETS); + if (value != null) { + List<PolicySet> policySets = attachPoint.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) { + policySet.getOperations().add(operation); + } + policySets.add(policySet); + } + } + } + + /** + * Read list of reference targets + * @param reference + * @param reader + */ + protected void readTargets(Reference reference, XMLStreamReader reader) { + String value = reader.getAttributeValue(null, Constants.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); + } + } + } + + /** + * 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); + } + } + + /** + * Returns the value of a constrainingType attribute. + * @param reader + * @return + */ + protected ConstrainingType getConstrainingType(XMLStreamReader reader) { + QName constrainingTypeName = getQName(reader, "constrainingType"); + if (constrainingTypeName != null) { + ConstrainingType constrainingType = assemblyFactory.createConstrainingType(); + constrainingType.setName(constrainingTypeName); + constrainingType.setUnresolved(true); + return constrainingType; + } else { + return null; + } + } + + /** + * Reads an abstract property element. + * @param prop + * @param reader + * @throws XMLStreamException + * @throws ContributionReadException + */ + protected void readAbstractProperty(AbstractProperty prop, XMLStreamReader reader) + throws XMLStreamException, ContributionReadException { + prop.setName(getString(reader, "name")); + prop.setMany(getBoolean(reader, "many")); + prop.setMustSupply(getBoolean(reader, "mustSupply")); + prop.setXSDElement(getQName(reader, "element")); + prop.setXSDType(getQName(reader, "type")); + try { + Document value = readPropertyValue(reader, prop.getXSDType()); + prop.setValue(value); + } catch (ParserConfigurationException e) { + throw new ContributionReadException(e); + } + } + + /** + * Reads a property element. + * @param prop + * @param reader + * @throws XMLStreamException + * @throws ContributionReadException + */ + protected void readProperty(Property prop, XMLStreamReader reader) + throws XMLStreamException, ContributionReadException { + readAbstractProperty(prop, reader); + } + + /** + * Parse the next child element. + * @param reader + * @return + * @throws XMLStreamException + */ + protected boolean nextChildElement(XMLStreamReader reader) throws XMLStreamException { + while (reader.hasNext()) { + int event = reader.next(); + if (event == END_ELEMENT) { + return false; + } + if (event == START_ELEMENT) { + return true; + } + } + return false; + } + + /** + * Advance the stream to the next END_ELEMENT event skipping any nested + * content. + * @param reader the reader to advance + * @throws XMLStreamException if there was a problem reading the stream + */ + protected void skipToEndElement(XMLStreamReader reader) throws XMLStreamException { + int depth = 0; + while (reader.hasNext()) { + int event = reader.next(); + if (event == XMLStreamConstants.START_ELEMENT) { + depth++; + } else if (event == XMLStreamConstants.END_ELEMENT) { + if (depth == 0) { + return; + } + depth--; + } + } + } + + /** + * Resolve an implementation. + * @param implementation + * @param resolver + * @return + * @throws ContributionResolveException + */ + protected Implementation resolveImplementation(Implementation implementation, ModelResolver resolver) throws ContributionResolveException { + if (implementation != null) { + if (implementation.isUnresolved()) { + implementation = resolver.resolveModel(Implementation.class, implementation); + + // Lazily resolve implementations + if (implementation.isUnresolved()) { + extensionProcessor.resolve(implementation, resolver); + if (!implementation.isUnresolved()) { + resolver.addModel(implementation); + } + } + } + } + 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 + */ + protected <C extends Contract> void resolveContracts(List<C> contracts, ModelResolver resolver) throws ContributionResolveException { + for (Contract contract: contracts) { + + // Resolve the interface contract + InterfaceContract interfaceContract = contract.getInterfaceContract(); + if (interfaceContract != null) { + extensionProcessor.resolve(interfaceContract, resolver); + } + + // Resolve bindings + for (int i = 0, n = contract.getBindings().size(); i < n; i++) { + Binding binding = contract.getBindings().get(i); + extensionProcessor.resolve(binding, resolver); + } + } + } + + /** + * 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 + */ + protected <C extends AbstractContract> void resolveAbstractContracts(List<C> contracts, ModelResolver resolver) throws ContributionResolveException { + for (AbstractContract contract: contracts) { + + // Resolve the interface contract + InterfaceContract interfaceContract = contract.getInterfaceContract(); + if (interfaceContract != null) { + extensionProcessor.resolve(interfaceContract, resolver); + } + } + } + + /** + * Start an element. + * @param uri + * @param name + * @param attrs + * @throws XMLStreamException + */ + protected void writeStart(XMLStreamWriter writer, String uri, String name, XAttr... attrs) throws XMLStreamException { + writer.writeStartElement(uri, name); + writeAttributes(writer, attrs); + } + + /** + * Start an element. + * @param writer + * @param name + * @param attrs + * @throws XMLStreamException + */ + protected void writeStart(XMLStreamWriter writer, String name, XAttr... attrs) throws XMLStreamException { + writer.writeStartElement(SCA10_NS, name); + writeAttributes(writer, attrs); + } + + /** + * End an element. + * @param writer + * @throws XMLStreamException + */ + protected void writeEnd(XMLStreamWriter writer) throws XMLStreamException { + writer.writeEndElement(); + } + + /** + * Start a document. + * @param writer + * @throws XMLStreamException + */ + protected void writeStartDocument(XMLStreamWriter writer, String name, XAttr... attrs) throws XMLStreamException { + writer.writeStartDocument(); + writer.setDefaultNamespace(SCA10_NS); + writeStart(writer, name, attrs); + writer.writeDefaultNamespace(SCA10_NS); + } + + /** + * End a document. + * @param writer + * @throws XMLStreamException + */ + protected void writeEndDocument(XMLStreamWriter writer) throws XMLStreamException { + writer.writeEndDocument(); + } + + /** + * Write attributes to the current element. + * @param writer + * @param attrs + * @throws XMLStreamException + */ + protected void writeAttributes(XMLStreamWriter writer, XAttr... attrs) throws XMLStreamException { + for (XAttr attr : attrs) { + if (attr != null) + attr.write(writer); + } + } + + /** + * Write an SCA abstract property declaration. + * @param writer + * @param prop + */ + protected void writeAbstractProperty(XMLStreamWriter writer, AbstractProperty prop) throws XMLStreamException { + } + + /** + * Write an SCA property declaration. + * @param writer + * @param prop + */ + protected void writeProperty(XMLStreamWriter writer, Property prop) throws XMLStreamException { + writeAbstractProperty(writer, prop); + } + + /** + * Returns a constrainingType attribute. + * @param componentType + * @return + */ + protected QName getConstrainingTypeAttr(ComponentType componentType) { + ConstrainingType constrainingType = componentType.getConstrainingType(); + if (constrainingType != null) + return constrainingType.getName(); + else + return null; + } + + /** + * Read a property value into a DOM document. + * @param reader + * @param type + * @return + * @throws XMLStreamException + * @throws ContributionReadException + * @throws ParserConfigurationException + */ + protected Document readPropertyValue(XMLStreamReader reader, QName type) + throws XMLStreamException, ParserConfigurationException { + + Document doc = createDocument(); + + // root element has no namespace and local name "value" + Element root = doc.createElementNS(null, "value"); + if (type != null) { + org.w3c.dom.Attr xsi = doc.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 = doc.createAttributeNS(W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi:type"); + xsiType.setValue(prefix + ":" + type.getLocalPart()); + root.setAttributeNodeNS(xsiType); + } + doc.appendChild(root); + + loadElement(reader, root); + return doc; + } + + /** + * Create a new DOM document. + * @return + * @throws ContributionReadException + */ + private Document createDocument() throws ParserConfigurationException { + return domFactory.newDocumentBuilder().newDocument(); + } + + /** + * 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) { + 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.next()) { + case XMLStreamConstants.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; + + declareNamespace(child, name.getPrefix(), name.getNamespaceURI()); + + 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); + } + + // 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 localPart = reader.getAttributeLocalName(i); + String value = reader.getAttributeValue(i); + child.setAttributeNS(ns, localPart, value); + declareNamespace(child, prefix, ns); + } + + break; + case XMLStreamConstants.CDATA: + current.appendChild(document.createCDATASection(reader.getText())); + break; + case XMLStreamConstants.CHARACTERS: + current.appendChild(document.createTextNode(reader.getText())); + break; + case XMLStreamConstants.END_ELEMENT: + // if we are back at the root then we are done + if (current == root) { + return; + } + + // pop the element off the stack + current = current.getParentNode(); + } + } + } +} diff --git a/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeDocumentProcessor.java b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeDocumentProcessor.java new file mode 100644 index 0000000000..32655689b2 --- /dev/null +++ b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeDocumentProcessor.java @@ -0,0 +1,94 @@ +/* + * 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.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.assembly.ComponentType; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; + +/** + * A componentType processor. + * + * @version $Rev$ $Date$ + */ +public class ComponentTypeDocumentProcessor extends BaseArtifactProcessor implements URLArtifactProcessor<ComponentType> { + private XMLInputFactory inputFactory; + + /** + * Constructs a new componentType processor. + * @param factory + * @param policyFactory + * @param registry + */ + public ComponentTypeDocumentProcessor(StAXArtifactProcessor staxProcessor, XMLInputFactory inputFactory) { + super(null, null, staxProcessor); + this.inputFactory = inputFactory; + } + + public ComponentType read(URL contributionURL, URI uri, URL url) throws ContributionReadException { + InputStream urlStream = null; + try { + urlStream = url.openStream(); + XMLStreamReader reader = inputFactory.createXMLStreamReader(urlStream); + reader.nextTag(); + ComponentType componentType = (ComponentType)extensionProcessor.read(reader); + componentType.setURI(url.toString()); + return componentType; + + } catch (XMLStreamException e) { + throw new ContributionReadException(e); + } catch (IOException e) { + throw new ContributionReadException(e); + } finally { + try { + if (urlStream != null) { + urlStream.close(); + urlStream = null; + } + } catch (IOException ioe) { + //ignore + } + } + } + + public void resolve(ComponentType componentType, ModelResolver resolver) throws ContributionResolveException { + extensionProcessor.resolve(componentType, resolver); + } + + public String getArtifactType() { + return ".componentType"; + } + + public Class<ComponentType> getModelType() { + return ComponentType.class; + } +} diff --git a/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java new file mode 100644 index 0000000000..386842e8d6 --- /dev/null +++ b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ComponentTypeProcessor.java @@ -0,0 +1,271 @@ +/* + * 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 java.util.ArrayList; +import java.util.List; + +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.Base; +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.Property; +import org.apache.tuscany.sca.assembly.Reference; +import org.apache.tuscany.sca.assembly.Service; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.contribution.service.ContributionWriteException; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.policy.PolicyFactory; + +/** + * A componentType processor. + * + * @version $Rev$ $Date$ + */ +public class ComponentTypeProcessor extends BaseArtifactProcessor implements StAXArtifactProcessor<ComponentType> { + + /** + * Constructs a new componentType processor. + * @param factory + * @param policyFactory + * @param registry + */ + public ComponentTypeProcessor(AssemblyFactory factory, PolicyFactory policyFactory, StAXArtifactProcessor extensionProcessor) { + super(factory, policyFactory, extensionProcessor); + } + + public ComponentType read(XMLStreamReader reader) 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> + componentType = assemblyFactory.createComponentType(); + componentType.setConstrainingType(getConstrainingType(reader)); + readPolicies(componentType, reader); + + } else if (Constants.SERVICE_QNAME.equals(name)) { + + // Read a <service> + service = assemblyFactory.createService(); + contract = service; + service.setName(getString(reader, Constants.NAME)); + componentType.getServices().add(service); + readPolicies(service, reader); + + } else if (Constants.REFERENCE_QNAME.equals(name)) { + // Read a <reference> + 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); + readPolicies(reference, reader); + + } else if (Constants.PROPERTY_QNAME.equals(name)) { + + // Read a <property> + property = assemblyFactory.createProperty(); + readPolicies(property, reader); + readProperty(property, reader); + componentType.getProperties().add(property); + + } else if (Constants.CALLBACK_QNAME.equals(name)) { + + // Read a <callback> + callback = assemblyFactory.createCallback(); + contract.setCallback(callback); + readPolicies(callback, reader); + + } else if (OPERATION.equals(name)) { + + // Read an <operation> + Operation operation = assemblyFactory.createOperation(); + operation.setName(getString(reader, NAME)); + operation.setUnresolved(true); + if (callback != null) { + readPolicies(callback, operation, reader); + } else { + readPolicies(contract, operation, reader); + } + } else { + + // Read an extension element + Object extension = extensionProcessor.read(reader); + if (extension != null) { + if (extension instanceof InterfaceContract) { + + // <service><interface> and <reference><interface> + contract.setInterfaceContract((InterfaceContract)extension); + + } else if (extension instanceof Binding) { + + // <service><binding> and <reference><binding> + contract.getBindings().add((Binding)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) { + throw new ContributionReadException(e); + } + return componentType; + } + + public void validate(ComponentType componentType, List<Base> problems) { + if (problems == null) { + problems = new ArrayList<Base>(); + } + validatePropertyDefinitions(componentType.getProperties(), problems); + } + + public void validatePropertyDefinitions(List<Property> properties, List<Base> problems) { + for(Property aProperty : properties) { + if (aProperty.isMustSupply() && aProperty.getValue() != null) { + problems.add(aProperty); + } + } + } + + public void write(ComponentType componentType, XMLStreamWriter writer) throws ContributionWriteException { + + try { + writeStartDocument(writer, COMPONENT_TYPE, + new XAttr(CONSTRAINING_TYPE, getConstrainingTypeAttr(componentType))); + + for (Service service : componentType.getServices()) { + writeStart(writer, SERVICE, new XAttr(NAME, service.getName())); + + extensionProcessor.write(service.getInterfaceContract(), writer); + + for (Binding binding: service.getBindings()) { + extensionProcessor.write(binding, writer); + } + + if (service.getCallback() != null) { + writeStart(writer, CALLBACK); + writeEnd(writer); + } + writeEnd(writer); + } + + for (Reference reference : componentType.getReferences()) { + // TODO handle multivalued target attribute + String target = reference.getTargets().isEmpty() ? null : reference.getTargets().get(0).getName(); + writeStart(writer, REFERENCE, + new XAttr(NAME, reference.getName()), + new XAttr(TARGET, target)); + + extensionProcessor.write(reference.getInterfaceContract(), writer); + + for (Binding binding: reference.getBindings()) { + extensionProcessor.write(binding, writer); + } + + if (reference.getCallback() != null) { + writeStart(writer, CALLBACK); + writeEnd(writer); + } + writeEnd(writer); + } + + for (Property property : componentType.getProperties()) { + writeStart(writer, PROPERTY, new XAttr(NAME, property.getName())); + writeEnd(writer); + } + + writeEndDocument(writer); + + } catch (XMLStreamException e) { + throw new ContributionWriteException(e); + } + } + + public void resolve(ComponentType componentType, ModelResolver resolver) throws ContributionResolveException { + + // Resolve component type services and references + resolveContracts(componentType.getServices(), resolver); + resolveContracts(componentType.getReferences(), resolver); + } + + public QName getArtifactType() { + return COMPONENT_TYPE_QNAME; + } + + public Class<ComponentType> getModelType() { + return ComponentType.class; + } +} diff --git a/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeDocumentProcessor.java b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeDocumentProcessor.java new file mode 100644 index 0000000000..feafb73c05 --- /dev/null +++ b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeDocumentProcessor.java @@ -0,0 +1,93 @@ +/* + * 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.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.assembly.Composite; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; + +/** + * A composite processor. + * + * @version $Rev$ $Date$ + */ +public class CompositeDocumentProcessor extends BaseArtifactProcessor implements URLArtifactProcessor<Composite> { + private XMLInputFactory inputFactory; + + /** + * Construct a new composite processor + * @param assemblyFactory + * @param policyFactory + * @param staxProcessor + */ + public CompositeDocumentProcessor(StAXArtifactProcessor staxProcessor, XMLInputFactory inputFactory) { + super(null, null, staxProcessor); + this.inputFactory = inputFactory; + } + + public Composite read(URL contributionURL, URI uri, URL url) throws ContributionReadException { + InputStream urlStream = null; + try { + urlStream = url.openStream(); + XMLStreamReader reader = inputFactory.createXMLStreamReader(urlStream); + reader.nextTag(); + Composite composite = (Composite)extensionProcessor.read(reader); + return composite; + + } catch (XMLStreamException e) { + throw new ContributionReadException(e); + } catch (IOException e) { + throw new ContributionReadException(e); + } finally { + try { + if (urlStream != null) { + urlStream.close(); + urlStream = null; + } + } catch (IOException ioe) { + //ignore + } + } + } + + public void resolve(Composite composite, ModelResolver resolver) throws ContributionResolveException { + extensionProcessor.resolve(composite, resolver); + } + + public String getArtifactType() { + return ".composite"; + } + + public Class<Composite> getModelType() { + return Composite.class; + } +} diff --git a/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java new file mode 100644 index 0000000000..962aeb67dd --- /dev/null +++ b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/CompositeProcessor.java @@ -0,0 +1,498 @@ +/* + * 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 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 org.apache.tuscany.sca.assembly.AssemblyFactory; +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.ConstrainingType; +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.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.contribution.service.ContributionWriteException; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.policy.PolicyFactory; + +/** + * A composite processor. + * + * @version $Rev$ $Date$ + */ +public class CompositeProcessor extends BaseArtifactProcessor implements StAXArtifactProcessor<Composite> { + + /** + * Construct a new composite processor + * @param assemblyFactory + * @param policyFactory + * @param extensionProcessor + */ + public CompositeProcessor(AssemblyFactory factory, PolicyFactory policyFactory, + InterfaceContractMapper interfaceContractMapper, + StAXArtifactProcessor extensionProcessor) { + super(factory, policyFactory, extensionProcessor); + } + + public Composite read(XMLStreamReader reader) 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; + + 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> + composite = assemblyFactory.createComposite(); + composite.setName(new QName(getString(reader, TARGET_NAMESPACE), getString(reader, NAME))); + composite.setAutowire(getBoolean(reader, AUTOWIRE)); + composite.setLocal(getBoolean(reader, LOCAL)); + composite.setConstrainingType(getConstrainingType(reader)); + readPolicies(composite, reader); + + } else if (INCLUDE_QNAME.equals(name)) { + + // Read an <include> + include = assemblyFactory.createComposite(); + include.setName(getQName(reader, "name")); + include.setUnresolved(true); + composite.getIncludes().add(include); + + } else if (SERVICE_QNAME.equals(name)) { + if (component != null) { + + // Read a <component><service> + componentService = assemblyFactory.createComponentService(); + contract = componentService; + componentService.setName(getString(reader, NAME)); + component.getServices().add(componentService); + readPolicies(contract, reader); + } else { + + // Read a <composite><service> + compositeService = assemblyFactory.createCompositeService(); + contract = compositeService; + compositeService.setName(getString(reader, NAME)); + + ComponentService promoted = assemblyFactory.createComponentService(); + promoted.setUnresolved(true); + promoted.setName(getString(reader, PROMOTE)); + compositeService.setPromotedService(promoted); + + composite.getServices().add(compositeService); + readPolicies(contract, reader); + } + + } else if (REFERENCE_QNAME.equals(name)) { + if (component != null) { + // Read a <component><reference> + componentReference = assemblyFactory.createComponentReference(); + contract = componentReference; + componentReference.setName(getString(reader, NAME)); + readMultiplicity(componentReference, reader); + componentReference.setAutowire(getBoolean(reader, AUTOWIRE)); + readTargets(componentReference, reader); + componentReference.setWiredByImpl(getBoolean(reader, WIRED_BY_IMPL)); + component.getReferences().add(componentReference); + readPolicies(contract, reader); + } else { + // Read a <composite><reference> + compositeReference = assemblyFactory.createCompositeReference(); + contract = compositeReference; + compositeReference.setName(getString(reader, NAME)); + readMultiplicity(compositeReference, reader); + readTargets(compositeReference, reader); + readPromotes(compositeReference, reader); + compositeReference.setWiredByImpl(getBoolean(reader, WIRED_BY_IMPL)); + composite.getReferences().add(compositeReference); + readPolicies(contract, reader); + } + + } else if (PROPERTY_QNAME.equals(name)) { + if (component != null) { + // Read a <component><property> + componentProperty = assemblyFactory.createComponentProperty(); + property = componentProperty; + componentProperty.setSource(getString(reader, SOURCE)); + componentProperty.setFile(getString(reader, FILE)); + readPolicies(property, reader); + readProperty(componentProperty, reader); + component.getProperties().add(componentProperty); + } else { + + // Read a <composite><property> + property = assemblyFactory.createProperty(); + readPolicies(property, reader); + readProperty(property, reader); + composite.getProperties().add(property); + } + + } else if (COMPONENT_QNAME.equals(name)) { + + // Read a <component> + component = assemblyFactory.createComponent(); + component.setName(getString(reader, NAME)); + component.setConstrainingType(getConstrainingType(reader)); + composite.getComponents().add(component); + readPolicies(component, reader); + + } else if (WIRE_QNAME.equals(name)) { + + // Read a <wire> + wire = assemblyFactory.createWire(); + ComponentReference source = assemblyFactory.createComponentReference(); + source.setUnresolved(true); + source.setName(getString(reader, SOURCE)); + wire.setSource(source); + + ComponentService target = assemblyFactory.createComponentService(); + target.setUnresolved(true); + target.setName(getString(reader, TARGET)); + wire.setTarget(target); + + composite.getWires().add(wire); + readPolicies(wire, reader); + + } else if (CALLBACK_QNAME.equals(name)) { + + // Read a <callback> + callback = assemblyFactory.createCallback(); + contract.setCallback(callback); + readPolicies(callback, reader); + + } else if (OPERATION_QNAME.equals(name)) { + + // Read an <operation> + Operation operation = assemblyFactory.createOperation(); + operation.setName(getString(reader, NAME)); + operation.setUnresolved(true); + if (callback != null) { + readPolicies(callback, operation, reader); + } else { + readPolicies(contract, operation, reader); + } + } else if (IMPLEMENTATION_COMPOSITE_QNAME.equals(name)) { + + // Read an implementation.composite + Composite implementation = assemblyFactory.createComposite(); + implementation.setName(getQName(reader, NAME)); + implementation.setUnresolved(true); + component.setImplementation(implementation); + + } else { + + // Read an extension element + Object extension = extensionProcessor.read(reader); + if (extension != null) { + if (extension instanceof InterfaceContract) { + + // <service><interface> and + // <reference><interface> + contract.setInterfaceContract((InterfaceContract)extension); + + } else if (extension instanceof Binding) { + // <service><binding> and <reference><binding> + contract.getBindings().add((Binding)extension); + + } else if (extension instanceof Implementation) { + + // <component><implementation> + component.setImplementation((Implementation)extension); + } else { + + // FIXME: We need to decide where to host the extensions + 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(); + } + } + return composite; + + } catch (XMLStreamException e) { + throw new ContributionReadException(e); + } + } + + public void write(Composite composite, XMLStreamWriter writer) throws ContributionWriteException { + + try { + writeStartDocument(writer, COMPOSITE, + new XAttr(CONSTRAINING_TYPE, getConstrainingTypeAttr(composite)), + new XAttr(TARGET_NAMESPACE, composite.getName().getNamespaceURI()), + new XAttr(NAME, composite.getName().getLocalPart())); + + for (Service service : composite.getServices()) { + CompositeService compositeService = (CompositeService)service; + ComponentService promotedService = compositeService.getPromotedService(); + String promote = promotedService != null ? promotedService.getName() : null; + writeStart(writer, SERVICE, new XAttr(NAME, service.getName()), new XAttr(PROMOTE, promote)); + + extensionProcessor.write(service.getInterfaceContract(), writer); + + for (Binding binding: service.getBindings()) { + extensionProcessor.write(binding, writer); + } + + if (service.getCallback() != null) { + writeStart(writer, CALLBACK); + writeEnd(writer); + } + writeEnd(writer); + } + + for (Component component : composite.getComponents()) { + writeStart(writer, COMPONENT, new XAttr(NAME, component.getName())); + + for (ComponentService service : component.getServices()) { + writeStart(writer, SERVICE, new XAttr(NAME, service.getName())); + + extensionProcessor.write(service.getInterfaceContract(), writer); + + for (Binding binding: service.getBindings()) { + extensionProcessor.write(binding, writer); + } + + if (service.getCallback() != null) { + writeStart(writer, CALLBACK); + writeEnd(writer); + } + writeEnd(writer); + } + + for (ComponentReference reference : component.getReferences()) { + // TODO handle multivalued target attribute + String target = reference.getTargets().isEmpty() ? null : reference.getTargets().get(0).getName(); + writeStart(writer, REFERENCE, + new XAttr(NAME, reference.getName()), + new XAttr(TARGET,target)); + + extensionProcessor.write(reference.getInterfaceContract(), writer); + + for (Binding binding: reference.getBindings()) { + extensionProcessor.write(binding, writer); + } + + if (reference.getCallback() != null) { + writeStart(writer, CALLBACK); + writeEnd(writer); + } + writeEnd(writer); + } + + for (ComponentProperty property : component.getProperties()) { + writeStart(writer, PROPERTY, new XAttr(NAME, property.getName())); + writeEnd(writer); + } + + // Write the component implementation + Implementation implementation = component.getImplementation(); + if (implementation instanceof Composite) { + writeStart(writer, IMPLEMENTATION_COMPOSITE, + new XAttr(NAME, composite.getName())); + writeEnd(writer); + } else { + extensionProcessor.write(component.getImplementation(), writer); + } + + writeEnd(writer); + } + + for (Reference reference : composite.getReferences()) { + // TODO handle multivalued promote attribute + CompositeReference compositeReference = (CompositeReference)reference; + String promote; + if (!compositeReference.getPromotedReferences().isEmpty()) + promote = compositeReference.getPromotedReferences().get(0).getName(); + else + promote = null; + writeStart(writer, REFERENCE, + new XAttr(NAME, reference.getName()), + new XAttr(PROMOTE, promote)); + + extensionProcessor.write(reference.getInterfaceContract(), writer); + + for (Binding binding: reference.getBindings()) { + extensionProcessor.write(binding, writer); + } + + if (reference.getCallback() != null) { + writeStart(writer, CALLBACK); + writeEnd(writer); + } + writeEnd(writer); + } + + for (Property property : composite.getProperties()) { + writeStart(writer, PROPERTY, new XAttr(NAME, property.getName())); + writeEnd(writer); + } + + writeEndDocument(writer); + + } catch (XMLStreamException e) { + throw new ContributionWriteException(e); + } + } + + public void resolve(Composite composite, ModelResolver resolver) throws ContributionResolveException { + + // Resolve constraining type + ConstrainingType constrainingType = composite.getConstrainingType(); + constrainingType = resolver.resolveModel(ConstrainingType.class, constrainingType); + composite.setConstrainingType(constrainingType); + + // Resolve includes in the composite + for (int i = 0, n = composite.getIncludes().size(); i < n; i++) { + Composite include = composite.getIncludes().get(i); + include = resolver.resolveModel(Composite.class, include); + composite.getIncludes().set(i, include); + } + + // resolve and extensions to the standard SCDL that appear in the + // SCDL. + for (int i = 0, n = composite.getExtensions().size(); i < n; i++) { + Object model = composite.getExtensions().get(i); + extensionProcessor.resolve(model, resolver); + } + + // Resolve component implementations, services and references + for (Component component: composite.getComponents()) { + constrainingType = component.getConstrainingType(); + constrainingType = resolver.resolveModel(ConstrainingType.class, constrainingType); + component.setConstrainingType(constrainingType); + + Implementation implementation = component.getImplementation(); + implementation = resolveImplementation(implementation, resolver); + component.setImplementation(implementation); + + resolveContracts(component.getServices(), resolver); + resolveContracts(component.getReferences(), resolver); + } + + // Resolve composite services and references + resolveContracts(composite.getServices(), resolver); + resolveContracts(composite.getReferences(), resolver); + } + + public QName getArtifactType() { + return COMPOSITE_QNAME; + } + + public Class<Composite> getModelType() { + return Composite.class; + } + + /** + * Read list of refence targets + * @param reference + * @param reader + */ + protected void readPromotes(CompositeReference reference, XMLStreamReader reader) { + String value = reader.getAttributeValue(null, Constants.PROMOTE); + ComponentReference promoted = null; + if (value != null) { + for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) { + promoted = assemblyFactory.createComponentReference(); + promoted.setUnresolved(true); + promoted.setName(tokens.nextToken()); + reference.getPromotedReferences().add(promoted); + } + } + } +} diff --git a/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Constants.java b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Constants.java new file mode 100644 index 0000000000..cd98685203 --- /dev/null +++ b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/Constants.java @@ -0,0 +1,74 @@ +/* + * 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. + */ +public interface Constants { + String SCA10_NS = "http://www.osoa.org/xmlns/sca/1.0"; + String COMPONENT_TYPE = "componentType"; + QName COMPONENT_TYPE_QNAME = new QName(SCA10_NS, COMPONENT_TYPE); + String SERVICE = "service"; + QName SERVICE_QNAME = new QName(SCA10_NS, SERVICE); + String REFERENCE = "reference"; + QName REFERENCE_QNAME = new QName(SCA10_NS, REFERENCE); + String PROPERTY = "property"; + QName PROPERTY_QNAME = new QName(SCA10_NS, PROPERTY); + String CONSTRAINING_TYPE = "constrainingType"; + QName CONSTRAINING_TYPE_QNAME = new QName(SCA10_NS, CONSTRAINING_TYPE); + String COMPOSITE = "composite"; + QName COMPOSITE_QNAME = new QName(SCA10_NS, COMPOSITE); + String INCLUDE = "include"; + QName INCLUDE_QNAME = new QName(SCA10_NS, INCLUDE); + String COMPONENT = "component"; + QName COMPONENT_QNAME = new QName(SCA10_NS, COMPONENT); + String WIRE = "wire"; + QName WIRE_QNAME = new QName(SCA10_NS, WIRE); + String NAME = "name"; + String TARGET_NAMESPACE = "targetNamespace"; + String LOCAL = "local"; + String AUTOWIRE = "autowire"; + String REQUIRES = "requires"; + String POLICY_SETS = "policySets"; + String OPERATION = "operation"; + QName OPERATION_QNAME = new QName(SCA10_NS, OPERATION); + String CALLBACK = "callback"; + QName CALLBACK_QNAME = new QName(SCA10_NS, CALLBACK); + 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 IMPLEMENTATION_COMPOSITE = "implementation.composite"; + QName IMPLEMENTATION_COMPOSITE_QNAME = new QName(SCA10_NS, IMPLEMENTATION_COMPOSITE); +} diff --git a/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeDocumentProcessor.java b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeDocumentProcessor.java new file mode 100644 index 0000000000..fa22602356 --- /dev/null +++ b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeDocumentProcessor.java @@ -0,0 +1,93 @@ +/* + * 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.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.assembly.ConstrainingType; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; + +/** + * A contrainingType content handler. + * + * @version $Rev$ $Date$ + */ +public class ConstrainingTypeDocumentProcessor extends BaseArtifactProcessor implements URLArtifactProcessor<ConstrainingType> { + private XMLInputFactory inputFactory; + + /** + * Construct a new constrainingType processor. + * @param factory + * @param policyFactory + * @param staxProcessor + */ + public ConstrainingTypeDocumentProcessor(StAXArtifactProcessor staxProcessor, XMLInputFactory inputFactory) { + super(null, null, staxProcessor); + this.inputFactory = inputFactory; + } + + public ConstrainingType read(URL contributionURL, URI uri, URL url) throws ContributionReadException { + InputStream urlStream = null; + try { + urlStream = url.openStream(); + XMLStreamReader reader = inputFactory.createXMLStreamReader(urlStream); + reader.nextTag(); + ConstrainingType constrainingType = (ConstrainingType)extensionProcessor.read(reader); + return constrainingType; + + } catch (XMLStreamException e) { + throw new ContributionReadException(e); + } catch (IOException e) { + throw new ContributionReadException(e); + } finally { + try { + if (urlStream != null) { + urlStream.close(); + urlStream = null; + } + } catch (IOException ioe) { + //ignore + } + } + } + + public void resolve(ConstrainingType constrainingType, ModelResolver resolver) throws ContributionResolveException { + extensionProcessor.resolve(constrainingType, resolver); + } + + public String getArtifactType() { + return ".constrainingType"; + } + + public Class<ConstrainingType> getModelType() { + return ConstrainingType.class; + } +} diff --git a/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeProcessor.java b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeProcessor.java new file mode 100644 index 0000000000..74a9fc430e --- /dev/null +++ b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/ConstrainingTypeProcessor.java @@ -0,0 +1,205 @@ +/* + * 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.AbstractContract; +import org.apache.tuscany.sca.assembly.AbstractProperty; +import org.apache.tuscany.sca.assembly.AbstractReference; +import org.apache.tuscany.sca.assembly.AbstractService; +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.ConstrainingType; +import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.service.ContributionReadException; +import org.apache.tuscany.sca.contribution.service.ContributionResolveException; +import org.apache.tuscany.sca.contribution.service.ContributionWriteException; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.policy.PolicyFactory; + +/** + * A contrainingType content handler. + * + * @version $Rev$ $Date$ + */ +public class ConstrainingTypeProcessor extends BaseArtifactProcessor implements StAXArtifactProcessor<ConstrainingType> { + + /** + * Construct a new constrainingType processor. + * @param factory + * @param policyFactory + * @param extensionProcessor + */ + public ConstrainingTypeProcessor(AssemblyFactory factory, PolicyFactory policyFactory, StAXArtifactProcessor extensionProcessor) { + super(factory, policyFactory, extensionProcessor); + } + + public ConstrainingType read(XMLStreamReader reader) throws ContributionReadException { + ConstrainingType constrainingType = null; + AbstractService abstractService = null; + AbstractReference abstractReference = null; + AbstractProperty abstractProperty = null; + AbstractContract abstractContract = null; + QName name = null; + + try { + + // Read the constrainingType document + while (reader.hasNext()) { + int event = reader.getEventType(); + switch (event) { + + case START_ELEMENT: + name = reader.getName(); + + // Read a <constrainingType> + if (Constants.CONSTRAINING_TYPE_QNAME.equals(name)) { + constrainingType = assemblyFactory.createConstrainingType(); + constrainingType.setName(new QName(getString(reader, TARGET_NAMESPACE), getString(reader, NAME))); + readIntents(constrainingType, reader); + + } else if (Constants.SERVICE_QNAME.equals(name)) { + + // Read a <service> + abstractService = assemblyFactory.createAbstractService(); + abstractContract = abstractService; + abstractService.setName(getString(reader, Constants.NAME)); + constrainingType.getServices().add(abstractService); + readIntents(abstractService, reader); + + } else if (Constants.REFERENCE_QNAME.equals(name)) { + + // Read a <reference> + abstractReference = assemblyFactory.createAbstractReference(); + abstractContract = abstractReference; + abstractReference.setName(getString(reader, Constants.NAME)); + readMultiplicity(abstractReference, reader); + constrainingType.getReferences().add(abstractReference); + readIntents(abstractReference, reader); + + } else if (Constants.PROPERTY_QNAME.equals(name)) { + + // Read a <property> + abstractProperty = assemblyFactory.createAbstractProperty(); + readAbstractProperty(abstractProperty, reader); + constrainingType.getProperties().add(abstractProperty); + readIntents(abstractProperty, reader); + + } else if (OPERATION.equals(name)) { + + // Read an <operation> + Operation operation = assemblyFactory.createOperation(); + operation.setName(getString(reader, NAME)); + operation.setUnresolved(true); + readIntents(abstractContract, operation, reader); + + } else { + + // Read an extension element + Object extension = extensionProcessor.read(reader); + if (extension instanceof InterfaceContract) { + + // <service><interface> and <reference><interface> + abstractContract.setInterfaceContract((InterfaceContract)extension); + } + } + break; + + case END_ELEMENT: + name = reader.getName(); + + // Clear current state when reading reaching end element + if (SERVICE_QNAME.equals(name)) { + abstractService = null; + abstractContract = null; + } else if (REFERENCE_QNAME.equals(name)) { + abstractReference = null; + abstractContract = null; + } else if (PROPERTY_QNAME.equals(name)) { + abstractProperty = null; + } + break; + } + if (reader.hasNext()) { + reader.next(); + } + } + return constrainingType; + + } catch (XMLStreamException e) { + throw new ContributionReadException(e); + } + } + + public void write(ConstrainingType constrainingType, XMLStreamWriter writer) throws ContributionWriteException { + + try { + writeStartDocument(writer, CONSTRAINING_TYPE, + new XAttr(TARGET_NAMESPACE, constrainingType.getName().getNamespaceURI()), + new XAttr(NAME, constrainingType.getName().getLocalPart())); + + for (AbstractService service : constrainingType.getServices()) { + writeStart(writer, SERVICE, new XAttr(NAME, service.getName())); + extensionProcessor.write(service.getInterfaceContract(), writer); + writeEnd(writer); + } + + for (AbstractReference reference : constrainingType.getReferences()) { + writeStart(writer, REFERENCE, + new XAttr(NAME, reference.getName())); + extensionProcessor.write(reference.getInterfaceContract(), writer); + writeEnd(writer); + } + + for (AbstractProperty property : constrainingType.getProperties()) { + writeStart(writer, PROPERTY, new XAttr(NAME, property.getName())); + writeEnd(writer); + } + + writeEndDocument(writer); + + } catch (XMLStreamException e) { + throw new ContributionWriteException(e); + } + } + + public void resolve(ConstrainingType constrainingType, ModelResolver resolver) throws ContributionResolveException { + + // Resolve component type services and references + resolveAbstractContracts(constrainingType.getServices(), resolver); + resolveAbstractContracts(constrainingType.getReferences(), resolver); + } + + public QName getArtifactType() { + return CONSTRAINING_TYPE_QNAME; + } + + public Class<ConstrainingType> getModelType() { + return ConstrainingType.class; + } +} diff --git a/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/XAttr.java b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/XAttr.java new file mode 100644 index 0000000000..3c991798de --- /dev/null +++ b/sca-java-1.x/tags/0.91-incubating/modules/assembly-xml/src/main/java/org/apache/tuscany/sca/assembly/xml/XAttr.java @@ -0,0 +1,122 @@ +/* + * 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.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; + + +/** + * Represents an XML attribute that needs to be written to a document. + * + * @version $Rev$ $Date$ + */ +class XAttr { + + String uri = Constants.SCA10_NS; + String name; + Object value; + + public XAttr(String uri, String name, String value) { + this.uri = uri; + this.name = name; + this.value = value; + } + + public XAttr(String name, String value) { + this.name = name; + this.value = value; + } + + public XAttr(String uri, String name, boolean value) { + this.uri = uri; + this.name = name; + this.value = value; + } + + public XAttr(String name, boolean value) { + this.name = name; + this.value = value; + } + + public XAttr(String uri, String name, QName value) { + this.uri = uri; + this.name = name; + this.value = value; + } + + public XAttr(String name, QName value) { + this.name = name; + this.value = value; + } + + /** + * Writes a string from a qname and registers a prefix for its namespace. + * @param reader + * @param value + * @return + */ + protected String writeQNameValue(XMLStreamWriter writer, QName qname) throws XMLStreamException { + if (qname != null) { + String prefix = qname.getPrefix(); + String uri = qname.getNamespaceURI(); + prefix = writer.getPrefix(uri); + if (prefix != null) { + + // Use the prefix already bound to the given uri + return prefix + ":" + qname.getLocalPart(); + } else { + + // Find an available prefix and bind it to the given uri + NamespaceContext nsc = writer.getNamespaceContext(); + for (int i=1; ; i++) { + prefix = "ns" + i; + if (nsc.getNamespaceURI(prefix) == null) { + break; + } + } + writer.setPrefix(prefix, uri); + writer.writeNamespace(prefix, uri); + return prefix + ":" + qname.getLocalPart(); + } + } else { + return null; + } + } + + void write(XMLStreamWriter writer) throws XMLStreamException { + if (value != null) { + String str; + if (value instanceof QName) { + str = writeQNameValue(writer, (QName)value); + } else { + str = String.valueOf(value); + } + if (uri != null && !uri.equals(Constants.SCA10_NS)) { + writer.writeAttribute(uri, name, str); + } else { + writer.writeAttribute(name,str); + } + } + } + +} |