Example usage for javax.script ScriptException getCause

List of usage examples for javax.script ScriptException getCause

Introduction

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

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:com.github.safrain.remotegsh.server.RgshFilter.java

private void performShellExecute(HttpServletRequest request, HttpServletResponse response) throws IOException {
    ShellSession session = getSession(request.getParameter("sid"));
    if (session == null) {
        response.setStatus(410);// Http status GONE
        return;//from  ww  w . j a  va2 s.c  om
    }
    ScriptEngine engine = session.getEngine();

    String action = request.getParameter("action");
    if (action == null) {
        StringWriter responseWriter = new StringWriter();
        engine.getContext().setWriter(responseWriter);
        engine.getContext().setErrorWriter(response.getWriter());
        String script = toString(request.getInputStream(), charset);
        JSONObject json = new JSONObject();
        try {
            try {
                Object result = engine.eval(script);
                json.put("result", String.valueOf(result));
                response.setStatus(200);
                json.put("response", responseWriter.getBuffer().toString());
            } catch (ScriptException e) {
                log.log(Level.SEVERE, "Error while running shell command:" + script, e);
                response.setStatus(500);
                e.getCause().printStackTrace(response.getWriter());
                return;
            }
        } catch (JSONException e) {
            log.log(Level.SEVERE, "Error while running shell command:" + script, e);
            response.setStatus(500);
            e.printStackTrace(response.getWriter());
            return;
        }
        response.getWriter().write(json.toString());
    } else {
        Invocable invocable = (Invocable) engine;
        try {
            invocable.invokeFunction("shellAction", action);
        } catch (ScriptException e) {
            response.setStatus(500);
            e.printStackTrace(response.getWriter());
        } catch (NoSuchMethodException e) {
            response.setStatus(500);
            response.getWriter().println("Action not supported");
        } catch (Exception e) {
            response.setStatus(500);
            e.printStackTrace(response.getWriter());
        }
    }

}

From source file:org.graphwalker.machines.ExtendedFiniteStateMachine.java

@Override
public boolean walkEdge(Edge edge) {
    boolean hasWalkedEdge = super.walkEdge(edge);
    if (hasWalkedEdge) {
        if (hasAction(edge)) {
            PrintStream ps = System.out;
            System.setOut(Void);

            if (jsEngine != null) {
                try {
                    jsEngine.eval(getAction(edge));
                } catch (ScriptException e) {
                    logger.error("Problem when running: '" + getAction(edge) + "' in Java Script engine");
                    logger.error("EvalError: " + e);
                    logger.error(e.getCause());
                    throw new RuntimeException("Malformed action sequence\n\t" + edge + "\n\tAction sequence: "
                            + edge.getActionsKey() + "\n\tJava Script error message: '" + e.getMessage()
                            + "'\nDetails: " + e.getCause());
                } finally {
                    System.setOut(ps);
                }//from  w  w w  .j a  v a2s.  c o m
            } else if (beanShellEngine != null) {
                try {
                    beanShellEngine.eval(getAction(edge));
                } catch (EvalError e) {
                    logger.error("Problem when running: '" + getAction(edge) + "' in BeanShell");
                    logger.error("EvalError: " + e);
                    logger.error(e.getCause());
                    throw new RuntimeException("Malformed action sequence\n\t" + edge + "\n\tAction sequence: "
                            + edge.getActionsKey() + "\n\tBeanShell error message: '" + e.getMessage()
                            + "'\nDetails: " + e.getCause());
                } finally {
                    System.setOut(ps);
                }
            }
        }
    }
    return hasWalkedEdge;
}

From source file:org.openadaptor.auxil.processor.script.ScriptProcessor.java

/**
 * Process a data item.//from w w  w.  j a  v a  2 s.c  om
 * It will bind the data using the configured databinding, to
 * make it available to the script.
 * 
 * The bound object will be returned in a single Element Object[]
 * <br>
 * Note: If compilation is not possible, and the script is contained in a 
 * file, then the file will be read each time a datum is being processed. This
 * should be avoided for obvious reasons :-)
 */
protected Object[] doProcess(Object data) {
    if (data == null) { //conform to IDataProcessor contract.
        throw new NullRecordException("Null record not permitted");
    }
    //Clone it if possible.
    //data=ReflectionUtils.clone(data);
    data = cloner.clone(data);
    try {
        scriptEngine.put(metadataBinding, metadata);
        scriptEngine.put(dataBinding, data);
        if (compiledScript != null) {
            lastResult = compiledScript.eval();
        } else {
            if (script != null) {
                lastResult = scriptEngine.eval(script);
            } else {
                lastResult = scriptEngine.eval(new FileReader(scriptFilename));
            }
        }
        data = scriptEngine.get(dataBinding);
        if (data == null) {
            data = new Object[] {};
        } else {
            if (boxReturnedArrays || (!(data instanceof Object[]))) {
                data = new Object[] { data }; //Wrap it in an Object array.
            }
        }
        return (Object[]) data;
    } catch (ScriptException e) {
        log.debug("Script cause: " + e.getCause());

        throw new ProcessingException("failed to execute script, " + e.getMessage() + " line "
                + e.getLineNumber() + " col " + e.getColumnNumber(), e, this);
    } catch (FileNotFoundException e) {
        throw new ConnectionException("failed to load script file, " + e.getMessage() + scriptFilename, e,
                this);
    }
}

From source file:org.freeplane.plugin.script.GenericScript.java

