Path: blob/master/test/jdk/java/net/URL/HandlerLoop.java
41149 views
/*1* Copyright (c) 1998, 2013, 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.lang.reflect.InvocationTargetException;24import java.net.URL;25import java.net.URLStreamHandler;26import java.net.URLStreamHandlerFactory;2728/*29* @test30* @bug 413503131* @summary Test bootstrap problem when a URLStreamHandlerFactory is loaded32* by the application class loader.33* @modules java.base/sun.net.www.protocol.file34* @run main/othervm HandlerLoop35*/36public class HandlerLoop {3738public static void main(String args[]) throws Exception {39URL.setURLStreamHandlerFactory(40new HandlerFactory("sun.net.www.protocol"));41URL url = new URL("file:///bogus/index.html");42System.out.println("url = " + url);43url.openConnection();44}4546private static class HandlerFactory implements URLStreamHandlerFactory {47private String pkg;4849HandlerFactory(String pkg) {50this.pkg = pkg;51}5253public URLStreamHandler createURLStreamHandler(String protocol) {54String name = pkg + "." + protocol + ".Handler";55System.out.println("Loading handler class: " + name);56// Loading this dummy class demonstrates the bootstrap57// problem as the stream handler factory will be reentered58// over and over again if the application class loader59// shares the same stream handler factory.60new Dummy();61try {62Class<?> c = Class.forName(name);63return (URLStreamHandler)c.getDeclaredConstructor().newInstance();64} catch (ClassNotFoundException |65IllegalAccessException |66InstantiationException |67NoSuchMethodException |68InvocationTargetException e) {69e.printStackTrace();70}71return null;72}73}7475private static class Dummy {76}77}787980