You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by ra...@apache.org on 2009/08/10 14:13:17 UTC

svn commit: r802758 - in /tuscany/branches/sca-java-1.5.1: itest/wsdl/src/main/resources/imports/wsdl/ itest/wsdl/src/main/resources/imports/wsdl/META-INF/ itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/ modules/interface-wsdl-xml/src/main/ja...

Author: ramkumar
Date: Mon Aug 10 12:13:16 2009
New Revision: 802758

URL: http://svn.apache.org/viewvc?rev=802758&view=rev
Log:
Fixes for TUSCANY-2906 - to resolve wsdl:import and xsd:import using namespace

Added:
    tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/
    tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/
    tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld-import.wsdl
    tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld.xsd   (with props)
Modified:
    tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworld.wsdl
    tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworldws.composite
    tuscany/branches/sca-java-1.5.1/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLModelResolver.java
    tuscany/branches/sca-java-1.5.1/modules/xsd-xml/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDModelResolver.java

Added: tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld-import.wsdl
URL: http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld-import.wsdl?rev=802758&view=auto
==============================================================================
--- tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld-import.wsdl (added)
+++ tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld-import.wsdl Mon Aug 10 12:13:16 2009
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+-->
+
+<wsdl:definitions targetNamespace="http://helloworld3" 
+                  xmlns:tns="http://helloworld3" 
+                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
+                  xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" 
+                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+	          	  name="helloworld-import">
+
+    <wsdl:types>
+        <schema xmlns="http://www.w3.org/2001/XMLSchema" 
+                xmlns:tns2="http://helloworld2" 
+                targetNamespace="http://helloworld3" 
+                elementFormDefault="qualified">
+                
+  	        <xsd:import namespace="http://helloworld2" schemaLocation="../../helloworld.xsd" />            
+  	        
+  	       	<element name="getGreetings" type="tns2:getGreetingsType"/>
+		    <element name="getGreetingsResponse" type="tns2:getGreetingsResponseType"/>
+
+        </schema>
+    </wsdl:types>
+
+    <wsdl:message name="getGreetingsRequest">
+        <wsdl:part element="tns:getGreetings" name="parameters"/>
+    </wsdl:message>
+
+    <wsdl:message name="getGreetingsResponse">
+        <wsdl:part element="tns:getGreetingsResponse" name="parameters"/>
+    </wsdl:message>
+
+    <wsdl:portType name="HelloWorld">
+        <wsdl:operation name="getGreetings">
+            <wsdl:input message="tns:getGreetingsRequest" name="getGreetingsRequest"/>
+            <wsdl:output message="tns:getGreetingsResponse" name="getGreetingsResponse"/>
+        </wsdl:operation>
+    </wsdl:portType>
+
+</wsdl:definitions>

Added: tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld.xsd
URL: http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld.xsd?rev=802758&view=auto
==============================================================================
--- tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld.xsd (added)
+++ tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld.xsd Mon Aug 10 12:13:16 2009
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   Licensed to the Apache Software Foundation (ASF) under one
+   or more contributor license agreements.  See the NOTICE file
+   distributed with this work for additional information
+   regarding copyright ownership.  The ASF licenses this file
+   to you under the Apache License, Version 2.0 (the
+   "License"); you may not use this file except in compliance
+   with the License.  You may obtain a copy of the License at
+   
+     http://www.apache.org/licenses/LICENSE-2.0
+     
+   Unless required by applicable law or agreed to in writing,
+   software distributed under the License is distributed on an
+   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+   KIND, either express or implied.  See the License for the
+   specific language governing permissions and limitations
+   under the License.
+-->
+<schema targetNamespace="http://helloworld2" 
+        xmlns="http://www.w3.org/2001/XMLSchema" 
+	    xmlns:ns="http://helloworld2">
+	
+	<complexType name="getGreetingsType">
+		<sequence>
+			<element name="name" type="string"/>
+		</sequence>
+	</complexType>
+
+	<complexType name="getGreetingsResponseType">
+		<sequence>
+			<element name="getGreetingsReturn" type="string"/>
+		</sequence>
+	</complexType>
+</schema>
\ No newline at end of file

Propchange: tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld.xsd
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/META-INF/wsdl/helloworld.xsd
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworld.wsdl
URL: http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworld.wsdl?rev=802758&r1=802757&r2=802758&view=diff
==============================================================================
--- tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworld.wsdl (original)
+++ tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworld.wsdl Mon Aug 10 12:13:16 2009
@@ -26,7 +26,7 @@
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 	          name="helloworld">
 	          
