From 58df2b272dd2f14d95d0a9e959ca452f02eaa37d Mon Sep 17 00:00:00 2001 From: jimtng <2554958+jimtng@users.noreply.github.com> Date: Mon, 1 May 2023 02:38:25 +1000 Subject: [PATCH] Remove GenericScriptEngineFactory (#3550) Signed-off-by: Jimmy Tanagra --- .../internal/GenericScriptEngineFactory.java | 30 ------------- .../internal/ScriptEngineFactoryHelper.java | 1 - .../internal/ScriptEngineManagerImpl.java | 43 ++++--------------- 3 files changed, 8 insertions(+), 66 deletions(-) delete mode 100644 bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/GenericScriptEngineFactory.java diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/GenericScriptEngineFactory.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/GenericScriptEngineFactory.java deleted file mode 100644 index e6fe0c49fd..0000000000 --- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/GenericScriptEngineFactory.java +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Copyright (c) 2010-2023 Contributors to the openHAB project - * - * See the NOTICE file(s) distributed with this work for additional - * information. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.openhab.core.automation.module.script.internal; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.openhab.core.automation.module.script.AbstractScriptEngineFactory; -import org.openhab.core.automation.module.script.ScriptEngineFactory; -import org.osgi.service.component.annotations.Component; - -/** - * An implementation of {@link ScriptEngineFactory} for ScriptEngines that do not require customizations. - * - * @author Simon Merschjohann - Initial contribution - * @author Scott Rushworth - added service and removed methods now inherited from AbstractScriptEngineFactory - */ -@NonNullByDefault -@Component(service = ScriptEngineFactory.class) -public class GenericScriptEngineFactory extends AbstractScriptEngineFactory { - -} diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptEngineFactoryHelper.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptEngineFactoryHelper.java index 46871bc9cc..aa9935baba 100644 --- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptEngineFactoryHelper.java +++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptEngineFactoryHelper.java @@ -76,7 +76,6 @@ public class ScriptEngineFactoryHelper { } public static Optional getPreferredExtension(ScriptEngineFactory factory) { - // return an Optional because GenericScriptEngineFactory has no scriptTypes return factory.getScriptTypes().stream().filter(type -> !type.contains("/")) .min(Comparator.comparing(String::length)); } diff --git a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptEngineManagerImpl.java b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptEngineManagerImpl.java index 3467aa7698..bdab13e386 100644 --- a/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptEngineManagerImpl.java +++ b/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/ScriptEngineManagerImpl.java @@ -60,8 +60,7 @@ public class ScriptEngineManagerImpl implements ScriptEngineManager { private final Logger logger = LoggerFactory.getLogger(ScriptEngineManagerImpl.class); private final Map loadedScriptEngineInstances = new HashMap<>(); - private final Map customSupport = new HashMap<>(); - private final Map genericSupport = new HashMap<>(); + private final Map factories = new HashMap<>(); private final ScriptExtensionManager scriptExtensionManager; private final Set listeners = new HashSet<>(); @@ -75,11 +74,7 @@ public class ScriptEngineManagerImpl implements ScriptEngineManager { List scriptTypes = engineFactory.getScriptTypes(); logger.trace("{}.getScriptTypes(): {}", engineFactory.getClass().getSimpleName(), scriptTypes); for (String scriptType : scriptTypes) { - if (isCustomFactory(engineFactory)) { - this.customSupport.put(scriptType, engineFactory); - } else { - this.genericSupport.put(scriptType, engineFactory); - } + factories.put(scriptType, engineFactory); listeners.forEach(listener -> listener.factoryAdded(scriptType)); } if (logger.isDebugEnabled()) { @@ -88,10 +83,10 @@ public class ScriptEngineManagerImpl implements ScriptEngineManager { if (scriptEngine != null) { javax.script.ScriptEngineFactory factory = scriptEngine.getFactory(); logger.debug( - "Initialized a {} ScriptEngineFactory for {} ({}): supports {} ({}) with file extensions {}, names {}, and mimetypes {}", - (isCustomFactory(engineFactory)) ? "custom" : "generic", factory.getEngineName(), - factory.getEngineVersion(), factory.getLanguageName(), factory.getLanguageVersion(), - factory.getExtensions(), factory.getNames(), factory.getMimeTypes()); + "Initialized a ScriptEngineFactory for {} ({}): supports {} ({}) with file extensions {}, names {}, and mimetypes {}", + factory.getEngineName(), factory.getEngineVersion(), factory.getLanguageName(), + factory.getLanguageVersion(), factory.getExtensions(), factory.getNames(), + factory.getMimeTypes()); } else { logger.trace("addScriptEngineFactory: engine was null"); } @@ -105,26 +100,12 @@ public class ScriptEngineManagerImpl implements ScriptEngineManager { List scriptTypes = engineFactory.getScriptTypes(); logger.trace("{}.getScriptTypes(): {}", engineFactory.getClass().getSimpleName(), scriptTypes); for (String scriptType : scriptTypes) { - if (isCustomFactory(engineFactory)) { - this.customSupport.remove(scriptType, engineFactory); - } else { - this.genericSupport.remove(scriptType, engineFactory); - } + factories.remove(scriptType, engineFactory); listeners.forEach(listener -> listener.factoryRemoved(scriptType)); } logger.debug("Removed {}", engineFactory.getClass().getSimpleName()); } - /** - * This method is used to determine if a given {@link ScriptEngineFactory} is generic or customized. - * - * @param engineFactory {@link ScriptEngineFactory} - * @return true, if the {@link ScriptEngineFactory} is custom, otherwise false - */ - private boolean isCustomFactory(ScriptEngineFactory engineFactory) { - return !(engineFactory instanceof GenericScriptEngineFactory); - } - @Override public @Nullable ScriptEngineContainer createScriptEngine(String scriptType, String engineIdentifier) { ScriptEngineContainer result = null; @@ -256,15 +237,7 @@ public class ScriptEngineManagerImpl implements ScriptEngineManager { * @return {@link ScriptEngineFactory} or null */ private @Nullable ScriptEngineFactory findEngineFactory(String scriptType) { - ScriptEngineFactory customFactory = customSupport.get(scriptType); - if (customFactory != null) { - return customFactory; - } - ScriptEngineFactory genericFactory = genericSupport.get(scriptType); - if (genericFactory != null) { - return genericFactory; - } - return null; + return factories.get(scriptType); } @Override