/** * Given a filename, evaluate the file's contents as a JavaScript program. Return the value of the * program. If the test throws a Java exception or JavaScript runtime or compilation error, return * the string value of the error message. * * @param s full path to the file that will be exectued. * @return test result object. If the test is positive, result should be an instance of * Scriptable. if the test is negative, the result should be a String, whose value is the * message in the JavaScript error or Java exception. */ public Object executeTestFile(String s) { // this bit is stolen from Main.java FileReader in = null; try { in = new FileReader(s); } catch (FileNotFoundException ex) { driver.p("couldn't open file " + s); } Object result = null; try { // Here we evalute the entire contents of the file as // as script. Text is printed only if the print() function // is called. // cx.evaluateReader((Scriptable) global, in, args[i], 1, null); result = ((Scriptable) (((Context) cx).evaluateReader((Scriptable) global, (Reader) in, s, 1, null))); } catch (WrappedException we) { driver.p("Wrapped Exception: " + we.getWrappedException().toString()); result = we.getWrappedException().toString(); } catch (Exception jse) { driver.p("JavaScriptException: " + jse.getMessage()); result = jse.getMessage(); } return (result); }
/** {@inheritDoc} */ @Override public void beforeProcess() throws Throwable { // prepare execution context AuthenticationUtil.pushAuthentication(); AuthenticationUtil.setFullyAuthenticatedUser(this.fullyAuthenticatedUser); if (this.runAsUser != null && !this.runAsUser.equals(this.fullyAuthenticatedUser)) { AuthenticationUtil.setRunAsUser(this.runAsUser); } I18NUtil.setLocale(this.locale); if (this.contentLocale != null) { I18NUtil.setContentLocale(this.contentLocale); } try { try { super.doBeforeProcess(); } catch (final WrappedException ex) { // super should already handle unwrap runtime exceptions final Throwable wrappedException = ex.getWrappedException(); if (wrappedException instanceof RuntimeException) { // super should have handled this throw (RuntimeException) wrappedException; } throw new AlfrescoRuntimeException(wrappedException.getMessage(), wrappedException); } catch (final Throwable ex) { throw new AlfrescoRuntimeException(ex.getMessage(), ex); } } catch (final Throwable ex) { /* * The TxnCallback does not propagate non-retryable exceptions to the retrying transaction handler. Some exceptions may be * caused by execution of the provided script callback without passing through a service with its transaction interceptor which * would mark the transaction for rollback. We have to mark the transaction for rollback manually otherwise we end up with * commits of partial changes from the batch. (rollback on any exception is the default behaviour of Alfresco * SpringAwareUserTransaction) */ final RuleBasedTransactionAttribute transactionAttribute = new RuleBasedTransactionAttribute(); transactionAttribute.setReadOnly(true); transactionAttribute.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); // this never creates a new "real" transaction due to our propagation behavior final TransactionStatus transaction = this.txnManager.getTransaction(transactionAttribute); try { if (!transaction.isRollbackOnly()) { LOGGER.debug( "Marking transaction as rollback-only due to exception during batch processing", ex); transaction.setRollbackOnly(); } } finally { // this never actually commits a "real" transaction - it just clears transaction // synchronizations this.txnManager.commit(transaction); } throw ex; } }
/** * Since a continuation can only capture JavaScript frames and not Java frames, ensure that Rhino * throws an exception when the JavaScript frames don't reach all the way to the code called by * executeScriptWithContinuations or callFunctionWithContinuations. */ public void testErrorOnEvalCall() { Context cx = Context.enter(); try { cx.setOptimizationLevel(-1); // must use interpreter mode Script script = cx.compileString("eval('myObject.f(3);');", "test source", 1, null); cx.executeScriptWithContinuations(script, globalScope); fail("Should throw IllegalStateException"); } catch (WrappedException we) { Throwable t = we.getWrappedException(); assertTrue(t instanceof IllegalStateException); assertTrue(t.getMessage().startsWith("Cannot capture continuation")); } finally { Context.exit(); } }
/** {@inheritDoc} */ @Override public void process(final Object entry) throws Throwable { try { try { super.doProcess(entry); } catch (final WrappedException ex) { // super should already handle unwrap runtime exceptions final Throwable wrappedException = ex.getWrappedException(); if (wrappedException instanceof RuntimeException) { // super should have handled this throw (RuntimeException) wrappedException; } throw new AlfrescoRuntimeException(wrappedException.getMessage(), wrappedException); } catch (final Throwable ex) { throw new AlfrescoRuntimeException(ex.getMessage(), ex); } } catch (final Throwable ex) { /* * The TxnCallback does not propagate non-retryable exceptions to the retrying transaction handler. Some exceptions may be * caused by execution of the provided script callback without passing through a service with its transaction interceptor which * would mark the transaction for rollback. We have to mark the transaction for rollback manually otherwise we end up with * commits of partial changes from the batch. (rollback on any exception is the default behaviour of Alfresco * SpringAwareUserTransaction) */ final RuleBasedTransactionAttribute transactionAttribute = new RuleBasedTransactionAttribute(); transactionAttribute.setReadOnly(true); transactionAttribute.setPropagationBehavior(TransactionDefinition.PROPAGATION_MANDATORY); final TransactionStatus transaction = this.txnManager.getTransaction(transactionAttribute); if (!transaction.isRollbackOnly()) { LOGGER.debug( "Marking transaction as rollback-only due to exception during batch processing", ex); transaction.setRollbackOnly(); } throw ex; } }
/** * Evaluate JavaScript source. * * @param cx the current context * @param filename the name of the file to compile, or null for interactive mode. */ private void processSource(Context cx, String filename) { if (filename == null) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String sourceName = "<stdin>"; int lineno = 1; boolean hitEOF = false; do { int startline = lineno; System.err.print("js> "); System.err.flush(); try { String source = ""; // Collect lines of source to compile. while (true) { String newline; newline = in.readLine(); if (newline == null) { hitEOF = true; break; } source = source + newline + "\n"; lineno++; // Continue collecting as long as more lines // are needed to complete the current // statement. stringIsCompilableUnit is also // true if the source statement will result in // any error other than one that might be // resolved by appending more source. if (cx.stringIsCompilableUnit(source)) { break; } } Object result = cx.evaluateString(this, source, sourceName, startline, null); if (result != Context.getUndefinedValue()) { System.err.println(Context.toString(result)); } } catch (WrappedException we) { // Some form of exception was caught by JavaScript and // propagated up. System.err.println(we.getWrappedException().toString()); we.printStackTrace(); } catch (EvaluatorException ee) { // Some form of JavaScript error. System.err.println("js: " + ee.getMessage()); } catch (JavaScriptException jse) { // Some form of JavaScript error. System.err.println("js: " + jse.getMessage()); } catch (IOException ioe) { System.err.println(ioe.toString()); } if (quitting) { // The user executed the quit() function. break; } } while (!hitEOF); System.err.println(); } else { FileReader in = null; try { in = new FileReader(filename); } catch (FileNotFoundException ex) { Context.reportError("Couldn't open file \"" + filename + "\"."); return; } try { // Here we evalute the entire contents of the file as // a script. Text is printed only if the print() function // is called. cx.evaluateReader(this, in, filename, 1, null); } catch (WrappedException we) { System.err.println(we.getWrappedException().toString()); we.printStackTrace(); } catch (EvaluatorException ee) { System.err.println("js: " + ee.getMessage()); } catch (JavaScriptException jse) { System.err.println("js: " + jse.getMessage()); } catch (IOException ioe) { System.err.println(ioe.toString()); } finally { try { in.close(); } catch (IOException ioe) { System.err.println(ioe.toString()); } } } }