1 |
14 |
jlechner |
/* JarURLConnection.java -- Class for manipulating remote jar files
|
2 |
|
|
Copyright (C) 1998, 2002, 2003 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
package java.net;
|
39 |
|
|
|
40 |
|
|
import java.io.IOException;
|
41 |
|
|
import java.security.cert.Certificate;
|
42 |
|
|
import java.util.jar.Attributes;
|
43 |
|
|
import java.util.jar.JarEntry;
|
44 |
|
|
import java.util.jar.JarFile;
|
45 |
|
|
import java.util.jar.JarInputStream;
|
46 |
|
|
import java.util.jar.Manifest;
|
47 |
|
|
import java.util.zip.ZipEntry;
|
48 |
|
|
|
49 |
|
|
|
50 |
|
|
/**
|
51 |
|
|
* This abstract class represents a common superclass for implementations
|
52 |
|
|
* of jar URL's. A jar URL is a special type of URL that allows JAR
|
53 |
|
|
* files on remote systems to be accessed. It has the form:
|
54 |
|
|
* <p>
|
55 |
|
|
* jar:<standard URL pointing to jar filei>!/file/within/jarfile
|
56 |
|
|
* <p> for example:
|
57 |
|
|
* <p>
|
58 |
|
|
* jar:http://www.urbanophile.com/java/foo.jar!/com/urbanophile/bar.class
|
59 |
|
|
* <p>
|
60 |
|
|
* That example URL points to the file /com/urbanophile/bar.class in the
|
61 |
|
|
* remote JAR file http://www.urbanophile.com/java/foo.jar. The HTTP
|
62 |
|
|
* protocol is used only as an example. Any supported remote protocol
|
63 |
|
|
* can be used.
|
64 |
|
|
* <p>
|
65 |
|
|
* This class currently works by retrieving the entire jar file into a
|
66 |
|
|
* local cache file, then performing standard jar operations on it.
|
67 |
|
|
* (At least this is true for the default protocol implementation).
|
68 |
|
|
*
|
69 |
|
|
* @author Aaron M. Renn (arenn@urbanophile.com)
|
70 |
|
|
* @author Kresten Krab Thorup (krab@gnu.org)
|
71 |
|
|
* @date Aug 10, 1999.
|
72 |
|
|
*
|
73 |
|
|
* @since 1.2
|
74 |
|
|
*/
|
75 |
|
|
public abstract class JarURLConnection extends URLConnection
|
76 |
|
|
{
|
77 |
|
|
/**
|
78 |
|
|
* This is the actual URL that points the remote jar file. This is parsed
|
79 |
|
|
* out of the jar URL by the constructor.
|
80 |
|
|
*/
|
81 |
|
|
private final URL jarFileURL;
|
82 |
|
|
|
83 |
|
|
/**
|
84 |
|
|
* The connection to the jar file itself. A JarURLConnection
|
85 |
|
|
* can represent an entry in a jar file or an entire jar file. In
|
86 |
|
|
* either case this describes just the jar file itself.
|
87 |
|
|
*/
|
88 |
|
|
protected URLConnection jarFileURLConnection;
|
89 |
|
|
|
90 |
|
|
/**
|
91 |
|
|
* This is the jar file "entry name" or portion after the "!/" in the
|
92 |
|
|
* URL which represents the pathname inside the actual jar file.
|
93 |
|
|
*/
|
94 |
|
|
private final String entryName;
|
95 |
|
|
|
96 |
|
|
/**
|
97 |
|
|
* Creates a JarURLConnection from an URL object
|
98 |
|
|
*
|
99 |
|
|
* @param url The URL object for this connection.
|
100 |
|
|
*
|
101 |
|
|
* @exception MalformedURLException If url is invalid
|
102 |
|
|
*
|
103 |
|
|
* @specnote This constructor is protected since JDK 1.4
|
104 |
|
|
*/
|
105 |
|
|
protected JarURLConnection(URL url) throws MalformedURLException
|
106 |
|
|
{
|
107 |
|
|
super(url);
|
108 |
|
|
|
109 |
|
|
if (! url.getProtocol().equals("jar"))
|
110 |
|
|
throw new MalformedURLException(url + ": Not jar protocol.");
|
111 |
|
|
|
112 |
|
|
String spec = url.getFile();
|
113 |
|
|
int bang = spec.indexOf("!/");
|
114 |
|
|
if (bang == -1)
|
115 |
|
|
throw new MalformedURLException(url + ": No `!/' in spec.");
|
116 |
|
|
|
117 |
|
|
// Extract the url for the jar itself.
|
118 |
|
|
jarFileURL = new URL(spec.substring(0, bang));
|
119 |
|
|
|
120 |
|
|
// Get the name of the entry, if any.
|
121 |
|
|
entryName = spec.length() == (bang + 2) ? null : spec.substring(bang + 2);
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
/**
|
125 |
|
|
* This method returns the "real" URL where the JarFile is located.
|
126 |
|
|
* //****Is this right?*****
|
127 |
|
|
*
|
128 |
|
|
* @return The remote URL
|
129 |
|
|
*/
|
130 |
|
|
public URL getJarFileURL()
|
131 |
|
|
{
|
132 |
|
|
return jarFileURL;
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
/**
|
136 |
|
|
* Returns the "entry name" portion of the jar URL. This is the portion
|
137 |
|
|
* after the "!/" in the jar URL that represents the pathname inside the
|
138 |
|
|
* actual jar file.
|
139 |
|
|
*
|
140 |
|
|
* @return The entry name.
|
141 |
|
|
*/
|
142 |
|
|
public String getEntryName()
|
143 |
|
|
{
|
144 |
|
|
return entryName;
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
/**
|
148 |
|
|
* Returns the entry in this jar file specified by the URL.
|
149 |
|
|
*
|
150 |
|
|
* @return The jar entry
|
151 |
|
|
*
|
152 |
|
|
* @exception IOException If an error occurs
|
153 |
|
|
*/
|
154 |
|
|
public JarEntry getJarEntry() throws IOException
|
155 |
|
|
{
|
156 |
|
|
JarFile jarfile = null;
|
157 |
|
|
|
158 |
|
|
if (entryName == null)
|
159 |
|
|
return null;
|
160 |
|
|
|
161 |
|
|
if (! doInput)
|
162 |
|
|
throw new ProtocolException("Can't open JarEntry if doInput is false");
|
163 |
|
|
|
164 |
|
|
try
|
165 |
|
|
{
|
166 |
|
|
jarfile = getJarFile ();
|
167 |
|
|
}
|
168 |
|
|
catch (IOException x)
|
169 |
|
|
{
|
170 |
|
|
/* ignore */
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
if (jarfile == null)
|
174 |
|
|
{
|
175 |
|
|
JarInputStream zis = new JarInputStream(
|
176 |
|
|
jarFileURLConnection.getInputStream ());
|
177 |
|
|
|
178 |
|
|
// This is hideous, we're doing a linear search for the thing...
|
179 |
|
|
for (ZipEntry ent = zis.getNextEntry ();
|
180 |
|
|
ent != null;
|
181 |
|
|
ent = zis.getNextEntry ())
|
182 |
|
|
{
|
183 |
|
|
if (entryName.equals (ent.getName()))
|
184 |
|
|
{
|
185 |
|
|
return new JarEntry (ent);
|
186 |
|
|
}
|
187 |
|
|
}
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
else
|
191 |
|
|
{
|
192 |
|
|
return jarfile.getJarEntry (entryName);
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
return null;
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
/**
|
199 |
|
|
* Returns a read-only JarFile object for the remote jar file
|
200 |
|
|
*
|
201 |
|
|
* @return The JarFile object
|
202 |
|
|
*
|
203 |
|
|
* @exception IOException If an error occurs
|
204 |
|
|
*/
|
205 |
|
|
public abstract JarFile getJarFile() throws IOException;
|
206 |
|
|
|
207 |
|
|
/**
|
208 |
|
|
* Returns an array of Certificate objects for the jar file entry specified
|
209 |
|
|
* by this URL or null if there are none
|
210 |
|
|
*
|
211 |
|
|
* @return A Certificate array
|
212 |
|
|
*
|
213 |
|
|
* @exception IOException If an error occurs
|
214 |
|
|
*/
|
215 |
|
|
public Certificate[] getCertificates() throws IOException
|
216 |
|
|
{
|
217 |
|
|
JarEntry entry = getJarEntry();
|
218 |
|
|
|
219 |
|
|
return entry != null ? entry.getCertificates() : null;
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
/**
|
223 |
|
|
* Returns the main Attributes for the jar file specified in the URL or
|
224 |
|
|
* null if there are none
|
225 |
|
|
*
|
226 |
|
|
* @return The main Attributes for the JAR file for this connection
|
227 |
|
|
*
|
228 |
|
|
* @exception IOException If an error occurs
|
229 |
|
|
*/
|
230 |
|
|
public Attributes getMainAttributes() throws IOException
|
231 |
|
|
{
|
232 |
|
|
Manifest manifest = getManifest();
|
233 |
|
|
|
234 |
|
|
return manifest != null ? manifest.getMainAttributes() : null;
|
235 |
|
|
}
|
236 |
|
|
|
237 |
|
|
/**
|
238 |
|
|
* Returns the Attributes for the Jar entry specified by the URL or null
|
239 |
|
|
* if none
|
240 |
|
|
*
|
241 |
|
|
* @return The Attributes object for this connection if the URL for it points
|
242 |
|
|
* to a JAR file entry, null otherwise
|
243 |
|
|
*
|
244 |
|
|
* @exception IOException If an error occurs
|
245 |
|
|
*/
|
246 |
|
|
public Attributes getAttributes() throws IOException
|
247 |
|
|
{
|
248 |
|
|
JarEntry entry = getJarEntry();
|
249 |
|
|
|
250 |
|
|
return entry != null ? entry.getAttributes() : null;
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
/**
|
254 |
|
|
* Returns a Manifest object for this jar file, or null if there is no
|
255 |
|
|
* manifest.
|
256 |
|
|
*
|
257 |
|
|
* @return The Manifest for this connection, or null if none
|
258 |
|
|
*
|
259 |
|
|
* @exception IOException If an error occurs
|
260 |
|
|
*/
|
261 |
|
|
public Manifest getManifest() throws IOException
|
262 |
|
|
{
|
263 |
|
|
JarFile file = getJarFile();
|
264 |
|
|
|
265 |
|
|
return file != null ? file.getManifest() : null;
|
266 |
|
|
}
|
267 |
|
|
}
|