diff options
Diffstat (limited to 'sca-java-1.x/branches/sca-java-0.91/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml')
4 files changed, 523 insertions, 0 deletions
diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLConstants.java b/sca-java-1.x/branches/sca-java-0.91/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLConstants.java new file mode 100644 index 0000000000..7de5d5349f --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLConstants.java @@ -0,0 +1,35 @@ +/* + * 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.interfacedef.wsdl.xml; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.assembly.xml.Constants; + +public interface WSDLConstants { + + String INTERFACE_WSDL = "interface.wsdl"; + QName INTERFACE_WSDL_QNAME = new QName(Constants.SCA10_NS, "interface.wsdl"); + String INTERFACE = "interface"; + String CALLBACK_INTERFACE = "callbackInterface"; + String WSDL_LOCATION = "wsdlLocation"; + String WSDLI_NS = "http://www.w3.org/2004/08/wsdl-instance"; + QName WSDL_LOCATION_QNAME = new QName(WSDLI_NS, WSDL_LOCATION); + +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessor.java b/sca-java-1.x/branches/sca-java-0.91/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessor.java new file mode 100644 index 0000000000..782340982a --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLDocumentProcessor.java @@ -0,0 +1,190 @@ +/* + * 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.interfacedef.wsdl.xml; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URL; + +import javax.wsdl.Definition; +import javax.wsdl.Types; +import javax.wsdl.WSDLException; +import javax.wsdl.extensions.ExtensionRegistry; +import javax.wsdl.extensions.schema.Schema; +import javax.wsdl.xml.WSDLLocator; +import javax.wsdl.xml.WSDLReader; + +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; +import org.apache.tuscany.sca.contribution.service.ContributionRuntimeException; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory; +import org.apache.ws.commons.schema.XmlSchemaCollection; +import org.apache.ws.commons.schema.resolver.URIResolver; +import org.w3c.dom.Element; +import org.xml.sax.InputSource; + +/** + * An ArtifactProcessor for WSDL documents. + * + * @version $Rev$ $Date$ + */ +public class WSDLDocumentProcessor implements URLArtifactProcessor<WSDLDefinition> { + + private javax.wsdl.factory.WSDLFactory wsdlFactory; + private ExtensionRegistry wsdlExtensionRegistry; + private WSDLFactory factory; + + /** + * Implementation of a WSDL locator. + */ + private class WSDLLocatorImpl implements WSDLLocator { + private InputStream inputStream; + private URL base; + private String latestImportURI; + + public WSDLLocatorImpl(URL base, InputStream is) { + this.base = base; + this.inputStream = is; + } + + public void close() { + } + + public InputSource getBaseInputSource() { + return new InputSource(inputStream); + } + + public String getBaseURI() { + return base.toString(); + } + + public InputSource getImportInputSource(String parentLocation, String importLocation) { + try { + URL url = new URL(new URL(parentLocation), importLocation); + latestImportURI = url.toString(); + return new InputSource(url.openStream()); + } catch (Exception e) { + throw new ContributionRuntimeException(e); + } + } + + public String getLatestImportURI() { + return latestImportURI; + } + + } + + /** + * URI resolver implementation for xml schema + */ + private class URIResolverImpl implements URIResolver { + + public org.xml.sax.InputSource resolveEntity(java.lang.String targetNamespace, + java.lang.String schemaLocation, + java.lang.String baseUri) { + try { + URL url = new URL(new URL(baseUri), schemaLocation); + return new InputSource(url.openStream()); + } catch (IOException e) { + return null; + } + } + } + + public WSDLDocumentProcessor(WSDLFactory factory, javax.wsdl.factory.WSDLFactory wsdlFactory) { + this.factory = factory; + + if (wsdlFactory != null) { + this.wsdlFactory = wsdlFactory; + } else { + try { + this.wsdlFactory = javax.wsdl.factory.WSDLFactory.newInstance(); + } catch (WSDLException e) { + throw new ContributionRuntimeException(e); + } + } + + wsdlExtensionRegistry = this.wsdlFactory.newPopulatedExtensionRegistry(); + } + + public WSDLDefinition read(URL contributionURL, URI artifactURI, URL artifactURL) throws ContributionReadException { + try { + + // Read a WSDL document + InputStream is = artifactURL.openStream(); + WSDLReader reader = wsdlFactory.newWSDLReader(); + reader.setFeature("javax.wsdl.verbose", false); + reader.setExtensionRegistry(wsdlExtensionRegistry); + + WSDLLocatorImpl locator = new WSDLLocatorImpl(artifactURL, is); + Definition definition = reader.readWSDL(locator); + + WSDLDefinition wsdlDefinition = factory.createWSDLDefinition(); + wsdlDefinition.setDefinition(definition); + + // get base uri for any relative schema includes + + + // Read inline schemas + Types types = definition.getTypes(); + if (types != null) { + wsdlDefinition.getInlinedSchemas().setSchemaResolver(new URIResolverImpl()); + for (Object ext : types.getExtensibilityElements()) { + if (ext instanceof Schema) { + Element element = ((Schema)ext).getElement(); + + // TODO: fix to make includes in imported + // schema work. The XmlSchema library was crashing + // because the base uri was not set. This doesn't + // affect imports. Need to check that this + // is the right approach for XSDs included by a + // XSD which is itself imported inline in a WSDL + XmlSchemaCollection schemaCollection = wsdlDefinition.getInlinedSchemas(); + schemaCollection.setBaseUri(((Schema)ext).getDocumentBaseURI()); + + wsdlDefinition.getInlinedSchemas().read(element, element.getBaseURI()); + } + } + } + + return wsdlDefinition; + + } catch (WSDLException e) { + throw new ContributionReadException(e); + } catch (IOException e) { + throw new ContributionReadException(e); + } + } + + public void resolve(WSDLDefinition model, ModelResolver resolver) throws ContributionResolveException { + } + + public String getArtifactType() { + return ".wsdl"; + } + + public Class<WSDLDefinition> getModelType() { + return WSDLDefinition.class; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLInterfaceProcessor.java b/sca-java-1.x/branches/sca-java-0.91/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLInterfaceProcessor.java new file mode 100644 index 0000000000..9f5c83cda6 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLInterfaceProcessor.java @@ -0,0 +1,195 @@ +/* + * 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.interfacedef.wsdl.xml; + +import static javax.xml.stream.XMLStreamConstants.END_ELEMENT; + +import javax.wsdl.PortType; +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.xml.Constants; +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.InvalidInterfaceException; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterfaceContract; +import org.apache.tuscany.sca.interfacedef.wsdl.introspect.WSDLInterfaceIntrospector; + +public class WSDLInterfaceProcessor implements StAXArtifactProcessor<WSDLInterfaceContract>, WSDLConstants { + + private WSDLFactory wsdlFactory; + private WSDLInterfaceIntrospector wsdlIntrospector; + + public WSDLInterfaceProcessor(WSDLFactory wsdlFactory, WSDLInterfaceIntrospector wsdlIntrospector) { + this.wsdlFactory = wsdlFactory; + this.wsdlIntrospector = wsdlIntrospector; + } + + /** + * Create a WSDL interface from a URI. + * @param uri + * @return + * @throws ContributionReadException + */ + private WSDLInterface createWSDLInterface(String uri) throws ContributionReadException { + WSDLInterface wsdlInterface = wsdlFactory.createWSDLInterface(); + wsdlInterface.setUnresolved(true); + + // Read a qname in the form: + // namespace#wsdl.interface(name) + int index = uri.indexOf('#'); + if (index == -1) { + throw new ContributionReadException("Invalid WSDL interface attribute: " + uri); + } + String namespace = uri.substring(0, index); + String name = uri.substring(index + 1); + name = name.substring("wsdl.interface(".length(), name.length() - 1); + wsdlInterface.setName(new QName(namespace, name)); + + return wsdlInterface; + } + + public WSDLInterfaceContract read(XMLStreamReader reader) throws ContributionReadException { + try { + + // Read an <interface.wsdl> + WSDLInterfaceContract wsdlInterfaceContract = wsdlFactory.createWSDLInterfaceContract(); + + // Read wsdlLocation + String location = reader.getAttributeValue(WSDLI_NS, WSDL_LOCATION); + wsdlInterfaceContract.setLocation(location); + + String uri = reader.getAttributeValue(null, INTERFACE); + if (uri != null) { + WSDLInterface wsdlInterface = createWSDLInterface(uri); + wsdlInterfaceContract.setInterface(wsdlInterface); + } + + uri = reader.getAttributeValue(null, CALLBACK_INTERFACE); + if (uri != null) { + WSDLInterface wsdlCallbackInterface = createWSDLInterface(uri); + wsdlInterfaceContract.setCallbackInterface(wsdlCallbackInterface); + } + + // Skip to end element + while (reader.hasNext()) { + if (reader.next() == END_ELEMENT && INTERFACE_WSDL_QNAME.equals(reader.getName())) { + break; + } + } + return wsdlInterfaceContract; + + } catch (XMLStreamException e) { + throw new ContributionReadException(e); + } + } + + public void write(WSDLInterfaceContract wsdlInterfaceContract, XMLStreamWriter writer) throws ContributionWriteException { + try { + // Write an <interface.wsdl> + writer.writeStartElement(Constants.SCA10_NS, INTERFACE_WSDL); + + // Write interface name + WSDLInterface wsdlInterface = (WSDLInterface)wsdlInterfaceContract.getInterface(); + if (wsdlInterface != null) { + QName qname = wsdlInterface.getName(); + String uri = qname.getNamespaceURI() + "#wsdl.interface(" + qname.getLocalPart() + ")"; + writer.writeAttribute(INTERFACE, uri); + } + + WSDLInterface wsdlCallbackInterface = (WSDLInterface)wsdlInterfaceContract.getCallbackInterface(); + if (wsdlCallbackInterface != null) { + QName qname = wsdlCallbackInterface.getName(); + String uri = qname.getNamespaceURI() + "#wsdl.interface(" + qname.getLocalPart() + ")"; + writer.writeAttribute(CALLBACK_INTERFACE, uri); + } + + // Write location + if (wsdlInterfaceContract.getLocation() != null) { + writer.writeAttribute(WSDLI_NS, WSDL_LOCATION, wsdlInterfaceContract.getLocation()); + } + + writer.writeEndElement(); + + } catch (XMLStreamException e) { + throw new ContributionWriteException(e); + } + } + + private WSDLInterface resolveWSDLInterface(WSDLInterface wsdlInterface, ModelResolver resolver) throws ContributionResolveException { + + if (wsdlInterface != null && wsdlInterface.isUnresolved()) { + + // Resolve the WSDL interface + wsdlInterface = resolver.resolveModel(WSDLInterface.class, wsdlInterface); + if (wsdlInterface.isUnresolved()) { + + // If the WSDL interface has never been resolved yet, do it now + // First, resolve the WSDL definition for the given namespace + WSDLDefinition wsdlDefinition = wsdlFactory.createWSDLDefinition(); + wsdlDefinition.setUnresolved(true); + wsdlDefinition.setNamespace(wsdlInterface.getName().getNamespaceURI()); + wsdlDefinition = resolver.resolveModel(WSDLDefinition.class, wsdlDefinition); + if (!wsdlDefinition.isUnresolved()) { + PortType portType = wsdlDefinition.getDefinition().getPortType(wsdlInterface.getName()); + if (portType != null) { + + // Introspect the WSDL portType and add the resulting + // WSDLInterface to the resolver + try { + wsdlInterface = wsdlIntrospector.introspect(portType, wsdlDefinition.getInlinedSchemas(), resolver); + } catch (InvalidInterfaceException e) { + throw new ContributionResolveException(e); + } + resolver.addModel(wsdlInterface); + wsdlInterface.setWsdlDefinition(wsdlDefinition); + } + } + } + } + return wsdlInterface; + } + + public void resolve(WSDLInterfaceContract wsdlInterfaceContract, ModelResolver resolver) throws ContributionResolveException { + + // Resolve the interface and callback interface + WSDLInterface wsdlInterface = resolveWSDLInterface((WSDLInterface)wsdlInterfaceContract.getInterface(), resolver); + wsdlInterfaceContract.setInterface(wsdlInterface); + + WSDLInterface wsdlCallbackInterface = resolveWSDLInterface((WSDLInterface)wsdlInterfaceContract.getCallbackInterface(), resolver); + wsdlInterfaceContract.setCallbackInterface(wsdlCallbackInterface); + } + + public QName getArtifactType() { + return WSDLConstants.INTERFACE_WSDL_QNAME; + } + + public Class<WSDLInterfaceContract> getModelType() { + return WSDLInterfaceContract.class; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.91/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XSDDocumentProcessor.java b/sca-java-1.x/branches/sca-java-0.91/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XSDDocumentProcessor.java new file mode 100644 index 0000000000..d80f24b74a --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.91/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/XSDDocumentProcessor.java @@ -0,0 +1,103 @@ +/* + * 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.interfacedef.wsdl.xml; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URI; +import java.net.URL; + +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; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory; +import org.apache.tuscany.sca.interfacedef.wsdl.XSDefinition; +import org.apache.ws.commons.schema.XmlSchema; +import org.apache.ws.commons.schema.XmlSchemaCollection; +import org.apache.ws.commons.schema.resolver.URIResolver; +import org.xml.sax.InputSource; + +/** + * An ArtifactProcessor for XSD documents. + * + * @version $Rev$ $Date$ + */ +public class XSDDocumentProcessor implements URLArtifactProcessor<XSDefinition> { + + private WSDLFactory factory; + + /** + * URI resolver implementation for xml schema + */ + private class URIResolverImpl implements URIResolver { + + public org.xml.sax.InputSource resolveEntity(java.lang.String targetNamespace, + java.lang.String schemaLocation, + java.lang.String baseUri) { + try { + URL url = new URL(new URL(baseUri), schemaLocation); + return new InputSource(url.openStream()); + } catch (IOException e) { + return null; + } + } + } + + public XSDDocumentProcessor(WSDLFactory factory) { + this.factory = factory; + } + + public XSDefinition read(URL contributionURL, URI artifactURI, URL artifactURL) throws ContributionReadException { + try { + + // Read an XSD document + InputStream is = artifactURL.openStream(); + try { + + XmlSchemaCollection collection = new XmlSchemaCollection(); + collection.setSchemaResolver(new URIResolverImpl()); + XmlSchema schema = collection.read(new InputStreamReader(is), null); + + XSDefinition xsDefinition = factory.createXSDefinition(); + xsDefinition.setSchema(schema); + + return xsDefinition; + } finally { + is.close(); + } + + } catch (IOException e) { + throw new ContributionReadException(e); + } + } + + public void resolve(XSDefinition model, ModelResolver resolver) throws ContributionResolveException { + } + + public String getArtifactType() { + return ".xsd"; + } + + public Class<XSDefinition> getModelType() { + return XSDefinition.class; + } +} |