1

I want evaluate dynamically JavaScript code inside the Google App Engine runtime.

Java have this feature but Want to know if this is supported by GAE too.

If you can provide a simple code will be very appreciate, and if you use it, please shares comments about it, thanks.

...

GAE support Scripting Languages but by default 'JavaScript' service is not register. So GAE out-of-the-box do not evaluate JavaScript.

Daniel De León
  • 11,681
  • 5
  • 76
  • 66

2 Answers2

2

https://developers.google.com/appengine/docs/java/jrewhitelist includes javax.script.ScriptEngine in its whitelisted (allowed) APIs, so, yes.

Louis Wasserman
  • 172,699
  • 23
  • 307
  • 375
  • Right, But the Rhino service must be register and I can't find the way to achieve it. – Daniel De León Nov 08 '13 at 00:40
  • Although the classes are whitelisted, their functionality is limited. On your local machine "ScriptEngineManager.getEngineFactories()" will return several different engines. On App Engine it returns nothing. Many code examples use use "ScriptEngineManager.getEngineByName("JavaScript")" but on App Engine you'll just get null returned. I recommend using Rhino instead. – Ian Feb 09 '15 at 10:44
2

Last time I tried, though ScriptEngine is whitelisted, it is not available in the production environment. I had to package the Rhino.jar along with my app.

For examples on general usage of scripting in Java, you can refer to the Java documentation itself.

Though, note that in the GAE/J environment you will need to invoke the Rhino APIs directly.

For example,

// Import statements.
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

private Object executeUsingRhino(String script) throws Exception
{
    Context ctx = Context.enter();
    try
    {
        Scriptable scope = ctx.initStandardObjects();
        return ctx.evaluateString(scope, script, "<cmd>", 1, null);
    }
    finally
    {
        Context.exit();
    }
}


// Invoke a script that returns a string output using the following code snippet
String output = Context.toString(executeUsingRhino(script));
Harsha R
  • 687
  • 6
  • 11
  • Even with the jar do not work. So you do not have it running, right? – Daniel De León Nov 08 '13 at 00:39
  • Of course I have it running :) ... I am away from my personal machine at the moment. I shall share the code required once I get back. – Harsha R Nov 08 '13 at 07:54
  • 1
    Sorry about the initial goof up. Just adding the jar does not work, you will need to use the Rhino APIs directly. Please check the original answer for information on how to invoke a script. – Harsha R Nov 08 '13 at 19:59