KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > classloader > JarFileUrlStreamHandler


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.kernel.classloader;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.URLConnection JavaDoc;
25 import java.net.URLStreamHandler JavaDoc;
26 import java.util.jar.JarEntry JavaDoc;
27 import java.util.jar.JarFile JavaDoc;
28
29 /**
30  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
31  */

32 public class JarFileUrlStreamHandler extends URLStreamHandler JavaDoc {
33     public static URL JavaDoc createUrl(JarFile JavaDoc jarFile, JarEntry JavaDoc jarEntry) throws MalformedURLException JavaDoc {
34         return createUrl(jarFile, jarEntry, new File JavaDoc(jarFile.getName()).toURL());
35     }
36
37     public static URL JavaDoc createUrl(JarFile JavaDoc jarFile, JarEntry JavaDoc jarEntry, URL JavaDoc codeSource) throws MalformedURLException JavaDoc {
38         JarFileUrlStreamHandler handler = new JarFileUrlStreamHandler(jarFile, jarEntry);
39         URL JavaDoc url = new URL JavaDoc("jar", "", -1, codeSource + "!/" + jarEntry.getName(), handler);
40         handler.setExpectedUrl(url);
41         return url;
42     }
43
44     private URL JavaDoc expectedUrl;
45     private final JarFile JavaDoc jarFile;
46     private final JarEntry JavaDoc jarEntry;
47
48     public JarFileUrlStreamHandler(JarFile JavaDoc jarFile, JarEntry JavaDoc jarEntry) {
49         if (jarFile == null) throw new NullPointerException JavaDoc("jarFile is null");
50         if (jarEntry == null) throw new NullPointerException JavaDoc("jarEntry is null");
51
52         this.jarFile = jarFile;
53         this.jarEntry = jarEntry;
54     }
55
56     public void setExpectedUrl(URL JavaDoc expectedUrl) {
57         if (expectedUrl == null) throw new NullPointerException JavaDoc("expectedUrl is null");
58         this.expectedUrl = expectedUrl;
59     }
60
61     public URLConnection JavaDoc openConnection(URL JavaDoc url) throws IOException JavaDoc {
62         if (expectedUrl == null) throw new IllegalStateException JavaDoc("expectedUrl was not set");
63
64         // the caller copied the URL reusing a stream handler from a previous call
65
if (!expectedUrl.equals(url)) {
66             // the new url is supposed to be within our context, so it must have a jar protocol
67
if (!url.getProtocol().equals("jar")) {
68                 throw new IllegalArgumentException JavaDoc("Unsupported protocol " + url.getProtocol());
69             }
70
71             // split the path at "!/" into the file part and entry part
72
String JavaDoc path = url.getPath();
73             String JavaDoc[] chunks = path.split("!/", 2);
74
75             // if we only got only one chunk, it didn't contain the required "!/" delimiter
76
if (chunks.length == 1) {
77                 throw new MalformedURLException JavaDoc("Url does not contain a '!' character: " + url);
78             }
79
80             String JavaDoc file = chunks[0];
81             String JavaDoc entryPath = chunks[1];
82
83             // this handler only supports jars on the local file system
84
if (!file.startsWith("file:")) {
85                 // let the system handler deal with this
86
return new URL JavaDoc(url.toExternalForm()).openConnection();
87             }
88             file = file.substring("file:".length());
89
90             // again the new url is supposed to be within our context so it must reference the same jar file
91
if (!jarFile.getName().equals(file)) {
92                 // let the system handler deal with this
93
return new URL JavaDoc(url.toExternalForm()).openConnection();
94             }
95
96             // get the entry
97
JarEntry JavaDoc newEntry = jarFile.getJarEntry(entryPath);
98             if (newEntry == null) {
99                 throw new FileNotFoundException JavaDoc("Entry not found: " + url);
100             }
101             return new JarFileUrlConnection(url, jarFile, newEntry);
102         }
103
104         return new JarFileUrlConnection(url, jarFile, jarEntry);
105     }
106 }
107
Popular Tags