6394N/A/*
6394N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
6394N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6394N/A *
6394N/A * This code is free software; you can redistribute it and/or modify it
6394N/A * under the terms of the GNU General Public License version 2 only, as
6394N/A * published by the Free Software Foundation.
6394N/A *
6394N/A * This code is distributed in the hope that it will be useful, but WITHOUT
6394N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6394N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6394N/A * version 2 for more details (a copy is included in the LICENSE file that
6394N/A * accompanied this code).
6394N/A *
6394N/A * You should have received a copy of the GNU General Public License version
6394N/A * 2 along with this work; if not, write to the Free Software Foundation,
6394N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6394N/A *
6394N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6394N/A * or visit www.oracle.com if you need additional information or have any
6394N/A * questions.
6394N/A */
6394N/A
6394N/Aimport java.lang.reflect.InvocationTargetException;
6394N/Aimport java.lang.reflect.Method;
6394N/Aimport java.net.URL;
6394N/Aimport java.net.URLClassLoader;
6394N/Aimport java.nio.file.Paths;
6394N/A
6394N/A/**
6394N/A * This class constructs a scenario where a bundle is accessible on the call
6394N/A * stack two levels up from the call to getLogger(), but not on the immediate
6394N/A * caller. This tests that getLogger() isn't doing a stack crawl more than one
6394N/A * level up to find a bundle.
6394N/A *
6394N/A * @author Jim Gish
6394N/A */
6394N/Apublic class TwiceIndirectlyLoadABundle {
6394N/A
6394N/A private final static String rbName = "StackSearchableResource";
6394N/A
6394N/A public boolean loadAndTest() throws Throwable {
6394N/A // Find out where we are running from so we can setup the URLClassLoader URLs
6394N/A // test.src and test.classes will be set if running in jtreg, but probably
6394N/A // not otherwise
6394N/A String testDir = System.getProperty("test.src", System.getProperty("user.dir"));
6394N/A String testClassesDir = System.getProperty("test.classes",
6394N/A System.getProperty("user.dir"));
6394N/A URL[] urls = new URL[2];
6394N/A
6394N/A // Allow for both jtreg and standalone cases here
6394N/A // Unlike the 1-level test where we can get the bundle from the caller's
6394N/A // class loader, for this one we don't want to expose the resource directory
6394N/A // to the next class. That way we're invoking the LoadItUp2Invoker class
6394N/A // from this class that does have access to the resources (two levels
6394N/A // up the call stack), but the Invoker itself won't have access to resource
6394N/A urls[0] = Paths.get(testDir,"resources").toUri().toURL();
6394N/A urls[1] = Paths.get(testClassesDir).toUri().toURL();
6394N/A
6394N/A // Make sure we can find it via the URLClassLoader
6394N/A URLClassLoader yetAnotherResourceCL = new URLClassLoader(urls, null);
6394N/A Class<?> loadItUp2InvokerClazz = Class.forName("LoadItUp2Invoker", true,
6394N/A yetAnotherResourceCL);
6394N/A ClassLoader actual = loadItUp2InvokerClazz.getClassLoader();
6394N/A if (actual != yetAnotherResourceCL) {
6394N/A throw new Exception("LoadItUp2Invoker was loaded by an unexpected CL: "
6394N/A + actual);
6394N/A }
6394N/A Object loadItUp2Invoker = loadItUp2InvokerClazz.newInstance();
6394N/A
6394N/A Method setupMethod = loadItUp2InvokerClazz.getMethod("setup",
6394N/A urls.getClass(), String.class);
6394N/A try {
6394N/A // For the next class loader we create, we want to leave off
6394N/A // the resources. That way loadItUp2Invoker will have access to
6394N/A // them, but the next class won't.
6394N/A URL[] noResourceUrl = new URL[1];
6394N/A noResourceUrl[0] = urls[1]; // from above -- just the test classes
6394N/A setupMethod.invoke(loadItUp2Invoker, noResourceUrl, rbName);
6394N/A } catch (InvocationTargetException ex) {
6394N/A throw ex.getTargetException();
6394N/A }
6394N/A
6394N/A Method testMethod = loadItUp2InvokerClazz.getMethod("test");
6394N/A try {
6394N/A return (Boolean) testMethod.invoke(loadItUp2Invoker);
6394N/A } catch (InvocationTargetException ex) {
6394N/A throw ex.getTargetException();
6394N/A }
6394N/A }
6394N/A}