author | juh |
Tue, 30 Jul 2013 11:04:19 -0700 | |
changeset 19069 | 1d9cb0d080e3 |
parent 15682 | 793a36de151d |
child 19409 | d7c7b9d56631 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
19069 | 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 java.net; |
|
27 |
||
7983
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
28 |
import java.io.*; |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
29 |
import java.util.*; |
2 | 30 |
import java.util.jar.Manifest; |
7983
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
31 |
import java.util.jar.JarFile; |
2 | 32 |
import java.util.jar.Attributes; |
33 |
import java.util.jar.Attributes.Name; |
|
34 |
import java.security.CodeSigner; |
|
35 |
import java.security.PrivilegedAction; |
|
36 |
import java.security.PrivilegedExceptionAction; |
|
37 |
import java.security.AccessController; |
|
38 |
import java.security.AccessControlContext; |
|
39 |
import java.security.SecureClassLoader; |
|
40 |
import java.security.CodeSource; |
|
41 |
import java.security.Permission; |
|
42 |
import java.security.PermissionCollection; |
|
43 |
import sun.misc.Resource; |
|
44 |
import sun.misc.URLClassPath; |
|
45 |
import sun.net.www.ParseUtil; |
|
46 |
import sun.security.util.SecurityConstants; |
|
47 |
||
48 |
/** |
|
49 |
* This class loader is used to load classes and resources from a search |
|
50 |
* path of URLs referring to both JAR files and directories. Any URL that |
|
51 |
* ends with a '/' is assumed to refer to a directory. Otherwise, the URL |
|
52 |
* is assumed to refer to a JAR file which will be opened as needed. |
|
53 |
* <p> |
|
54 |
* The AccessControlContext of the thread that created the instance of |
|
55 |
* URLClassLoader will be used when subsequently loading classes and |
|
56 |
* resources. |
|
57 |
* <p> |
|
58 |
* The classes that are loaded are by default granted permission only to |
|
59 |
* access the URLs specified when the URLClassLoader was created. |
|
15682
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
10596
diff
changeset
|
60 |
* <p> |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
10596
diff
changeset
|
61 |
* Where a JAR file contains the {@link Name#PROFILE Profile} attribute |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
10596
diff
changeset
|
62 |
* then its value is the name of the Java SE profile that the library |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
10596
diff
changeset
|
63 |
* minimally requires. If this runtime does not support the profile then |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
10596
diff
changeset
|
64 |
* it causes {@link java.util.jar.UnsupportedProfileException} to be |
793a36de151d
8003255: (profiles) Update JAR file specification to support profiles
alanb
parents:
10596
diff
changeset
|
65 |
* thrown at some unspecified time. |
2 | 66 |
* |
67 |
* @author David Connelly |
|
68 |
* @since 1.2 |
|
69 |
*/ |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
70 |
public class URLClassLoader extends SecureClassLoader implements Closeable { |
2 | 71 |
/* The search path for classes and resources */ |
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
72 |
private final URLClassPath ucp; |
2 | 73 |
|
74 |
/* The context to be used when loading classes and resources */ |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
75 |
private final AccessControlContext acc; |
2 | 76 |
|
77 |
/** |
|
78 |
* Constructs a new URLClassLoader for the given URLs. The URLs will be |
|
79 |
* searched in the order specified for classes and resources after first |
|
80 |
* searching in the specified parent class loader. Any URL that ends with |
|
81 |
* a '/' is assumed to refer to a directory. Otherwise, the URL is assumed |
|
82 |
* to refer to a JAR file which will be downloaded and opened as needed. |
|
83 |
* |
|
84 |
* <p>If there is a security manager, this method first |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
85 |
* calls the security manager's {@code checkCreateClassLoader} method |
2 | 86 |
* to ensure creation of a class loader is allowed. |
87 |
* |
|
88 |
* @param urls the URLs from which to load classes and resources |
|
89 |
* @param parent the parent class loader for delegation |
|
90 |
* @exception SecurityException if a security manager exists and its |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
91 |
* {@code checkCreateClassLoader} method doesn't allow |
2 | 92 |
* creation of a class loader. |
93 |
* @see SecurityManager#checkCreateClassLoader |
|
94 |
*/ |
|
95 |
public URLClassLoader(URL[] urls, ClassLoader parent) { |
|
96 |
super(parent); |
|
97 |
// this is to make the stack depth consistent with 1.1 |
|
98 |
SecurityManager security = System.getSecurityManager(); |
|
99 |
if (security != null) { |
|
100 |
security.checkCreateClassLoader(); |
|
101 |
} |
|
102 |
ucp = new URLClassPath(urls); |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
103 |
this.acc = AccessController.getContext(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
104 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
105 |
|
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
106 |
URLClassLoader(URL[] urls, ClassLoader parent, |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
107 |
AccessControlContext acc) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
108 |
super(parent); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
109 |
// this is to make the stack depth consistent with 1.1 |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
110 |
SecurityManager security = System.getSecurityManager(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
111 |
if (security != null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
112 |
security.checkCreateClassLoader(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
113 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
114 |
ucp = new URLClassPath(urls); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
115 |
this.acc = acc; |
2 | 116 |
} |
117 |
||
118 |
/** |
|
119 |
* Constructs a new URLClassLoader for the specified URLs using the |
|
19069 | 120 |
* default delegation parent {@code ClassLoader}. The URLs will |
2 | 121 |
* be searched in the order specified for classes and resources after |
122 |
* first searching in the parent class loader. Any URL that ends with |
|
123 |
* a '/' is assumed to refer to a directory. Otherwise, the URL is |
|
124 |
* assumed to refer to a JAR file which will be downloaded and opened |
|
125 |
* as needed. |
|
126 |
* |
|
127 |
* <p>If there is a security manager, this method first |
|
19069 | 128 |
* calls the security manager's {@code checkCreateClassLoader} method |
2 | 129 |
* to ensure creation of a class loader is allowed. |
130 |
* |
|
131 |
* @param urls the URLs from which to load classes and resources |
|
132 |
* |
|
133 |
* @exception SecurityException if a security manager exists and its |
|
19069 | 134 |
* {@code checkCreateClassLoader} method doesn't allow |
2 | 135 |
* creation of a class loader. |
136 |
* @see SecurityManager#checkCreateClassLoader |
|
137 |
*/ |
|
138 |
public URLClassLoader(URL[] urls) { |
|
139 |
super(); |
|
140 |
// this is to make the stack depth consistent with 1.1 |
|
141 |
SecurityManager security = System.getSecurityManager(); |
|
142 |
if (security != null) { |
|
143 |
security.checkCreateClassLoader(); |
|
144 |
} |
|
145 |
ucp = new URLClassPath(urls); |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
146 |
this.acc = AccessController.getContext(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
147 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
148 |
|
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
149 |
URLClassLoader(URL[] urls, AccessControlContext acc) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
150 |
super(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
151 |
// this is to make the stack depth consistent with 1.1 |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
152 |
SecurityManager security = System.getSecurityManager(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
153 |
if (security != null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
154 |
security.checkCreateClassLoader(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
155 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
156 |
ucp = new URLClassPath(urls); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
157 |
this.acc = acc; |
2 | 158 |
} |
159 |
||
160 |
/** |
|
161 |
* Constructs a new URLClassLoader for the specified URLs, parent |
|
162 |
* class loader, and URLStreamHandlerFactory. The parent argument |
|
163 |
* will be used as the parent class loader for delegation. The |
|
164 |
* factory argument will be used as the stream handler factory to |
|
165 |
* obtain protocol handlers when creating new jar URLs. |
|
166 |
* |
|
167 |
* <p>If there is a security manager, this method first |
|
19069 | 168 |
* calls the security manager's {@code checkCreateClassLoader} method |
2 | 169 |
* to ensure creation of a class loader is allowed. |
170 |
* |
|
171 |
* @param urls the URLs from which to load classes and resources |
|
172 |
* @param parent the parent class loader for delegation |
|
173 |
* @param factory the URLStreamHandlerFactory to use when creating URLs |
|
174 |
* |
|
175 |
* @exception SecurityException if a security manager exists and its |
|
19069 | 176 |
* {@code checkCreateClassLoader} method doesn't allow |
2 | 177 |
* creation of a class loader. |
178 |
* @see SecurityManager#checkCreateClassLoader |
|
179 |
*/ |
|
180 |
public URLClassLoader(URL[] urls, ClassLoader parent, |
|
181 |
URLStreamHandlerFactory factory) { |
|
182 |
super(parent); |
|
183 |
// this is to make the stack depth consistent with 1.1 |
|
184 |
SecurityManager security = System.getSecurityManager(); |
|
185 |
if (security != null) { |
|
186 |
security.checkCreateClassLoader(); |
|
187 |
} |
|
188 |
ucp = new URLClassPath(urls, factory); |
|
189 |
acc = AccessController.getContext(); |
|
190 |
} |
|
191 |
||
7983
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
192 |
/* A map (used as a set) to keep track of closeable local resources |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
193 |
* (either JarFiles or FileInputStreams). We don't care about |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
194 |
* Http resources since they don't need to be closed. |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
195 |
* |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
196 |
* If the resource is coming from a jar file |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
197 |
* we keep a (weak) reference to the JarFile object which can |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
198 |
* be closed if URLClassLoader.close() called. Due to jar file |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
199 |
* caching there will typically be only one JarFile object |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
200 |
* per underlying jar file. |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
201 |
* |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
202 |
* For file resources, which is probably a less common situation |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
203 |
* we have to keep a weak reference to each stream. |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
204 |
*/ |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
205 |
|
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
206 |
private WeakHashMap<Closeable,Void> |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
207 |
closeables = new WeakHashMap<>(); |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
208 |
|
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
209 |
/** |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
210 |
* Returns an input stream for reading the specified resource. |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
211 |
* If this loader is closed, then any resources opened by this method |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
212 |
* will be closed. |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
213 |
* |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
214 |
* <p> The search order is described in the documentation for {@link |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
215 |
* #getResource(String)}. </p> |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
216 |
* |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
217 |
* @param name |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
218 |
* The resource name |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
219 |
* |
19069 | 220 |
* @return An input stream for reading the resource, or {@code null} |
7983
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
221 |
* if the resource could not be found |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
222 |
* |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
223 |
* @since 1.7 |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
224 |
*/ |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
225 |
public InputStream getResourceAsStream(String name) { |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
226 |
URL url = getResource(name); |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
227 |
try { |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
228 |
if (url == null) { |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
229 |
return null; |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
230 |
} |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
231 |
URLConnection urlc = url.openConnection(); |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
232 |
InputStream is = urlc.getInputStream(); |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
233 |
if (urlc instanceof JarURLConnection) { |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
234 |
JarURLConnection juc = (JarURLConnection)urlc; |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
235 |
JarFile jar = juc.getJarFile(); |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
236 |
synchronized (closeables) { |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
237 |
if (!closeables.containsKey(jar)) { |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
238 |
closeables.put(jar, null); |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
239 |
} |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
240 |
} |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
241 |
} else if (urlc instanceof sun.net.www.protocol.file.FileURLConnection) { |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
242 |
synchronized (closeables) { |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
243 |
closeables.put(is, null); |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
244 |
} |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
245 |
} |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
246 |
return is; |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
247 |
} catch (IOException e) { |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
248 |
return null; |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
249 |
} |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
250 |
} |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
251 |
|
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
252 |
/** |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
253 |
* Closes this URLClassLoader, so that it can no longer be used to load |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
254 |
* new classes or resources that are defined by this loader. |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
255 |
* Classes and resources defined by any of this loader's parents in the |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
256 |
* delegation hierarchy are still accessible. Also, any classes or resources |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
257 |
* that are already loaded, are still accessible. |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
258 |
* <p> |
7983
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
259 |
* In the case of jar: and file: URLs, it also closes any files |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
260 |
* that were opened by it. If another thread is loading a |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
261 |
* class when the {@code close} method is invoked, then the result of |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
262 |
* that load is undefined. |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
263 |
* <p> |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
264 |
* The method makes a best effort attempt to close all opened files, |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
265 |
* by catching {@link IOException}s internally. Unchecked exceptions |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
266 |
* and errors are not caught. Calling close on an already closed |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
267 |
* loader has no effect. |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
268 |
* <p> |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
269 |
* @throws IOException if closing any file opened by this class loader |
7983
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
270 |
* resulted in an IOException. Any such exceptions are caught internally. |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
271 |
* If only one is caught, then it is re-thrown. If more than one exception |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
272 |
* is caught, then the second and following exceptions are added |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
273 |
* as suppressed exceptions of the first one caught, which is then re-thrown. |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
274 |
* |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
275 |
* @throws SecurityException if a security manager is set, and it denies |
19069 | 276 |
* {@link RuntimePermission}{@code ("closeClassLoader")} |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
277 |
* |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
278 |
* @since 1.7 |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
279 |
*/ |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
280 |
public void close() throws IOException { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
281 |
SecurityManager security = System.getSecurityManager(); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
282 |
if (security != null) { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
283 |
security.checkPermission(new RuntimePermission("closeClassLoader")); |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
284 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
285 |
List<IOException> errors = ucp.closeLoaders(); |
7983
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
286 |
|
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
287 |
// now close any remaining streams. |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
288 |
|
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
289 |
synchronized (closeables) { |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
290 |
Set<Closeable> keys = closeables.keySet(); |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
291 |
for (Closeable c : keys) { |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
292 |
try { |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
293 |
c.close(); |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
294 |
} catch (IOException ioex) { |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
295 |
errors.add(ioex); |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
296 |
} |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
297 |
} |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
298 |
closeables.clear(); |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
299 |
} |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
300 |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
301 |
if (errors.isEmpty()) { |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
302 |
return; |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
303 |
} |
7983
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
304 |
|
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
305 |
IOException firstex = errors.remove(0); |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
306 |
|
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
307 |
// Suppress any remaining exceptions |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
308 |
|
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
309 |
for (IOException error: errors) { |
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
310 |
firstex.addSuppressed(error); |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
311 |
} |
7983
fb83b663cadc
6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
5506
diff
changeset
|
312 |
throw firstex; |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
313 |
} |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
314 |
|
2 | 315 |
/** |
316 |
* Appends the specified URL to the list of URLs to search for |
|
317 |
* classes and resources. |
|
318 |
* <p> |
|
19069 | 319 |
* If the URL specified is {@code null} or is already in the |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
320 |
* list of URLs, or if this loader is closed, then invoking this |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
321 |
* method has no effect. |
2 | 322 |
* |
323 |
* @param url the URL to be added to the search path of URLs |
|
324 |
*/ |
|
325 |
protected void addURL(URL url) { |
|
326 |
ucp.addURL(url); |
|
327 |
} |
|
328 |
||
329 |
/** |
|
330 |
* Returns the search path of URLs for loading classes and resources. |
|
331 |
* This includes the original list of URLs specified to the constructor, |
|
332 |
* along with any URLs subsequently appended by the addURL() method. |
|
333 |
* @return the search path of URLs for loading classes and resources. |
|
334 |
*/ |
|
335 |
public URL[] getURLs() { |
|
336 |
return ucp.getURLs(); |
|
337 |
} |
|
338 |
||
339 |
/** |
|
340 |
* Finds and loads the class with the specified name from the URL search |
|
341 |
* path. Any URLs referring to JAR files are loaded and opened as needed |
|
342 |
* until the class is found. |
|
343 |
* |
|
344 |
* @param name the name of the class |
|
345 |
* @return the resulting class |
|
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
346 |
* @exception ClassNotFoundException if the class could not be found, |
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
347 |
* or if the loader is closed. |
2 | 348 |
*/ |
349 |
protected Class<?> findClass(final String name) |
|
350 |
throws ClassNotFoundException |
|
351 |
{ |
|
352 |
try { |
|
51 | 353 |
return AccessController.doPrivileged( |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
9035
diff
changeset
|
354 |
new PrivilegedExceptionAction<Class<?>>() { |
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
9035
diff
changeset
|
355 |
public Class<?> run() throws ClassNotFoundException { |
2 | 356 |
String path = name.replace('.', '/').concat(".class"); |
357 |
Resource res = ucp.getResource(path, false); |
|
358 |
if (res != null) { |
|
359 |
try { |
|
360 |
return defineClass(name, res); |
|
361 |
} catch (IOException e) { |
|
362 |
throw new ClassNotFoundException(name, e); |
|
363 |
} |
|
364 |
} else { |
|
365 |
throw new ClassNotFoundException(name); |
|
366 |
} |
|
367 |
} |
|
368 |
}, acc); |
|
369 |
} catch (java.security.PrivilegedActionException pae) { |
|
370 |
throw (ClassNotFoundException) pae.getException(); |
|
371 |
} |
|
372 |
} |
|
373 |
||
374 |
/* |
|
3228
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
375 |
* Retrieve the package using the specified package name. |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
376 |
* If non-null, verify the package using the specified code |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
377 |
* source and manifest. |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
378 |
*/ |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
379 |
private Package getAndVerifyPackage(String pkgname, |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
380 |
Manifest man, URL url) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
381 |
Package pkg = getPackage(pkgname); |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
382 |
if (pkg != null) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
383 |
// Package found, so check package sealing. |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
384 |
if (pkg.isSealed()) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
385 |
// Verify that code source URL is the same. |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
386 |
if (!pkg.isSealed(url)) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
387 |
throw new SecurityException( |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
388 |
"sealing violation: package " + pkgname + " is sealed"); |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
389 |
} |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
390 |
} else { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
391 |
// Make sure we are not attempting to seal the package |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
392 |
// at this code source URL. |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
393 |
if ((man != null) && isSealed(pkgname, man)) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
394 |
throw new SecurityException( |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
395 |
"sealing violation: can't seal package " + pkgname + |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
396 |
": already loaded"); |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
397 |
} |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
398 |
} |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
399 |
} |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
400 |
return pkg; |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
401 |
} |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
402 |
|
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
403 |
/* |
2 | 404 |
* Defines a Class using the class bytes obtained from the specified |
405 |
* Resource. The resulting Class must be resolved before it can be |
|
406 |
* used. |
|
407 |
*/ |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
9035
diff
changeset
|
408 |
private Class<?> defineClass(String name, Resource res) throws IOException { |
3849 | 409 |
long t0 = System.nanoTime(); |
2 | 410 |
int i = name.lastIndexOf('.'); |
411 |
URL url = res.getCodeSourceURL(); |
|
412 |
if (i != -1) { |
|
413 |
String pkgname = name.substring(0, i); |
|
414 |
// Check if package already loaded. |
|
415 |
Manifest man = res.getManifest(); |
|
3228
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
416 |
if (getAndVerifyPackage(pkgname, man, url) == null) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
417 |
try { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
418 |
if (man != null) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
419 |
definePackage(pkgname, man, url); |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
420 |
} else { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
421 |
definePackage(pkgname, null, null, null, null, null, null, null); |
2 | 422 |
} |
3228
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
423 |
} catch (IllegalArgumentException iae) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
424 |
// parallel-capable class loaders: re-verify in case of a |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
425 |
// race condition |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
426 |
if (getAndVerifyPackage(pkgname, man, url) == null) { |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
427 |
// Should never happen |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
428 |
throw new AssertionError("Cannot find package " + |
d7c00dbea781
6832540: IllegalArgumentException in ClassLoader.definePackage when classes are loaded in parallel
valeriep
parents:
2448
diff
changeset
|
429 |
pkgname); |
2 | 430 |
} |
431 |
} |
|
432 |
} |
|
433 |
} |
|
434 |
// Now read the class bytes and define the class |
|
435 |
java.nio.ByteBuffer bb = res.getByteBuffer(); |
|
436 |
if (bb != null) { |
|
437 |
// Use (direct) ByteBuffer: |
|
438 |
CodeSigner[] signers = res.getCodeSigners(); |
|
439 |
CodeSource cs = new CodeSource(url, signers); |
|
3849 | 440 |
sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0); |
2 | 441 |
return defineClass(name, bb, cs); |
442 |
} else { |
|
443 |
byte[] b = res.getBytes(); |
|
444 |
// must read certificates AFTER reading bytes. |
|
445 |
CodeSigner[] signers = res.getCodeSigners(); |
|
446 |
CodeSource cs = new CodeSource(url, signers); |
|
3849 | 447 |
sun.misc.PerfCounter.getReadClassBytesTime().addElapsedTimeFrom(t0); |
2 | 448 |
return defineClass(name, b, 0, b.length, cs); |
449 |
} |
|
450 |
} |
|
451 |
||
452 |
/** |
|
453 |
* Defines a new package by name in this ClassLoader. The attributes |
|
454 |
* contained in the specified Manifest will be used to obtain package |
|
455 |
* version and sealing information. For sealed packages, the additional |
|
456 |
* URL specifies the code source URL from which the package was loaded. |
|
457 |
* |
|
458 |
* @param name the package name |
|
459 |
* @param man the Manifest containing package version and sealing |
|
460 |
* information |
|
461 |
* @param url the code source url for the package, or null if none |
|
462 |
* @exception IllegalArgumentException if the package name duplicates |
|
463 |
* an existing package either in this class loader or one |
|
464 |
* of its ancestors |
|
465 |
* @return the newly defined Package object |
|
466 |
*/ |
|
467 |
protected Package definePackage(String name, Manifest man, URL url) |
|
468 |
throws IllegalArgumentException |
|
469 |
{ |
|
470 |
String path = name.replace('.', '/').concat("/"); |
|
471 |
String specTitle = null, specVersion = null, specVendor = null; |
|
472 |
String implTitle = null, implVersion = null, implVendor = null; |
|
473 |
String sealed = null; |
|
474 |
URL sealBase = null; |
|
475 |
||
476 |
Attributes attr = man.getAttributes(path); |
|
477 |
if (attr != null) { |
|
478 |
specTitle = attr.getValue(Name.SPECIFICATION_TITLE); |
|
479 |
specVersion = attr.getValue(Name.SPECIFICATION_VERSION); |
|
480 |
specVendor = attr.getValue(Name.SPECIFICATION_VENDOR); |
|
481 |
implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE); |
|
482 |
implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION); |
|
483 |
implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR); |
|
484 |
sealed = attr.getValue(Name.SEALED); |
|
485 |
} |
|
486 |
attr = man.getMainAttributes(); |
|
487 |
if (attr != null) { |
|
488 |
if (specTitle == null) { |
|
489 |
specTitle = attr.getValue(Name.SPECIFICATION_TITLE); |
|
490 |
} |
|
491 |
if (specVersion == null) { |
|
492 |
specVersion = attr.getValue(Name.SPECIFICATION_VERSION); |
|
493 |
} |
|
494 |
if (specVendor == null) { |
|
495 |
specVendor = attr.getValue(Name.SPECIFICATION_VENDOR); |
|
496 |
} |
|
497 |
if (implTitle == null) { |
|
498 |
implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE); |
|
499 |
} |
|
500 |
if (implVersion == null) { |
|
501 |
implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION); |
|
502 |
} |
|
503 |
if (implVendor == null) { |
|
504 |
implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR); |
|
505 |
} |
|
506 |
if (sealed == null) { |
|
507 |
sealed = attr.getValue(Name.SEALED); |
|
508 |
} |
|
509 |
} |
|
510 |
if ("true".equalsIgnoreCase(sealed)) { |
|
511 |
sealBase = url; |
|
512 |
} |
|
513 |
return definePackage(name, specTitle, specVersion, specVendor, |
|
514 |
implTitle, implVersion, implVendor, sealBase); |
|
515 |
} |
|
516 |
||
517 |
/* |
|
518 |
* Returns true if the specified package name is sealed according to the |
|
519 |
* given manifest. |
|
520 |
*/ |
|
521 |
private boolean isSealed(String name, Manifest man) { |
|
522 |
String path = name.replace('.', '/').concat("/"); |
|
523 |
Attributes attr = man.getAttributes(path); |
|
524 |
String sealed = null; |
|
525 |
if (attr != null) { |
|
526 |
sealed = attr.getValue(Name.SEALED); |
|
527 |
} |
|
528 |
if (sealed == null) { |
|
529 |
if ((attr = man.getMainAttributes()) != null) { |
|
530 |
sealed = attr.getValue(Name.SEALED); |
|
531 |
} |
|
532 |
} |
|
533 |
return "true".equalsIgnoreCase(sealed); |
|
534 |
} |
|
535 |
||
536 |
/** |
|
537 |
* Finds the resource with the specified name on the URL search path. |
|
538 |
* |
|
539 |
* @param name the name of the resource |
|
19069 | 540 |
* @return a {@code URL} for the resource, or {@code null} |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
541 |
* if the resource could not be found, or if the loader is closed. |
2 | 542 |
*/ |
543 |
public URL findResource(final String name) { |
|
544 |
/* |
|
545 |
* The same restriction to finding classes applies to resources |
|
546 |
*/ |
|
51 | 547 |
URL url = AccessController.doPrivileged( |
548 |
new PrivilegedAction<URL>() { |
|
549 |
public URL run() { |
|
2 | 550 |
return ucp.findResource(name, true); |
551 |
} |
|
552 |
}, acc); |
|
553 |
||
554 |
return url != null ? ucp.checkURL(url) : null; |
|
555 |
} |
|
556 |
||
557 |
/** |
|
558 |
* Returns an Enumeration of URLs representing all of the resources |
|
559 |
* on the URL search path having the specified name. |
|
560 |
* |
|
561 |
* @param name the resource name |
|
562 |
* @exception IOException if an I/O exception occurs |
|
19069 | 563 |
* @return an {@code Enumeration} of {@code URL}s |
1940
e81514210873
4167874: URL-downloaded jar files can consume all available file descriptors
michaelm
parents:
715
diff
changeset
|
564 |
* If the loader is closed, the Enumeration will be empty. |
2 | 565 |
*/ |
566 |
public Enumeration<URL> findResources(final String name) |
|
567 |
throws IOException |
|
568 |
{ |
|
51 | 569 |
final Enumeration<URL> e = ucp.findResources(name, true); |
2 | 570 |
|
571 |
return new Enumeration<URL>() { |
|
572 |
private URL url = null; |
|
573 |
||
574 |
private boolean next() { |
|
575 |
if (url != null) { |
|
576 |
return true; |
|
577 |
} |
|
578 |
do { |
|
51 | 579 |
URL u = AccessController.doPrivileged( |
580 |
new PrivilegedAction<URL>() { |
|
581 |
public URL run() { |
|
2 | 582 |
if (!e.hasMoreElements()) |
583 |
return null; |
|
584 |
return e.nextElement(); |
|
585 |
} |
|
586 |
}, acc); |
|
587 |
if (u == null) |
|
588 |
break; |
|
589 |
url = ucp.checkURL(u); |
|
590 |
} while (url == null); |
|
591 |
return url != null; |
|
592 |
} |
|
593 |
||
594 |
public URL nextElement() { |
|
595 |
if (!next()) { |
|
596 |
throw new NoSuchElementException(); |
|
597 |
} |
|
598 |
URL u = url; |
|
599 |
url = null; |
|
600 |
return u; |
|
601 |
} |
|
602 |
||
603 |
public boolean hasMoreElements() { |
|
604 |
return next(); |
|
605 |
} |
|
606 |
}; |
|
607 |
} |
|
608 |
||
609 |
/** |
|
610 |
* Returns the permissions for the given codesource object. |
|
611 |
* The implementation of this method first calls super.getPermissions |
|
612 |
* and then adds permissions based on the URL of the codesource. |
|
613 |
* <p> |
|
614 |
* If the protocol of this URL is "jar", then the permission granted |
|
615 |
* is based on the permission that is required by the URL of the Jar |
|
616 |
* file. |
|
617 |
* <p> |
|
618 |
* If the protocol is "file" and there is an authority component, then |
|
619 |
* permission to connect to and accept connections from that authority |
|
620 |
* may be granted. If the protocol is "file" |
|
621 |
* and the path specifies a file, then permission to read that |
|
622 |
* file is granted. If protocol is "file" and the path is |
|
623 |
* a directory, permission is granted to read all files |
|
624 |
* and (recursively) all files and subdirectories contained in |
|
625 |
* that directory. |
|
626 |
* <p> |
|
627 |
* If the protocol is not "file", then permission |
|
628 |
* to connect to and accept connections from the URL's host is granted. |
|
629 |
* @param codesource the codesource |
|
630 |
* @return the permissions granted to the codesource |
|
631 |
*/ |
|
632 |
protected PermissionCollection getPermissions(CodeSource codesource) |
|
633 |
{ |
|
634 |
PermissionCollection perms = super.getPermissions(codesource); |
|
635 |
||
636 |
URL url = codesource.getLocation(); |
|
637 |
||
638 |
Permission p; |
|
639 |
URLConnection urlConnection; |
|
640 |
||
641 |
try { |
|
642 |
urlConnection = url.openConnection(); |
|
643 |
p = urlConnection.getPermission(); |
|
644 |
} catch (java.io.IOException ioe) { |
|
645 |
p = null; |
|
646 |
urlConnection = null; |
|
647 |
} |
|
648 |
||
649 |
if (p instanceof FilePermission) { |
|
650 |
// if the permission has a separator char on the end, |
|
651 |
// it means the codebase is a directory, and we need |
|
652 |
// to add an additional permission to read recursively |
|
653 |
String path = p.getName(); |
|
654 |
if (path.endsWith(File.separator)) { |
|
655 |
path += "-"; |
|
656 |
p = new FilePermission(path, SecurityConstants.FILE_READ_ACTION); |
|
657 |
} |
|
658 |
} else if ((p == null) && (url.getProtocol().equals("file"))) { |
|
659 |
String path = url.getFile().replace('/', File.separatorChar); |
|
660 |
path = ParseUtil.decode(path); |
|
661 |
if (path.endsWith(File.separator)) |
|
662 |
path += "-"; |
|
663 |
p = new FilePermission(path, SecurityConstants.FILE_READ_ACTION); |
|
664 |
} else { |
|
665 |
/** |
|
666 |
* Not loading from a 'file:' URL so we want to give the class |
|
667 |
* permission to connect to and accept from the remote host |
|
668 |
* after we've made sure the host is the correct one and is valid. |
|
669 |
*/ |
|
670 |
URL locUrl = url; |
|
671 |
if (urlConnection instanceof JarURLConnection) { |
|
672 |
locUrl = ((JarURLConnection)urlConnection).getJarFileURL(); |
|
673 |
} |
|
674 |
String host = locUrl.getHost(); |
|
675 |
if (host != null && (host.length() > 0)) |
|
676 |
p = new SocketPermission(host, |
|
677 |
SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION); |
|
678 |
} |
|
679 |
||
680 |
// make sure the person that created this class loader |
|
681 |
// would have this permission |
|
682 |
||
683 |
if (p != null) { |
|
684 |
final SecurityManager sm = System.getSecurityManager(); |
|
685 |
if (sm != null) { |
|
686 |
final Permission fp = p; |
|
51 | 687 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
688 |
public Void run() throws SecurityException { |
|
2 | 689 |
sm.checkPermission(fp); |
690 |
return null; |
|
691 |
} |
|
692 |
}, acc); |
|
693 |
} |
|
694 |
perms.add(p); |
|
695 |
} |
|
696 |
return perms; |
|
697 |
} |
|
698 |
||
699 |
/** |
|
700 |
* Creates a new instance of URLClassLoader for the specified |
|
701 |
* URLs and parent class loader. If a security manager is |
|
19069 | 702 |
* installed, the {@code loadClass} method of the URLClassLoader |
2 | 703 |
* returned by this method will invoke the |
19069 | 704 |
* {@code SecurityManager.checkPackageAccess} method before |
2 | 705 |
* loading the class. |
706 |
* |
|
707 |
* @param urls the URLs to search for classes and resources |
|
708 |
* @param parent the parent class loader for delegation |
|
709 |
* @return the resulting class loader |
|
710 |
*/ |
|
711 |
public static URLClassLoader newInstance(final URL[] urls, |
|
712 |
final ClassLoader parent) { |
|
713 |
// Save the caller's context |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
714 |
final AccessControlContext acc = AccessController.getContext(); |
2 | 715 |
// Need a privileged block to create the class loader |
51 | 716 |
URLClassLoader ucl = AccessController.doPrivileged( |
717 |
new PrivilegedAction<URLClassLoader>() { |
|
718 |
public URLClassLoader run() { |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
719 |
return new FactoryURLClassLoader(urls, parent, acc); |
2 | 720 |
} |
721 |
}); |
|
722 |
return ucl; |
|
723 |
} |
|
724 |
||
725 |
/** |
|
726 |
* Creates a new instance of URLClassLoader for the specified |
|
727 |
* URLs and default parent class loader. If a security manager is |
|
19069 | 728 |
* installed, the {@code loadClass} method of the URLClassLoader |
2 | 729 |
* returned by this method will invoke the |
19069 | 730 |
* {@code SecurityManager.checkPackageAccess} before |
2 | 731 |
* loading the class. |
732 |
* |
|
733 |
* @param urls the URLs to search for classes and resources |
|
734 |
* @return the resulting class loader |
|
735 |
*/ |
|
736 |
public static URLClassLoader newInstance(final URL[] urls) { |
|
737 |
// Save the caller's context |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
738 |
final AccessControlContext acc = AccessController.getContext(); |
2 | 739 |
// Need a privileged block to create the class loader |
51 | 740 |
URLClassLoader ucl = AccessController.doPrivileged( |
741 |
new PrivilegedAction<URLClassLoader>() { |
|
742 |
public URLClassLoader run() { |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
743 |
return new FactoryURLClassLoader(urls, acc); |
2 | 744 |
} |
745 |
}); |
|
746 |
return ucl; |
|
747 |
} |
|
748 |
||
749 |
static { |
|
750 |
sun.misc.SharedSecrets.setJavaNetAccess ( |
|
751 |
new sun.misc.JavaNetAccess() { |
|
752 |
public URLClassPath getURLClassPath (URLClassLoader u) { |
|
753 |
return u.ucp; |
|
754 |
} |
|
755 |
} |
|
756 |
); |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
757 |
ClassLoader.registerAsParallelCapable(); |
2 | 758 |
} |
759 |
} |
|
760 |
||
761 |
final class FactoryURLClassLoader extends URLClassLoader { |
|
762 |
||
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
763 |
static { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
764 |
ClassLoader.registerAsParallelCapable(); |
2 | 765 |
} |
766 |
||
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
767 |
FactoryURLClassLoader(URL[] urls, ClassLoader parent, |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
768 |
AccessControlContext acc) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
769 |
super(urls, parent, acc); |
2 | 770 |
} |
771 |
||
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
772 |
FactoryURLClassLoader(URL[] urls, AccessControlContext acc) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
773 |
super(urls, acc); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
774 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
1940
diff
changeset
|
775 |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
9035
diff
changeset
|
776 |
public final Class<?> loadClass(String name, boolean resolve) |
2 | 777 |
throws ClassNotFoundException |
778 |
{ |
|
779 |
// First check if we have permission to access the package. This |
|
780 |
// should go away once we've added support for exported packages. |
|
781 |
SecurityManager sm = System.getSecurityManager(); |
|
782 |
if (sm != null) { |
|
783 |
int i = name.lastIndexOf('.'); |
|
784 |
if (i != -1) { |
|
785 |
sm.checkPackageAccess(name.substring(0, i)); |
|
786 |
} |
|
787 |
} |
|
788 |
return super.loadClass(name, resolve); |
|
789 |
} |
|
790 |
} |