author | sdrach |
Mon, 15 Feb 2016 17:47:53 -0800 | |
changeset 36129 | 332b49163fc9 |
parent 25859 | 3317bb8137f4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
16491
diff
changeset
|
2 |
* Copyright (c) 1997, 2013, 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 |
||
3437 | 28 |
import java.io.IOException; |
29 |
import java.io.FileNotFoundException; |
|
30 |
import java.net.URL; |
|
31 |
import java.net.URLConnection; |
|
32 |
import java.util.HashMap; |
|
33 |
import java.util.jar.JarFile; |
|
2 | 34 |
import java.security.Permission; |
3437 | 35 |
import sun.net.util.URLUtil; |
2 | 36 |
|
37 |
/* A factory for cached JAR file. This class is used to both retrieve |
|
38 |
* and cache Jar files. |
|
39 |
* |
|
40 |
* @author Benjamin Renaud |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
41 |
* @since 1.2 |
2 | 42 |
*/ |
43 |
class JarFileFactory implements URLJarFile.URLJarFileCloseController { |
|
44 |
||
45 |
/* the url to file cache */ |
|
16491
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
46 |
private static final HashMap<String, JarFile> fileCache = new HashMap<>(); |
2 | 47 |
|
48 |
/* the file to url cache */ |
|
16491
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
49 |
private static final HashMap<JarFile, URL> urlCache = new HashMap<>(); |
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
50 |
|
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
51 |
private static final JarFileFactory instance = new JarFileFactory(); |
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
52 |
|
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
53 |
private JarFileFactory() { } |
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
54 |
|
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
55 |
public static JarFileFactory getInstance() { |
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
56 |
return instance; |
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
57 |
} |
2 | 58 |
|
59 |
URLConnection getConnection(JarFile jarFile) throws IOException { |
|
16491
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
60 |
URL u; |
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
61 |
synchronized (instance) { |
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
62 |
u = urlCache.get(jarFile); |
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
63 |
} |
2 | 64 |
if (u != null) |
65 |
return u.openConnection(); |
|
66 |
||
67 |
return null; |
|
68 |
} |
|
69 |
||
70 |
public JarFile get(URL url) throws IOException { |
|
71 |
return get(url, true); |
|
72 |
} |
|
73 |
||
74 |
JarFile get(URL url, boolean useCaches) throws IOException { |
|
75 |
||
16491
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
76 |
JarFile result; |
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
77 |
JarFile local_result; |
2 | 78 |
|
79 |
if (useCaches) { |
|
16491
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
80 |
synchronized (instance) { |
2 | 81 |
result = getCachedJarFile(url); |
82 |
} |
|
83 |
if (result == null) { |
|
84 |
local_result = URLJarFile.getJarFile(url, this); |
|
16491
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
85 |
synchronized (instance) { |
2 | 86 |
result = getCachedJarFile(url); |
87 |
if (result == null) { |
|
36129
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
25859
diff
changeset
|
88 |
fileCache.put(urlKey(url), local_result); |
2 | 89 |
urlCache.put(local_result, url); |
90 |
result = local_result; |
|
91 |
} else { |
|
92 |
if (local_result != null) { |
|
93 |
local_result.close(); |
|
94 |
} |
|
95 |
} |
|
96 |
} |
|
97 |
} |
|
98 |
} else { |
|
99 |
result = URLJarFile.getJarFile(url, this); |
|
100 |
} |
|
101 |
if (result == null) |
|
102 |
throw new FileNotFoundException(url.toString()); |
|
103 |
||
104 |
return result; |
|
105 |
} |
|
106 |
||
107 |
/** |
|
108 |
* Callback method of the URLJarFileCloseController to |
|
109 |
* indicate that the JarFile is close. This way we can |
|
110 |
* remove the JarFile from the cache |
|
111 |
*/ |
|
112 |
public void close(JarFile jarFile) { |
|
16491
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
113 |
synchronized (instance) { |
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
114 |
URL urlRemoved = urlCache.remove(jarFile); |
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
115 |
if (urlRemoved != null) |
36129
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
25859
diff
changeset
|
116 |
fileCache.remove(urlKey(urlRemoved)); |
2 | 117 |
} |
118 |
} |
|
119 |
||
120 |
private JarFile getCachedJarFile(URL url) { |
|
16491
272ad530ce3a
8010282: sun.net.www.protocol.jar.JarFileFactory.close(JarFile) should be thread-safe
chegar
parents:
5506
diff
changeset
|
121 |
assert Thread.holdsLock(instance); |
36129
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
25859
diff
changeset
|
122 |
JarFile result = fileCache.get(urlKey(url)); |
2 | 123 |
|
124 |
/* if the JAR file is cached, the permission will always be there */ |
|
125 |
if (result != null) { |
|
126 |
Permission perm = getPermission(result); |
|
127 |
if (perm != null) { |
|
128 |
SecurityManager sm = System.getSecurityManager(); |
|
129 |
if (sm != null) { |
|
130 |
try { |
|
131 |
sm.checkPermission(perm); |
|
132 |
} catch (SecurityException se) { |
|
133 |
// fallback to checkRead/checkConnect for pre 1.2 |
|
134 |
// security managers |
|
135 |
if ((perm instanceof java.io.FilePermission) && |
|
136 |
perm.getActions().indexOf("read") != -1) { |
|
137 |
sm.checkRead(perm.getName()); |
|
138 |
} else if ((perm instanceof |
|
139 |
java.net.SocketPermission) && |
|
140 |
perm.getActions().indexOf("connect") != -1) { |
|
141 |
sm.checkConnect(url.getHost(), url.getPort()); |
|
142 |
} else { |
|
143 |
throw se; |
|
144 |
} |
|
145 |
} |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
return result; |
|
150 |
} |
|
151 |
||
36129
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
25859
diff
changeset
|
152 |
private String urlKey(URL url) { |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
25859
diff
changeset
|
153 |
String urlstr = URLUtil.urlNoFragString(url); |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
25859
diff
changeset
|
154 |
if ("runtime".equals(url.getRef())) urlstr += "#runtime"; |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
25859
diff
changeset
|
155 |
return urlstr; |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
25859
diff
changeset
|
156 |
} |
332b49163fc9
8132734: JDK 9 runtime changes to support multi-release jar files
sdrach
parents:
25859
diff
changeset
|
157 |
|
2 | 158 |
private Permission getPermission(JarFile jarFile) { |
159 |
try { |
|
160 |
URLConnection uc = getConnection(jarFile); |
|
161 |
if (uc != null) |
|
162 |
return uc.getPermission(); |
|
163 |
} catch (IOException ioe) { |
|
164 |
// gulp |
|
165 |
} |
|
166 |
||
167 |
return null; |
|
168 |
} |
|
169 |
} |