1 22 package org.jboss.proxy.compiler; 23 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.Field ; 26 27 import java.io.InputStream ; 28 29 import java.net.URL ; 30 import java.security.PrivilegedAction ; 31 import java.security.AccessController ; 32 33 42 public class Runtime 43 extends ClassLoader 44 { 45 48 public final static String RUNTIME_FN = "runtime"; 49 50 55 public Runtime(ClassLoader parent) 56 { 57 super(parent); 58 } 59 60 64 Class targetTypes[]; 65 Method methods[]; 66 ProxyCompiler compiler; 68 public Class [] copyTargetTypes() 69 { 70 return (Class []) targetTypes.clone(); 71 } 72 73 public Object invoke(InvocationHandler invocationHandler, int methodNum, Object values[]) 74 throws Throwable 75 { 76 return invocationHandler.invoke(null, methods[methodNum], values); 77 } 78 79 void makeProxyType(ProxyCompiler compiler) 80 throws Exception 81 { 82 this.compiler = compiler; byte code[] = compiler.getCode(); 84 85 compiler.proxyType = super.defineClass(compiler.getProxyClassName(), code, 0, code.length); 86 super.resolveClass(compiler.proxyType); 87 88 Field field = compiler.proxyType.getField(RUNTIME_FN); 90 field.set(null, this); 91 92 compiler = null; 93 } 94 95 ClassLoader getTargetClassLoader() 96 { 97 PrivilegedAction action = new PrivilegedAction () 98 { 99 public Object run() 100 { 101 return getParent(); 102 } 103 }; 104 return (ClassLoader ) AccessController.doPrivileged(action); 105 } 106 107 public synchronized Class loadClass(String name, boolean resolve) 108 throws ClassNotFoundException 109 { 110 if (name.endsWith("$Proxy") && name.equals(compiler.getProxyClassName())) 112 { 113 return compiler.proxyType; 114 } 115 116 ClassLoader cl = getTargetClassLoader(); 118 if (cl == null) 119 { 120 return super.findSystemClass(name); 121 } 122 123 return cl.loadClass(name); 124 } 125 126 129 public InputStream getResourceAsStream(String name) 130 { 131 ClassLoader cl = getTargetClassLoader(); 132 133 if (cl == null) 134 { 135 return super.getSystemResourceAsStream(name); 136 } 137 138 return cl.getResourceAsStream(name); 139 } 140 141 144 public URL getResource(String name) 145 { 146 ClassLoader cl = getTargetClassLoader(); 147 148 if (cl == null) 149 { 150 return super.getSystemResource(name); 151 } 152 153 return cl.getResource(name); 154 } 155 } 156 | Popular Tags |