-    <wsdl:import namespace="http://helloworld3" location="../../META-INF/wsdl/helloworld-import.wsdl" />
+    <wsdl:import namespace="http://helloworld3" location="../../helloworld-import.wsdl" />
     
     <wsdl:binding name="HelloWorldSoapBinding" type="tns3:HelloWorld">
         <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

Modified: tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworldws.composite
URL: http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworldws.composite?rev=802758&r1=802757&r2=802758&view=diff
==============================================================================
--- tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworldws.composite (original)
+++ tuscany/branches/sca-java-1.5.1/itest/wsdl/src/main/resources/imports/wsdl/helloworldws.composite Mon Aug 10 12:13:16 2009
@@ -24,7 +24,7 @@
 
     <component name="HelloWorldServiceComponent">
 	    <service name="HelloWorldService">
-	        <interface.wsdl interface="http://helloworld3#wsdl.interface(HelloWorld)"  />
+	        <interface.java interface="helloworld.HelloWorldService"/>
 	        <binding.ws wsdlElement="http://helloworld#wsdl.port(HelloWorldService/HelloWorldSoapPort)" />
 	    </service>
         <implementation.java class="helloworld.HelloWorldImpl" />

Modified: tuscany/branches/sca-java-1.5.1/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLModelResolver.java
URL: http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.5.1/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLModelResolver.java?rev=802758&r1=802757&r2=802758&view=diff
==============================================================================
--- tuscany/branches/sca-java-1.5.1/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLModelResolver.java (original)
+++ tuscany/branches/sca-java-1.5.1/modules/interface-wsdl-xml/src/main/java/org/apache/tuscany/sca/interfacedef/wsdl/xml/WSDLModelResolver.java Mon Aug 10 12:13:16 2009
@@ -46,6 +46,10 @@
 import javax.wsdl.xml.WSDLLocator;
 import javax.wsdl.xml.WSDLReader;
 import javax.xml.namespace.QName;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
 
 import org.apache.tuscany.sca.contribution.Artifact;
 import org.apache.tuscany.sca.contribution.Contribution;
@@ -58,6 +62,7 @@
 import org.apache.tuscany.sca.contribution.service.ContributionReadException;
 import org.apache.tuscany.sca.contribution.service.ContributionRuntimeException;
 import org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition;
+import org.apache.tuscany.sca.interfacedef.wsdl.impl.WSDLDefinitionImpl;
 import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory;
 import org.apache.tuscany.sca.xsd.XSDFactory;
 import org.apache.tuscany.sca.xsd.XSDefinition;
