01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.aspectwerkz.hook.impl;
05:
06: import sun.misc.Resource;
07: import sun.misc.URLClassPath;
08:
09: import java.io.File;
10: import java.io.IOException;
11: import java.lang.reflect.Method;
12: import java.net.URL;
13: import java.net.URLClassLoader;
14: import java.util.ArrayList;
15: import java.util.StringTokenizer;
16:
17: /**
18: * Very basic classloader that do online weaving. <p/>This classloader can be used thru several means
19: * <ul>
20: * <li>as a URLClassLoader in a custom development</li>
21: * <li>as a <i>MainClass </i> to allow on the fly weaving (without support for classloader hierarchy)</li>
22: * </ul>
23: * It can also be used for debugging step by step in any IDE
24: *
25: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
26: * TODO rewrite based on SUN src (definePackage missing)
27: */
28: public class WeavingClassLoader extends URLClassLoader {
29: public WeavingClassLoader(URL[] urls, ClassLoader parent) {
30: super (urls, parent);
31: }
32:
33: protected Class findClass(String name)
34: throws ClassNotFoundException {
35: String path = name.replace('.', '/').concat(".class");
36: Resource res = new URLClassPath(getURLs()).getResource(path,
37: false);
38: if (res != null) {
39: //definePackage(name.substring(0, name.lastIndexOf(".")), null, null);
40: try {
41: byte[] b = res.getBytes();
42: byte[] transformed = ClassPreProcessorHelper
43: .defineClass0Pre(this , name, b, 0, b.length,
44: null);
45: return defineClass(name, transformed, 0,
46: transformed.length);
47: } catch (IOException e) {
48: throw new ClassNotFoundException(e.getMessage());
49: }
50: } else {
51: throw new ClassNotFoundException(name);
52: }
53: }
54:
55: public static void main(String[] args) throws Exception {
56: String path = System.getProperty("java.class.path");
57: ArrayList paths = new ArrayList();
58: StringTokenizer st = new StringTokenizer(path,
59: File.pathSeparator);
60: while (st.hasMoreTokens()) {
61: paths.add((new File(st.nextToken())).getCanonicalFile()
62: .toURL());
63: }
64:
65: //System.setProperty("aspectwerkz.transform.verbose", "yes");
66: //System.setProperty("aspectwerkz.transform.dump", "*");
67: //System.setProperty("aspectwerkz.definition.file", "...");
68: // TODO check child of extension classloader instead of boot classloader
69: ClassLoader cl = new WeavingClassLoader((URL[]) paths
70: .toArray(new URL[] {}), ClassLoader
71: .getSystemClassLoader().getParent());
72: Thread.currentThread().setContextClassLoader(cl);
73: String s = args[0];
74: String[] args1 = new String[args.length - 1];
75: if (args1.length > 0) {
76: System.arraycopy(args, 1, args1, 0, args.length - 1);
77: }
78: Class class1 = Class.forName(s, false, cl);
79: Method method = class1.getMethod("main",
80: new Class[] { String[].class });
81: method.invoke(null, new Object[] { args1 });
82: }
83: }
|