1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.IOException; 25 import java.net.URL; 26 import java.util.Enumeration; 27 import java.lang.System.LoggerFinder; 28 29 /** 30 * A custom class loader which can hide the registered LoggerProvider 31 * depending on the value of a test.logger.hidesProvider system property. 32 * @author danielfuchs 33 */ 34 public class SystemClassLoader extends ClassLoader { 35 36 final public boolean hidesProvider; 37 38 public SystemClassLoader() { 39 hidesProvider = Boolean.getBoolean("test.logger.hidesProvider"); 40 } 41 public SystemClassLoader(ClassLoader parent) { 42 super(parent); 43 hidesProvider = Boolean.getBoolean("test.logger.hidesProvider"); 44 } 45 46 boolean accept(String name) { 47 final boolean res = !name.endsWith(LoggerFinder.class.getName()); 48 if (res == false) { 49 System.out.println("Hiding " + name); 50 } 51 return res; 52 } 53 54 @Override 55 public URL getResource(String name) { 56 if (hidesProvider && !accept(name)) { 57 return null; 58 } else { 59 return super.getResource(name); 60 } 61 } 62 63 class Enumerator implements Enumeration<URL> { 64 final Enumeration<URL> enumeration; 65 volatile URL next; 66 Enumerator(Enumeration<URL> enumeration) { 67 this.enumeration = enumeration; 68 } 69 70 @Override 71 public boolean hasMoreElements() { 72 if (next != null) return true; 73 if (!enumeration.hasMoreElements()) return false; 74 if (hidesProvider == false) return true; 75 next = enumeration.nextElement(); 76 if (accept(next.getPath())) return true; 77 next = null; 78 return hasMoreElements(); 79 } 80 81 @Override 82 public URL nextElement() { 83 final URL res = next == null ? enumeration.nextElement() : next; 84 next = null; 85 if (hidesProvider == false || accept(res.getPath())) return res; 86 return nextElement(); 87 } 88 } 89 90 @Override 91 public Enumeration<URL> getResources(String name) throws IOException { 92 final Enumeration<URL> enumeration = super.getResources(name); 93 return hidesProvider ? new Enumerator(enumeration) : enumeration; 94 } 95 96 97 98 }