package org.jruby.embed.bsf;
import java.io.File;
import java.io.InputStream;
import java.io.Reader;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
import org.apache.bsf.BSFDeclaredBean;
import org.apache.bsf.BSFException;
import org.apache.bsf.BSFManager;
import org.apache.bsf.util.BSFEngineImpl;
import org.apache.bsf.util.BSFFunctions;
import org.jruby.Ruby;
import org.jruby.RubyObject;
import org.jruby.embed.EmbedRubyObjectAdapter;
import org.jruby.embed.LocalContextScope;
import org.jruby.embed.LocalVariableBehavior;
import org.jruby.embed.PathType;
import org.jruby.embed.ScriptingContainer;
import org.jruby.embed.util.SystemPropertyCatcher;
import org.jruby.embed.variable.BiVariable;
import org.jruby.embed.variable.VariableInterceptor;
import org.jruby.exceptions.RaiseException;
import org.jruby.internal.runtime.GlobalVariable;
import org.jruby.javasupport.Java;
import org.jruby.javasupport.JavaEmbedUtils;
import org.jruby.javasupport.JavaEmbedUtils.EvalUnit;
import org.jruby.javasupport.JavaObject;
import org.jruby.javasupport.JavaUtil;
import org.jruby.runtime.IAccessor;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
public class JRubyEngine extends BSFEngineImpl {
private ScriptingContainer container;
@Override
public Object apply(String file, int line, int col, Object funcBody, Vector paramNames, Vector args) {
ThreadContext context = container.getProvider().getRuntime().getCurrentContext();
try {
if (paramNames != null && args != null) {
for (int i = 0; i < paramNames.size(); i++) {
Object o = paramNames.get(i);
if (o instanceof String) {
String name = (String) o;
container.put(name, args.get(i));
}
}
}
return run(file, line, funcBody);
} catch (StackOverflowError soe) {
throw context.runtime.newSystemStackError("stack level too deep", soe);
}
}
private Object run(String fileinfo, int line, Object scriptUnit) {
EvalUnit unit = null;
if (scriptUnit instanceof String) {
unit = container.parse(scriptUnit.toString(), line);
} else if (scriptUnit instanceof Reader) {
unit = container.parse((Reader)scriptUnit, fileinfo, line);
} else if (scriptUnit instanceof InputStream) {
unit = container.parse((InputStream)scriptUnit, fileinfo, line);
} else if (scriptUnit instanceof PathType) {
unit = container.parse((PathType)scriptUnit, fileinfo, line);
}
if (unit == null) {
return null;
}
IRubyObject ret = unit.run();
return JavaEmbedUtils.rubyToJava(ret);
}
public Object eval(String file, int line, int col, Object expr) throws BSFException {
try {
return run(file, line, expr);
} catch (Exception excptn) {
throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "Exception", excptn);
}
}
@Override
public void exec(String file, int line, int col, Object expr) throws BSFException {
try {
run(file, line, expr);
} catch (Exception excptn) {
throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "Exception", excptn);
}
}
public Object call(Object recv, String method, Object[] args) throws BSFException {
try {
EmbedRubyObjectAdapter adapter = container.newObjectAdapter();
if (args == null) {
return adapter.callMethod(recv, method, Object.class);
} else {
return adapter.callMethod(recv, method, args, Object.class);
}
} catch (Exception excptn) {
throw new BSFException(BSFException.REASON_EXECUTION_ERROR, excptn.getMessage(), excptn);
}
}
@Override
public void initialize(BSFManager manager, String language, Vector someDeclaredBeans) throws BSFException {
super.initialize(manager, language, someDeclaredBeans);
LocalContextScope scope = SystemPropertyCatcher.getScope(LocalContextScope.SINGLETON);
LocalVariableBehavior behavior = LocalVariableBehavior.BSF;
container = new ScriptingContainer(scope, behavior);
SystemPropertyCatcher.setConfiguration(container);
Ruby runtime = container.getProvider().getRuntime();
if (someDeclaredBeans != null && someDeclaredBeans.size() > 0) {
for (int i = 0; i < someDeclaredBeans.size(); i++) {
BSFDeclaredBean bean = (BSFDeclaredBean) someDeclaredBeans.get(i);
setVariable(bean);
}
}
runtime.getGlobalVariables().defineReadonly("$bsf", new FunctionsGlobalVariable(runtime, new BSFFunctions(manager, this)), GlobalVariable.Scope.GLOBAL);
}
private void setVariable(BSFDeclaredBean bean) {
String name = bean.name;
if ("$bsf".equals(name)) {
return;
}
if (!name.startsWith("$")) {
name = "$".concat(name);
}
RubyObject receiver = (RubyObject)container.getProvider().getRuntime().getTopSelf();
BiVariable v =
VariableInterceptor.getVariableInstance(LocalVariableBehavior.BSF,receiver, name, bean.bean, bean.type);
container.getVarMap().setVariable(receiver, v);
}
private List<String> getClassPath(BSFManager manager) {
String classpath = manager.getClassPath();
String s = System.getProperty("org.jruby.embed.class.path");
if (s != null) {
classpath = classpath + File.pathSeparator + s;
}
return Arrays.asList(classpath.split(System.getProperty("path.separator")));
}
@Override
public void declareBean(BSFDeclaredBean bean) throws BSFException {
assert bean != null;
setVariable(bean);
}
@Override
public void undeclareBean(BSFDeclaredBean bean) throws BSFException {
assert bean != null;
String name = bean.name;
if (!name.startsWith("$")) {
name = "$".concat(name);
}
container.getVarMap().remove(name);
}
public void handleException(BSFException bsfExcptn) {
printException(container.getProvider().getRuntime(), (Exception) bsfExcptn.getTargetException());
}
private static void printException(Ruby runtime, Exception exception) {
assert exception != null;
if (exception instanceof RaiseException) runtime.printError(((RaiseException)exception).getException());
}
private static class FunctionsGlobalVariable implements IAccessor {
private final Ruby runtime;
private final BSFFunctions functions;
public FunctionsGlobalVariable(Ruby runtime, BSFFunctions functions) {
this.runtime = runtime;
this.functions = functions;
}
public IRubyObject getValue() {
IRubyObject result = JavaUtil.convertJavaToRuby(runtime, functions, BSFFunctions.class);
return result instanceof JavaObject ? Java.wrap(runtime, result) : result;
}
public IRubyObject setValue(IRubyObject value) {
return value;
}
}
@Override
public void terminate() {
container.getVarMap().clear();
container.terminate();
super.terminate();
}
}