JavaScript ScriptEngine不适用于Google App Engine for Java(GAE / J) [英] JavaScript ScriptEngine isn't working within Google App Engine for Java (GAE/J)

查看:325
本文介绍了JavaScript ScriptEngine不适用于Google App Engine for Java(GAE / J)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,当我尝试使用ScriptEngine eval时,总会得到返回的0值。通过使用Logger,我可以确定有NullPointerExceptions正在生成。经过进一步检查,GAE似乎并不总是返回有效的脚本引擎(如果曾经),因为它在尝试使用它时会引发异常。



我的代码如下所示:

  public double myEval(String JsFormulaStr){
double solutionValue = 0;
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine eng = mgr.getEngineByName(JavaScript);
if(eng == null){//添加了这段代码来防止java.lang.NullPointerException ...
log.severe(Unable to get Script Engine。);
返回0;
}
尝试{
Object jsResults = eng.eval(JsFormulaStr);
solutionValue = Double.parseDouble(jsResults.toString());
返回solutionValue;
} catch(Exception e){
log.severe([ERROR] in getCalculatedSolution_FromJS_ToDouble():: \\\
\t+
公式字符串是:+ JsFormulaStr + \\\
\t+ e);
返回0;


code
$ b $ p
$ b如果我在本地以WebApp方式运行它, (在Eclipse和Netbeans中以及在Tomcat和Glassfish 4.0中)。

我试图评估的一些字符串:


  • 62.0 / 100
  • 0.0 * 352.0
  • (0 - 428)* 1000

  • (0 - 597)* 1000

  • 73.0 / 100


    $ b

    注意:0或0.0来自先前调用失败的其他评估。由于此函数在出错时返回0。



    根据 Google的JRE类白名单,ScriptEngineManager和ScriptEngine类都是允许的。所以我不明白为什么它不按预期工作。



    有什么建议吗?



    谢谢提前,

    Randy

    解决方案

    我碰到同样的问题。尽管这些类已列入白名单,但它们的功能似乎在App Engine上受到限制。该代码在本地机器上正常工作,但在部署到App Engine时失败,因为没有任何脚本引擎可用(因此为NullPointerException)。

    幸运的是,您可以使用Rhino引擎执行相同的操作。



    注意:此示例基于Harsha R在中给出的示例https://stackoverflow.com/a/19828128/578821



    下载 Rhino Jar 并将js.jar添加到您的类路径中(如果您使用的是Java 1.4,则只需要js-14.jar)。

      / *示例1:运行JavaScript函数(摘自示例)* / 
    String script =function abc (x,y){return x + y;};
    上下文context = Context.enter();
    try {
    ScriptableObject scope = context.initStandardObjects();
    Scriptable that = context.newObject(scope);
    函数fct = context.compileFunction(范围,脚本,script,1,null);
    Object result = fct.call(context,scope,that,new Object [] {2,3});
    System.out.println(Context.jsToJava(result,int.class));
    }
    finally {
    Context.exit();

    $ b $ *例2:执行一个JavaScript语句* /
    script =3 + 2 *(4 * 5);
    context = Context.enter();

    尝试{
    Scriptable scope = context.initStandardObjects();
    Object result = context.evaluateString(scope,script,< cmd>,1,null);
    System.out.println(result);
    }
    finally {
    Context.exit();
    }


    I am having an issue where I always get a 0 value returned when I try to use the ScriptEngine eval. By using Logger, I was able to determine that there are NullPointerExceptions being generated. After further inspection, it appears that GAE doesn't always return a valid script engine (if ever), because it throws an exception when you try to use it.

    My code looks like:

    public double myEval(String JsFormulaStr ) {
        double solutionValue = 0;
        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine eng = mgr.getEngineByName("JavaScript");
        if(eng == null) {  // Added this block of code to prevent java.lang.NullPointerException...
            log.severe("Unable to get Script Engine." );
            return 0;
        }
        try {
            Object jsResults = eng.eval(JsFormulaStr);
            solutionValue = Double.parseDouble(jsResults.toString());
            return solutionValue;
        } catch(Exception e) {
            log.severe("[ERROR] in getCalculatedSolution_FromJS_ToDouble()::\n\t" +
                    "Formula String is: " + JsFormulaStr + "\n\t" + e);
            return 0;
        }     
    }
    

    Everything works fine if I run it locally as a WebApp (Both in Eclipse & Netbeans. And within Tomcat & Glassfish 4.0).

    Some of the strings which I tried to eval:

    • 62.0 / 100
    • 0.0 * 352.0
    • (0 - 428) * 1000
    • (0 - 597) * 1000
    • 73.0 / 100

    NOTE: The 0's or 0.0's are from other evaluations which have failed in previous calls. Since this function returns 0 on error.

    According to Google's JRE Class Whitelist, the ScriptEngineManager and ScriptEngine classes are allowed. So I don't understand why it isn't working as expected.

    Any suggestions?

    Thanks in advance,

    Randy

    解决方案

    I've hit the same problem. Although the classes are whitelisted, it seems like their functionality is limited on App Engine. The code works fine on your local machine but fails when deployed to App Engine as there aren't any script engines available (hence the NullPointerException).

    Luckily, you can do the same thing using the Rhino engine.

    Note: this example builds on that given by Harsha R in https://stackoverflow.com/a/19828128/578821

    Download the Rhino Jar and add js.jar to your classpath (you only need js-14.jar if you're using Java 1.4).

        /* Example 1: Running a JavaScript function (taken from examples) */
        String script = "function abc(x,y) {return x+y;}";
        Context context = Context.enter();
        try {
            ScriptableObject scope = context.initStandardObjects();
            Scriptable that = context.newObject(scope);
            Function fct = context.compileFunction(scope, script, "script", 1, null);
            Object result = fct.call(context, scope, that, new Object[] { 2, 3 });
            System.out.println(Context.jsToJava(result, int.class));
        } 
        finally {
            Context.exit();
        }
    
        /* Example 2: execute a JavaScript statement */
        script = "3 + 2 * (4*5)";
        context = Context.enter();
    
        try{
            Scriptable scope = context.initStandardObjects();
            Object result = context.evaluateString(scope, script, "<cmd>", 1, null);
            System.out.println(result);
        }
        finally{
            Context.exit();
        }
    

    这篇关于JavaScript ScriptEngine不适用于Google App Engine for Java(GAE / J)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