Example usage for javax.script ScriptEngine eval

List of usage examples for javax.script ScriptEngine eval

Introduction

In this page you can find the example usage for javax.script ScriptEngine eval.

Prototype

public Object eval(Reader reader) throws ScriptException;

Source Link

Document

Same as eval(String) except that the source of the script is provided as a Reader

Usage

From source file:org.jdto.mergers.ScriptMerger.java

private Object executeScript(ScriptEngine engine, String script) {

    try {/*from  ww  w  . jav a  2 s. c om*/
        return engine.eval(script);
    } catch (ScriptException ex) {
        String engineName = engine.toString();
        logger.error("Could not execute script in engine: " + engineName, ex);
        throw new RuntimeException("Failure while executing script", ex);
    }
}

From source file:reconf.server.services.JavaScriptEngine.java

public Object eval(String js, Map<String, Object> params, String resultVariableName) {
    try {//  w  w w  . j a v  a 2  s. c  o  m
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        for (Entry<String, Object> each : params.entrySet()) {
            engine.put(each.getKey(), each.getValue());
        }
        engine.eval(js);
        return engine.get(resultVariableName);

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:de.perdoctus.synology.jdadapter.controller.JdAdapter.java

private String extractKey(String jk) throws ScriptException {
    final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    final ScriptEngine scriptEngine = scriptEngineManager.getEngineByMimeType("text/javascript");
    scriptEngine.eval(jk + "\nvar result = f();");
    return scriptEngine.get("result").toString();
}

From source file:net.mindengine.galen.suite.actions.GalenPageActionRunJavascript.java

private void importClasses(ScriptEngine engine, Class<?>[] classes) throws ScriptException {
    for (Class<?> clazz : classes) {
        engine.eval("importClass(" + clazz.getName() + ")");
    }/*from  ww w .  j a  v  a  2s .c om*/
}

From source file:org.rhq.jndi.test.JndiAccessTest.java

public void testLocalJNDILookupFailsFromScripts() throws Exception {
    Subject overlord = LookupUtil.getSubjectManager().getOverlord();

    ScriptEngine engine = getEngine(overlord);

    try {/*from  w  ww .j ava 2 s . co m*/
        engine.eval("" + "var ctx = new javax.naming.InitialContext();\n"
                + "var entityManagerFactory = ctx.lookup('java:/RHQEntityManagerFactory');\n"
                + "var entityManager = entityManagerFactory.createEntityManager();\n"
                + "entityManager.find(java.lang.Class.forName('org.rhq.core.domain.resource.Resource'), java.lang.Integer.valueOf('10001'));");

        Assert.fail("The script shouldn't have been able to use the EntityManager.");
    } catch (ScriptException e) {
        checkIsDesiredSecurityException(e);
    }
}

From source file:org.rhq.jndi.test.JndiAccessTest.java

@Parameters("jnp.port")
public void testRemoteJNDILookupWorksFromScripts(int jnpPort) throws Exception {
    Subject overlord = LookupUtil.getSubjectManager().getOverlord();

    ScriptEngine engine = getEngine(overlord);

    try {// w ww  . ja  va 2s  .c o  m
        engine.eval("" + "var env = new java.util.Hashtable();"
                + "env.put('java.naming.factory.initial', 'org.jboss.naming.NamingContextFactory');"
                + "env.put('java.naming.provider.url', 'jnp://localhost:" + jnpPort + "');"
                + "var ctx = new javax.naming.InitialContext(env);\n" + "var kachny = ctx.lookup('kachny');\n"
                + "assertNotNull(kachny);\n");
    } catch (ScriptException e) {
        Assert.fail("The script should have been able to access a remote JNDI server.", e);
    }
}

From source file:com.orange.clara.cloud.servicedbdumper.config.CatalogConfig.java

private float getCostFromQuota(String quota, String formula) throws ParseException, ScriptException {
    long size = ByteFormat.parse(quota);
    String formulaInjected = formula.replaceAll("quota", String.valueOf(size * this.getDefaultCost()));
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("JavaScript");
    Object eval = engine.eval(formulaInjected);
    if (eval instanceof Integer) {
        return new Float((Integer) eval);
    }// w  w w .  ja  v a  2s.c  o m
    if (eval instanceof Float) {
        return (Float) eval;
    }
    return Math.round((Double) eval);
}

From source file:org.sonar.fortify.crawler.Main.java

private void executeJSCode(String pageSrc) throws ScriptException {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("JavaScript");
    engine.put("d", this);
    engine.eval(pageSrc);
}

From source file:com.thoughtworks.gauge.refactor.RefactoringMethodVisitor.java

public void visit(MethodDeclaration methodDeclaration, Object arg) {
    try {/*from   ww w.j  a v  a 2s  .com*/
        List<AnnotationExpr> annotations = methodDeclaration.getAnnotations();
        if (annotations == null)
            return;
        for (AnnotationExpr annotationExpr : annotations) {
            if (!(annotationExpr instanceof SingleMemberAnnotationExpr))
                continue;

            SingleMemberAnnotationExpr annotation = (SingleMemberAnnotationExpr) annotationExpr;
            if (annotation.getMemberValue() instanceof BinaryExpr) {
                ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
                try {
                    Object result = engine.eval(annotation.getMemberValue().toString());
                    refactor(methodDeclaration, new StringLiteralExpr(result.toString()), annotation);
                } catch (ScriptException e) {
                    continue;
                }
            }
            if (annotation.getMemberValue() instanceof StringLiteralExpr) {
                StringLiteralExpr memberValue = (StringLiteralExpr) annotation.getMemberValue();
                refactor(methodDeclaration, memberValue, annotation);
            }
        }
    } catch (Exception ignored) {
    }
}

From source file:org.wso2.developerstudio.datamapper.diagram.custom.dialogs.ConfigureMatchOperatorDialog.java

private String validateRegex(String input) {
    String output = input;/* ww w .ja  v a  2s  .c o m*/
    String scriptExecutorType = NASHORN;
    String javaVersion = System.getProperty(JAVA_VERSION);
    if (javaVersion.startsWith(JAVA_VERSION_7) || javaVersion.startsWith(JAVA_VERSION_6)) {
        scriptExecutorType = RHINO;
    }
    ScriptEngine engine = new ScriptEngineManager().getEngineByName(scriptExecutorType);
    try {
        engine.eval("function myFunction(input) {\n" + "var sampleString = \"acbdacfac\";\n" + "\n"
                + "   return sampleString.match(input);\n" + "}\n");
        engine.eval("myFunction(" + input + ");");
    } catch (ScriptException e) {
        output = "\"" + input + "\"";
    }
    return output;
}