KickJava   Java API By Example, From Geeks To Geeks.

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


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.InputStream JavaDoc;
22 import java.net.JarURLConnection JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.URLConnection JavaDoc;
26 import java.net.URLStreamHandler JavaDoc;
27 import java.security.Permission JavaDoc;
28 import java.security.cert.Certificate JavaDoc;
29 import java.util.jar.Attributes JavaDoc;
30 import java.util.jar.JarEntry JavaDoc;
31 import java.util.jar.JarFile JavaDoc;
32 import java.util.jar.Manifest JavaDoc;
33
34 /**
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  */

37 public class JarFileUrlConnection extends JarURLConnection JavaDoc {
38     public static final URL JavaDoc DUMMY_JAR_URL;
39     static {
40         try {
41             DUMMY_JAR_URL = new URL JavaDoc("jar", "", -1, "file:dummy!/", new URLStreamHandler JavaDoc() {
42                 protected URLConnection JavaDoc openConnection(URL JavaDoc u) {
43                     throw new UnsupportedOperationException JavaDoc();
44                 }
45             });
46         } catch (Exception JavaDoc e) {
47             throw new ExceptionInInitializerError JavaDoc(e);
48         }
49     }
50
51     private final URL JavaDoc url;
52     private final JarFile JavaDoc jarFile;
53     private final JarEntry JavaDoc jarEntry;
54     private final URL JavaDoc jarFileUrl;
55
56     public JarFileUrlConnection(URL JavaDoc url, JarFile JavaDoc jarFile, JarEntry JavaDoc jarEntry) throws MalformedURLException JavaDoc {
57         super(DUMMY_JAR_URL);
58
59         if (url == null) throw new NullPointerException JavaDoc("url is null");
60         if (jarFile == null) throw new NullPointerException JavaDoc("jarFile is null");
61         if (jarEntry == null) throw new NullPointerException JavaDoc("jarEntry is null");
62
63         this.url = url;
64         this.jarFile = jarFile;
65         this.jarEntry = jarEntry;
66         jarFileUrl = new File JavaDoc(jarFile.getName()).toURL();
67     }
68
69     public JarFile JavaDoc getJarFile() throws IOException JavaDoc {
70         return jarFile;
71     }
72
73     public synchronized void connect() {
74     }
75
76     public URL JavaDoc getJarFileURL() {
77         return jarFileUrl;
78     }
79
80     public String JavaDoc getEntryName() {
81         return getJarEntry().getName();
82     }
83
84     public Manifest JavaDoc getManifest() throws IOException JavaDoc {
85         return jarFile.getManifest();
86     }
87
88     public JarEntry JavaDoc getJarEntry() {
89         return jarEntry;
90     }
91
92     public Attributes JavaDoc getAttributes() throws IOException JavaDoc {
93         return getJarEntry().getAttributes();
94     }
95
96     public Attributes JavaDoc getMainAttributes() throws IOException JavaDoc {
97         return getManifest().getMainAttributes();
98     }
99
100     public Certificate JavaDoc[] getCertificates() throws IOException JavaDoc {
101         return getJarEntry().getCertificates();
102     }
103
104     public URL JavaDoc getURL() {
105         return url;
106     }
107
108     public int getContentLength() {
109         long size = getJarEntry().getSize();
110         if (size > Integer.MAX_VALUE) {
111             return -1;
112         }
113         return (int) size;
114     }
115
116     public long getLastModified() {
117         return getJarEntry().getTime();
118     }
119
120     public synchronized InputStream JavaDoc getInputStream() throws IOException JavaDoc {
121         return jarFile.getInputStream(jarEntry);
122     }
123
124     public Permission JavaDoc getPermission() throws IOException JavaDoc {
125         URL JavaDoc jarFileUrl = new File JavaDoc(jarFile.getName()).toURL();
126         return jarFileUrl.openConnection().getPermission();
127     }
128
129     public String JavaDoc toString() {
130         return JarFileUrlConnection.class.getName() + ":" + url;
131     }
132 }
133
Popular Tags