Path: blob/master/test/jdk/java/lang/System/LoggerFinder/BaseLoggerFinderTest/CustomSystemClassLoader.java
41154 views
/*1* Copyright (c) 2015, 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;303132/**33* A custom ClassLoader to load the concrete LoggerFinder class34* with all permissions.35*36* @author danielfuchs37*/38public class CustomSystemClassLoader extends ClassLoader {394041Class<?> finderClass = null;4243public CustomSystemClassLoader() {44super();45}46public CustomSystemClassLoader(ClassLoader parent) {47super(parent);48}4950private Class<?> defineFinderClass(String name)51throws ClassNotFoundException {52final Object obj = getClassLoadingLock(name);53synchronized(obj) {54if (finderClass != null) return finderClass;5556URL url = this.getClass().getProtectionDomain().getCodeSource().getLocation();57File file = new File(url.getPath(), name+".class");58if (file.canRead()) {59try {60byte[] b = Files.readAllBytes(file.toPath());61Permissions perms = new Permissions();62perms.add(new AllPermission());63finderClass = defineClass(64name, b, 0, b.length, new ProtectionDomain(65this.getClass().getProtectionDomain().getCodeSource(),66perms));67System.out.println("Loaded " + name);68return finderClass;69} catch (Throwable ex) {70ex.printStackTrace();71throw new ClassNotFoundException(name, ex);72}73} else {74throw new ClassNotFoundException(name,75new IOException(file.toPath() + ": can't read"));76}77}78}7980@Override81public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {82if (name.equals("BaseLoggerFinder")) {83Class<?> c = defineFinderClass(name);84if (resolve) {85resolveClass(c);86}87return c;88}89return super.loadClass(name, resolve);90}9192@Override93protected Class<?> findClass(String name) throws ClassNotFoundException {94if (name.equals("BaseLoggerFinder")) {95return defineFinderClass(name);96}97return super.findClass(name);98}99100}101102103