Path: blob/master/test/jdk/java/lang/System/LoggerFinder/internal/BaseDefaultLoggerFinderTest/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");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) {69finderClasses.get(name);70if (finderClass != null) return finderClass;7172if (testLoggerFinderClass == null) {73// Hack: we load testLoggerFinderClass to get its code source.74// we can't use this.getClass() since we are in the boot.75testLoggerFinderClass = super.loadClass("BaseDefaultLoggerFinderTest$TestLoggerFinder");76}77URL url = testLoggerFinderClass.getProtectionDomain().getCodeSource().getLocation();78File file = new File(url.getPath(), name+".class");79if (file.canRead()) {80try {81byte[] b = Files.readAllBytes(file.toPath());82Permissions perms = new Permissions();83perms.add(new AllPermission());84finderClass = defineClass(85name, b, 0, b.length, new ProtectionDomain(86this.getClass().getProtectionDomain().getCodeSource(),87perms));88System.out.println("Loaded " + name);89finderClasses.put(name, finderClass);90return finderClass;91} catch (Throwable ex) {92ex.printStackTrace();93throw new ClassNotFoundException(name, ex);94}95} else {96throw new ClassNotFoundException(name,97new IOException(file.toPath() + ": can't read"));98}99}100}101102private static boolean matches(String prefix, String name) {103return prefix.equals(name) || name.startsWith(prefix + "$");104}105106@Override107public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {108if (finderClassNames.stream().anyMatch(n -> matches(n, name))) {109Class<?> c = defineFinderClass(name);110if (resolve) {111resolveClass(c);112}113return c;114}115return super.loadClass(name, resolve);116}117118@Override119protected Class<?> findClass(String name) throws ClassNotFoundException {120if (finderClassNames.stream().anyMatch(n -> matches(n, name))) {121return defineFinderClass(name);122}123return super.findClass(name);124}125126}127128129