1 33 package org.jruby.javasupport; 34 35 import java.lang.ref.WeakReference ; 36 import java.net.URL ; 37 import java.util.Collections ; 38 import java.util.HashMap ; 39 import java.util.Map ; 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 exceptionHandlers = new HashMap (); 52 53 private JRubyClassLoader javaClassLoader; 54 55 private Map 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 loadJavaClass(String className) { 63 try { 64 Class result = primitiveClass(className); 65 if (result == null) { 66 return Class.forName(className, true, javaClassLoader); 67 } 68 return result; 69 } catch (ClassNotFoundException cnfExcptn) { 70 throw runtime.newNameError("cannot load Java class " + className, className); 71 } 72 } 73 74 public JavaClass getJavaClassFromCache(Class clazz) { 75 WeakReference ref = (WeakReference ) 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 (clazz)); 82 } 83 84 public void addToClasspath(URL url) { 85 javaClassLoader.addURL(url); 87 } 88 89 public void defineExceptionHandler(String exceptionClass, RubyProc handler) { 90 exceptionHandlers.put(exceptionClass, handler); 91 } 92 93 public void handleNativeException(Throwable exception) { 94 if (exception instanceof RaiseException) { 95 throw (RaiseException) exception; 96 } 97 Class excptnClass = exception.getClass(); 98 RubyProc handler = (RubyProc)exceptionHandlers.get(excptnClass.getName()); 99 while (handler == null && 100 excptnClass != Throwable .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 exception) { 111 RaiseException re = RaiseException.createNativeRaiseException(runtime, exception); 112 113 return re; 114 } 115 116 private static Class primitiveClass(String 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 getJavaClassLoader() { 138 return javaClassLoader; 139 } 140 141 public JavaObject getJavaObjectFromCache(Object object) { 142 WeakReference ref = (WeakReference )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 (object)); 155 } 156 } 157 | Popular Tags |