KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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