author | herrick |
Fri, 12 Jun 2009 14:56:32 -0400 | |
changeset 3111 | fefdeafb7ab9 |
parent 2448 | 1e8128f3ff61 |
child 3958 | b8acd5ee4f4f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
2 |
* Copyright 1998-2009 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.misc; |
|
27 |
||
28 |
import java.io.File; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.FilePermission; |
|
31 |
import java.net.URL; |
|
32 |
import java.net.URLClassLoader; |
|
33 |
import java.net.MalformedURLException; |
|
34 |
import java.net.URLStreamHandler; |
|
35 |
import java.net.URLStreamHandlerFactory; |
|
36 |
import java.util.HashSet; |
|
37 |
import java.util.StringTokenizer; |
|
38 |
import java.util.Set; |
|
39 |
import java.util.Vector; |
|
40 |
import java.security.AccessController; |
|
3111
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
41 |
import java.security.AllPermission; |
2 | 42 |
import java.security.PrivilegedAction; |
43 |
import java.security.PrivilegedExceptionAction; |
|
44 |
import java.security.AccessControlContext; |
|
45 |
import java.security.PermissionCollection; |
|
46 |
import java.security.Permissions; |
|
47 |
import java.security.Permission; |
|
48 |
import java.security.ProtectionDomain; |
|
49 |
import java.security.CodeSource; |
|
50 |
import sun.security.action.GetPropertyAction; |
|
51 |
import sun.security.util.SecurityConstants; |
|
52 |
import sun.net.www.ParseUtil; |
|
3111
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
53 |
import sun.jkernel.Bundle; |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
54 |
import sun.jkernel.DownloadManager; |
2 | 55 |
|
56 |
/** |
|
57 |
* This class is used by the system to launch the main application. |
|
58 |
Launcher */ |
|
59 |
public class Launcher { |
|
60 |
private static URLStreamHandlerFactory factory = new Factory(); |
|
61 |
private static Launcher launcher = new Launcher(); |
|
62 |
||
63 |
public static Launcher getLauncher() { |
|
64 |
return launcher; |
|
65 |
} |
|
66 |
||
67 |
private ClassLoader loader; |
|
68 |
||
69 |
public Launcher() { |
|
70 |
// Create the extension class loader |
|
71 |
ClassLoader extcl; |
|
72 |
try { |
|
73 |
extcl = ExtClassLoader.getExtClassLoader(); |
|
74 |
} catch (IOException e) { |
|
75 |
throw new InternalError( |
|
76 |
"Could not create extension class loader"); |
|
77 |
} |
|
78 |
||
79 |
// Now create the class loader to use to launch the application |
|
80 |
try { |
|
81 |
loader = AppClassLoader.getAppClassLoader(extcl); |
|
82 |
} catch (IOException e) { |
|
83 |
throw new InternalError( |
|
84 |
"Could not create application class loader"); |
|
85 |
} |
|
86 |
||
87 |
// Also set the context class loader for the primordial thread. |
|
88 |
Thread.currentThread().setContextClassLoader(loader); |
|
89 |
||
90 |
// Finally, install a security manager if requested |
|
91 |
String s = System.getProperty("java.security.manager"); |
|
92 |
if (s != null) { |
|
93 |
SecurityManager sm = null; |
|
94 |
if ("".equals(s) || "default".equals(s)) { |
|
95 |
sm = new java.lang.SecurityManager(); |
|
96 |
} else { |
|
97 |
try { |
|
98 |
sm = (SecurityManager)loader.loadClass(s).newInstance(); |
|
99 |
} catch (IllegalAccessException e) { |
|
100 |
} catch (InstantiationException e) { |
|
101 |
} catch (ClassNotFoundException e) { |
|
102 |
} catch (ClassCastException e) { |
|
103 |
} |
|
104 |
} |
|
105 |
if (sm != null) { |
|
106 |
System.setSecurityManager(sm); |
|
107 |
} else { |
|
108 |
throw new InternalError( |
|
109 |
"Could not create SecurityManager: " + s); |
|
110 |
} |
|
111 |
} |
|
112 |
} |
|
113 |
||
114 |
/* |
|
115 |
* Returns the class loader used to launch the main application. |
|
116 |
*/ |
|
117 |
public ClassLoader getClassLoader() { |
|
118 |
return loader; |
|
119 |
} |
|
120 |
||
3111
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
121 |
public static void addURLToAppClassLoader(URL u) { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
122 |
AccessController.checkPermission(new AllPermission()); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
123 |
ClassLoader loader = Launcher.getLauncher().getClassLoader(); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
124 |
((Launcher.AppClassLoader) loader).addAppURL(u); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
125 |
} |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
126 |
|
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
127 |
public static void addURLToExtClassLoader(URL u) { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
128 |
AccessController.checkPermission(new AllPermission()); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
129 |
ClassLoader loader = Launcher.getLauncher().getClassLoader(); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
130 |
((Launcher.ExtClassLoader) loader.getParent()).addExtURL(u); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
131 |
} |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
132 |
|
2 | 133 |
/* |
134 |
* The class loader used for loading installed extensions. |
|
135 |
*/ |
|
136 |
static class ExtClassLoader extends URLClassLoader { |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
137 |
|
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
138 |
static { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
139 |
ClassLoader.registerAsParallelCapable(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
140 |
} |
2 | 141 |
|
142 |
/** |
|
143 |
* create an ExtClassLoader. The ExtClassLoader is created |
|
144 |
* within a context that limits which files it can read |
|
145 |
*/ |
|
146 |
public static ExtClassLoader getExtClassLoader() throws IOException |
|
147 |
{ |
|
148 |
final File[] dirs = getExtDirs(); |
|
149 |
||
150 |
try { |
|
151 |
// Prior implementations of this doPrivileged() block supplied |
|
152 |
// aa synthesized ACC via a call to the private method |
|
153 |
// ExtClassLoader.getContext(). |
|
154 |
||
51 | 155 |
return AccessController.doPrivileged( |
156 |
new PrivilegedExceptionAction<ExtClassLoader>() { |
|
157 |
public ExtClassLoader run() throws IOException { |
|
2 | 158 |
int len = dirs.length; |
159 |
for (int i = 0; i < len; i++) { |
|
160 |
MetaIndex.registerDirectory(dirs[i]); |
|
161 |
} |
|
162 |
return new ExtClassLoader(dirs); |
|
163 |
} |
|
164 |
}); |
|
165 |
} catch (java.security.PrivilegedActionException e) { |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
166 |
throw (IOException) e.getException(); |
2 | 167 |
} |
168 |
} |
|
169 |
||
170 |
void addExtURL(URL url) { |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
171 |
super.addURL(url); |
2 | 172 |
} |
173 |
||
174 |
/* |
|
175 |
* Creates a new ExtClassLoader for the specified directories. |
|
176 |
*/ |
|
177 |
public ExtClassLoader(File[] dirs) throws IOException { |
|
178 |
super(getExtURLs(dirs), null, factory); |
|
179 |
} |
|
180 |
||
181 |
private static File[] getExtDirs() { |
|
182 |
String s = System.getProperty("java.ext.dirs"); |
|
183 |
File[] dirs; |
|
184 |
if (s != null) { |
|
185 |
StringTokenizer st = |
|
186 |
new StringTokenizer(s, File.pathSeparator); |
|
187 |
int count = st.countTokens(); |
|
188 |
dirs = new File[count]; |
|
189 |
for (int i = 0; i < count; i++) { |
|
190 |
dirs[i] = new File(st.nextToken()); |
|
191 |
} |
|
192 |
} else { |
|
193 |
dirs = new File[0]; |
|
194 |
} |
|
195 |
return dirs; |
|
196 |
} |
|
197 |
||
198 |
private static URL[] getExtURLs(File[] dirs) throws IOException { |
|
51 | 199 |
Vector<URL> urls = new Vector<URL>(); |
2 | 200 |
for (int i = 0; i < dirs.length; i++) { |
201 |
String[] files = dirs[i].list(); |
|
202 |
if (files != null) { |
|
203 |
for (int j = 0; j < files.length; j++) { |
|
204 |
if (!files[j].equals("meta-index")) { |
|
205 |
File f = new File(dirs[i], files[j]); |
|
206 |
urls.add(getFileURL(f)); |
|
207 |
} |
|
208 |
} |
|
209 |
} |
|
210 |
} |
|
211 |
URL[] ua = new URL[urls.size()]; |
|
212 |
urls.copyInto(ua); |
|
213 |
return ua; |
|
214 |
} |
|
215 |
||
216 |
/* |
|
217 |
* Searches the installed extension directories for the specified |
|
218 |
* library name. For each extension directory, we first look for |
|
219 |
* the native library in the subdirectory whose name is the value |
|
220 |
* of the system property <code>os.arch</code>. Failing that, we |
|
221 |
* look in the extension directory itself. |
|
222 |
*/ |
|
223 |
public String findLibrary(String name) { |
|
224 |
name = System.mapLibraryName(name); |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
225 |
URL[] urls = super.getURLs(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
226 |
File prevDir = null; |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
227 |
for (int i = 0; i < urls.length; i++) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
228 |
// Get the ext directory from the URL |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
229 |
File dir = new File(urls[i].getPath()).getParentFile(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
230 |
if (dir != null && !dir.equals(prevDir)) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
231 |
// Look in architecture-specific subdirectory first |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
232 |
String arch = System.getProperty("os.arch"); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
233 |
if (arch != null) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
234 |
File file = new File(new File(dir, arch), name); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
235 |
if (file.exists()) { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
236 |
return file.getAbsolutePath(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
237 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
238 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
239 |
// Then check the extension directory |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
240 |
File file = new File(dir, name); |
2 | 241 |
if (file.exists()) { |
242 |
return file.getAbsolutePath(); |
|
243 |
} |
|
244 |
} |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
245 |
prevDir = dir; |
2 | 246 |
} |
247 |
return null; |
|
248 |
} |
|
249 |
||
3111
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
250 |
protected Class findClass(String name) throws ClassNotFoundException { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
251 |
if (VM.isBootedKernelVM()) { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
252 |
// Check for download before we look for it. If |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
253 |
// DownloadManager ends up downloading it, it will add it to |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
254 |
// our search path before we proceed to the findClass(). |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
255 |
DownloadManager.getBootClassPathEntryForClass(name); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
256 |
} |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
257 |
return super.findClass(name); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
258 |
} |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
259 |
|
2 | 260 |
private static AccessControlContext getContext(File[] dirs) |
261 |
throws IOException |
|
262 |
{ |
|
263 |
PathPermissions perms = |
|
264 |
new PathPermissions(dirs); |
|
265 |
||
266 |
ProtectionDomain domain = new ProtectionDomain( |
|
267 |
new CodeSource(perms.getCodeBase(), |
|
268 |
(java.security.cert.Certificate[]) null), |
|
269 |
perms); |
|
270 |
||
271 |
AccessControlContext acc = |
|
272 |
new AccessControlContext(new ProtectionDomain[] { domain }); |
|
273 |
||
274 |
return acc; |
|
275 |
} |
|
276 |
} |
|
277 |
||
278 |
/** |
|
279 |
* The class loader used for loading from java.class.path. |
|
280 |
* runs in a restricted security context. |
|
281 |
*/ |
|
282 |
static class AppClassLoader extends URLClassLoader { |
|
283 |
||
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
284 |
static { |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
285 |
ClassLoader.registerAsParallelCapable(); |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
286 |
} |
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
287 |
|
2 | 288 |
public static ClassLoader getAppClassLoader(final ClassLoader extcl) |
289 |
throws IOException |
|
290 |
{ |
|
291 |
final String s = System.getProperty("java.class.path"); |
|
292 |
final File[] path = (s == null) ? new File[0] : getClassPath(s); |
|
293 |
||
294 |
// Note: on bugid 4256530 |
|
295 |
// Prior implementations of this doPrivileged() block supplied |
|
296 |
// a rather restrictive ACC via a call to the private method |
|
297 |
// AppClassLoader.getContext(). This proved overly restrictive |
|
298 |
// when loading classes. Specifically it prevent |
|
299 |
// accessClassInPackage.sun.* grants from being honored. |
|
300 |
// |
|
51 | 301 |
return AccessController.doPrivileged( |
302 |
new PrivilegedAction<AppClassLoader>() { |
|
303 |
public AppClassLoader run() { |
|
2 | 304 |
URL[] urls = |
305 |
(s == null) ? new URL[0] : pathToURLs(path); |
|
306 |
return new AppClassLoader(urls, extcl); |
|
307 |
} |
|
308 |
}); |
|
309 |
} |
|
310 |
||
311 |
/* |
|
312 |
* Creates a new AppClassLoader |
|
313 |
*/ |
|
314 |
AppClassLoader(URL[] urls, ClassLoader parent) { |
|
315 |
super(urls, parent, factory); |
|
316 |
} |
|
317 |
||
318 |
/** |
|
319 |
* Override loadClass so we can checkPackageAccess. |
|
320 |
*/ |
|
2448
1e8128f3ff61
4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents:
715
diff
changeset
|
321 |
public Class loadClass(String name, boolean resolve) |
2 | 322 |
throws ClassNotFoundException |
323 |
{ |
|
3111
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
324 |
if (VM.isBootedKernelVM()) { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
325 |
DownloadManager.getBootClassPathEntryForClass(name); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
326 |
} |
2 | 327 |
int i = name.lastIndexOf('.'); |
328 |
if (i != -1) { |
|
329 |
SecurityManager sm = System.getSecurityManager(); |
|
330 |
if (sm != null) { |
|
331 |
sm.checkPackageAccess(name.substring(0, i)); |
|
332 |
} |
|
333 |
} |
|
334 |
return (super.loadClass(name, resolve)); |
|
335 |
} |
|
336 |
||
337 |
/** |
|
338 |
* allow any classes loaded from classpath to exit the VM. |
|
339 |
*/ |
|
340 |
protected PermissionCollection getPermissions(CodeSource codesource) |
|
341 |
{ |
|
342 |
PermissionCollection perms = super.getPermissions(codesource); |
|
343 |
perms.add(new RuntimePermission("exitVM")); |
|
344 |
return perms; |
|
345 |
} |
|
346 |
||
347 |
/** |
|
348 |
* This class loader supports dynamic additions to the class path |
|
349 |
* at runtime. |
|
350 |
* |
|
351 |
* @see java.lang.instrument.Instrumentation#appendToSystemClassPathSearch |
|
352 |
*/ |
|
353 |
private void appendToClassPathForInstrumentation(String path) { |
|
354 |
assert(Thread.holdsLock(this)); |
|
355 |
||
356 |
// addURL is a no-op if path already contains the URL |
|
357 |
super.addURL( getFileURL(new File(path)) ); |
|
358 |
} |
|
359 |
||
360 |
/** |
|
361 |
* create a context that can read any directories (recursively) |
|
362 |
* mentioned in the class path. In the case of a jar, it has to |
|
363 |
* be the directory containing the jar, not just the jar, as jar |
|
364 |
* files might refer to other jar files. |
|
365 |
*/ |
|
366 |
||
367 |
private static AccessControlContext getContext(File[] cp) |
|
368 |
throws java.net.MalformedURLException |
|
369 |
{ |
|
370 |
PathPermissions perms = |
|
371 |
new PathPermissions(cp); |
|
372 |
||
373 |
ProtectionDomain domain = |
|
374 |
new ProtectionDomain(new CodeSource(perms.getCodeBase(), |
|
375 |
(java.security.cert.Certificate[]) null), |
|
376 |
perms); |
|
377 |
||
378 |
AccessControlContext acc = |
|
379 |
new AccessControlContext(new ProtectionDomain[] { domain }); |
|
380 |
||
381 |
return acc; |
|
382 |
} |
|
3111
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
383 |
|
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
384 |
void addAppURL(URL url) { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
385 |
super.addURL(url); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
386 |
} |
2 | 387 |
} |
388 |
||
3111
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
389 |
private static URLClassPath bootstrapClassPath; |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
390 |
|
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
391 |
public static synchronized URLClassPath getBootstrapClassPath() { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
392 |
if (bootstrapClassPath == null) { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
393 |
String prop = AccessController.doPrivileged( |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
394 |
new GetPropertyAction("sun.boot.class.path")); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
395 |
URL[] urls; |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
396 |
if (prop != null) { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
397 |
final String path = prop; |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
398 |
urls = AccessController.doPrivileged( |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
399 |
new PrivilegedAction<URL[]>() { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
400 |
public URL[] run() { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
401 |
File[] classPath = getClassPath(path); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
402 |
int len = classPath.length; |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
403 |
Set<File> seenDirs = new HashSet<File>(); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
404 |
for (int i = 0; i < len; i++) { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
405 |
File curEntry = classPath[i]; |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
406 |
// Negative test used to properly handle |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
407 |
// nonexistent jars on boot class path |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
408 |
if (!curEntry.isDirectory()) { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
409 |
curEntry = curEntry.getParentFile(); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
410 |
} |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
411 |
if (curEntry != null && seenDirs.add(curEntry)) { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
412 |
MetaIndex.registerDirectory(curEntry); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
413 |
} |
2 | 414 |
} |
3111
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
415 |
return pathToURLs(classPath); |
2 | 416 |
} |
417 |
} |
|
3111
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
418 |
); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
419 |
} else { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
420 |
urls = new URL[0]; |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
421 |
} |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
422 |
|
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
423 |
bootstrapClassPath = new URLClassPath(urls, factory); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
424 |
if (VM.isBootedKernelVM()) { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
425 |
final File[] additionalBootStrapPaths = |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
426 |
DownloadManager.getAdditionalBootStrapPaths(); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
427 |
AccessController.doPrivileged(new PrivilegedAction() { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
428 |
public Object run() { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
429 |
for (int i=0; i<additionalBootStrapPaths.length; i++) { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
430 |
bootstrapClassPath.addURL( |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
431 |
getFileURL(additionalBootStrapPaths[i])); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
432 |
} |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
433 |
return null; |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
434 |
} |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
435 |
}); |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
436 |
} |
2 | 437 |
} |
3111
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
438 |
return bootstrapClassPath; |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
439 |
} |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
440 |
|
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
441 |
public static synchronized void flushBootstrapClassPath() { |
fefdeafb7ab9
6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents:
2448
diff
changeset
|
442 |
bootstrapClassPath = null; |
2 | 443 |
} |
444 |
||
445 |
private static URL[] pathToURLs(File[] path) { |
|
446 |
URL[] urls = new URL[path.length]; |
|
447 |
for (int i = 0; i < path.length; i++) { |
|
448 |
urls[i] = getFileURL(path[i]); |
|
449 |
} |
|
450 |
// DEBUG |
|
451 |
//for (int i = 0; i < urls.length; i++) { |
|
452 |
// System.out.println("urls[" + i + "] = " + '"' + urls[i] + '"'); |
|
453 |
//} |
|
454 |
return urls; |
|
455 |
} |
|
456 |
||
457 |
private static File[] getClassPath(String cp) { |
|
458 |
File[] path; |
|
459 |
if (cp != null) { |
|
460 |
int count = 0, maxCount = 1; |
|
461 |
int pos = 0, lastPos = 0; |
|
462 |
// Count the number of separators first |
|
463 |
while ((pos = cp.indexOf(File.pathSeparator, lastPos)) != -1) { |
|
464 |
maxCount++; |
|
465 |
lastPos = pos + 1; |
|
466 |
} |
|
467 |
path = new File[maxCount]; |
|
468 |
lastPos = pos = 0; |
|
469 |
// Now scan for each path component |
|
470 |
while ((pos = cp.indexOf(File.pathSeparator, lastPos)) != -1) { |
|
471 |
if (pos - lastPos > 0) { |
|
472 |
path[count++] = new File(cp.substring(lastPos, pos)); |
|
473 |
} else { |
|
474 |
// empty path component translates to "." |
|
475 |
path[count++] = new File("."); |
|
476 |
} |
|
477 |
lastPos = pos + 1; |
|
478 |
} |
|
479 |
// Make sure we include the last path component |
|
480 |
if (lastPos < cp.length()) { |
|
481 |
path[count++] = new File(cp.substring(lastPos)); |
|
482 |
} else { |
|
483 |
path[count++] = new File("."); |
|
484 |
} |
|
485 |
// Trim array to correct size |
|
486 |
if (count != maxCount) { |
|
487 |
File[] tmp = new File[count]; |
|
488 |
System.arraycopy(path, 0, tmp, 0, count); |
|
489 |
path = tmp; |
|
490 |
} |
|
491 |
} else { |
|
492 |
path = new File[0]; |
|
493 |
} |
|
494 |
// DEBUG |
|
495 |
//for (int i = 0; i < path.length; i++) { |
|
496 |
// System.out.println("path[" + i + "] = " + '"' + path[i] + '"'); |
|
497 |
//} |
|
498 |
return path; |
|
499 |
} |
|
500 |
||
501 |
private static URLStreamHandler fileHandler; |
|
502 |
||
503 |
static URL getFileURL(File file) { |
|
504 |
try { |
|
505 |
file = file.getCanonicalFile(); |
|
506 |
} catch (IOException e) {} |
|
507 |
||
508 |
try { |
|
509 |
return ParseUtil.fileToEncodedURL(file); |
|
510 |
} catch (MalformedURLException e) { |
|
511 |
// Should never happen since we specify the protocol... |
|
512 |
throw new InternalError(); |
|
513 |
} |
|
514 |
} |
|
515 |
||
516 |
/* |
|
517 |
* The stream handler factory for loading system protocol handlers. |
|
518 |
*/ |
|
519 |
private static class Factory implements URLStreamHandlerFactory { |
|
520 |
private static String PREFIX = "sun.net.www.protocol"; |
|
521 |
||
522 |
public URLStreamHandler createURLStreamHandler(String protocol) { |
|
523 |
String name = PREFIX + "." + protocol + ".Handler"; |
|
524 |
try { |
|
525 |
Class c = Class.forName(name); |
|
526 |
return (URLStreamHandler)c.newInstance(); |
|
527 |
} catch (ClassNotFoundException e) { |
|
528 |
e.printStackTrace(); |
|
529 |
} catch (InstantiationException e) { |
|
530 |
e.printStackTrace(); |
|
531 |
} catch (IllegalAccessException e) { |
|
532 |
e.printStackTrace(); |
|
533 |
} |
|
534 |
throw new InternalError("could not load " + protocol + |
|
535 |
"system protocol handler"); |
|
536 |
} |
|
537 |
} |
|
538 |
} |
|
539 |
||
540 |
class PathPermissions extends PermissionCollection { |
|
541 |
// use serialVersionUID from JDK 1.2.2 for interoperability |
|
542 |
private static final long serialVersionUID = 8133287259134945693L; |
|
543 |
||
544 |
private File path[]; |
|
545 |
private Permissions perms; |
|
546 |
||
547 |
URL codeBase; |
|
548 |
||
549 |
PathPermissions(File path[]) |
|
550 |
{ |
|
551 |
this.path = path; |
|
552 |
this.perms = null; |
|
553 |
this.codeBase = null; |
|
554 |
} |
|
555 |
||
556 |
URL getCodeBase() |
|
557 |
{ |
|
558 |
return codeBase; |
|
559 |
} |
|
560 |
||
561 |
public void add(java.security.Permission permission) { |
|
562 |
throw new SecurityException("attempt to add a permission"); |
|
563 |
} |
|
564 |
||
565 |
private synchronized void init() |
|
566 |
{ |
|
567 |
if (perms != null) |
|
568 |
return; |
|
569 |
||
570 |
perms = new Permissions(); |
|
571 |
||
572 |
// this is needed to be able to create the classloader itself! |
|
573 |
perms.add(SecurityConstants.CREATE_CLASSLOADER_PERMISSION); |
|
574 |
||
575 |
// add permission to read any "java.*" property |
|
576 |
perms.add(new java.util.PropertyPermission("java.*", |
|
577 |
SecurityConstants.PROPERTY_READ_ACTION)); |
|
578 |
||
51 | 579 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
580 |
public Void run() { |
|
2 | 581 |
for (int i=0; i < path.length; i++) { |
582 |
File f = path[i]; |
|
583 |
String path; |
|
584 |
try { |
|
585 |
path = f.getCanonicalPath(); |
|
586 |
} catch (IOException ioe) { |
|
587 |
path = f.getAbsolutePath(); |
|
588 |
} |
|
589 |
if (i == 0) { |
|
590 |
codeBase = Launcher.getFileURL(new File(path)); |
|
591 |
} |
|
592 |
if (f.isDirectory()) { |
|
593 |
if (path.endsWith(File.separator)) { |
|
594 |
perms.add(new FilePermission(path+"-", |
|
595 |
SecurityConstants.FILE_READ_ACTION)); |
|
596 |
} else { |
|
597 |
perms.add(new FilePermission( |
|
598 |
path + File.separator+"-", |
|
599 |
SecurityConstants.FILE_READ_ACTION)); |
|
600 |
} |
|
601 |
} else { |
|
602 |
int endIndex = path.lastIndexOf(File.separatorChar); |
|
603 |
if (endIndex != -1) { |
|
604 |
path = path.substring(0, endIndex+1) + "-"; |
|
605 |
perms.add(new FilePermission(path, |
|
606 |
SecurityConstants.FILE_READ_ACTION)); |
|
607 |
} else { |
|
608 |
// XXX? |
|
609 |
} |
|
610 |
} |
|
611 |
} |
|
612 |
return null; |
|
613 |
} |
|
614 |
}); |
|
615 |
} |
|
616 |
||
617 |
public boolean implies(java.security.Permission permission) { |
|
618 |
if (perms == null) |
|
619 |
init(); |
|
620 |
return perms.implies(permission); |
|
621 |
} |
|
622 |
||
51 | 623 |
public java.util.Enumeration<Permission> elements() { |
2 | 624 |
if (perms == null) |
625 |
init(); |
|
626 |
synchronized (perms) { |
|
627 |
return perms.elements(); |
|
628 |
} |
|
629 |
} |
|
630 |
||
631 |
public String toString() { |
|
632 |
if (perms == null) |
|
633 |
init(); |
|
634 |
return perms.toString(); |
|
635 |
} |
|
636 |
} |