diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/handler/AbstractScriptModuleHandler.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/handler/AbstractScriptModuleHandler.java index 1b5d7dacca..dc22f79302 100644 --- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/handler/AbstractScriptModuleHandler.java +++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/handler/AbstractScriptModuleHandler.java @@ -101,11 +101,11 @@ public abstract class AbstractScriptModuleHandler extends Base } /** - * Adds the passed context variables of the rule engine to the context scope of the ScriptEngine, this should be - * updated each time the module is executed + * Adds the passed context variables of the rule engine to the context scope of the ScriptEngine + * this should be done each time the module is executed to prevent leaking context to later executions * * @param engine the script engine that is used - * @param context the variables and types to put into the execution context + * @param context the variables and types to remove from the execution context */ protected void setExecutionContext(ScriptEngine engine, Map context) { ScriptContext executionContext = engine.getContext(); @@ -130,4 +130,25 @@ public abstract class AbstractScriptModuleHandler extends Base executionContext.setAttribute(key, value, ScriptContext.ENGINE_SCOPE); } } + + /** + * Removes passed context variables of the rule engine from the context scope of the ScriptEngine, this should be + * updated each time the module is executed + * + * @param engine the script engine that is used + * @param context the variables and types to put into the execution context + */ + protected void resetExecutionContext(ScriptEngine engine, Map context) { + ScriptContext executionContext = engine.getContext(); + + for (Entry entry : context.entrySet()) { + Object value = entry.getValue(); + String key = entry.getKey(); + int dotIndex = key.indexOf('.'); + if (dotIndex != -1) { + key = key.substring(dotIndex + 1); + } + executionContext.removeAttribute(key, ScriptContext.ENGINE_SCOPE); + } + } } diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/handler/ScriptActionHandler.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/handler/ScriptActionHandler.java index 571d7a4800..774833fe33 100644 --- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/handler/ScriptActionHandler.java +++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/handler/ScriptActionHandler.java @@ -65,6 +65,7 @@ public class ScriptActionHandler extends AbstractScriptModuleHandler imp logger.error("Script execution of rule with UID '{}' failed: {}", ruleUID, e.getMessage(), logger.isDebugEnabled() ? e : null); } + resetExecutionContext(scriptEngine, context); }); return resultMap;