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

Signed-off-by: Mark Hilbush <mark@hilbush.com>
This commit is contained in:
Mark Hilbush 2021-02-20 13:53:44 -05:00 committed by GitHub
parent c7b7390c91
commit 7de57f9ffe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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
}
}
}