-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
16 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,23 +6,26 @@ | |
|
||
/** | ||
* The JavaScript evaluator. It is designed to run Nashorn engine in separate | ||
* thread (using provided {@link ExecutorService}), to allow limit cpu time | ||
* consumed. | ||
* thread (using provided {@link ExecutorService}), to allow limit cpu time | ||
* consumed. | ||
* | ||
* <p>Created on 2017.11.22</p> | ||
* <p> | ||
* Created on 2017.11.22 | ||
* </p> | ||
* | ||
* @author <a href="mailto:[email protected]">Marcin Golebski</a> | ||
* @version $Id$ | ||
*/ | ||
class JsEvaluator implements Runnable { | ||
private final ThreadMonitor threadMonitor; | ||
private final ScriptEngine scriptEngine; | ||
|
||
private Object result = null; | ||
private Exception exception = null; | ||
private final ScriptEngineOperation operation; | ||
|
||
JsEvaluator(final ScriptEngine scriptEngine, final long maxCPUTime, final long maxMemory, ScriptEngineOperation operation) { | ||
JsEvaluator(final ScriptEngine scriptEngine, final long maxCPUTime, final long maxMemory, | ||
ScriptEngineOperation operation) { | ||
this.scriptEngine = scriptEngine; | ||
this.threadMonitor = new ThreadMonitor(maxCPUTime, maxMemory); | ||
this.operation = operation; | ||
|
@@ -35,37 +38,34 @@ boolean isScriptKilled() { | |
boolean isCPULimitExceeded() { | ||
return threadMonitor.isCPULimitExceeded(); | ||
} | ||
|
||
boolean isMemoryLimitExceeded() { | ||
return threadMonitor.isMemoryLimitExceeded(); | ||
} | ||
|
||
/** | ||
* Enter the monitor method. It should be called from main thread. | ||
* Enter the monitor method. It should be called from main thread. | ||
*/ | ||
void runMonitor() { | ||
threadMonitor.run(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
boolean registered = threadMonitor.registerThreadToMonitor(Thread.currentThread()); | ||
if (registered) { | ||
result = operation.executeScriptEngineOperation(scriptEngine); | ||
} | ||
} | ||
catch (final RuntimeException e) { | ||
} catch (final RuntimeException e) { | ||
// InterruptedException means script was successfully interrupted, | ||
// so no exception should be propagated | ||
if(!(e.getCause() instanceof InterruptedException)) { | ||
if (!(e.getCause() instanceof InterruptedException)) { | ||
exception = e; | ||
} | ||
} | ||
catch (final Exception e) { | ||
} catch (final Exception e) { | ||
exception = e; | ||
} | ||
finally { | ||
} finally { | ||
threadMonitor.scriptFinished(); | ||
threadMonitor.stopMonitor(); | ||
} | ||
|
@@ -74,7 +74,7 @@ public void run() { | |
Exception getException() { | ||
return exception; | ||
} | ||
|
||
Object getResult() { | ||
return result; | ||
} | ||
|