Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Commit 9a371c6

Browse files
committed
ScriptEngine is thread safe
Without `new ScriptEngineManager(null)` the tests were failing
1 parent 0ae43ee commit 9a371c6

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

web/src/main/java/org/perfrepo/web/alerting/ConditionCheckerImpl.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public class ConditionCheckerImpl implements ConditionChecker {
5353

5454
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm");
5555

56+
private static final ScriptEngine JAVA_SCRIPT_ENGINE = new ScriptEngineManager(null).getEngineByName("JavaScript");
57+
5658
@Inject
5759
private TestExecutionDAO testExecutionDAO;
5860

@@ -82,7 +84,7 @@ public class ConditionCheckerImpl implements ConditionChecker {
8284
public void checkConditionSyntax(String condition, Metric metric) {
8385
// creates dummy execution and triggers evaluation against it
8486
// if we had a 'perfect' grammar, we would only need to call parseTree(condition);
85-
// but ATM we need script engine to evaluate CONDITION and tell us if there were any errors
87+
// but ATM we need script JAVA_SCRIPT_ENGINE to evaluate CONDITION and tell us if there were any errors
8688
// e.g. current grammar cannot catch nonsenses such as: CONDITION x <!= 10
8789
TestExecution testExecution;
8890
TestExecutionBuilder builder = TestExecution.builder();
@@ -184,10 +186,9 @@ public boolean checkCondition(String condition, TestExecution currentResult, Met
184186
* @return evaluated condition, true if it holds, false otherwise
185187
*/
186188
private boolean evaluate(String expression, Map<String, Object> variables) {
187-
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
188189
Object result;
189190
try {
190-
result = engine.eval(expression, new SimpleBindings(variables));
191+
result = JAVA_SCRIPT_ENGINE.eval(expression, new SimpleBindings(variables));
191192
} catch (ScriptException e) {
192193
throw new IllegalArgumentException("Error occurred while evaluating the expression.", e);
193194
}
@@ -485,12 +486,12 @@ private Double getValueFromMetric(TestExecution testExecution) {
485486
private CommonTree parseTree(String string) {
486487
//lexer splits input into tokens
487488
ANTLRStringStream input = new ANTLRStringStream(string);
488-
TokenStream tokens = new CommonTokenStream(new AlertingDSLLexer(input));
489+
TokenStream tokens = new CommonTokenStream(new org.perfrepo.web.alerting.AlertingDSLLexer(input));
489490

490491
//parser generates abstract syntax tree
491-
AlertingDSLParser parser = new AlertingDSLParser(tokens);
492+
org.perfrepo.web.alerting.AlertingDSLParser parser = new org.perfrepo.web.alerting.AlertingDSLParser(tokens);
492493

493-
AlertingDSLParser.expression_return ret;
494+
org.perfrepo.web.alerting.AlertingDSLParser.expression_return ret;
494495
try {
495496
ret = parser.expression();
496497
} catch (RecognitionException ex) {

0 commit comments

Comments
 (0)