1 /* 2 * Copyright (c) 2008, 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 24 import java.lang.instrument.*; 25 import java.net.*; 26 import java.io.*; 27 import java.security.*; 28 29 /** 30 * Test Java Agent 31 * 32 * @author Daryl Puryear 33 * @copyright 1999-2004 Wily Technology, Inc. All rights reserved. 34 */ 35 public class ParallelTransformerLoaderAgent 36 { 37 private static URL sURL; 38 private static ClassLoader sClassLoader; 39 40 public static synchronized ClassLoader getClassLoader()41 getClassLoader() 42 { 43 return sClassLoader; 44 } 45 46 public static synchronized void generateNewClassLoader()47 generateNewClassLoader() 48 { 49 sClassLoader = new URLClassLoader(new URL[] {sURL}); 50 } 51 52 public static void premain( String agentArgs, Instrumentation instrumentation)53 premain( String agentArgs, 54 Instrumentation instrumentation) 55 throws Exception 56 { 57 if (agentArgs == null || agentArgs == "") 58 { 59 System.err.println("Error: No jar file name provided, test will not run."); 60 return; 61 } 62 63 sURL = (new File(agentArgs)).toURL(); 64 System.out.println("Using jar file: " + sURL); 65 generateNewClassLoader(); 66 67 instrumentation.addTransformer(new TestTransformer()); 68 } 69 70 private static class TestTransformer 71 implements ClassFileTransformer 72 { 73 public byte[] transform( ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer)74 transform( ClassLoader loader, 75 String className, 76 Class classBeingRedefined, 77 ProtectionDomain protectionDomain, 78 byte[] classfileBuffer) 79 throws IllegalClassFormatException 80 { 81 String tName = Thread.currentThread().getName(); 82 83 // Load additional classes when called from thread 'TestThread' 84 // When a class is loaded during a callback handling the boot loader, we can 85 // run into ClassCircularityError if the ClassFileLoadHook is set early enough 86 // to catch classes needed during class loading, e.g. 87 // sun.misc.URLClassPath$JarLoader$2. 88 // The goal of the test is to stress class loading on the test class loaders. 89 90 if (tName.equals("TestThread") && loader != null) 91 { 92 loadClasses(3); 93 } 94 return null; 95 } 96 97 public static void loadClasses( int index)98 loadClasses( int index) 99 { 100 ClassLoader loader = ParallelTransformerLoaderAgent.getClassLoader(); 101 try 102 { 103 Class.forName("TestClass" + index, true, loader); 104 } 105 catch (Exception e) 106 { 107 e.printStackTrace(); 108 } 109 } 110 } 111 } 112