Created
March 24, 2011 01:31
-
-
Save rbackhouse/884399 to your computer and use it in GitHub Desktop.
Example demonstrating calling the commonjs loader via Rhino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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