| 1 | |
package org.webslinger.ext.bsf.jruby; |
| 2 | |
|
| 3 | |
import java.util.Arrays; |
| 4 | |
import java.util.ArrayList; |
| 5 | |
import java.util.List; |
| 6 | |
import java.util.Vector; |
| 7 | |
|
| 8 | |
import org.apache.bsf.BSFException; |
| 9 | |
import org.apache.bsf.BSFManager; |
| 10 | |
|
| 11 | |
import org.jruby.IRuby; |
| 12 | |
import org.jruby.Ruby; |
| 13 | |
import org.jruby.RubyException; |
| 14 | |
import org.jruby.RubyProc; |
| 15 | |
import org.jruby.runtime.builtin.IRubyObject; |
| 16 | |
import org.jruby.exceptions.RaiseException; |
| 17 | |
|
| 18 | |
import org.webslinger.bsf.CompilingLanguageEngineImpl; |
| 19 | |
import org.webslinger.bsf.LanguageEngineInfo; |
| 20 | |
|
| 21 | |
public class JRubyEngine extends CompilingLanguageEngineImpl<Object, Object> { |
| 22 | 0 | protected final IRuby ruby = Ruby.getDefaultInstance(); |
| 23 | |
|
| 24 | 0 | public JRubyEngine() { |
| 25 | 0 | compiler = new JRubyCompiler(this); |
| 26 | 0 | } |
| 27 | |
|
| 28 | |
public LanguageEngineInfo getLanguageEngineInfo() { |
| 29 | 0 | return JRubyInfo.INSTANCE; |
| 30 | |
} |
| 31 | |
|
| 32 | |
public void initialize(BSFManager manager, String lang, Vector declaredBeans) throws BSFException { |
| 33 | 0 | super.initialize(manager, lang, declaredBeans); |
| 34 | 0 | ruby.getLoadService().init(new ArrayList()); |
| 35 | |
|
| 36 | 0 | ruby.getLoadService().require("java"); |
| 37 | 0 | } |
| 38 | |
|
| 39 | |
public IRuby getRuby() { |
| 40 | 0 | return ruby; |
| 41 | |
} |
| 42 | |
|
| 43 | |
public Object apply(Object body, Object compiled, Object[] args) throws Exception { |
| 44 | |
try { |
| 45 | |
try { |
| 46 | 0 | return ((RubyProc) compiled).call((IRubyObject[]) args); |
| 47 | 0 | } catch (RaiseException e) { |
| 48 | 0 | e.getException().printBacktrace(System.err); |
| 49 | 0 | throw e; |
| 50 | |
} |
| 51 | 0 | } catch (RuntimeException e) { |
| 52 | 0 | e.printStackTrace(); |
| 53 | 0 | throw e; |
| 54 | 0 | } catch (Exception e) { |
| 55 | 0 | e.printStackTrace(); |
| 56 | 0 | throw e; |
| 57 | 0 | } catch (Error e) { |
| 58 | 0 | e.printStackTrace(); |
| 59 | 0 | throw e; |
| 60 | |
} |
| 61 | |
|
| 62 | |
} |
| 63 | |
|
| 64 | |
public Object eval(Object body, Object compiled) throws Exception { |
| 65 | 0 | return ((IRubyObject) compiled).callMethod("method"); |
| 66 | |
} |
| 67 | |
|
| 68 | |
public void exec(Object body, Object compiled) throws Exception { |
| 69 | 0 | ((IRubyObject) compiled).callMethod("method"); |
| 70 | 0 | } |
| 71 | |
} |