From 7d8126e89f442b1ab2dffc085ea147ed209db64b Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Tue, 13 Oct 2020 23:13:30 +0200 Subject: [PATCH] Fix ScriptEngine parameter option removal (#1716) When one of the engines is unset the ScriptModuleTypeProvider clears all parameter options instead of only those that apply to that engine. This fixes the Nashorn engine missing from the parameter options on the first openHAB startup. Signed-off-by: Wouter Born --- .../provider/ScriptModuleTypeProvider.java | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/provider/ScriptModuleTypeProvider.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/provider/ScriptModuleTypeProvider.java index f973956034..396ff0c492 100755 --- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/provider/ScriptModuleTypeProvider.java +++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/provider/ScriptModuleTypeProvider.java @@ -145,26 +145,34 @@ public class ScriptModuleTypeProvider implements ModuleTypeProvider { public void setScriptEngineFactory(ScriptEngineFactory engineFactory) { ScriptEngine scriptEngine = engineFactory.createScriptEngine(engineFactory.getScriptTypes().get(0)); if (scriptEngine != null) { - List mimeTypes = new ArrayList<>(); - javax.script.ScriptEngineFactory factory = scriptEngine.getFactory(); - String languageName = String.format("%s (%s)", - factory.getLanguageName().substring(0, 1).toUpperCase() + factory.getLanguageName().substring(1), - factory.getLanguageVersion()); - mimeTypes.addAll(factory.getMimeTypes()); - String preferredMimeType = mimeTypes.get(0); - mimeTypes.removeIf(mimeType -> !mimeType.contains("application") || mimeType.contains("x-")); - if (!mimeTypes.isEmpty()) { - preferredMimeType = mimeTypes.get(0); - } - parameterOptions.put(preferredMimeType, languageName); + parameterOptions.put(getPreferredMimeType(factory), getLanguageName(factory)); logger.trace("ParameterOptions: {}", parameterOptions); } else { logger.trace("setScriptEngineFactory: engine was null"); } } - public void unsetScriptEngineFactory(ScriptEngineFactory scriptEngineFactory) { - parameterOptions.clear(); + public void unsetScriptEngineFactory(ScriptEngineFactory engineFactory) { + ScriptEngine scriptEngine = engineFactory.createScriptEngine(engineFactory.getScriptTypes().get(0)); + if (scriptEngine != null) { + javax.script.ScriptEngineFactory factory = scriptEngine.getFactory(); + parameterOptions.remove(getPreferredMimeType(factory)); + logger.trace("ParameterOptions: {}", parameterOptions); + } else { + logger.trace("unsetScriptEngineFactory: engine was null"); + } + } + + private String getPreferredMimeType(javax.script.ScriptEngineFactory factory) { + List mimeTypes = new ArrayList<>(factory.getMimeTypes()); + mimeTypes.removeIf(mimeType -> !mimeType.contains("application") || mimeType.contains("x-")); + return mimeTypes.isEmpty() ? factory.getMimeTypes().get(0) : mimeTypes.get(0); + } + + private String getLanguageName(javax.script.ScriptEngineFactory factory) { + return String.format("%s (%s)", + factory.getLanguageName().substring(0, 1).toUpperCase() + factory.getLanguageName().substring(1), + factory.getLanguageVersion()); } }