KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > hook > GeneratePluggableInstrumentedClassLoader


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.hook;
23
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import javassist.CannotCompileException;
29 import javassist.ClassPool;
30 import javassist.CtClass;
31 import javassist.CtMethod;
32 import javassist.CtNewMethod;
33 import javassist.LoaderClassPath;
34 import javassist.Modifier;
35 import javassist.NotFoundException;
36
37 /**
38  * Generate the instrumented version of the classloader and store it in the filesystem.
39  *
40  * @author kevin
41  */

42 public class GeneratePluggableInstrumentedClassLoader
43 {
44    private static void declare5(CtClass clazz, CtMethod method) throws NotFoundException, CannotCompileException
45    {
46       method.setName("wrappedDefineClass");
47       CtMethod wrapper = CtNewMethod.make(Modifier.PROTECTED, method.getReturnType(), "defineClass", method.getParameterTypes(), method.getExceptionTypes(), null, clazz);
48
49       String JavaDoc code = "{"
50               + " byte[] newBytes = org.jboss.aop.hook.JDK14TransformerManager.transform($0, $1, $2) ;"
51               + " if (newBytes != (byte[])null) {"
52               + " return wrappedDefineClass($1, newBytes, 0, newBytes.length, $5); "
53               + " } else {"
54               + " return wrappedDefineClass($1, $2, $3, $4, $5); "
55               + " }"
56               + "}";
57       wrapper.setBody(code);
58       clazz.addMethod(wrapper);
59
60    }
61
62    /**
63     * Get the instrumented version of the class loader.
64     *
65     * @return The instrumented version of the class loader.
66     * @throws javassist.NotFoundException if the class loader cannot be found.
67     * @throws javassist.CannotCompileException
68     * if the class cannot be compiled.
69     * @throws java.io.IOException If the class bytecodes cannot be obtained.
70     */

71    public static byte[] getInstrumentedClassLoader()
72            throws NotFoundException, IOException JavaDoc, CannotCompileException
73    {
74       ClassPool classpool = ClassPool.getDefault();
75       classpool = ClassPool.getDefault();
76       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
77       classpool.insertClassPath(new LoaderClassPath(cl));
78
79       final CtClass clazz = classpool.get(ClassLoader JavaDoc.class.getName());
80       CtMethod[] methods = clazz.getDeclaredMethods();
81       for (int i = 0; i < methods.length; i++)
82       {
83          if (methods[i].getName().equals("defineClass"))
84          {
85             if (methods[i].getParameterTypes().length == 5
86                      && methods[i].getParameterTypes()[1].isArray())
87             {
88                declare5(clazz, methods[i]);
89             }
90          }
91       }
92       return clazz.toBytecode();
93    }
94
95    /**
96     * Store the instrumented classloader in the filesystem.
97     *
98     * @param args The filename as the first argument.
99     */

100    public static void main(final String JavaDoc[] args)
101    {
102       if (args.length != 1)
103       {
104          System.err.println("Usage: java " + GeneratePluggableInstrumentedClassLoader.class.getName() + " <output directory>");
105          System.exit(1);
106       }
107       final String JavaDoc filename = args[0] + File.separatorChar +
108               "java" + File.separatorChar +
109               "lang" + File.separatorChar +
110               "ClassLoader.class";
111       final File JavaDoc file = new File JavaDoc(filename);
112       if (file.exists())
113       {
114          if (!file.canWrite())
115          {
116             System.err.println("Cannot write to existing file: " + file.getAbsolutePath());
117             System.exit(2);
118          }
119       }
120       else
121       {
122          final File JavaDoc dir = file.getParentFile();
123          if (!dir.exists())
124          {
125             dir.mkdirs();
126          }
127          if (!dir.canWrite())
128          {
129             System.err.println("Cannot write to parent directory: " + dir.getAbsolutePath());
130          }
131       }
132
133       final byte[] bytes;
134       try
135       {
136          bytes = getInstrumentedClassLoader();
137       }
138       catch (final Throwable JavaDoc th)
139       {
140          System.err.println("Unexpected exception caught during instrumentation: " + th.getMessage());
141          th.printStackTrace(System.err);
142          System.exit(5);
143          return;
144       }
145
146       try
147       {
148          final FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(file);
149          fos.write(bytes);
150          fos.close();
151       }
152       catch (final IOException JavaDoc ioe)
153       {
154          System.err.println("Unexpected exception caught while writing class file: " + ioe.getMessage());
155          ioe.printStackTrace(System.err);
156          System.exit(6);
157       }
158    }
159 }
160
Popular Tags