Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <github@klug.nrw>
  • Loading branch information
J-N-K committed Jan 2, 2023
1 parent af8a0b1 commit a625a3b
Showing 1 changed file with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
import org.openhab.core.automation.module.script.profile.ScriptProfile;
import org.openhab.core.common.ThreadPoolManager;
import org.openhab.core.common.registry.RegistryChangeListener;
import org.openhab.core.transform.Transformation;
import org.openhab.core.config.core.ConfigOptionProvider;
import org.openhab.core.config.core.ParameterOption;
import org.openhab.core.transform.Transformation;
import org.openhab.core.transform.TransformationException;
import org.openhab.core.transform.TransformationRegistry;
import org.openhab.core.transform.TransformationService;
Expand Down Expand Up @@ -148,10 +148,10 @@ public void deactivate() {
try {
CompiledScript compiledScript = scriptRecord.compiledScript;

if (compiledScript == null && scriptEngineContainer.getScriptEngine() instanceof Compilable) {
if (compiledScript == null
&& scriptEngineContainer.getScriptEngine()instanceof Compilable scriptCompiler) {
// no compiled script available but compiling is supported
compiledScript = ((Compilable) scriptEngineContainer.getScriptEngine())
.compile(scriptRecord.script);
compiledScript = scriptCompiler.compile(scriptRecord.script);
scriptRecord.compiledScript = compiledScript;
}

Expand Down Expand Up @@ -218,14 +218,13 @@ private void disposeScriptRecord(ScriptRecord scriptRecord) {
}

private void disposeScriptEngine(ScriptEngine scriptEngine) {
if (scriptEngine instanceof AutoCloseable) {
if (scriptEngine instanceof AutoCloseable closableScriptEngine) {
// we cannot not use ScheduledExecutorService.execute here as it might execute the task in the calling
// thread (calling ScriptEngine.close in the same thread may result in a deadlock if the ScriptEngine
// tries to Thread.join)
scheduler.schedule(() -> {
AutoCloseable closeable = (AutoCloseable) scriptEngine;
try {
closeable.close();
closableScriptEngine.close();
} catch (Exception e) {
logger.error("Error while closing script engine", e);
}
Expand Down Expand Up @@ -274,5 +273,4 @@ private static class ScriptRecord {

public final Lock lock = new ReentrantLock();
}

}

0 comments on commit a625a3b

Please sign in to comment.