owenb 2002/06/26 07:08:53 Modified: java/test/wsdl ClassLoaderTest.java java/src/org/apache/wsif/util WSIFUtils.java Added: java/src/org/apache/wsif/wsdl WSIFWSDLLocatorImpl.java Log: Removed read methods which take ClassLoaders as arguments and replaced with support for the javax.wsdl.xml.WSDLLocator interface. Added WSIF implementation of javax.wsdl.xml.WSDLLocator which allows wsdl files and imports to be found using ClassLoaders. Revision Changes Path 1.2 +90 -38 xml-axis-wsif/java/test/wsdl/ClassLoaderTest.java Index: ClassLoaderTest.java =================================================================== RCS file: /home/cvs/xml-axis-wsif/java/test/wsdl/ClassLoaderTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ClassLoaderTest.java 6 Jun 2002 08:29:01 -0000 1.1 +++ ClassLoaderTest.java 26 Jun 2002 14:08:52 -0000 1.2 @@ -69,12 +69,19 @@ * @author Owen Burroughs */ +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.net.URL; + +import javax.wsdl.Definition; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; -import junit.textui.TestRunner; + import org.apache.wsif.WSIFService; import org.apache.wsif.WSIFServiceFactory; +import org.apache.wsif.util.WSIFUtils; import util.TestUtilities; public class ClassLoaderTest extends TestCase { @@ -82,55 +89,100 @@ String wsdlLocation2 = "test/ImportingTest2.wsdl"; String wsdlLocation3 = "test/ImportingTest3.wsdl"; String wsdlLocation4 = "ImportingTest4.wsdl"; - String wsdlLocation5 = TestUtilities.getWsdlPath( - "java\\test\\addressbook")+"ImportingAddressBook.wsdl"; + String wsdlLocation5 = + TestUtilities.getWsdlPath("java\\test\\addressbook") + + "ImportingAddressBook.wsdl"; + + public ClassLoaderTest(String name) { + super(name); + } + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static Test suite() { + return new TestSuite(ClassLoaderTest.class); + } + + public void setUp() { + TestUtilities.setUpExtensionsAndProviders(); + } - public ClassLoaderTest(String name) { super(name); } - - public static void main(String[] args) - { junit.textui.TestRunner.run (suite()); } - - public static Test suite() - { return new TestSuite(ClassLoaderTest.class); } - - public void setUp() { TestUtilities.setUpExtensionsAndProviders(); } - // wsdl location = test/ImportingTest.wsdl // import location = AddressBookTest.wsdl - public void test() { doit(wsdlLocation); } + public void test() { + doit(wsdlLocation); + } // wsdl location = test/ImportingTest2.wsdl // import location = /test/subtest/subtest2/AddressBookTest2.wsdl - public void test2() { doit(wsdlLocation2); } - + public void test2() { + doit(wsdlLocation2); + } + // wsdl location = test/ImportingTest3.wsdl // import location = subtest/subtest2/AddressBookTest3.wsdl - public void test3() { doit(wsdlLocation3); } + public void test3() { + doit(wsdlLocation3); + } // wsdl location = ImportingTest4.wsdl // import location = test/subtest/subtest2/AddressBookTest4.wsdl - public void test4() { doit(wsdlLocation4); } + public void test4() { + doit(wsdlLocation4); + } // wsdl location = <wsdlPath>\testcases\com\ibm\wsif\test\ImportingTest.wsdl // import location = http://localhost:8080/wsdl/AddressBook.wsdl - public void test5() { doit(wsdlLocation5); } - - private void doit (String wsdlLoc) - { - try { - WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); - WSIFService service = factory.getService( - wsdlLoc, - this.getClass().getClassLoader(), - null, // serviceNS - null, // serviceName - "http://wsifservice.addressbook/", // portTypeNS - "AddressBook"); // portTypeName - } catch (Exception e) { - System.err.println("ClassLoaderTest(" + wsdlLoc + - ") caught exception " + e); - e.printStackTrace(); - assertTrue(false); - } - } + public void test5() { + if (TestUtilities.areWeTesting("remotewsdl")) { + doit(wsdlLocation5); + } + } + + public void test6() { + doItWithReader(wsdlLocation2); + } + + private void doit(String wsdlLoc) { + try { + WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); + WSIFService service = + factory + .getService(wsdlLoc, this.getClass().getClassLoader(), null, + // serviceNS + null, // serviceName + "http://wsifservice.addressbook/", // portTypeNS + "AddressBook"); // portTypeName + } catch (Exception e) { + System.err.println( + "ClassLoaderTest(" + wsdlLoc + ") caught exception " + e); + e.printStackTrace(); + assertTrue(false); + } + } + + private void doItWithReader(String wsdlLoc) { + InputStream in = null; + ClassLoader loader = this.getClass().getClassLoader(); + try { + URL url = null; + if (wsdlLoc.indexOf(":") == -1) + url = new URL("file", null, wsdlLocation); + else + url = new URL(wsdlLocation); + String wsdlRelativeLocation = url.getPath(); + if (wsdlRelativeLocation.startsWith("/")) + wsdlRelativeLocation = wsdlRelativeLocation.substring(1); + in = loader.getResourceAsStream(wsdlRelativeLocation); + Reader reader = new InputStreamReader(in); + Definition def = WSIFUtils.readWSDL(url, reader, loader); + assertNotNull("Definition is null!!", def); + } catch (Exception e) { + System.err.println( + "ClassLoaderTest(" + wsdlLoc + ") caught exception " + e); + assertTrue(false); + } + } } 1.7 +23 -7 xml-axis-wsif/java/src/org/apache/wsif/util/WSIFUtils.java Index: WSIFUtils.java =================================================================== RCS file: /home/cvs/xml-axis-wsif/java/src/org/apache/wsif/util/WSIFUtils.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- WSIFUtils.java 24 Jun 2002 13:11:20 -0000 1.6 +++ WSIFUtils.java 26 Jun 2002 14:08:52 -0000 1.7 @@ -97,6 +97,7 @@ import org.apache.wsif.format.WSIFFormatHandler; import org.apache.wsif.logging.MessageLogger; import org.apache.wsif.logging.Tr; +import org.apache.wsif.wsdl.WSIFWSDLLocatorImpl; import org.apache.wsif.wsdl.extensions.format.TypeMapping; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -475,11 +476,26 @@ * resources. */ public static Definition readWSDL( - URL contextURL, + URL documentBase, + Reader reader, + ClassLoader cl) + throws WSDLException { + String base = (documentBase == null) ? null : documentBase.toString(); + return readWSDL(base, reader, cl); + } + + /** + * Read WSDL - it is different from standard readWSDL method as it is + * using extensibility elements that were registered for dynamic port + * factory. It also uses the accompanying class loader to load imported WSDL + * resources. + */ + public static Definition readWSDL( + String documentBase, Reader reader, ClassLoader cl) throws WSDLException { - Tr.entry(null, contextURL, reader, cl); + Tr.entry(null, documentBase, reader, cl); initializeProviders(); @@ -495,9 +511,8 @@ WSDLReader wsdlReader = factory.newWSDLReader(); wsdlReader.setFeature(Constants.FEATURE_VERBOSE, false); try { - String url = (contextURL == null) ? null : contextURL.toString(); - Definition def = - wsdlReader.readWSDL(url, new InputSource(reader), cl); + WSIFWSDLLocatorImpl lo = new WSIFWSDLLocatorImpl(documentBase, reader, cl); + Definition def = wsdlReader.readWSDL(lo); if (oldPropValue != null) { props.setProperty( @@ -517,7 +532,7 @@ messageLog.message( WSIFConstants.TYPE_ERROR, "WSIF.0002E", - new Object[] { contextURL }); + new Object[] { documentBase }); messageLog.destroy(); // End message throw e; @@ -553,7 +568,8 @@ try { String url = (contextURL == null) ? null : contextURL.toString(); - Definition def = wsdlReader.readWSDL(url, wsdlLoc, cl); + WSIFWSDLLocatorImpl lo = new WSIFWSDLLocatorImpl(url, wsdlLoc, cl); + Definition def = wsdlReader.readWSDL(lo); if (oldPropValue != null) { props.setProperty( 1.1 xml-axis-wsif/java/src/org/apache/wsif/wsdl/WSIFWSDLLocatorImpl.java Index: WSIFWSDLLocatorImpl.java =================================================================== /* * The Apache Software License, Version 1.1 * * * Copyright (c) 2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "WSIF" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 2001, 2002, International * Business Machines, Inc., http://www.apache.org. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */ package org.apache.wsif.wsdl; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import com.ibm.wsdl.util.StringUtils; public class WSIFWSDLLocatorImpl implements javax.wsdl.xml.WSDLLocator { Reader baseReader = null; Reader importReader = null; String contextURI = null; String wsdlLocation = null; String documentBase = null; String importBase = null; ClassLoader loader = null; public WSIFWSDLLocatorImpl(String ctxt, String wsdlURI, ClassLoader cl) { contextURI = ctxt; wsdlLocation = wsdlURI; loader = cl; } public WSIFWSDLLocatorImpl(String docBase, Reader reader, ClassLoader cl) { documentBase = docBase; baseReader = reader; loader = cl; } public Reader getBaseReader() { if (baseReader == null) { try { URL url = null; URL contextURL = (contextURI != null) ? StringUtils.getURL(null, contextURI) : null; if (loader != null) { InputStream in = null; try { if (contextURL != null) url = new URL(contextURL, wsdlLocation); else { if (wsdlLocation.indexOf(":") == -1) url = new URL("file", null, wsdlLocation); else url = new URL(wsdlLocation); } String wsdlRelativeLocation = url.getPath(); if (wsdlRelativeLocation.startsWith("/")) wsdlRelativeLocation = wsdlRelativeLocation.substring(1); in = loader.getResourceAsStream(wsdlRelativeLocation); baseReader = new InputStreamReader(in); } catch (Exception exc) { } } if (baseReader == null) { url = StringUtils.getURL(contextURL, wsdlLocation); baseReader = StringUtils.getContentAsReader(url); } if (url != null) documentBase = url.toString(); } catch (Exception e) { } } return baseReader; } public Reader getImportReader(String base, String relativeLocation) { try { // If a ClassLoader was used to load the base document, chances // are we need to use it to find the import. URL url = null; if (loader != null) { if (relativeLocation.startsWith("/") || relativeLocation.startsWith("\\")) { // Relative location has been specified from a root dir. However, // using a ClassLoader, root dirs don't mean anything. relativeLocation = relativeLocation.substring(1, relativeLocation.length()); InputStream in = loader.getResourceAsStream(relativeLocation); importReader = new InputStreamReader(in); } else if (relativeLocation.indexOf("://") != -1) { // This is a fully specified URL of some kind so don't use the // ClassLoader to find the import. url = StringUtils.getURL(null, relativeLocation); importReader = StringUtils.getContentAsReader(url); } else { // Import location has been specified relative to the base document // and so we can to try to form the complete path to it. if (base != null && base.startsWith("file:")) { int i = base.lastIndexOf("/"); if (i == -1) i = base.lastIndexOf("\\"); if (i != -1) { String path = base.substring(0, i + 1); url = new URL(null, path + relativeLocation); } else { url = new URL(null, "file:" + relativeLocation); } InputStream in = loader.getResourceAsStream(url.getPath()); importReader = new InputStreamReader(in); } } } else { URL contextURL = (base != null) ? StringUtils.getURL(null, base) : null; url = StringUtils.getURL(contextURL, relativeLocation); importReader = StringUtils.getContentAsReader(url); } importBase = (url == null) ? relativeLocation : url.toString(); } catch (Exception e) { } return importReader; } public String getBaseURI() { return documentBase; } public String getLatestImportURI() { return importBase; } }