001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.classloader;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.io.FileNotFoundException;
024: import java.net.MalformedURLException;
025: import java.net.URL;
026: import java.net.URLConnection;
027: import java.net.URLStreamHandler;
028: import java.util.jar.JarEntry;
029: import java.util.jar.JarFile;
030:
031: /**
032: * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
033: */
034: public class JarFileUrlStreamHandler extends URLStreamHandler {
035: public static URL createUrl(JarFile jarFile, JarEntry jarEntry)
036: throws MalformedURLException {
037: return createUrl(jarFile, jarEntry, new File(jarFile.getName())
038: .toURL());
039: }
040:
041: public static URL createUrl(JarFile jarFile, JarEntry jarEntry,
042: URL codeSource) throws MalformedURLException {
043: JarFileUrlStreamHandler handler = new JarFileUrlStreamHandler(
044: jarFile, jarEntry);
045: URL url = new URL("jar", "", -1, codeSource + "!/"
046: + jarEntry.getName(), handler);
047: handler.setExpectedUrl(url);
048: return url;
049: }
050:
051: private URL expectedUrl;
052: private JarFile jarFile = null;
053: private JarEntry jarEntry = null;
054:
055: public JarFileUrlStreamHandler() {
056: }
057:
058: public JarFileUrlStreamHandler(JarFile jarFile, JarEntry jarEntry) {
059: if (jarFile == null)
060: throw new NullPointerException("jarFile is null");
061: if (jarEntry == null)
062: throw new NullPointerException("jarEntry is null");
063:
064: this .jarFile = jarFile;
065: this .jarEntry = jarEntry;
066: }
067:
068: public void setExpectedUrl(URL expectedUrl) {
069: if (expectedUrl == null)
070: throw new NullPointerException("expectedUrl is null");
071: this .expectedUrl = expectedUrl;
072: }
073:
074: public URLConnection openConnection(URL url) throws IOException {
075:
076: if (expectedUrl == null || !expectedUrl.equals(url)) {
077: // the new url is supposed to be within our context, so it must have a jar protocol
078: if (!url.getProtocol().equals("jar")) {
079: throw new IllegalArgumentException(
080: "Unsupported protocol " + url.getProtocol());
081: }
082:
083: // split the path at "!/" into the file part and entry part
084: String path = url.getPath();
085: String[] chunks = path.split("!/", 2);
086:
087: // if we only got only one chunk, it didn't contain the required "!/" delimiter
088: if (chunks.length == 1) {
089: throw new MalformedURLException(
090: "Url does not contain a '!' character: " + url);
091: }
092:
093: String file = chunks[0];
094: String entryPath = chunks[1];
095:
096: // this handler only supports jars on the local file system
097: if (!file.startsWith("file:")) {
098: // let the system handler deal with this
099: return new URL(url.toExternalForm()).openConnection();
100: }
101: file = file.substring("file:".length());
102:
103: File f = new File(file);
104: if (f.exists()) {
105: jarFile = new JarFile(f);
106: }
107:
108: if (jarFile == null) {
109: throw new FileNotFoundException("Cannot find JarFile: "
110: + file);
111: }
112:
113: // get the entry
114: jarEntry = jarFile.getJarEntry(entryPath);
115: if (jarEntry == null) {
116: throw new FileNotFoundException("Entry not found: "
117: + url);
118: }
119: expectedUrl = url;
120: }
121:
122: return new JarFileUrlConnection(url, jarFile, jarEntry);
123: }
124: }
|