org.cometd.javascript.JavaScriptThreadModel Maven / Gradle / Ivy
package org.cometd.javascript;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
/**
* The thread model object, that runs all javascript in a single thread to simulate browser's environment.
*
* @version $Revision: 1036 $ $Date: 2010-03-22 18:17:18 +0100 (Mon, 22 Mar 2010) $
*/
public class JavaScriptThreadModel extends ScriptableObject implements Runnable, ThreadModel
{
private final Thread thread = new Thread(this);
private final BlockingQueue> queue = new LinkedBlockingQueue>();
private final ScriptableObject rootScope;
private volatile boolean running;
private volatile Context context;
private AtomicInteger scripts = new AtomicInteger();
public JavaScriptThreadModel()
{
this(null);
}
public JavaScriptThreadModel(ScriptableObject rootScope)
{
this.rootScope = rootScope;
}
public void init() throws Exception
{
assert rootScope != null;
running = true;
thread.start();
}
public void destroy() throws Exception
{
running = false;
thread.interrupt();
thread.join();
}
public String getClassName()
{
return "JavaScriptThreadModel";
}
public void run()
{
context = Context.enter();
context.setGeneratingDebug(true);
context.setGeneratingSource(true);
context.setOptimizationLevel(-1);
try
{
while (running)
{
FutureTask
© 2015 - 2025 Weber Informatics LLC | Privacy Policy