Example usage for javax.script ScriptException getMessage

List of usage examples for javax.script ScriptException getMessage

Introduction

In this page you can find the example usage for javax.script ScriptException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns a message containing the String passed to a constructor as well as line and column numbers and filename if any of these are known.

Usage

From source file:FuncEvaluator.java

  public static void main(String[] args) {
  if (args.length != 2) {
    System.err/*from  w  ww  . java  2 s.  c  o  m*/
        .println("usage: java FuncEvaluator scriptfile " + "script-exp");
    return;
  }

  ScriptEngineManager manager = new ScriptEngineManager();
  ScriptEngine engine = manager.getEngineByName("rhino");

  try {
    System.out.println(engine.eval(new FileReader(args[0])));
    System.out.println(engine.eval(args[1]));
  } catch (ScriptException se) {
    System.err.println(se.getMessage());
  } catch (IOException ioe) {
    System.err.println(ioe.getMessage());
  }
}

From source file:GetToKnowBindingsAndScopes.java

public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    manager.put("global", "global bindings");

    dumpBindings(manager.getBindings());
    ScriptEngine engine = manager.getEngineByExtension("js");
    engine.put("engine", "engine bindings");

    dumpBindings(engine.getBindings(ScriptContext.GLOBAL_SCOPE));

    dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE));

    try {//w w  w  .jav a  2  s.  c  om
        Bindings bindings = engine.createBindings();
        bindings.put("engine", "overridden engine bindings");
        bindings.put("app", new GetToKnowBindingsAndScopes());
        bindings.put("bindings", bindings);
        engine.eval("app.dumpBindings (bindings);", bindings);
    } catch (ScriptException se) {
        System.err.println(se.getMessage());
    }

    ScriptEngine engine2 = manager.getEngineByExtension("js");
    engine2.put("engine2", "engine2 bindings");

    dumpBindings(engine2.getBindings(ScriptContext.GLOBAL_SCOPE));
    dumpBindings(engine2.getBindings(ScriptContext.ENGINE_SCOPE));
    dumpBindings(engine.getBindings(ScriptContext.ENGINE_SCOPE));
}

From source file:Main.java

public static void main(String[] args) {
    ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");

    String[] ops = { "+", "-", "*", "/" };

    JPanel gui = new JPanel(new BorderLayout(2, 2));
    JPanel labels = new JPanel(new GridLayout(0, 1));
    gui.add(labels, BorderLayout.WEST);
    labels.add(new JLabel("a"));
    labels.add(new JLabel("operand"));
    labels.add(new JLabel("b"));
    labels.add(new JLabel("="));

    JPanel controls = new JPanel(new GridLayout(0, 1));
    gui.add(controls, BorderLayout.CENTER);
    JTextField a = new JTextField(10);
    controls.add(a);/* w ww.  ja v a 2  s .  c o  m*/
    JComboBox operand = new JComboBox(ops);
    controls.add(operand);
    JTextField b = new JTextField(10);
    controls.add(b);
    JTextField output = new JTextField(10);
    controls.add(output);

    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            String expression = a.getText() + operand.getSelectedItem() + b.getText();
            try {
                Object result = engine.eval(expression);
                if (result == null) {
                    output.setText("Output was 'null'");
                } else {
                    output.setText(result.toString());
                }
            } catch (ScriptException se) {
                output.setText(se.getMessage());
            }
        }
    };

    operand.addActionListener(al);
    a.addActionListener(al);
    b.addActionListener(al);

    JOptionPane.showMessageDialog(null, gui);
}

From source file:nl.mawoo.wcmscript.cli.Application.java

private static void eval(WCMScript wcmScript, StringBuilder code) {
    try {//ww w .  ja  va  2 s. co  m
        wcmScript.eval(code.toString());
    } catch (ScriptException e) {
        wcmScript.getScriptLogger().error("SCRIPT ERROR: " + e.getMessage(), e);
    } catch (Exception e) {
        LOGGER.error("Uncaught exception: " + e.getMessage(), e);
    } finally {
        code.setLength(0);
    }
}

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

private static void checkIsDesiredSecurityException(ScriptException e) {
    String message = e.getMessage();
    String permissionTrace = AllowRhqServerInternalsAccessPermission.class.getName();

    Assert.assertTrue(message.contains(permissionTrace),
            "The script exception doesn't seem to be caused by the AllowRhqServerInternalsAccessPermission security exception. "
                    + message);//  w  w w.jav a 2s  .  c o m
}

From source file:com.espertech.esper.epl.script.jsr223.JSR223Helper.java

public static String getScriptCompileMsg(ScriptException ex) {
    if (ex.getLineNumber() != 1 && ex.getColumnNumber() != -1) {
        return "At line " + ex.getLineNumber() + " column " + ex.getColumnNumber() + ": " + ex.getMessage();
    } else {//from   w  ww .ja  va2  s  .  c  om
        return ex.getMessage();
    }
}

From source file:org.jahia.tools.patches.GroovyPatcher.java

protected static ScriptEngine getEngine() throws ScriptException {
    try {/*ww w  . j  a  va 2 s.co  m*/
        return ScriptEngineUtils.getInstance().scriptEngine("groovy");
    } catch (ScriptException e) {
        if (e.getMessage() != null && e.getMessage().startsWith("Script engine not found for extension")) {
            return null;
        } else {
            throw e;
        }
    }
}

From source file:com.datatorrent.stram.client.StramClientUtils.java

