[automation] Fixed memory leak caused by invalid UI DSL rule (#2212)

Signed-off-by: Mark Hilbush <mark@hilbush.com>
3.0.x
Mark Hilbush 2021-02-20 13:53:44 -05:00 committed by Kai Kreuzer
parent 728731a799
commit d23349b00c
1 changed files with 11 additions and 1 deletions

View File

@ -14,6 +14,7 @@ package org.openhab.core.model.script.runtime.internal.engine;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@ -136,21 +137,23 @@ public class ScriptEngineImpl implements ScriptEngine, ModelParser {
List<Diagnostic> errors = resource.getErrors();
if (!errors.isEmpty()) {
deleteResource(resource);
throw new ScriptParsingException("Failed to parse expression (due to managed SyntaxError/s)",
scriptAsString).addDiagnosticErrors(errors);
}
EList<EObject> contents = resource.getContents();
if (!contents.isEmpty()) {
Iterable<Issue> validationErrors = getValidationErrors(contents.get(0));
if (!validationErrors.iterator().hasNext()) {
return (XExpression) contents.get(0);
} else {
deleteResource(resource);
throw new ScriptParsingException("Failed to parse expression (due to managed ValidationError/s)",
scriptAsString).addValidationIssues(validationErrors);
}
} else {
deleteResource(resource);
return null;
}
}
@ -184,4 +187,11 @@ public class ScriptEngineImpl implements ScriptEngine, ModelParser {
return "script";
}
private void deleteResource(Resource resource) {
try {
resource.delete(Collections.emptyMap());
} catch(IOException e) {
// Do nothing
}
}
}