Path: blob/master/test/jdk/java/lang/System/LoggerFinder/internal/LoggerBridgeTest/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.concurrent.ConcurrentHashMap;313233/**34* A custom ClassLoader to load the concrete LoggerFinder class35* with all permissions.36*37* @author danielfuchs38*/39public class CustomSystemClassLoader extends ClassLoader {404142private final ConcurrentHashMap<String, Class<?>> classes = new ConcurrentHashMap<>();4344public CustomSystemClassLoader() {45super();46}47public CustomSystemClassLoader(ClassLoader parent) {48super(parent);49}5051private Class<?> defineFinderClass(String name)52throws ClassNotFoundException {5354Class<?> loggerFinderClass = classes.get(name);55if (loggerFinderClass != null) return loggerFinderClass;5657final Object obj = getClassLoadingLock(name);58synchronized(obj) {59loggerFinderClass = classes.get(name);60if (loggerFinderClass != null) return loggerFinderClass;6162URL url = this.getClass().getProtectionDomain().getCodeSource().getLocation();63File file = new File(url.getPath(), name+".class");64if (file.canRead()) {65try {66byte[] b = Files.readAllBytes(file.toPath());67Permissions perms = new Permissions();68perms.add(new AllPermission());69loggerFinderClass = defineClass(70name, b, 0, b.length, new ProtectionDomain(71this.getClass().getProtectionDomain().getCodeSource(),72perms));73classes.put(name, loggerFinderClass);74System.out.println("Loaded " + name);75return loggerFinderClass;76} catch (Throwable ex) {77ex.printStackTrace();78throw new ClassNotFoundException(name, ex);79}80} else {81throw new ClassNotFoundException(name,82new IOException(file.toPath() + ": can't read"));83}84}85}8687@Override88public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {89if (name.equals("LogProducerFinder") || name.startsWith("LogProducerFinder$")) {90Class<?> c = defineFinderClass(name);91if (resolve) {92resolveClass(c);93}94return c;95}96return super.loadClass(name, resolve);97}9899@Override100protected Class<?> findClass(String name) throws ClassNotFoundException {101if (name.equals("LogProducerFinder") || name.startsWith("LogProducerFinder$")) {102return defineFinderClass(name);103}104return super.findClass(name);105}106107}108109110