author | sdrach |
Mon, 15 Feb 2016 17:47:53 -0800 | |
changeset 36129 | 332b49163fc9 |
parent 34943 | 52923a144ff6 |
child 39646 | 2bf99fe5023b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
34943 | 2 |
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.net.www.protocol.jar; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.net.*; |
|
8400
b52368af8137
7018392: update URLJarFile.java to use try-with-resources
smarks
parents:
5506
diff
changeset
|
30 |
import java.nio.file.Files; |
b52368af8137
7018392: update URLJarFile.java to use try-with-resources
smarks
parents:
5506
diff
changeset
|
31 |
import java.nio.file.Path; |
b52368af8137
7018392: update URLJarFile.java to use try-with-resources
smarks
parents:
5506
diff
changeset
|
32 |
import java.nio.file.StandardCopyOption; |
2 | 33 |
import java.util.*; |
34 |
import java.util.jar.*; |
|
35 |
import java.util.zip.ZipFile; |
|
36 |
import java.util.zip.ZipEntry; |
|
37 |
import java.security.CodeSigner; |
|
38 |
import java.security.cert.Certificate; |
|
39 |
import java.security.AccessController; |
|
40 |
import java.security.PrivilegedAction; |
|
41 |
import java.security.PrivilegedExceptionAction; |
|
42 |
import java.security.PrivilegedActionException; |
|
43 |
import sun.net.www.ParseUtil; |
|
44 |
||
45 |
/* URL jar file is a common JarFile subtype used for JarURLConnection */ |
|
46 |
public class URLJarFile extends JarFile { |
|
47 |
||
48 |
/* |
|
49 |
* Interface to be able to call retrieve() in plugin if |
|
50 |
* this variable is set. |
|
51 |
*/ |
|
52 |
private static URLJarFileCallBack callback = null; |
|
53 |
||
54 |
/* Controller of the Jar File's closing */ |
|
55 |
private URLJarFileCloseController closeController = null; |
|
56 |
||
57 |
private static int BUF_SIZE = 2048; |
|
58 |
||
59 |
private Manifest superMan; |
|
60 |
private Attributes superAttr; |
|
51 | 61 |
private Map<String, Attributes> superEntries; |
2 | 62 |
|
63 |
static JarFile getJarFile(URL url) throws IOException { |
|
64 |
return getJarFile(url, null); |
|
65 |
} |
|
66 |
||
67 |
static JarFile getJarFile(URL url, URLJarFileCloseController closeController) throws IOException { |
|
36129
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
34943
diff
changeset
|
68 |
if (isFileURL(url)) { |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
34943
diff
changeset
|
69 |
Release version = "runtime".equals(url.getRef()) ? Release.RUNTIME : Release.BASE; |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
34943
diff
changeset
|
70 |
return new URLJarFile(url, closeController, version); |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
34943
diff
changeset
|
71 |
} else { |
2 | 72 |
return retrieve(url, closeController); |
73 |
} |
|
74 |
} |
|
75 |
||
76 |
/* |
|
77 |
* Changed modifier from private to public in order to be able |
|
78 |
* to instantiate URLJarFile from sun.plugin package. |
|
79 |
*/ |
|
80 |
public URLJarFile(File file) throws IOException { |
|
81 |
this(file, null); |
|
82 |
} |
|
83 |
||
84 |
/* |
|
85 |
* Changed modifier from private to public in order to be able |
|
86 |
* to instantiate URLJarFile from sun.plugin package. |
|
87 |
*/ |
|
88 |
public URLJarFile(File file, URLJarFileCloseController closeController) throws IOException { |
|
89 |
super(file, true, ZipFile.OPEN_READ | ZipFile.OPEN_DELETE); |
|
90 |
this.closeController = closeController; |
|
91 |
} |
|
92 |
||
36129
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
34943
diff
changeset
|
93 |
private URLJarFile(File file, URLJarFileCloseController closeController, Release version) throws IOException { |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
34943
diff
changeset
|
94 |
super(file, true, ZipFile.OPEN_READ | ZipFile.OPEN_DELETE, version); |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
34943
diff
changeset
|
95 |
this.closeController = closeController; |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
34943
diff
changeset
|
96 |
} |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
34943
diff
changeset
|
97 |
|
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
34943
diff
changeset
|
98 |
private URLJarFile(URL url, URLJarFileCloseController closeController, Release version) throws IOException { |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
34943
diff
changeset
|
99 |
super(new File(ParseUtil.decode(url.getFile())), true, ZipFile.OPEN_READ, version); |
2 | 100 |
this.closeController = closeController; |
101 |
} |
|
102 |
||
103 |
private static boolean isFileURL(URL url) { |
|
104 |
if (url.getProtocol().equalsIgnoreCase("file")) { |
|
105 |
/* |
|
106 |
* Consider this a 'file' only if it's a LOCAL file, because |
|
107 |
* 'file:' URLs can be accessible through ftp. |
|
108 |
*/ |
|
109 |
String host = url.getHost(); |
|
110 |
if (host == null || host.equals("") || host.equals("~") || |
|
111 |
host.equalsIgnoreCase("localhost")) |
|
112 |
return true; |
|
113 |
} |
|
114 |
return false; |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* Returns the <code>ZipEntry</code> for the given entry name or |
|
119 |
* <code>null</code> if not found. |
|
120 |
* |
|
121 |
* @param name the JAR file entry name |
|
122 |
* @return the <code>ZipEntry</code> for the given entry name or |
|
123 |
* <code>null</code> if not found |
|
124 |
* @see java.util.zip.ZipEntry |
|
125 |
*/ |
|
126 |
public ZipEntry getEntry(String name) { |
|
127 |
ZipEntry ze = super.getEntry(name); |
|
128 |
if (ze != null) { |
|
129 |
if (ze instanceof JarEntry) |
|
130 |
return new URLJarFileEntry((JarEntry)ze); |
|
131 |
else |
|
132 |
throw new InternalError(super.getClass() + |
|
133 |
" returned unexpected entry type " + |
|
134 |
ze.getClass()); |
|
135 |
} |
|
136 |
return null; |
|
137 |
} |
|
138 |
||
139 |
public Manifest getManifest() throws IOException { |
|
140 |
||
141 |
if (!isSuperMan()) { |
|
142 |
return null; |
|
143 |
} |
|
144 |
||
145 |
Manifest man = new Manifest(); |
|
146 |
Attributes attr = man.getMainAttributes(); |
|
147 |
attr.putAll((Map)superAttr.clone()); |
|
148 |
||
149 |
// now deep copy the manifest entries |
|
150 |
if (superEntries != null) { |
|
51 | 151 |
Map<String, Attributes> entries = man.getEntries(); |
152 |
for (String key : superEntries.keySet()) { |
|
153 |
Attributes at = superEntries.get(key); |
|
154 |
entries.put(key, (Attributes) at.clone()); |
|
2 | 155 |
} |
156 |
} |
|
157 |
||
158 |
return man; |
|
159 |
} |
|
160 |
||
161 |
/* If close controller is set the notify the controller about the pending close */ |
|
162 |
public void close() throws IOException { |
|
163 |
if (closeController != null) { |
|
164 |
closeController.close(this); |
|
165 |
} |
|
166 |
super.close(); |
|
167 |
} |
|
168 |
||
169 |
// optimal side-effects |
|
170 |
private synchronized boolean isSuperMan() throws IOException { |
|
171 |
||
172 |
if (superMan == null) { |
|
173 |
superMan = super.getManifest(); |
|
174 |
} |
|
175 |
||
176 |
if (superMan != null) { |
|
177 |
superAttr = superMan.getMainAttributes(); |
|
178 |
superEntries = superMan.getEntries(); |
|
179 |
return true; |
|
180 |
} else |
|
181 |
return false; |
|
182 |
} |
|
183 |
||
184 |
/** |
|
185 |
* Given a URL, retrieves a JAR file, caches it to disk, and creates a |
|
186 |
* cached JAR file object. |
|
187 |
*/ |
|
188 |
private static JarFile retrieve(final URL url, final URLJarFileCloseController closeController) throws IOException { |
|
189 |
/* |
|
190 |
* See if interface is set, then call retrieve function of the class |
|
191 |
* that implements URLJarFileCallBack interface (sun.plugin - to |
|
192 |
* handle the cache failure for JARJAR file.) |
|
193 |
*/ |
|
194 |
if (callback != null) |
|
195 |
{ |
|
196 |
return callback.retrieve(url); |
|
197 |
} |
|
198 |
||
199 |
else |
|
200 |
{ |
|
201 |
||
202 |
JarFile result = null; |
|
36129
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
34943
diff
changeset
|
203 |
Release version = "runtime".equals(url.getRef()) ? Release.RUNTIME : Release.BASE; |
2 | 204 |
|
205 |
/* get the stream before asserting privileges */ |
|
8400
b52368af8137
7018392: update URLJarFile.java to use try-with-resources
smarks
parents:
5506
diff
changeset
|
206 |
try (final InputStream in = url.openConnection().getInputStream()) { |
51 | 207 |
result = AccessController.doPrivileged( |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
208 |
new PrivilegedExceptionAction<>() { |
51 | 209 |
public JarFile run() throws IOException { |
8400
b52368af8137
7018392: update URLJarFile.java to use try-with-resources
smarks
parents:
5506
diff
changeset
|
210 |
Path tmpFile = Files.createTempFile("jar_cache", null); |
2 | 211 |
try { |
8400
b52368af8137
7018392: update URLJarFile.java to use try-with-resources
smarks
parents:
5506
diff
changeset
|
212 |
Files.copy(in, tmpFile, StandardCopyOption.REPLACE_EXISTING); |
36129
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
34943
diff
changeset
|
213 |
JarFile jarFile = new URLJarFile(tmpFile.toFile(), closeController, version); |
8400
b52368af8137
7018392: update URLJarFile.java to use try-with-resources
smarks
parents:
5506
diff
changeset
|
214 |
tmpFile.toFile().deleteOnExit(); |
b52368af8137
7018392: update URLJarFile.java to use try-with-resources
smarks
parents:
5506
diff
changeset
|
215 |
return jarFile; |
b52368af8137
7018392: update URLJarFile.java to use try-with-resources
smarks
parents:
5506
diff
changeset
|
216 |
} catch (Throwable thr) { |
b52368af8137
7018392: update URLJarFile.java to use try-with-resources
smarks
parents:
5506
diff
changeset
|
217 |
try { |
b52368af8137
7018392: update URLJarFile.java to use try-with-resources
smarks
parents:
5506
diff
changeset
|
218 |
Files.delete(tmpFile); |
b52368af8137
7018392: update URLJarFile.java to use try-with-resources
smarks
parents:
5506
diff
changeset
|
219 |
} catch (IOException ioe) { |
b52368af8137
7018392: update URLJarFile.java to use try-with-resources
smarks
parents:
5506
diff
changeset
|
220 |
thr.addSuppressed(ioe); |
2 | 221 |
} |
8400
b52368af8137
7018392: update URLJarFile.java to use try-with-resources
smarks
parents:
5506
diff
changeset
|
222 |
throw thr; |
2 | 223 |
} |
224 |
} |
|
225 |
}); |
|
226 |
} catch (PrivilegedActionException pae) { |
|
227 |
throw (IOException) pae.getException(); |
|
228 |
} |
|
229 |
||
230 |
return result; |
|
231 |
} |
|
232 |
} |
|
233 |
||
234 |
/* |
|
235 |
* Set the call back interface to call retrive function in sun.plugin |
|
236 |
* package if plugin is running. |
|
237 |
*/ |
|
238 |
public static void setCallBack(URLJarFileCallBack cb) |
|
239 |
{ |
|
240 |
callback = cb; |
|
241 |
} |
|
242 |
||
243 |
||
244 |
private class URLJarFileEntry extends JarEntry { |
|
245 |
private JarEntry je; |
|
246 |
||
247 |
URLJarFileEntry(JarEntry je) { |
|
248 |
super(je); |
|
249 |
this.je=je; |
|
250 |
} |
|
251 |
||
252 |
public Attributes getAttributes() throws IOException { |
|
253 |
if (URLJarFile.this.isSuperMan()) { |
|
51 | 254 |
Map<String, Attributes> e = URLJarFile.this.superEntries; |
2 | 255 |
if (e != null) { |
51 | 256 |
Attributes a = e.get(getName()); |
2 | 257 |
if (a != null) |
258 |
return (Attributes)a.clone(); |
|
259 |
} |
|
260 |
} |
|
261 |
return null; |
|
262 |
} |
|
263 |
||
264 |
public java.security.cert.Certificate[] getCertificates() { |
|
265 |
Certificate[] certs = je.getCertificates(); |
|
266 |
return certs == null? null: certs.clone(); |
|
267 |
} |
|
268 |
||
269 |
public CodeSigner[] getCodeSigners() { |
|
270 |
CodeSigner[] csg = je.getCodeSigners(); |
|
271 |
return csg == null? null: csg.clone(); |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
public interface URLJarFileCloseController { |
|
276 |
public void close(JarFile jarFile); |
|
277 |
} |
|
278 |
} |