Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example demonstrating calling the commonjs loader via Rhino
import java.io.IOException;
import org.dojotoolkit.server.util.resource.ResourceLoader;
import org.dojotoolkit.server.util.rhino.RhinoClassLoader;
import org.dojotoolkit.server.util.rhino.RhinoJSMethods;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;
public class RhinoCommonJSLoader {
private RhinoClassLoader rhinoClassLoader = null;
private ResourceLoader resourceLoader = null;
public RhinoCommonJSLoader(ResourceLoader resourceLoader) {
this.rhinoClassLoader = new RhinoClassLoader(resourceLoader, RhinoClassLoader.class.getClassLoader());
this.resourceLoader = resourceLoader;
}
public void run(String program) throws IOException {
Context ctx = null;
StringBuffer sb = new StringBuffer();
sb.append("loadJS('/jsutil/commonjs/loader.js');\n");
sb.append("require('"+program+"');\n");
try {
ctx = Context.enter();
ScriptableObject scope = ctx.initStandardObjects();
RhinoJSMethods.initScope(scope, resourceLoader, rhinoClassLoader, true, false);
ctx.evaluateString(scope, sb.toString(), "CommonJSLoader", 1, null);
}
catch(Throwable t) {
throw new IOException("Exception on load for ["+sb+"] : "+t.getMessage());
}
finally {
Context.exit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment