< prev index next >

test/jdk/java/lang/System/LoggerFinder/internal/LoggerFinderLoaderTest/CustomSystemClassLoader.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2015, 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. --- 1,7 ---- /* ! * 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,39 **** --- 30,40 ---- 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,58 **** */ public class CustomSystemClassLoader extends ClassLoader { final List<String> finderClassNames = ! Arrays.asList("LoggerFinderLoaderTest$BaseLoggerFinder", ! "LoggerFinderLoaderTest$BaseLoggerFinder2"); ! final Map<String, Class<?>> finderClasses = new HashMap<>(); Class<?> testLoggerFinderClass; public CustomSystemClassLoader() { super(); } --- 47,58 ---- */ public class CustomSystemClassLoader extends ClassLoader { final List<String> finderClassNames = ! Arrays.asList("BaseLoggerFinder", "BaseLoggerFinder2"); ! final Map<String, Class<?>> finderClasses = new ConcurrentHashMap<>(); Class<?> testLoggerFinderClass; public CustomSystemClassLoader() { super(); }
*** 60,72 **** super(parent); } private Class<?> defineFinderClass(String name) throws ClassNotFoundException { final Object obj = getClassLoadingLock(name); synchronized(obj) { ! if (finderClasses.get(name) != null) return finderClasses.get(name); 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("LoggerFinderLoaderTest$TestLoggerFinder"); } --- 60,76 ---- 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) { ! finderClass = 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("LoggerFinderLoaderTest$TestLoggerFinder"); }
*** 75,85 **** if (file.canRead()) { try { byte[] b = Files.readAllBytes(file.toPath()); Permissions perms = new Permissions(); perms.add(new AllPermission()); ! Class<?> finderClass = defineClass( name, b, 0, b.length, new ProtectionDomain( this.getClass().getProtectionDomain().getCodeSource(), perms)); System.out.println("Loaded " + name); finderClasses.put(name, finderClass); --- 79,89 ---- if (file.canRead()) { try { byte[] b = Files.readAllBytes(file.toPath()); Permissions perms = new Permissions(); perms.add(new AllPermission()); ! finderClass = defineClass( name, b, 0, b.length, new ProtectionDomain( this.getClass().getProtectionDomain().getCodeSource(), perms)); System.out.println("Loaded " + name); finderClasses.put(name, finderClass);
*** 93,105 **** new IOException(file.toPath() + ": can't read")); } } } @Override public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { ! if (finderClassNames.contains(name)) { Class<?> c = defineFinderClass(name); if (resolve) { resolveClass(c); } return c; --- 97,113 ---- 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.stream().anyMatch(n -> matches(n, name))) { Class<?> c = defineFinderClass(name); if (resolve) { resolveClass(c); } return c;
*** 107,117 **** return super.loadClass(name, resolve); } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { ! if (finderClassNames.contains(name)) { return defineFinderClass(name); } return super.findClass(name); } --- 115,125 ---- return super.loadClass(name, resolve); } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { ! if (finderClassNames.stream().anyMatch(n -> matches(n, name))) { return defineFinderClass(name); } return super.findClass(name); }
< prev index next >