KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > javasupport > JavaSupport


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
15  * Copyright (C) 2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
16  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
17  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
18  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
19  * Copyright (C) 2005 Charles O Nutter <headius@headius.com>
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * either of the GNU General Public License Version 2 or later (the "GPL"),
23  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
24  * in which case the provisions of the GPL or the LGPL are applicable instead
25  * of those above. If you wish to allow use of your version of this file only
26  * under the terms of either the GPL or the LGPL, and not to allow others to
27  * use your version of this file under the terms of the CPL, indicate your
28  * decision by deleting the provisions above and replace them with the notice
29  * and other provisions required by the GPL or the LGPL. If you do not delete
30  * the provisions above, a recipient may use your version of this file under
31  * the terms of any one of the CPL, the GPL or the LGPL.
32  ***** END LICENSE BLOCK *****/

33 package org.jruby.javasupport;
34
35 import java.lang.ref.WeakReference JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.util.Collections JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Map JavaDoc;
40
41 import org.jruby.Ruby;
42 import org.jruby.RubyProc;
43 import org.jruby.util.WeakIdentityHashMap;
44 import org.jruby.util.JRubyClassLoader;
45 import org.jruby.exceptions.RaiseException;
46 import org.jruby.runtime.builtin.IRubyObject;
47
48 public class JavaSupport {
49     private Ruby runtime;
50
51     private Map JavaDoc exceptionHandlers = new HashMap JavaDoc();
52
53     private JRubyClassLoader javaClassLoader;
54
55     private Map JavaDoc instanceCache = Collections.synchronizedMap(new WeakIdentityHashMap(100));
56
57     public JavaSupport(Ruby ruby) {
58         this.runtime = ruby;
59         this.javaClassLoader = ruby.getJRubyClassLoader();
60     }
61
62     public Class JavaDoc loadJavaClass(String JavaDoc className) {
63         try {
64             Class JavaDoc result = primitiveClass(className);
65             if (result == null) {
66                 return Class.forName(className, true, javaClassLoader);
67             }
68             return result;
69         } catch (ClassNotFoundException JavaDoc cnfExcptn) {
70             throw runtime.newNameError("cannot load Java class " + className, className);
71         }
72     }
73     
74     public JavaClass getJavaClassFromCache(Class JavaDoc clazz) {
75         WeakReference JavaDoc ref = (WeakReference JavaDoc) instanceCache.get(clazz);
76         
77         return ref == null ? null : (JavaClass) ref.get();
78     }
79     
80     public void putJavaClassIntoCache(JavaClass clazz) {
81         instanceCache.put(clazz.javaClass(), new WeakReference JavaDoc(clazz));
82     }
83     
84     public void addToClasspath(URL JavaDoc url) {
85         // javaClassLoader = URLClassLoader.newInstance(new URL[] { url }, javaClassLoader);
86
javaClassLoader.addURL(url);
87     }
88
89     public void defineExceptionHandler(String JavaDoc exceptionClass, RubyProc handler) {
90         exceptionHandlers.put(exceptionClass, handler);
91     }
92
93     public void handleNativeException(Throwable JavaDoc exception) {
94         if (exception instanceof RaiseException) {
95             throw (RaiseException) exception;
96         }
97         Class JavaDoc excptnClass = exception.getClass();
98         RubyProc handler = (RubyProc)exceptionHandlers.get(excptnClass.getName());
99         while (handler == null &&
100                excptnClass != Throwable JavaDoc.class) {
101             excptnClass = excptnClass.getSuperclass();
102         }
103         if (handler != null) {
104             handler.call(new IRubyObject[]{JavaUtil.convertJavaToRuby(runtime, exception)});
105         } else {
106             throw createRaiseException(exception);
107         }
108     }
109
110     private RaiseException createRaiseException(Throwable JavaDoc exception) {
111         RaiseException re = RaiseException.createNativeRaiseException(runtime, exception);
112         
113         return re;
114     }
115
116     private static Class JavaDoc primitiveClass(String JavaDoc name) {
117         if (name.equals("long")) {
118             return Long.TYPE;
119         } else if (name.equals("int")) {
120             return Integer.TYPE;
121         } else if (name.equals("boolean")) {
122             return Boolean.TYPE;
123         } else if (name.equals("char")) {
124             return Character.TYPE;
125         } else if (name.equals("short")) {
126             return Short.TYPE;
127         } else if (name.equals("byte")) {
128             return Byte.TYPE;
129         } else if (name.equals("float")) {
130             return Float.TYPE;
131         } else if (name.equals("double")) {
132             return Double.TYPE;
133         }
134         return null;
135     }
136
137     public ClassLoader JavaDoc getJavaClassLoader() {
138         return javaClassLoader;
139     }
140     
141     public JavaObject getJavaObjectFromCache(Object JavaDoc object) {
142         WeakReference JavaDoc ref = (WeakReference JavaDoc)instanceCache.get(object);
143         if (ref == null) {
144             return null;
145         }
146         JavaObject javaObject = (JavaObject) ref.get();
147         if (javaObject != null && javaObject.getValue() == object) {
148             return javaObject;
149         }
150         return null;
151     }
152     
153     public void putJavaObjectIntoCache(JavaObject object) {
154         instanceCache.put(object.getValue(), new WeakReference JavaDoc(object));
155     }
156 }
157
Popular Tags