1 /*
2 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
145 for (int i = 0; i < len; i++) {
146 MetaIndex.registerDirectory(dirs[i]);
147 }
148 return new ExtClassLoader(dirs);
149 }
150 });
151 } catch (java.security.PrivilegedActionException e) {
152 throw (IOException) e.getException();
153 }
154 }
155
156 void addExtURL(URL url) {
157 super.addURL(url);
158 }
159
160 /*
161 * Creates a new ExtClassLoader for the specified directories.
162 */
163 public ExtClassLoader(File[] dirs) throws IOException {
164 super(getExtURLs(dirs), null, factory);
165 }
166
167 private static File[] getExtDirs() {
168 String s = System.getProperty("java.ext.dirs");
169 File[] dirs;
170 if (s != null) {
171 StringTokenizer st =
172 new StringTokenizer(s, File.pathSeparator);
173 int count = st.countTokens();
174 dirs = new File[count];
175 for (int i = 0; i < count; i++) {
176 dirs[i] = new File(st.nextToken());
177 }
178 } else {
179 dirs = new File[0];
180 }
181 return dirs;
182 }
183
184 private static URL[] getExtURLs(File[] dirs) throws IOException {
268 final String s = System.getProperty("java.class.path");
269 final File[] path = (s == null) ? new File[0] : getClassPath(s);
270
271 // Note: on bugid 4256530
272 // Prior implementations of this doPrivileged() block supplied
273 // a rather restrictive ACC via a call to the private method
274 // AppClassLoader.getContext(). This proved overly restrictive
275 // when loading classes. Specifically it prevent
276 // accessClassInPackage.sun.* grants from being honored.
277 //
278 return AccessController.doPrivileged(
279 new PrivilegedAction<AppClassLoader>() {
280 public AppClassLoader run() {
281 URL[] urls =
282 (s == null) ? new URL[0] : pathToURLs(path);
283 return new AppClassLoader(urls, extcl);
284 }
285 });
286 }
287
288 /*
289 * Creates a new AppClassLoader
290 */
291 AppClassLoader(URL[] urls, ClassLoader parent) {
292 super(urls, parent, factory);
293 }
294
295 /**
296 * Override loadClass so we can checkPackageAccess.
297 */
298 public Class<?> loadClass(String name, boolean resolve)
299 throws ClassNotFoundException
300 {
301 int i = name.lastIndexOf('.');
302 if (i != -1) {
303 SecurityManager sm = System.getSecurityManager();
304 if (sm != null) {
305 sm.checkPackageAccess(name.substring(0, i));
306 }
307 }
308 return (super.loadClass(name, resolve));
309 }
310
311 /**
312 * allow any classes loaded from classpath to exit the VM.
313 */
314 protected PermissionCollection getPermissions(CodeSource codesource)
315 {
316 PermissionCollection perms = super.getPermissions(codesource);
317 perms.add(new RuntimePermission("exitVM"));
318 return perms;
319 }
320
321 /**
322 * This class loader supports dynamic additions to the class path
323 * at runtime.
324 *
325 * @see java.lang.instrument.Instrumentation#appendToSystemClassPathSearch
326 */
327 private void appendToClassPathForInstrumentation(String path) {
369 Set<File> seenDirs = new HashSet<File>();
370 for (int i = 0; i < len; i++) {
371 File curEntry = classPath[i];
372 // Negative test used to properly handle
373 // nonexistent jars on boot class path
374 if (!curEntry.isDirectory()) {
375 curEntry = curEntry.getParentFile();
376 }
377 if (curEntry != null && seenDirs.add(curEntry)) {
378 MetaIndex.registerDirectory(curEntry);
379 }
380 }
381 return pathToURLs(classPath);
382 }
383 }
384 );
385 } else {
386 urls = new URL[0];
387 }
388 bcp = new URLClassPath(urls, factory);
389 }
390 }
391
392 public static URLClassPath getBootstrapClassPath() {
393 return BootClassPathHolder.bcp;
394 }
395
396 private static URL[] pathToURLs(File[] path) {
397 URL[] urls = new URL[path.length];
398 for (int i = 0; i < path.length; i++) {
399 urls[i] = getFileURL(path[i]);
400 }
401 // DEBUG
402 //for (int i = 0; i < urls.length; i++) {
403 // System.out.println("urls[" + i + "] = " + '"' + urls[i] + '"');
404 //}
405 return urls;
406 }
407
408 private static File[] getClassPath(String cp) {
|
1 /*
2 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
145 for (int i = 0; i < len; i++) {
146 MetaIndex.registerDirectory(dirs[i]);
147 }
148 return new ExtClassLoader(dirs);
149 }
150 });
151 } catch (java.security.PrivilegedActionException e) {
152 throw (IOException) e.getException();
153 }
154 }
155
156 void addExtURL(URL url) {
157 super.addURL(url);
158 }
159
160 /*
161 * Creates a new ExtClassLoader for the specified directories.
162 */
163 public ExtClassLoader(File[] dirs) throws IOException {
164 super(getExtURLs(dirs), null, factory);
165 SharedSecrets.getJavaNetAccess().
166 getURLClassPath(this).initLookupCache(this);
167 }
168
169 private static File[] getExtDirs() {
170 String s = System.getProperty("java.ext.dirs");
171 File[] dirs;
172 if (s != null) {
173 StringTokenizer st =
174 new StringTokenizer(s, File.pathSeparator);
175 int count = st.countTokens();
176 dirs = new File[count];
177 for (int i = 0; i < count; i++) {
178 dirs[i] = new File(st.nextToken());
179 }
180 } else {
181 dirs = new File[0];
182 }
183 return dirs;
184 }
185
186 private static URL[] getExtURLs(File[] dirs) throws IOException {
270 final String s = System.getProperty("java.class.path");
271 final File[] path = (s == null) ? new File[0] : getClassPath(s);
272
273 // Note: on bugid 4256530
274 // Prior implementations of this doPrivileged() block supplied
275 // a rather restrictive ACC via a call to the private method
276 // AppClassLoader.getContext(). This proved overly restrictive
277 // when loading classes. Specifically it prevent
278 // accessClassInPackage.sun.* grants from being honored.
279 //
280 return AccessController.doPrivileged(
281 new PrivilegedAction<AppClassLoader>() {
282 public AppClassLoader run() {
283 URL[] urls =
284 (s == null) ? new URL[0] : pathToURLs(path);
285 return new AppClassLoader(urls, extcl);
286 }
287 });
288 }
289
290 final URLClassPath ucp;
291
292 /*
293 * Creates a new AppClassLoader
294 */
295 AppClassLoader(URL[] urls, ClassLoader parent) {
296 super(urls, parent, factory);
297 ucp = SharedSecrets.getJavaNetAccess().getURLClassPath(this);
298 ucp.initLookupCache(this);
299 }
300
301 /**
302 * Override loadClass so we can checkPackageAccess.
303 */
304 public Class<?> loadClass(String name, boolean resolve)
305 throws ClassNotFoundException
306 {
307 int i = name.lastIndexOf('.');
308 if (i != -1) {
309 SecurityManager sm = System.getSecurityManager();
310 if (sm != null) {
311 sm.checkPackageAccess(name.substring(0, i));
312 }
313 }
314
315 if (ucp.knownToNotExist(name)) {
316 // The class of the given name is not found in the parent
317 // class loader as well as its local URLClassPath.
318 // Check if this class has already been defined dynamically;
319 // if so, return the loaded class; otherwise, skip the parent
320 // delegation and findClass.
321 synchronized (getClassLoadingLock(name)) {
322 Class<?> c = findLoadedClass(name);
323 if (c != null) {
324 return c;
325 }
326 }
327 throw new ClassNotFoundException(name);
328 }
329
330 return (super.loadClass(name, resolve));
331 }
332
333 /**
334 * allow any classes loaded from classpath to exit the VM.
335 */
336 protected PermissionCollection getPermissions(CodeSource codesource)
337 {
338 PermissionCollection perms = super.getPermissions(codesource);
339 perms.add(new RuntimePermission("exitVM"));
340 return perms;
341 }
342
343 /**
344 * This class loader supports dynamic additions to the class path
345 * at runtime.
346 *
347 * @see java.lang.instrument.Instrumentation#appendToSystemClassPathSearch
348 */
349 private void appendToClassPathForInstrumentation(String path) {
391 Set<File> seenDirs = new HashSet<File>();
392 for (int i = 0; i < len; i++) {
393 File curEntry = classPath[i];
394 // Negative test used to properly handle
395 // nonexistent jars on boot class path
396 if (!curEntry.isDirectory()) {
397 curEntry = curEntry.getParentFile();
398 }
399 if (curEntry != null && seenDirs.add(curEntry)) {
400 MetaIndex.registerDirectory(curEntry);
401 }
402 }
403 return pathToURLs(classPath);
404 }
405 }
406 );
407 } else {
408 urls = new URL[0];
409 }
410 bcp = new URLClassPath(urls, factory);
411 bcp.initLookupCache(null);
412 }
413 }
414
415 public static URLClassPath getBootstrapClassPath() {
416 return BootClassPathHolder.bcp;
417 }
418
419 private static URL[] pathToURLs(File[] path) {
420 URL[] urls = new URL[path.length];
421 for (int i = 0; i < path.length; i++) {
422 urls[i] = getFileURL(path[i]);
423 }
424 // DEBUG
425 //for (int i = 0; i < urls.length; i++) {
426 // System.out.println("urls[" + i + "] = " + '"' + urls[i] + '"');
427 //}
428 return urls;
429 }
430
431 private static File[] getClassPath(String cp) {
|