< prev index next >
test/jdk/java/lang/System/LoggerFinder/internal/BaseDefaultLoggerFinderTest/CustomSystemClassLoader.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
@@ -30,10 +30,11 @@
import java.security.ProtectionDomain;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
/**
* A custom ClassLoader to load the concrete LoggerFinder class
* with all permissions. The CustomSystemClassLoader class must be
@@ -46,12 +47,12 @@
*/
public class CustomSystemClassLoader extends ClassLoader {
final List<String> finderClassNames =
- Arrays.asList("BaseDefaultLoggerFinderTest$BaseLoggerFinder");
- final Map<String, Class<?>> finderClasses = new HashMap<>();
+ Arrays.asList("BaseLoggerFinder");
+ final Map<String, Class<?>> finderClasses = new ConcurrentHashMap<>();
Class<?> testLoggerFinderClass;
public CustomSystemClassLoader() {
super();
}
@@ -59,13 +60,18 @@
super(parent);
}
private Class<?> defineFinderClass(String name)
throws ClassNotFoundException {
+ Class<?> finderClass = finderClasses.get(name);
+ if (finderClass != null) return finderClass;
+
final Object obj = getClassLoadingLock(name);
synchronized(obj) {
- if (finderClasses.get(name) != null) return finderClasses.get(name);
+ finderClasses.get(name);
+ if (finderClass != null) return finderClass;
+
if (testLoggerFinderClass == null) {
// Hack: we load testLoggerFinderClass to get its code source.
// we can't use this.getClass() since we are in the boot.
testLoggerFinderClass = super.loadClass("BaseDefaultLoggerFinderTest$TestLoggerFinder");
}
@@ -74,11 +80,11 @@
if (file.canRead()) {
try {
byte[] b = Files.readAllBytes(file.toPath());
Permissions perms = new Permissions();
perms.add(new AllPermission());
- Class<?> finderClass = defineClass(
+ finderClass = defineClass(
name, b, 0, b.length, new ProtectionDomain(
this.getClass().getProtectionDomain().getCodeSource(),
perms));
System.out.println("Loaded " + name);
finderClasses.put(name, finderClass);
@@ -92,13 +98,17 @@
new IOException(file.toPath() + ": can't read"));
}
}
}
+ private static boolean matches(String prefix, String name) {
+ return prefix.equals(name) || name.startsWith(prefix + "$");
+ }
+
@Override
public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
- if (finderClassNames.contains(name)) {
+ if (finderClassNames.stream().anyMatch(n -> matches(n, name))) {
Class<?> c = defineFinderClass(name);
if (resolve) {
resolveClass(c);
}
return c;
@@ -106,11 +116,11 @@
return super.loadClass(name, resolve);
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
- if (finderClassNames.contains(name)) {
+ if (finderClassNames.stream().anyMatch(n -> matches(n, name))) {
return defineFinderClass(name);
}
return super.findClass(name);
}
< prev index next >