Path: blob/master/test/jdk/java/lang/System/LoggerFinder/internal/backend/SystemClassLoader.java
41161 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.IOException;24import java.net.URL;25import java.util.Enumeration;26import java.lang.System.LoggerFinder;2728/**29* A custom class loader which can hide the registered LoggerProvider30* depending on the value of a test.logger.hidesProvider system property.31* @author danielfuchs32*/33public class SystemClassLoader extends ClassLoader {3435final public boolean hidesProvider;3637public SystemClassLoader() {38hidesProvider = Boolean.getBoolean("test.logger.hidesProvider");39}40public SystemClassLoader(ClassLoader parent) {41super(parent);42hidesProvider = Boolean.getBoolean("test.logger.hidesProvider");43}4445boolean accept(String name) {46final boolean res = !name.endsWith(LoggerFinder.class.getName());47if (res == false) {48System.out.println("Hiding " + name);49}50return res;51}5253@Override54public URL getResource(String name) {55if (hidesProvider && !accept(name)) {56return null;57} else {58return super.getResource(name);59}60}6162class Enumerator implements Enumeration<URL> {63final Enumeration<URL> enumeration;64volatile URL next;65Enumerator(Enumeration<URL> enumeration) {66this.enumeration = enumeration;67}6869@Override70public boolean hasMoreElements() {71if (next != null) return true;72if (!enumeration.hasMoreElements()) return false;73if (hidesProvider == false) return true;74next = enumeration.nextElement();75if (accept(next.getPath())) return true;76next = null;77return hasMoreElements();78}7980@Override81public URL nextElement() {82final URL res = next == null ? enumeration.nextElement() : next;83next = null;84if (hidesProvider == false || accept(res.getPath())) return res;85return nextElement();86}87}8889@Override90public Enumeration<URL> getResources(String name) throws IOException {91final Enumeration<URL> enumeration = super.getResources(name);92return hidesProvider ? new Enumerator(enumeration) : enumeration;93}94959697}9899100