Skip to content

Instantly share code, notes, and snippets.

@donv
Created August 22, 2011 18:23
Show Gist options
  • Select an option

  • Save donv/1163095 to your computer and use it in GitHub Desktop.

Select an option

Save donv/1163095 to your computer and use it in GitHub Desktop.
private static Object ruby;
Log.d(TAG, "Setting up JRuby runtime");
System.setProperty("jruby.bytecode.version", "1.5");
System.setProperty("jruby.interfaces.useProxy", "true");
System.setProperty("jruby.management.enabled", "false");
String packagePath = "org.ruboto.core";
String apkName = null;
try {
apkName = appContext.getPackageManager().getApplicationInfo(packagePath,0).sourceDir;
} catch (PackageManager.NameNotFoundException e) {
// FIXME(uwe): Ask the user to install the platform APK and stop or restart this app.
throw new RuntimeException("APK " + packagePath + " not found.");
}
System.out.println("apkName: " + apkName);
PathClassLoader pathClassLoader = new PathClassLoader(apkName, ClassLoader.getSystemClassLoader());
try {
Class<?> scriptingContainerClass = Class.forName("org.jruby.embed.ScriptingContainer", true, pathClassLoader);
System.out.println("scriptingContainerClass: " + scriptingContainerClass);
ruby = scriptingContainerClass.getConstructor().newInstance();
System.out.println("scriptingContainer: " + ruby);
Class<?> compileModeClass = Class.forName("org.jruby.RubyInstanceConfig.CompileMode", true, pathClassLoader);
System.out.println("compileModeClass: " + compileModeClass);
Method setCompileModeMethod = ruby.getClass().getMethod("setCompileMode", compileModeClass);
Object compileModenumValue = compileModeClass.getEnumConstants()[2];
System.out.println("compileModenumValue: " + compileModenumValue);
setCompileModeMethod.invoke(ruby, compileModenumValue);
Method setClassLoaderMethod = ruby.getClass().getMethod("setClassLoader");
setClassLoaderMethod.invoke(ruby, pathClassLoader);
Thread.currentThread().setContextClassLoader(pathClassLoader);
if (scriptsDir != null) {
Log.d(TAG, "Setting JRuby current directory to " + scriptsDir);
Method setCurrentDirectoryMethod = ruby.getClass().getMethod("setCurrentDirectory", String.class);
setCurrentDirectoryMethod.invoke(ruby, scriptsDir);
}
if (out != null) {
Method setOutputMethod = ruby.getClass().getMethod("setOutput");
setOutputMethod.invoke(ruby, out);
Method setErrorMethod = ruby.getClass().getMethod("setError");
setErrorMethod.invoke(ruby, out);
}
} catch (ClassNotFoundException e) {
// FIXME(uwe): ScriptingContainer not found in the platofrm
// APK...
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// The following code fails
try {
ruby.getClass().getMethod("put", String.class, Object.class).invoke(ruby, name, object);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment