KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > proxy > compiler > Runtime


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.proxy.compiler;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.Field JavaDoc;
26
27 import java.io.InputStream JavaDoc;
28
29 import java.net.URL JavaDoc;
30 import java.security.PrivilegedAction JavaDoc;
31 import java.security.AccessController JavaDoc;
32
33 /**
34  * Manages bytecode assembly for dynamic proxy generation.
35  * <p/>
36  * <p>This is the only data needed at runtime.
37  *
38  * @author Unknown
39  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
40  * @version <tt>$Revision: 37459 $</tt>
41  */

42 public class Runtime
43    extends ClassLoader JavaDoc
44 {
45    /**
46     * The field name of the runtime target proxies Runtime object.
47     */

48    public final static String JavaDoc RUNTIME_FN = "runtime";
49
50    /**
51     * Construct a new <tt>Runtime</tt>
52     *
53     * @param parent The parent classloader to delegate to.
54     */

55    public Runtime(ClassLoader JavaDoc parent)
56    {
57       super(parent);
58    }
59
60    // These members are common utilities used by ProxyTarget classes.
61
// They are all public so they can be linked to from generated code.
62
// I.e., they are the runtime support for the code compiled below.
63

64    Class JavaDoc targetTypes[];
65    Method JavaDoc methods[];
66    ProxyCompiler compiler;// temporary!
67

68    public Class JavaDoc[] copyTargetTypes()
69    {
70       return (Class JavaDoc[]) targetTypes.clone();
71    }
72
73    public Object JavaDoc invoke(InvocationHandler invocationHandler, int methodNum, Object JavaDoc values[])
74       throws Throwable JavaDoc
75    {
76       return invocationHandler.invoke(null, methods[methodNum], values);
77    }
78
79    void makeProxyType(ProxyCompiler compiler)
80       throws Exception JavaDoc
81    {
82       this.compiler = compiler; // temporary, for use during loading
83
byte code[] = compiler.getCode();
84
85       compiler.proxyType = super.defineClass(compiler.getProxyClassName(), code, 0, code.length);
86       super.resolveClass(compiler.proxyType);
87
88       // set the Foo$Impl.info pointer to myself
89
Field JavaDoc field = compiler.proxyType.getField(RUNTIME_FN);
90       field.set(null, this);
91
92       compiler = null;
93    }
94
95    ClassLoader JavaDoc getTargetClassLoader()
96    {
97       PrivilegedAction JavaDoc action = new PrivilegedAction JavaDoc()
98       {
99          public Object JavaDoc run()
100          {
101             return getParent();
102          }
103       };
104       return (ClassLoader JavaDoc) AccessController.doPrivileged(action);
105    }
106
107    public synchronized Class JavaDoc loadClass(String JavaDoc name, boolean resolve)
108       throws ClassNotFoundException JavaDoc
109    {
110       // isn't this redundant?
111
if (name.endsWith("$Proxy") && name.equals(compiler.getProxyClassName()))
112       {
113          return compiler.proxyType;
114       }
115
116       // delegate to the original class loader
117
ClassLoader JavaDoc cl = getTargetClassLoader();
118       if (cl == null)
119       {
120          return super.findSystemClass(name);
121       }
122
123       return cl.loadClass(name);
124    }
125
126    /**
127     * Delegate to the original class loader.
128     */

129    public InputStream JavaDoc getResourceAsStream(String JavaDoc name)
130    {
131       ClassLoader JavaDoc cl = getTargetClassLoader();
132
133       if (cl == null)
134       {
135          return super.getSystemResourceAsStream(name);
136       }
137
138       return cl.getResourceAsStream(name);
139    }
140
141    /**
142     * Delegate to the original class loader.
143     */

144    public URL JavaDoc getResource(String JavaDoc name)
145    {
146       ClassLoader JavaDoc 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