@@ -77,6 +82,7 @@
 public class WSDLModelResolver implements ModelResolver {
     //Schema element names
     public static final String ELEM_SCHEMA = "schema";
+    public static final QName WSDL11_IMPORT = new QName("http://schemas.xmlsoap.org/wsdl/", "import");
 
     //Schema URI
     public static final String NS_URI_XSD_1999 = "http://www.w3.org/1999/XMLSchema";
@@ -150,10 +156,12 @@
         private InputStream inputStream;
         private URL base;
         private String latestImportURI;
+        private Map<String, String> wsdlImports;
 
-        public WSDLLocatorImpl(URL base, InputStream is) {
+        public WSDLLocatorImpl(URL base, InputStream is, Map<String, String> imports) {
             this.base = base;
             this.inputStream = is;
+            this.wsdlImports = imports;
         }
 
         public void close() {
@@ -177,13 +185,14 @@
         }
 
         public InputSource getImportInputSource(String parentLocation, String importLocation) {
-            try {
+            try {           	
+            	
                 if (importLocation == null)
                     throw new IllegalArgumentException("Required attribute 'location' is missing.");
                 
                 if (importLocation.trim().equals(""))
                 	throw new IllegalArgumentException("Required attribute 'location' is empty.");
-
+                
                 URL url = null;
                 if (importLocation.startsWith("/")) {
                     // The URI is relative to the contribution
@@ -208,7 +217,26 @@
                 latestImportURI = url.toString();
                 return XMLDocumentHelper.getInputSource(url);
             } catch (IOException e) {            	
-                throw new ContributionRuntimeException(e);
+                // If we are not able to resolve the imports using location, then 
+            	// try resolving them using the namespace.
+            	try {
+	            	if (! wsdlImports.isEmpty()) {
+	                	for (Artifact artifact : contribution.getArtifacts()) {
+	            			if (artifact.getModel() instanceof WSDLDefinitionImpl) {
+	            				String namespace = ((WSDLDefinitionImpl)artifact.getModel()).getNamespace();
+	            				for (Map.Entry<String, String> entry : ((Map<String, String>)wsdlImports).entrySet()) {
+		                            if (entry.getKey().equals(namespace)) {
+		                            	URL url = ((WSDLDefinitionImpl)artifact.getModel()).getLocation().toURL();	            					
+			                            return XMLDocumentHelper.getInputSource(url);
+		                            }
+	            				}
+	            			}
+	            	    }
+	                }   
+            	} catch (IOException ex) {
+            		throw new ContributionRuntimeException(ex);
+            	}            	
+            	throw new ContributionRuntimeException(e);
             }
         }
 
@@ -332,7 +360,7 @@
     
     // Use non-sca mechanism to resolve the import location, if not 
     // found then use the sca mechanism
-    private <T> T resolveImports (Class<T> modelClass, WSDLDefinition unresolved) {
+    private <T> T resolveImports (Class<T> modelClass, WSDLDefinition unresolved) throws ContributionReadException {
     	
     	WSDLDefinition resolved = null;
     	if (unresolved.getDefinition() == null && unresolved.getLocation() != null) {            
@@ -350,11 +378,20 @@
             		return modelClass.cast(resolved);
             	}
             } catch (ContributionReadException e) {
-            	// Load the definition using sca mechanism.
-            	resolved = resolveModel(WSDLDefinition.class, unresolved);
-            	if (resolved != null && !resolved.isUnresolved()) {
-                    return modelClass.cast(resolved);
-                }
+            	// Resolve the wsdl definition using the namespace, by searching the
+            	// contribution artifacts for wsdl definition for the given namespace.
+            	for (Artifact artifact : contribution.getArtifacts()) {
+        			if (artifact.getModel() instanceof WSDLDefinitionImpl) {
+        				String namespace = ((WSDLDefinitionImpl)artifact.getModel()).getNamespace();
+        				if (unresolved.getNamespace().equals(namespace)) {        					
+        					WSDLDefinition wsdlDefinition = (WSDLDefinition)artifact.getModel();
+        					if (wsdlDefinition.getDefinition() == null) {
+        						loadDefinition(wsdlDefinition);
+        					}
+                            return modelClass.cast(wsdlDefinition);
+        				}
+        			}
+        	    }
             }
         }
         
@@ -381,11 +418,14 @@
             // FIXME: We need to decide if we should disable the import processing by WSDL4J
             // reader.setFeature("javax.wsdl.importDocuments", false);
             reader.setExtensionRegistry(wsdlExtensionRegistry);  // use a custom registry
-
-            WSDLLocatorImpl locator = new WSDLLocatorImpl(artifactURL, is);
-            Definition definition = reader.readWSDL(locator);
+            
+            // Collection of namespace,location for wsdl:import definition
+            Map<String, String> wsdlImports = indexRead(wsdlDef.getLocation().toURL());
+            
+            WSDLLocatorImpl locator = new WSDLLocatorImpl(artifactURL, is, wsdlImports);
+            Definition definition = reader.readWSDL(locator);            
             wsdlDef.setDefinition(definition);
-
+          
             // If this definition imports any definitions from other namespaces,
             // set the correct WSDLDefinition import relationships.
             for (Map.Entry<String, List<javax.wsdl.Import>> entry :
@@ -396,37 +436,43 @@
                     wsdlDefinition.setNamespace(entry.getKey());
                     WSDLDefinition resolved = null;
                     for (javax.wsdl.Import imp : entry.getValue()) {
-                    	if (imp.getDefinition() == null)
-                            throw new IllegalArgumentException("Required attribute 'location' is missing.");
-                        
+                    	if (imp.getDefinition() == null) {
+                            throw new IllegalArgumentException("Required attribute 'location' is missing.");                    		
+                    	}
                     	try {
-                    		wsdlDefinition.setLocation(new URI(imp.getDefinition().getDocumentBaseURI()));                    		
+                    		wsdlDefinition.setLocation(new URI(imp.getDefinition().getDocumentBaseURI()));
                     		resolved = resolveImports(WSDLDefinition.class, wsdlDefinition);
                     		if (!resolved.isUnresolved()) {
-                    			if (resolved.getDefinition().getDocumentBaseURI().equals(imp.getDefinition().getDocumentBaseURI())) {
-                                    // this WSDLDefinition contains the imported document
-                                    wsdlDef.getImportedDefinitions().add(resolved);
-                                } else {
-                                    // this is a facade, so look in its imported definitions
-                                    for (WSDLDefinition def : resolved.getImportedDefinitions()) {
-                                        if (def.getDefinition().getDocumentBaseURI().equals(imp.getDefinition().getDocumentBaseURI())) {
+                    			if (resolved.getImportedDefinitions().isEmpty()) {
+                    				if (resolved.getDefinition().getTargetNamespace().equals(imp.getDefinition().getTargetNamespace())) {
+	                    				// this WSDLDefinition contains the imported document
+	                                    wsdlDef.getImportedDefinitions().add(resolved);
+	                                    imp.setLocationURI(resolved.getURI().toString());
+                    				}
+                    			} else {
+                    				// this is a facade, so look in its imported definitions
+                    				for (WSDLDefinition def : resolved.getImportedDefinitions()) {
+                                        if (def.getDefinition().getTargetNamespace().equals(imp.getDefinition().getTargetNamespace())) {
                                             wsdlDef.getImportedDefinitions().add(def);
+                                            imp.setLocationURI(def.getURI().toString());
                                             break;
                                         }
                                     }
-                                }
+                    			}
                     		}
                     	} catch (Exception e) {
                     		throw new ContributionReadException(e);
                     	}
                     }
                 }
-            }
-
+            }            
+            
             //Read inline schemas 
             readInlineSchemas(wsdlDef, definition);
         } catch (WSDLException e) {
             throw new ContributionReadException(e);
+        } catch (XMLStreamException e) {
+            throw new ContributionReadException(e);
         } catch (IOException e) {
             throw new ContributionReadException(e);
         }
@@ -513,5 +559,42 @@
             }
         }
     }
