1 /**
2 *
3 * Copyright 2005 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.JarURLConnection;
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.net.URLConnection;
26 import java.net.URLStreamHandler;
27 import java.security.Permission;
28 import java.security.cert.Certificate;
29 import java.util.jar.Attributes;
30 import java.util.jar.JarEntry;
31 import java.util.jar.JarFile;
32 import java.util.jar.Manifest;
33
34 /**
35 * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
36 */
37 public class JarFileUrlConnection extends JarURLConnection {
38 public static final URL DUMMY_JAR_URL;
39 static {
40 try {
41 DUMMY_JAR_URL = new URL("jar", "", -1, "file:dummy!/", new URLStreamHandler() {
42 protected URLConnection openConnection(URL u) {
43 throw new UnsupportedOperationException();
44 }
45 });
46 } catch (Exception e) {
47 throw new ExceptionInInitializerError(e);
48 }
49 }
50
51 private final URL url;
52 private final JarFile jarFile;
53 private final JarEntry jarEntry;
54 private final URL jarFileUrl;
55
56 public JarFileUrlConnection(URL url, JarFile jarFile, JarEntry jarEntry) throws MalformedURLException {
57 super(DUMMY_JAR_URL);
58
59 if (url == null) throw new NullPointerException("url is null");
60 if (jarFile == null) throw new NullPointerException("jarFile is null");
61 if (jarEntry == null) throw new NullPointerException("jarEntry is null");
62
63 this.url = url;
64 this.jarFile = jarFile;
65 this.jarEntry = jarEntry;
66 jarFileUrl = new File(jarFile.getName()).toURL();
67 }
68
69 public JarFile getJarFile() throws IOException {
70 return jarFile;
71 }
72
73 public synchronized void connect() {
74 }
75
76 public URL getJarFileURL() {
77 return jarFileUrl;
78 }
79
80 public String getEntryName() {
81 return getJarEntry().getName();
82 }
83
84 public Manifest getManifest() throws IOException {
85 return jarFile.getManifest();
86 }
87
88 public JarEntry getJarEntry() {
89 return jarEntry;
90 }
91
92 public Attributes getAttributes() throws IOException {
93 return getJarEntry().getAttributes();
94 }
95
96 public Attributes getMainAttributes() throws IOException {
97 return getManifest().getMainAttributes();
98 }
99
100 public Certificate[] getCertificates() throws IOException {
101 return getJarEntry().getCertificates();
102 }
103
104 public URL 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 getInputStream() throws IOException {
121 return jarFile.getInputStream(jarEntry);
122 }
123
124 public Permission getPermission() throws IOException {
125 URL jarFileUrl = new File(jarFile.getName()).toURL();
126 return jarFileUrl.openConnection().getPermission();
127 }
128
129 public String toString() {
130 return JarFileUrlConnection.class.getName() + ":" + url;
131 }
132 }