Path: blob/master/test/jdk/java/lang/System/LoggerFinder/internal/BaseLoggerBridgeTest/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;3132/**33* A custom ClassLoader to load the concrete LoggerFinder class34* with all permissions.35*36* @author danielfuchs37*/38public class CustomSystemClassLoader extends ClassLoader {394041private final ConcurrentHashMap<String, Class<?>> classes = new ConcurrentHashMap<>();4243public CustomSystemClassLoader() {44super();45}46public CustomSystemClassLoader(ClassLoader parent) {47super(parent);48}4950private Class<?> defineFinderClass(String name)51throws ClassNotFoundException {52Class<?> finderClass = classes.get(name);53if (finderClass != null) return finderClass;5455final Object obj = getClassLoadingLock(name);5657synchronized(obj) {58finderClass = classes.get(name);59if (finderClass != null) return finderClass;6061URL url = this.getClass().getProtectionDomain().getCodeSource().getLocation();62File file = new File(url.getPath(), name+".class");63if (file.canRead()) {64try {65byte[] b = Files.readAllBytes(file.toPath());66Permissions perms = new Permissions();67perms.add(new AllPermission());68finderClass = defineClass(69name, b, 0, b.length, new ProtectionDomain(70this.getClass().getProtectionDomain().getCodeSource(),71perms));72System.out.println("Loaded " + name);73classes.put(name, finderClass);74return finderClass;75} catch (Throwable ex) {76ex.printStackTrace();77throw new ClassNotFoundException(name, ex);78}79} else {80throw new ClassNotFoundException(name,81new IOException(file.toPath() + ": can't read"));82}83}84}8586@Override87public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {88if (name.equals("BaseLoggerFinder") || name.startsWith("BaseLoggerFinder$")) {89Class<?> c = defineFinderClass(name);90if (resolve) {91resolveClass(c);92}93return c;94}95return super.loadClass(name, resolve);96}9798@Override99protected Class<?> findClass(String name) throws ClassNotFoundException {100if (name.equals("BaseLoggerFinder") || name.startsWith("BaseLoggerFinder$")) {101return defineFinderClass(name);102}103return super.findClass(name);104}105106}107108109