private void handleScriptRuntimeException(final ScriptException e) {
    outStream.print("message: " + e.getMessage());
    int lineNumber = e.getLineNumber();
    outStream.print("Line number: " + lineNumber);
    errorHandler.gotoLine(lineNumber);//  w  ww.  ja v  a2s .c o m
    throw new ExecuteScriptException(e.getMessage() + " at line " + lineNumber,
            // The ScriptException should have a cause. Use
            // that, it is what we want to know.
            (e.getCause() == null) ? e : e.getCause());
}

From source file:org.apache.camel.builder.script.ScriptBuilder.java

protected synchronized Object evaluateScript(Exchange exchange) {
    try {/*from  w w w . j a v  a  2 s .c o m*/
        getScriptContext();
        populateBindings(getEngine(), exchange);
        Object result = runScript();
        if (LOG.isDebugEnabled()) {
            LOG.debug("The script evaluation result is: " + result);
        }
        return result;
    } catch (ScriptException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Script evaluation failed: " + e, e);
        }
        throw createScriptEvaluationException(e.getCause());
    } catch (IOException e) {
        throw createScriptEvaluationException(e);
    }
}

From source file:com.qspin.qtaste.testsuite.impl.JythonTestScript.java

private void handleScriptException(ScriptException e, TestResult result) {
    Throwable cause = e.getCause();

    // handle ThreadDeath exception
    if (cause instanceof PyException) {
        PyException pe = (PyException) cause;
        if (pe.value instanceof PyObjectDerived) {
            Object javaError = pe.value.__tojava__(Throwable.class);
            if (javaError != null && javaError != Py.NoConversion) {
                if (javaError instanceof ThreadDeath) {
                    dumpScriptPythonStackDetails(result, cause);
                    throw (ThreadDeath) javaError;
                }//from   www.j  a va  2  s  .co m
            }
        }
    }

    result.setFailedLineNumber(e.getLineNumber());
    result.setStatus(TestResult.Status.NOT_AVAILABLE);
    String message = null;
    boolean dumpStack = true;

    if (cause instanceof PySyntaxError) {
        // set a clear syntax error message
        PySyntaxError syntaxError = (PySyntaxError) cause;
        try {
            PyString fileName, text;
            PyInteger lineNumber, columnNumber;
            if (syntaxError.value instanceof PyTuple) {
                PyObject[] infos = ((PyTuple) ((PyTuple) syntaxError.value).getArray()[1]).getArray();
                fileName = (PyString) infos[0];
                lineNumber = (PyInteger) infos[1];
                columnNumber = (PyInteger) infos[2];
                text = (PyString) infos[3];
            } else {
                fileName = (PyString) syntaxError.value.__getattr__(new PyString("filename"));
                lineNumber = (PyInteger) syntaxError.value.__getattr__(new PyString("lineno"));
                columnNumber = (PyInteger) syntaxError.value.__getattr__(new PyString("offset"));
                text = (PyString) syntaxError.value.__getattr__(new PyString("text"));
            }
            message = "Python syntax error in file " + fileName + " at line " + lineNumber + ", column "
                    + columnNumber + ":\n" + text;
            result.addStackTraceElement(
                    new StackTraceElement("", "", fileName.toString(), lineNumber.getValue()));
            dumpStack = false;
        } catch (PyException pye) {
            message = "Python syntax error (Couldn't decode localization of error)";
        }
    } else if (cause instanceof PyException) {
        PyException pe = (PyException) cause;
        if (pe.value instanceof PyObjectDerived) {
            // check  if exception is UndeclaredThrowableException
            // in this case status is "failed" and message is taken from cause exception
            Object javaError = pe.value.__tojava__(Throwable.class);
            if (javaError != null && javaError != Py.NoConversion) {
                if (javaError instanceof QTasteException) {
                    handleQTasteException((QTasteException) javaError, result);
                    message = result.getExtraResultDetails();
                } else if (javaError instanceof UndeclaredThrowableException) {
                    result.setStatus(TestResult.Status.FAIL);
                    Throwable undeclaredThrowable = ((UndeclaredThrowableException) javaError).getCause();
                    if (undeclaredThrowable instanceof InvocationTargetException) {
                        message = getThrowableDescription(undeclaredThrowable.getCause());
                    } else {
                        message = getThrowableDescription(undeclaredThrowable);
                    }
                } else if (javaError instanceof Throwable) {
                    message = getThrowableDescription((Throwable) javaError);
                }
            }
        }
        if (message == null) {
            if (pe.type instanceof PyType) {
                String errorName = null, errorValue;
                try {
                    PyObject doc = pe.value.__getattr__(new PyString("__doc__"));
                    if (doc != Py.None) {
                        errorName = doc.toString();
                        if (errorName.endsWith(".")) {
                            errorName = errorName.substring(0, errorName.length() - 1);
                        }
                    }
                } catch (PyException pye) {
                }
                if (errorName == null) {
                    errorName = ((PyType) pe.type).getName();
                }

                try {
                    errorValue = pe.value.__str__().toString();
                } catch (PyException pye) {
                    errorValue = pe.value.toString();
                }
                if (errorValue.startsWith(errorName)) {
                    message = errorValue;
                } else {
                    message = errorName + ": " + errorValue;
                }
            } else {
                message = getThrowableDescription(e);
            }
        }
    } else {
        message = getThrowableDescription(e);
    }

    result.setExtraResultDetails(message);
    if (dumpStack) {
        dumpScriptPythonStackDetails(result, cause);
    }

    if (!result.getExtraResultDetails().isEmpty()) {
        logger.error(result.getExtraResultDetails());
    }
    if ((result.getStackTrace() != null) && !result.getStackTrace().isEmpty()) {
        logger.error("Script stack trace: \n" + result.getStackTrace());
    }
}