Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping the Rhino Engine in middle of execution

Tags:

java

rhino

Does the Rhino engine have an api that can stop the execution of a script fie in the middle. For example, I have a script file in which there is an infinte loop. How can I stop the execution in the middle?

Of course, I can stop the jvm which started the Rhino engine to excecute the script. But I don't want to kill the entire jvm session for that reason as i have started the script programatically and the Rhino Engine also running in the same JVM as my application.

like image 265
Syam Avatar asked Apr 20 '12 12:04

Syam


1 Answers

Stopping the execution of running JavaScript can be done by using following way.

1) Create a dummy Debugger and attach it to the context created initially.

mContext = Context.enter();
ObservingDebugger observingDebugger = new ObservingDebugger();
mContext.setDebugger(observingDebugger, new Integer(0));
mContext.setGeneratingDebug(true);
mContext.setOptimizationLevel(-1);

The ObservingDebugger code looks as follows.

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.debug.DebugFrame;
import org.mozilla.javascript.debug.DebuggableScript;
import org.mozilla.javascript.debug.Debugger;

public class ObservingDebugger implements Debugger 
{
boolean isDisconnected = false;

private DebugFrame debugFrame = null;

public boolean isDisconnected() {
    return isDisconnected;
}

public void setDisconnected(boolean isDisconnected) {
    this.isDisconnected = isDisconnected;
    if(debugFrame != null){
       ((ObservingDebugFrame)debugFrame).setDisconnected(isDisconnected);
    }
}

public ObservingDebugger() {

}

public DebugFrame getFrame(Context cx, DebuggableScript fnOrScript)
{
    if(debugFrame == null){
        debugFrame = new ObservingDebugFrame(isDisconnected);
    }
    return debugFrame;      
}

@Override
public void handleCompilationDone(Context arg0, DebuggableScript arg1, String arg2) {   } }
// internal ObservingDebugFrame class
class ObservingDebugFrame implements DebugFrame
   {
boolean isDisconnected = false;

public boolean isDisconnected() {
    return isDisconnected;
}

public void setDisconnected(boolean isDisconnected) {
    this.isDisconnected = isDisconnected;
}

ObservingDebugFrame(boolean isDisconnected)
{
    this.isDisconnected = isDisconnected;
}

public void onEnter(Context cx, Scriptable activation,
        Scriptable thisObj, Object[] args)
{ }

public void onLineChange(Context cx, int lineNumber) 
{
    if(isDisconnected){
        throw new RuntimeException("Script Execution terminaed");
    }
}

public void onExceptionThrown(Context cx, Throwable ex)
{ }

public void onExit(Context cx, boolean byThrow,
        Object resultOrException)
{ }

@Override
public void onDebuggerStatement(Context arg0) { } }

ObservingDebugger class will manage the boolean variable "isDisconnected" and when user clicks on the stop button (wants to stop the execution) then this variable is set to true. Once the variable is set to true as follows the Rhino Execution will immediately terminates.

observingDebugger.setDisconnected(true);
like image 113
Syam Avatar answered Sep 21 '22 21:09

Syam