1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 7 8 9 import java.io.IOException; 10 import java.net.URL; 11 import java.util.Enumeration; 12 import java.lang.System.LoggerFinder; 13 14 /** 15 * A custom class loader which can hide the registered LoggerProvider 16 * depending on the value of a test.logger.hidesProvider system property. 17 * @author danielfuchs 18 */ 19 public class SystemClassLoader extends ClassLoader { 20 21 final public boolean hidesProvider; 22 23 public SystemClassLoader() { 24 hidesProvider = Boolean.getBoolean("test.logger.hidesProvider"); 25 } 26 public SystemClassLoader(ClassLoader parent) { 27 super(parent); 28 hidesProvider = Boolean.getBoolean("test.logger.hidesProvider"); 29 } 30 31 boolean accept(String name) { 32 final boolean res = !name.endsWith(LoggerFinder.class.getName()); 33 if (res == false) { 34 System.out.println("Hiding " + name); 35 } 36 return res; 37 } 38 39 @Override 40 public URL getResource(String name) { 41 if (hidesProvider && !accept(name)) { 42 return null; 43 } else { 44 return super.getResource(name); 45 } 46 } 47 48 class Enumerator implements Enumeration<URL> { 49 final Enumeration<URL> enumeration; 50 volatile URL next; 51 Enumerator(Enumeration<URL> enumeration) { 52 this.enumeration = enumeration; 53 } 54 55 @Override 56 public boolean hasMoreElements() { 57 if (next != null) return true; 58 if (!enumeration.hasMoreElements()) return false; 59 if (hidesProvider == false) return true; 60 next = enumeration.nextElement(); 61 if (accept(next.getPath())) return true; 62 next = null; 63 return hasMoreElements(); 64 } 65 66 @Override 67 public URL nextElement() { 68 final URL res = next == null ? enumeration.nextElement() : next; 69 next = null; 70 if (hidesProvider == false || accept(res.getPath())) return res; 71 return nextElement(); 72 } 73 } 74 75 @Override 76 public Enumeration<URL> getResources(String name) throws IOException { 77 final Enumeration<URL> enumeration = super.getResources(name); 78 return hidesProvider ? new Enumerator(enumeration) : enumeration; 79 } 80 81 82 83 }