-
+    
+    
+    /**
+     * Read the namespace and location for the WSDL imports
+     * 
+     * @param doc
+     * @return
+     * @throws IOException
+     * @throws XMLStreamException
+     */
+    protected Map<String, String> indexRead(URL doc) throws IOException, XMLStreamException {
+        
+    	Map<String, String> wsdlImports = new HashMap<String, String>();
+    	InputStream is = doc.openStream();
+        try {
+        	XMLInputFactory inputFactory = XMLInputFactory.newInstance();
+            XMLStreamReader reader = inputFactory.createXMLStreamReader(is);
+            int eventType = reader.getEventType();
+            int index = 0;
+            while (true) {
+                if (eventType == XMLStreamConstants.START_ELEMENT) {
+                    if (WSDL11_IMPORT.equals(reader.getName())) {
+                        String ns = reader.getAttributeValue(null, "namespace");
+                        String loc = reader.getAttributeValue(null, "location");
+                        wsdlImports.put(ns, loc);                        
+                    }
+                }
+                if (reader.hasNext()) {
+                    eventType = reader.next();
+                } else {
+                    break;
+                }
+            }
+            return wsdlImports;
+        } finally {
+            is.close();
+        }
+    }
 }

Modified: tuscany/branches/sca-java-1.5.1/modules/xsd-xml/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDModelResolver.java
URL: http://svn.apache.org/viewvc/tuscany/branches/sca-java-1.5.1/modules/xsd-xml/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDModelResolver.java?rev=802758&r1=802757&r2=802758&view=diff
==============================================================================
--- tuscany/branches/sca-java-1.5.1/modules/xsd-xml/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDModelResolver.java (original)
+++ tuscany/branches/sca-java-1.5.1/modules/xsd-xml/src/main/java/org/apache/tuscany/sca/xsd/xml/XSDModelResolver.java Mon Aug 10 12:13:16 2009
@@ -37,6 +37,7 @@
 import org.apache.tuscany.sca.xsd.DefaultXSDFactory;
 import org.apache.tuscany.sca.xsd.XSDFactory;
 import org.apache.tuscany.sca.xsd.XSDefinition;
+import org.apache.tuscany.sca.xsd.impl.XSDefinitionImpl;
 import org.apache.ws.commons.schema.XmlSchema;
 import org.apache.ws.commons.schema.XmlSchemaCollection;
 import org.apache.ws.commons.schema.XmlSchemaInclude;
@@ -314,7 +315,24 @@
                     url = new URL(new URL(baseUri), schemaLocation);
                 }
                 return XMLDocumentHelper.getInputSource(url);
-            } catch (IOException e) {
+            } catch (IOException e) {               
+            	// If we are not able to resolve the imports using location, then 
+            	// try resolving them using the namespace.
+            	try {
+	                for (Artifact artifact : contribution.getArtifacts()) {
+	        			if (artifact.getModel() instanceof XSDefinitionImpl) {
+	        				String artifactNamespace = ((XSDefinitionImpl)artifact.getModel()).getNamespace();
+	        				if (targetNamespace.equals(artifactNamespace)) {
+	        					URL artifactLocation = ((XSDefinitionImpl)artifact.getModel()).getLocation().toURL();            					
+	        					return XMLDocumentHelper.getInputSource(artifactLocation);
+	        				}
+	        			}
+	        	    }
+                } catch (IOException ex) {
+                	// Invalid URI; return a default InputSource so that the
+                    // XmlSchema code will produce a useful diagnostic
+                    return new InputSource(schemaLocation);
+                }
                 // Invalid URI; return a default InputSource so that the
                 // XmlSchema code will produce a useful diagnostic
                 return new InputSource(schemaLocation);