Path: blob/master/test/jdk/java/lang/System/LoggerFinder/internal/LoggerFinderLoaderTest/CustomSystemClassLoader.java
41161 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.io.IOException;25import java.net.URL;26import java.nio.file.Files;27import java.security.AllPermission;28import java.security.Permissions;29import java.security.ProtectionDomain;30import java.util.Arrays;31import java.util.HashMap;32import java.util.List;33import java.util.Map;34import java.util.concurrent.ConcurrentHashMap;353637/**38* A custom ClassLoader to load the concrete LoggerFinder class39* with all permissions. The CustomSystemClassLoader class must be40* in the BCL, otherwise when system classes - such as41* ZoneDateTime try to load their resource bundle a MissingResourceBundle42* caused by a SecurityException may be thrown, as the CustomSystemClassLoader43* code base will be found in the stack called by doPrivileged.44*45* @author danielfuchs46*/47public class CustomSystemClassLoader extends ClassLoader {484950final List<String> finderClassNames =51Arrays.asList("BaseLoggerFinder", "BaseLoggerFinder2");52final Map<String, Class<?>> finderClasses = new ConcurrentHashMap<>();53Class<?> testLoggerFinderClass;5455public CustomSystemClassLoader() {56super();57}58public CustomSystemClassLoader(ClassLoader parent) {59super(parent);60}6162private Class<?> defineFinderClass(String name)63throws ClassNotFoundException {64Class<?> finderClass = finderClasses.get(name);65if (finderClass != null) return finderClass;6667final Object obj = getClassLoadingLock(name);68synchronized(obj) {69finderClass = finderClasses.get(name);70if (finderClass != null) return finderClass;71if (testLoggerFinderClass == null) {72// Hack: we load testLoggerFinderClass to get its code source.73// we can't use this.getClass() since we are in the boot.74testLoggerFinderClass = super.loadClass("LoggerFinderLoaderTest$TestLoggerFinder");75}76URL url = testLoggerFinderClass.getProtectionDomain().getCodeSource().getLocation();77File file = new File(url.getPath(), name+".class");78if (file.canRead()) {79try {80byte[] b = Files.readAllBytes(file.toPath());81Permissions perms = new Permissions();82perms.add(new AllPermission());83finderClass = defineClass(84name, b, 0, b.length, new ProtectionDomain(85this.getClass().getProtectionDomain().getCodeSource(),86perms));87System.out.println("Loaded " + name);88finderClasses.put(name, finderClass);89return finderClass;90} catch (Throwable ex) {91ex.printStackTrace();92throw new ClassNotFoundException(name, ex);93}94} else {95throw new ClassNotFoundException(name,96new IOException(file.toPath() + ": can't read"));97}98}99}100101private static boolean matches(String prefix, String name) {102return prefix.equals(name) || name.startsWith(prefix + "$");103}104105@Override106public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {107if (finderClassNames.stream().anyMatch(n -> matches(n, name))) {108Class<?> c = defineFinderClass(name);109if (resolve) {110resolveClass(c);111}112return c;113}114return super.loadClass(name, resolve);115}116117@Override118protected Class<?> findClass(String name) throws ClassNotFoundException {119if (finderClassNames.stream().anyMatch(n -> matches(n, name))) {120return defineFinderClass(name);121}122return super.findClass(name);123}124125}126127128