1 /*
   2  * Copyright (c) 2013, 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 import java.lang.reflect.InvocationTargetException;
  24 import java.lang.reflect.Method;
  25 import java.net.URL;
  26 import java.net.URLClassLoader;
  27 
  28 /**
  29  * This class is loaded by a class loader that can see the resource. It creates
  30  * a new classloader for LoadItUp2 which cannot see the resource.  So, 2 levels
  31  * up the call chain we have a class/classloader that can see the resource, but
  32  * 1 level up the class/classloader cannot.
  33  *
  34  * @author Jim Gish
  35  */
  36 public class LoadItUp2Invoker {
  37     private URLClassLoader cl;
  38     private String rbName;
  39     private Object loadItUp2;
  40     private Method testMethod;
  41 
  42     public void setup(URL[] urls, String rbName) throws
  43             ReflectiveOperationException {
  44         this.cl = new URLClassLoader(urls, null);
  45         this.rbName = rbName;
  46         // Using this new classloader, load the actual test class
  47         // which is now two levels removed from the original caller
  48         Class<?> loadItUp2Clazz = Class.forName("LoadItUp2", true , cl);
  49         this.loadItUp2 = loadItUp2Clazz.newInstance();
  50         this.testMethod = loadItUp2Clazz.getMethod("test", String.class);
  51     }
  52 
  53     public Boolean test() throws Throwable {
  54         try {
  55             return (Boolean) testMethod.invoke(loadItUp2, rbName);
  56         } catch (InvocationTargetException ex) {
  57             throw ex.getTargetException();
  58         }
  59     }
  60 }