< prev index next >

test/jdk/java/lang/System/LoggerFinder/internal/BaseDefaultLoggerFinderTest/CustomSystemClassLoader.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 30,39 **** --- 30,40 ---- import java.security.ProtectionDomain; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; + import java.util.concurrent.ConcurrentHashMap; /** * A custom ClassLoader to load the concrete LoggerFinder class * with all permissions. The CustomSystemClassLoader class must be
*** 46,57 **** */ public class CustomSystemClassLoader extends ClassLoader { final List<String> finderClassNames = ! Arrays.asList("BaseDefaultLoggerFinderTest$BaseLoggerFinder"); ! final Map<String, Class<?>> finderClasses = new HashMap<>(); Class<?> testLoggerFinderClass; public CustomSystemClassLoader() { super(); } --- 47,58 ---- */ public class CustomSystemClassLoader extends ClassLoader { final List<String> finderClassNames = ! Arrays.asList("BaseLoggerFinder"); ! final Map<String, Class<?>> finderClasses = new ConcurrentHashMap<>(); Class<?> testLoggerFinderClass; public CustomSystemClassLoader() { super(); }
*** 59,71 **** super(parent); } private Class<?> defineFinderClass(String name) throws ClassNotFoundException { final Object obj = getClassLoadingLock(name); synchronized(obj) { ! if (finderClasses.get(name) != null) return finderClasses.get(name); if (testLoggerFinderClass == null) { // Hack: we load testLoggerFinderClass to get its code source. // we can't use this.getClass() since we are in the boot. testLoggerFinderClass = super.loadClass("BaseDefaultLoggerFinderTest$TestLoggerFinder"); } --- 60,77 ---- super(parent); } private Class<?> defineFinderClass(String name) throws ClassNotFoundException { + Class<?> finderClass = finderClasses.get(name); + if (finderClass != null) return finderClass; + final Object obj = getClassLoadingLock(name); synchronized(obj) { ! finderClasses.get(name); ! if (finderClass != null) return finderClass; ! if (testLoggerFinderClass == null) { // Hack: we load testLoggerFinderClass to get its code source. // we can't use this.getClass() since we are in the boot. testLoggerFinderClass = super.loadClass("BaseDefaultLoggerFinderTest$TestLoggerFinder"); }
*** 74,84 **** if (file.canRead()) { try { byte[] b = Files.readAllBytes(file.toPath()); Permissions perms = new Permissions(); perms.add(new AllPermission()); ! Class<?> finderClass = defineClass( name, b, 0, b.length, new ProtectionDomain( this.getClass().getProtectionDomain().getCodeSource(), perms)); System.out.println("Loaded " + name); finderClasses.put(name, finderClass); --- 80,90 ---- if (file.canRead()) { try { byte[] b = Files.readAllBytes(file.toPath()); Permissions perms = new Permissions(); perms.add(new AllPermission()); ! finderClass = defineClass( name, b, 0, b.length, new ProtectionDomain( this.getClass().getProtectionDomain().getCodeSource(), perms)); System.out.println("Loaded " + name); finderClasses.put(name, finderClass);
*** 92,104 **** new IOException(file.toPath() + ": can't read")); } } } @Override public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { ! if (finderClassNames.contains(name)) { Class<?> c = defineFinderClass(name); if (resolve) { resolveClass(c); } return c; --- 98,114 ---- new IOException(file.toPath() + ": can't read")); } } } + private static boolean matches(String prefix, String name) { + return prefix.equals(name) || name.startsWith(prefix + "$"); + } + @Override public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { ! if (finderClassNames.stream().anyMatch(n -> matches(n, name))) { Class<?> c = defineFinderClass(name); if (resolve) { resolveClass(c); } return c;
*** 106,116 **** return super.loadClass(name, resolve); } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { ! if (finderClassNames.contains(name)) { return defineFinderClass(name); } return super.findClass(name); } --- 116,126 ---- return super.loadClass(name, resolve); } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { ! if (finderClassNames.stream().anyMatch(n -> matches(n, name))) { return defineFinderClass(name); } return super.findClass(name); }
< prev index next >