Skip to content

Instantly share code, notes, and snippets.

@rbackhouse
Created March 24, 2011 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbackhouse/884399 to your computer and use it in GitHub Desktop.
Save rbackhouse/884399 to your computer and use it in GitHub Desktop.
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