KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > wsdl > WSIFWSDLLocatorImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57  
58 package org.apache.wsif.wsdl;
59  
60 import java.io.IOException JavaDoc;
61 import java.io.InputStream JavaDoc;
62 import java.io.InputStreamReader JavaDoc;
63 import java.io.Reader JavaDoc;
64 import java.net.URL JavaDoc;
65
66 import org.apache.wsif.logging.Trc;
67
68 import com.ibm.wsdl.util.StringUtils;
69
70 /**
71  * Implementation of javax.wsdl.xml.WSDLLocator. This class can be used to
72  * locate a wsdl document and its imports using a ClassLoader. This is useful
73  * when the wsdl is located in a jar/zip file.
74  *
75  * @author Owen Burroughs <owenb@apache.org>
76  */

77 public class WSIFWSDLLocatorImpl implements javax.wsdl.xml.WSDLLocator {
78  
79     Reader JavaDoc baseReader = null;
80     Reader JavaDoc importReader = null;
81     String JavaDoc contextURI = null;
82     String JavaDoc wsdlLocation = null;
83     String JavaDoc documentBase = null;
84     String JavaDoc importBase = null;
85     ClassLoader JavaDoc loader = null;
86
87     /**
88      * Create an instance of WSIFWSDLLocatorImpl.
89      * @param ctxt The context uri for the wsdl location
90      * @param wsdlURI The uri for the base wsdl document
91      * @param cl A ClassLoader to use in locating the base wsdl document and imports
92      */

93     public WSIFWSDLLocatorImpl(String JavaDoc ctxt, String JavaDoc wsdlURI, ClassLoader JavaDoc cl) {
94         Trc.entry(this,ctxt,wsdlURI,cl);
95         contextURI = ctxt;
96         wsdlLocation = wsdlURI;
97         loader = cl;
98         Trc.exit();
99     }
100
101     /**
102      * Create an instance of WSIFWSDLLocatorImpl.
103      * @param docBase The uri for the base wsdl document
104      * @param reader A reader "directed at" the base wsdl document
105      * @param cl A ClassLoader to use in locating the base wsdl document and imports
106      */

107     public WSIFWSDLLocatorImpl(String JavaDoc docBase, Reader JavaDoc reader, ClassLoader JavaDoc cl) {
108         Trc.entry(this,docBase,cl);
109         documentBase = docBase;
110         baseReader = reader;
111         loader = cl;
112         Trc.exit();
113     }
114
115     /**
116      * Get a reader for the base wsdl document. Returns null if the document
117      * cannot be located.
118      * @return The reader or null if the import cannot be resolved
119      */

120     public Reader JavaDoc getBaseReader() {
121         Trc.entry(this);
122         if (baseReader == null) {
123             try {
124                 URL JavaDoc url = null;
125                 URL JavaDoc contextURL =
126                     (contextURI != null) ? StringUtils.getURL(null, contextURI) : null;
127                 if (loader != null) {
128                     InputStream JavaDoc in = null;
129                     try {
130                         if (contextURL != null)
131                             url = new URL JavaDoc(contextURL, wsdlLocation);
132                         else {
133                             if (wsdlLocation.indexOf(":") == -1)
134                                 url = new URL JavaDoc("file", null, wsdlLocation);
135                             else
136                                 url = new URL JavaDoc(wsdlLocation);
137                         }
138                         String JavaDoc wsdlRelativeLocation = url.getPath();
139                         if (wsdlRelativeLocation.startsWith("/"))
140                             wsdlRelativeLocation = wsdlRelativeLocation.substring(1);
141                         in = loader.getResourceAsStream(wsdlRelativeLocation);
142                         baseReader = new InputStreamReader JavaDoc(in);
143                     } catch (Exception JavaDoc exc) {
144                         Trc.ignoredException(exc);
145                     }
146                 }
147                 if (baseReader == null) {
148                     url = StringUtils.getURL(contextURL, wsdlLocation);
149                     baseReader = StringUtils.getContentAsReader(url);
150                 }
151                 if (url != null)
152                     documentBase = url.toString();
153             } catch (Exception JavaDoc e) {
154                 Trc.exception(e);
155                 documentBase = wsdlLocation;
156             }
157         }
158         Trc.exit();
159         return baseReader;
160     }
161
162     /**
163      * Get a reader for an imported wsdl document. Returns null if the import document
164      * cannot be located.
165      * @param base The document base uri for the parent wsdl document
166      * @param relativeLocation The relative uri of the import wsdl document
167      * @return The reader or null if the import cannot be resolved
168      */

169     public Reader JavaDoc getImportReader(String JavaDoc base, String JavaDoc relativeLocation) {
170         Trc.entry(this,base,relativeLocation);
171         
172         // Reset importReader if finding import within import
173
importReader = null;
174         boolean triedSU = false;
175         try {
176             // If a ClassLoader was used to load the base document, chances
177
// are we need to use it to find the import.
178
URL JavaDoc url = null;
179             if (loader != null) {
180                 if (relativeLocation.startsWith("/") || relativeLocation.startsWith("\\")) {
181                     // Relative location has been specified from a root dir. However,
182
// using a ClassLoader, root dirs don't mean anything.
183
relativeLocation = relativeLocation.substring(1, relativeLocation.length());
184                     InputStream JavaDoc in = loader.getResourceAsStream(relativeLocation);
185                     importReader = new InputStreamReader JavaDoc(in);
186                 } else if (relativeLocation.indexOf("://") != -1) {
187                     // This is a fully specified URL of some kind so don't use the
188
// ClassLoader to find the import.
189
triedSU = true;
190                     url = StringUtils.getURL(null, relativeLocation);
191                     importReader = StringUtils.getContentAsReader(url);
192                 } else {
193                     // Import location has been specified relative to the base document
194
// and so we can to try to form the complete path to it.
195
if (base != null) {
196                         int i = base.lastIndexOf("/");
197                         if (i == -1)
198                             i = base.lastIndexOf("\\");
199                         if (i != -1) {
200                             String JavaDoc path = base.substring(0, i + 1);
201                             String JavaDoc resolvedPath = path + relativeLocation;
202                             if (relativeLocation.startsWith("..")) {
203                                 resolvedPath = resolvePath(path, relativeLocation);
204                             }
205                             if (resolvedPath == null) {
206                                 throw new Exception JavaDoc("Invalid Path");
207                             }
208                             
209                             // Make sure that resolved path starts with file:
210
if (resolvedPath.startsWith("file:")) {
211                                 url = new URL JavaDoc(null, resolvedPath);
212                             } else {
213                                 url = new URL JavaDoc(null, "file:" + resolvedPath);
214                             }
215                         } else {
216                             url = new URL JavaDoc(null, "file:" + relativeLocation);
217                         }
218                         InputStream JavaDoc in = loader.getResourceAsStream(url.getPath());
219                         importReader = new InputStreamReader JavaDoc(in);
220                     } else {
221                         url = new URL JavaDoc(null, "file:" + relativeLocation);
222                         InputStream JavaDoc in = loader.getResourceAsStream(url.getPath());
223                         importReader = new InputStreamReader JavaDoc(in);
224                     }
225                 }
226             } else {
227                 triedSU = true;
228                 URL JavaDoc contextURL = (base != null) ? StringUtils.getURL(null, base) : null;
229                 url = StringUtils.getURL(contextURL, relativeLocation);
230                 importReader = StringUtils.getContentAsReader(url);
231             }
232             importBase = (url == null) ? relativeLocation : url.toString();
233         } catch (Exception JavaDoc e) {
234             Trc.exception(e);
235             // If we have not tried using a non-ClassLoader route, try it now
236
// as a last resort.
237
if (!triedSU) {
238                 try {
239                     URL JavaDoc contextURL = (base != null) ? StringUtils.getURL(null, base) : null;
240                     URL JavaDoc url = StringUtils.getURL(contextURL, relativeLocation);
241                     importReader = StringUtils.getContentAsReader(url);
242                     importBase = (url == null) ? relativeLocation : url.toString();
243                 } catch (Exception JavaDoc e2) {
244                     Trc.exception(e2);
245                     // we can't find the import so set a temporary value for the import URI. This is
246
// necessary to avoid a NullPointerException in WSDLReaderImpl
247
importBase = "unknownImportURI";
248                 }
249             } else {
250                 // we can't find the import so set a temporary value for the import URI. This is
251
// necessary to avoid a NullPointerException in WSDLReaderImpl
252
importBase = "unknownImportURI";
253             }
254         }
255         Trc.exit();
256         return importReader;
257     }
258
259     /**
260      * Get the document base uri for the base wsdl document
261      * @return The document base uri
262      */

263     public String JavaDoc getBaseURI() {
264         Trc.entry(this);
265         Trc.exit(documentBase);
266         return documentBase;
267     }
268
269     /**
270      * Get the document base uri for the last import document to be resolved
271      * by this locator. This is useful if resolving imports within imports.
272      * @return The document base uri
273      */

274     public String JavaDoc getLatestImportURI() {
275         Trc.entry(this);
276         Trc.exit(importBase);
277         return importBase;
278     }
279
280     /**
281      * Resolve a path when the relative location begins with ..
282      */

283     private String JavaDoc resolvePath(String JavaDoc ba, String JavaDoc rel) {
284         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(rel);
285         int dd = 0;
286         while(sb.length() > 0) {
287             if(sb.length() > 3 && sb.charAt(0) == '.' && sb.charAt(1) == '.'
288             && (sb.charAt(2) == '/' || sb.charAt(2) == '\\')) {
289                 dd++;
290                 sb.delete(0,3);
291             } else {
292                 break;
293             }
294         }
295         StringBuffer JavaDoc sb2 = new StringBuffer JavaDoc(ba);
296         int j = sb2.length()-1;
297         int found = 0;
298         for (int k = j; k>=0; k--) {
299             if (k!=j && (sb2.charAt(k) == '/' || sb2.charAt(k) == '\\')) {
300                 found++;
301             }
302             if (found < dd) {
303                 sb2.deleteCharAt(k);
304             } else {
305                 break;
306             }
307         }
308         if (found+1 < dd) return null;
309         return sb2.toString() + sb.toString();
310     }
311
312     /**
313      * Close any Reader objects that have been created
314      * @throws IOException If a call to close() on one of the Reader objects fails
315      */

316     public void close() throws IOException JavaDoc {
317         if (baseReader != null) baseReader.close();
318         if (importReader != null) importReader.close();
319     }
320 }
321
322
323
Popular Tags