KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > hook > ClassLoaderPatcher


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.hook;
5
6 import java.io.BufferedOutputStream JavaDoc;
7 import java.io.ByteArrayOutputStream JavaDoc;
8 import java.io.DataOutputStream JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13
14 /**
15  * Utility methods to manipulate class redefinition of java.lang.ClassLoader in xxxStarter
16  *
17  * @author <a HREF="mailto:alex@gnilux.com">Alexandre Vasseur </a>
18  */

19 public class ClassLoaderPatcher {
20   /**
21    * Converts an input stream to a byte[]
22    */

23   public static byte[] inputStreamToByteArray(InputStream JavaDoc is) throws IOException JavaDoc {
24     ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
25     for (int b = is.read(); b != -1; b = is.read()) {
26       os.write(b);
27     }
28     return os.toByteArray();
29   }
30
31   /**
32    * Gets the bytecode of the modified java.lang.ClassLoader using given ClassLoaderPreProcessor class name
33    */

34   static byte[] getPatchedClassLoader(String JavaDoc preProcessorName) {
35     byte[] abyte = null;
36     InputStream JavaDoc is = null;
37     try {
38       is = ClassLoader.getSystemClassLoader().getParent().getResourceAsStream("java/lang/ClassLoader.class");
39       abyte = inputStreamToByteArray(is);
40     } catch (IOException JavaDoc e) {
41       throw new Error JavaDoc("failed to read java.lang.ClassLoader: " + e.toString());
42     } finally {
43       try {
44         is.close();
45       } catch (Exception JavaDoc e) {
46         ;
47       }
48     }
49     if (preProcessorName != null) {
50       try {
51         ClassLoaderPreProcessor clpi = (ClassLoaderPreProcessor) Class.forName(preProcessorName).newInstance();
52         abyte = clpi.preProcess(abyte);
53       } catch (Exception JavaDoc e) {
54         System.err.println("failed to instrument java.lang.ClassLoader: preprocessor not found");
55         e.printStackTrace();
56       }
57     }
58     return abyte;
59   }
60
61   /**
62    * Dump bytecode bytes in dir/className.class directory, created if needed
63    */

64   private static void writeClass(String JavaDoc className, byte[] bytes, String JavaDoc dir) {
65     String JavaDoc filename = dir + File.separatorChar + className.replace('.', File.separatorChar) + ".class";
66     int pos = filename.lastIndexOf(File.separatorChar);
67     if (pos > 0) {
68       String JavaDoc finalDir = filename.substring(0, pos);
69       (new File JavaDoc(finalDir)).mkdirs();
70     }
71     try {
72       DataOutputStream JavaDoc out = new DataOutputStream JavaDoc(new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(filename)));
73       out.write(bytes);
74       out.close();
75     } catch (IOException JavaDoc e) {
76       System.err.println("failed to write " + className + " in " + dir);
77       e.printStackTrace();
78     }
79   }
80
81   /**
82    * Patch java.lang.ClassLoader with preProcessorName instance and dump class bytecode in dir
83    */

84   public static void patchClassLoader(String JavaDoc preProcessorName, String JavaDoc dir) {
85     byte[] cl = getPatchedClassLoader(preProcessorName);
86     writeClass("java.lang.ClassLoader", cl, dir);
87   }
88
89 }
Popular Tags