1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
package org.perl.inline.java ;
import java.net.* ;
import java.util.* ;
import java.io.* ;
import java.lang.reflect.* ;
/*
This is the ClassLoader that loads the users code. It is also
used to pass reflection calls to the InlineJavaUserClassLink
so that it will execute them.
*/
class InlineJavaUserClassLoader extends URLClassLoader {
private HashMap<URL, String> urls = new HashMap<>() ;
private Object link = null ;
private Method invoke = null ;
private Method get = null ;
private Method set = null ;
private Method array_get = null ;
private Method array_set = null ;
private Method create = null ;
public InlineJavaUserClassLoader(){
// Added Thread.currentThread().getContextClassLoader() so that the code works
// in Tomcat and possibly other embedded environments asa well.
super(new URL [] {}, Thread.currentThread().getContextClassLoader()) ;
}
public void AddClassPath(String path) throws InlineJavaException {
try {
File p = new File(path) ;
URL u = p.toURI().toURL() ;
if (urls.get(u) == null){
urls.put(u, "1") ;
addURL(u) ;
InlineJavaUtils.debug(2, "added " + u + " to classpath") ;
}
}
catch (MalformedURLException e){
throw new InlineJavaException("Can't add invalid classpath entry '" + path + "'") ;
}
}
synchronized private void check_link() throws InlineJavaException {
if (link == null){
try {
InlineJavaUtils.debug(1, "loading InlineJavaUserClassLink via InlineJavaUserClassLoader") ;
Class<?> c = Class.forName("InlineJavaUserClassLink", true, this) ;
link = c.getDeclaredConstructor().newInstance() ;
invoke = find_method(c, "invoke") ;
get = find_method(c, "get") ;
set = find_method(c, "set") ;
array_get = find_method(c, "array_get") ;
array_set = find_method(c, "array_set") ;
create = find_method(c, "create") ;
}
catch (Exception e){
throw new InlineJavaException("InlineJavaUserClassLoader can't load InlineJavaUserClassLink: invalid classpath setup (" +
e.getClass().getName() + ": " + e.getMessage() + ")") ;
}
}
}
private Method find_method(Class c, String name) throws InlineJavaException {
Method ml[] = c.getMethods() ;
for (int i = 0 ; i < ml.length ; i++){
if (ml[i].getName().equals(name)){
return ml[i] ;
}
}
throw new InlineJavaException("Can't find method '" + name +
"' in class InlineJavaUserClassLink") ;
}
private Object invoke_via_link(Method m, Object p[]) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InlineJavaException {
try {
return m.invoke(link, p) ;
}
catch (IllegalAccessException e){
throw new InlineJavaException("Can't invoke method from class InlineJavaUserClassLink: IllegalAccessException") ;
}
catch (IllegalArgumentException e){
throw new InlineJavaException("Can't invoke method from class InlineJavaUserClassLink: IllegalArgumentException") ;
}
catch (InvocationTargetException e){
Throwable t = e.getTargetException() ;
if (t instanceof NoSuchMethodException){
throw (NoSuchMethodException)t ;
}
else if (t instanceof InstantiationException){
throw (InstantiationException)t ;
}
else if (t instanceof IllegalAccessException){
throw (IllegalAccessException)t ;
}
if (t instanceof IllegalAccessException){
throw (IllegalAccessException)t ;
}
else if (t instanceof IllegalArgumentException){
throw (IllegalArgumentException)t ;
}
else if (t instanceof InvocationTargetException){
throw (InvocationTargetException)t ;
}
// Not sure if this is really necessary, but...
else if (t instanceof RuntimeException){
RuntimeException re = (RuntimeException)t ;
throw re ;
}
else{
// In theory this case is impossible.
throw new InlineJavaException("Unexpected exception of type '" +
t.getClass().getName() + "': " + t.getMessage()) ;
}
}
}
public Object invoke(Method m, Object o, Object p[]) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InlineJavaException {
check_link() ;
try {
return invoke_via_link(invoke, new Object [] {m, o, p}) ;
}
catch (NoSuchMethodException me){/* Impossible */}
catch (InstantiationException ie){/* Impossible */}
return null ;
}
public Object get(Field f, Object o) throws IllegalAccessException, IllegalArgumentException, InlineJavaException {
check_link() ;
try {
return invoke_via_link(get, new Object [] {f, o}) ;
}
catch (NoSuchMethodException me){/* Impossible */}
catch (InstantiationException ie){/* Impossible */}
catch (InvocationTargetException e){/* Impossible */}
return null ;
}
public void set(Field f, Object o, Object p) throws IllegalAccessException, IllegalArgumentException, InlineJavaException {
check_link() ;
try {
invoke_via_link(set, new Object [] {f, o, p}) ;
}
catch (NoSuchMethodException me){/* Impossible */}
catch (InstantiationException ie){/* Impossible */}
catch (InvocationTargetException e){/* Impossible */}
}
public Object array_get(Object o, int idx) throws InlineJavaException {
check_link() ;
try {
return invoke_via_link(array_get, new Object [] {o, Integer.valueOf(idx)}) ;
}
catch (NoSuchMethodException me){/* Impossible */}
catch (InstantiationException ie){/* Impossible */}
catch (IllegalAccessException iae){/* Impossible */}
catch (IllegalArgumentException iae){/* Impossible */}
catch (InvocationTargetException e){/* Impossible */}
return null ;
}
public void array_set(Object o, int idx, Object elem) throws IllegalArgumentException, InlineJavaException {
check_link() ;
try {
invoke_via_link(array_set, new Object [] {o, Integer.valueOf(idx), elem}) ;
}
catch (NoSuchMethodException me){/* Impossible */}
catch (InstantiationException ie){/* Impossible */}
catch (IllegalAccessException iae){/* Impossible */}
catch (InvocationTargetException e){/* Impossible */}
}
public Object create(Class p, Object args[], Class proto[]) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InlineJavaException {
check_link() ;
return invoke_via_link(create, new Object [] {p, args, proto}) ;
}
}
|