< prev index next >

test/jdk/java/lang/instrument/ParallelTransformerLoaderAgent.java

Print this page

  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
 41         getClassLoader()
 42         {
 43                 return sClassLoader;
 44         }
 45 
 46         public static synchronized void
 47         generateNewClassLoader()
 48         {
 49                 sClassLoader = new URLClassLoader(new URL[] {sURL});
 50         }
 51 
 52         public static void
 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[]
 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
 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 }

  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 
 25 import java.io.File;
 26 import java.lang.instrument.ClassFileTransformer;
 27 import java.lang.instrument.IllegalClassFormatException;
 28 import java.lang.instrument.Instrumentation;
 29 import java.net.URL;
 30 import java.net.URLClassLoader;
 31 import java.security.ProtectionDomain;
 32 
 33 /**
 34  *      Test Java Agent
 35  *
 36  *      @author Daryl Puryear
 37  *      @copyright 1999-2004 Wily Technology, Inc.  All rights reserved.
 38  */
 39 public class ParallelTransformerLoaderAgent {
 40     private static URL sURL;
 41     private static ClassLoader sClassLoader;

 42 
 43     public static synchronized ClassLoader getClassLoader() {
 44         return sClassLoader;
 45     }


 46 
 47     public static synchronized void generateNewClassLoader() {
 48         sClassLoader = new URLClassLoader(new URL[]{sURL});
 49     }


 50 
 51     public static void premain(String agentArgs, Instrumentation instrumentation) throws Exception {
 52         if (agentArgs == null || agentArgs == "") {
 53             System.err.println("Error: No jar file name provided, test will not run.");
 54             return;
 55         }





 56 
 57         sURL = (new File(agentArgs)).toURI().toURL();
 58         System.out.println("Using jar file: " + sURL);
 59         generateNewClassLoader();
 60 
 61         instrumentation.addTransformer(new TestTransformer());
 62     }
 63 
 64     private static class TestTransformer implements ClassFileTransformer {
 65         public byte[] transform(ClassLoader loader,



 66                                 String className,
 67                                 Class classBeingRedefined,
 68                                 ProtectionDomain protectionDomain,
 69                                 byte[] classfileBuffer)
 70                 throws IllegalClassFormatException {
 71             String tName = Thread.currentThread().getName();

 72 
 73             // Load additional classes when called from thread 'TestThread'
 74             // When a class is loaded during a callback handling the boot loader, we can
 75             // run into ClassCircularityError if the ClassFileLoadHook is set early enough
 76             // to catch classes needed during class loading, e.g.
 77             //          sun.misc.URLClassPath$JarLoader$2.
 78             // The goal of the test is to stress class loading on the test class loaders.
 79 
 80             if (tName.equals("TestThread") && loader != null) {
 81                 loadClasses(3);
 82             }
 83             return null;
 84         }

 85 
 86         public static void loadClasses(int index) {
 87             ClassLoader loader = ParallelTransformerLoaderAgent.getClassLoader();
 88             try {
 89                 Class.forName("TestClass" + index, true, loader);
 90             } catch (Exception e) {
 91                 e.printStackTrace();
 92             }






 93         }
 94     }
 95 }
< prev index next >