public static void evalProperties(Properties target, Configuration vars) {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");

    Pattern substitutionPattern = Pattern.compile("\\$\\{(.+?)\\}");
    Pattern evalPattern = Pattern.compile("\\{% (.+?) %\\}");

    try {//ww  w .ja  v a 2 s. c o  m
        engine.eval("var _prop = {}");
        for (Map.Entry<String, String> entry : vars) {
            String evalString = String.format("_prop[\"%s\"] = \"%s\"",
                    StringEscapeUtils.escapeJava(entry.getKey()),
                    StringEscapeUtils.escapeJava(entry.getValue()));
            engine.eval(evalString);
        }
    } catch (ScriptException ex) {
        LOG.warn("Javascript error: {}", ex.getMessage());
    }

    for (Map.Entry<Object, Object> entry : target.entrySet()) {
        String value = entry.getValue().toString();

        Matcher matcher = substitutionPattern.matcher(value);
        if (matcher.find()) {
            StringBuilder newValue = new StringBuilder();
            int cursor = 0;
            do {
                newValue.append(value.substring(cursor, matcher.start()));
                String subst = vars.get(matcher.group(1));
                if (subst != null) {
                    newValue.append(subst);
                }
                cursor = matcher.end();
            } while (matcher.find());
            newValue.append(value.substring(cursor));
            target.put(entry.getKey(), newValue.toString());
        }

        matcher = evalPattern.matcher(value);
        if (matcher.find()) {
            StringBuilder newValue = new StringBuilder();
            int cursor = 0;
            do {
                newValue.append(value.substring(cursor, matcher.start()));
                try {
                    Object result = engine.eval(matcher.group(1));
                    String eval = result.toString();

                    if (eval != null) {
                        newValue.append(eval);
                    }
                } catch (ScriptException ex) {
                    LOG.warn("JavaScript exception {}", ex.getMessage());
                }
                cursor = matcher.end();
            } while (matcher.find());
            newValue.append(value.substring(cursor));
            target.put(entry.getKey(), newValue.toString());
        }
    }
}

From source file:org.wso2.carbon.event.template.manager.core.internal.util.TemplateManagerHelper.java

/**
 * Create a JavaScript engine packed with given scripts. If two scripts have methods with same name,
 * later method will override the previous method.
 *
 * @param domain/*from ww w  . j a  v a  2s.  com*/
 * @return JavaScript engine
 * @throws TemplateDeploymentException if there are any errors in JavaScript evaluation
 */
public static ScriptEngine createJavaScriptEngine(Domain domain) throws TemplateDeploymentException {

    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    ScriptEngine scriptEngine = scriptEngineManager
            .getEngineByName(TemplateManagerConstants.JAVASCRIPT_ENGINE_NAME);

    if (scriptEngine == null) {
        // Exception will be thrown later, only if function calls are used in the template
        log.warn("JavaScript engine is not available. Function calls in the templates cannot be evaluated");
    } else {
        if (domain != null && domain.getScripts() != null && domain.getScripts().getScript() != null) {
            Path scriptDirectory = Paths.get(TemplateManagerConstants.TEMPLATE_SCRIPT_PATH);
            if (Files.exists(scriptDirectory, LinkOption.NOFOLLOW_LINKS)
                    && Files.isDirectory(scriptDirectory)) {
                for (Script script : domain.getScripts().getScript()) {
                    String src = script.getSrc();
                    String content = script.getContent();
                    if (src != null) {
                        // Evaluate JavaScript file
                        Path scriptFile = scriptDirectory.resolve(src).normalize();
                        if (Files.exists(scriptFile, LinkOption.NOFOLLOW_LINKS)
                                && Files.isReadable(scriptFile)) {
                            if (!scriptFile.startsWith(scriptDirectory)) {
                                // The script file is not in the permitted directory
                                throw new TemplateDeploymentException("Script file "
                                        + scriptFile.toAbsolutePath() + " is not in the permitted directory "
                                        + scriptDirectory.toAbsolutePath());
                            }
                            try {
                                scriptEngine
                                        .eval(Files.newBufferedReader(scriptFile, Charset.defaultCharset()));
                            } catch (ScriptException e) {
                                throw new TemplateDeploymentException("Error in JavaScript "
                                        + scriptFile.toAbsolutePath() + ": " + e.getMessage(), e);
                            } catch (IOException e) {
                                throw new TemplateDeploymentException(
                                        "Error in reading JavaScript file: " + scriptFile.toAbsolutePath());
                            }
                        } else {
                            throw new TemplateDeploymentException("JavaScript file not exist at: "
                                    + scriptFile.toAbsolutePath() + " or not readable.");
                        }
                    }
                    if (content != null) {
                        // Evaluate JavaScript content
                        try {
                            scriptEngine.eval(content);
                        } catch (ScriptException e) {
                            throw new TemplateDeploymentException(
                                    "JavaScript declared in " + domain.getName() + " has error", e);
                        }
                    }
                }
            } else {
                log.warn("Script directory not found at: " + scriptDirectory.toAbsolutePath());
            }
        }
    }

    return scriptEngine;
}

From source file:org.suren.autotest.web.framework.data.JavaScriptDynamicData.java

@Override
public String getValue(String orginData) {
    String value = null;//from w  w w . j  a  v a  2  s.  co m
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
    try {
        Object resObj = engine.eval(orginData);
        if (resObj != null) {
            value = resObj.toString();
        } else {
            value = "js not return!";
        }
    } catch (ScriptException e) {
        value = e.getMessage();
        e.printStackTrace();
    }

    return value;
}