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 <github@maindrain.net>
pull/1719/head
Wouter Born 2020-10-13 23:13:30 +02:00 committed by GitHub
parent a29490a545
commit 7d8126e89f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 14 deletions

View File

@ -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<String> 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<String> 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());
}
}