Example usage for javax.script ScriptException ScriptException

List of usage examples for javax.script ScriptException ScriptException

Introduction

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

Prototype

public ScriptException(String message, String fileName, int lineNumber, int columnNumber) 

Source Link

Document

ScriptException constructor specifying message, filename, line number and column number.

Usage

From source file:com.intuit.tank.tools.script.ScriptRunner.java

/**
 * /*  w w w .j  a  va2  s.c  o  m*/
 * @param scriptName
 * @param script
 * @param engine
 * @param inputs
 * @param output
 * @return
 * @throws ScriptException
 */
public ScriptIOBean runScript(@Nullable String scriptName, @Nonnull String script, @Nonnull ScriptEngine engine,
        @Nonnull Map<String, Object> inputs, OutputLogger output) throws ScriptException {
    Reader reader = null;
    ScriptIOBean ioBean = null;
    try {
        reader = new StringReader(script);
        ioBean = new ScriptIOBean(inputs, output);
        engine.put("ioBean", ioBean);
        ioBean.println("Starting test...");
        engine.eval(reader, engine.getContext());
        ioBean.println("Finished test...");
    } catch (ScriptException e) {
        throw new ScriptException(e.getMessage(), scriptName, e.getLineNumber(), e.getColumnNumber());
    } finally {
        IOUtils.closeQuietly(reader);
    }
    return ioBean;
}

From source file:org.rhq.enterprise.server.plugins.alertCli.CliSender.java

public SenderResult send(Alert alert) {
    SenderResult result = new SenderResult();
    BufferedReader reader = null;
    ScriptEngine engine = null;/*  ww  w .j a va2 s  .  co m*/
    Subject subjectWithSession = null;
    final SessionManager sessionManager = SessionManager.getInstance();

    try {
        final Config config = getConfig();

        // simulate the login by getting a session ID
        config.subject = sessionManager.put(config.subject, pluginComponent.getScriptTimeout() * 1000);
        subjectWithSession = config.subject;

        result.setSummary(createSummary(config, SUMMARY_TEMPLATE));

        ByteArrayOutputStream scriptOutputStream = new ByteArrayOutputStream();
        PrintWriter scriptOut = new PrintWriter(scriptOutputStream);

        engine = getScriptEngine(alert, scriptOut, config);

        InputStream packageBits = getPackageBits(config.packageId, config.repoId);

        reader = new BufferedReader(new InputStreamReader(packageBits));

        final BufferedReader rdr = reader;

        final ExceptionHolder exceptionHolder = new ExceptionHolder();

        final ScriptEngine e = engine;
        Thread scriptRunner = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    e.eval(rdr);
                } catch (ScriptException e) {
                    exceptionHolder.scriptException = e;
                } catch (Throwable e) {
                    exceptionHolder.throwable = e;
                }
            }
        }, "Script Runner for alert " + alert);
        scriptRunner.setDaemon(true);
        scriptRunner.start();

        if (pluginComponent.getScriptTimeout() <= 0) {
            scriptRunner.join();
        } else {
            scriptRunner.join(pluginComponent.getScriptTimeout() * 1000);
        }

        scriptRunner.interrupt();

        if (exceptionHolder.scriptException != null) {
            LOG.info("The script execution for CLI notification of alert [" + alert + "] failed.",
                    exceptionHolder.scriptException);

            //make things pretty for the UI
            ScriptEngineInitializer initializer = ScriptEngineFactory.getInitializer(ENGINE_NAME);
            String message = initializer.extractUserFriendlyErrorMessage(exceptionHolder.scriptException);
            int col = exceptionHolder.scriptException.getColumnNumber();
            int line = exceptionHolder.scriptException.getLineNumber();
            String scriptName = createSummary(config,
                    "script $packageName ($packageVersion) in repo $repoName");
            throw new ScriptException(message, scriptName, line, col);
        } else if (exceptionHolder.throwable != null) {
            LOG.info("The script execution for CLI notification of alert [" + alert + "] failed.",
                    exceptionHolder.throwable);

            throw exceptionHolder.throwable;
        }

        scriptOut.flush();
        String scriptOutput = scriptOutputStream.toString(Charset.defaultCharset().name());

        if (scriptOutput.length() == 0) {
            scriptOutput = "Script generated no output.";
        }

        if (scriptOutput.length() > remainingResultSize(result)) {
            scriptOutput = scriptOutput.substring(0, remainingResultSize(result));
        }

        result.addSuccessMessage(scriptOutput);

        return result;
    } catch (IllegalArgumentException e) {
        return SenderResult.getSimpleFailure(e.getMessage()); //well, let's just hope the message doesn't exceed 4k.
    } catch (Throwable e) {
        result.addFailureMessage(ThrowableUtil.getAllMessages(e, true, remainingResultSize(result)));
        return result;
    } finally {
        if (subjectWithSession != null) {
            sessionManager.invalidate(subjectWithSession.getSessionId());
        }
        if (engine != null) {
            returnEngine(engine);
        }

        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                LOG.error("Failed to close the script reader.", e);
            }
        }
    }
}