Remove GenericScriptEngineFactory (#3550)

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
pull/3586/head
jimtng 2023-05-01 02:38:25 +10:00 committed by GitHub
parent 33233982b3
commit 58df2b272d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 66 deletions

View File

@ -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 {
}

View File

@ -76,7 +76,6 @@ public class ScriptEngineFactoryHelper {
}
public static Optional<String> getPreferredExtension(ScriptEngineFactory factory) {
// return an Optional because GenericScriptEngineFactory has no scriptTypes
return factory.getScriptTypes().stream().filter(type -> !type.contains("/"))
.min(Comparator.comparing(String::length));
}

View File

@ -60,8 +60,7 @@ public class ScriptEngineManagerImpl implements ScriptEngineManager {
private final Logger logger = LoggerFactory.getLogger(ScriptEngineManagerImpl.class);
private final Map<String, ScriptEngineContainer> loadedScriptEngineInstances = new HashMap<>();
private final Map<String, ScriptEngineFactory> customSupport = new HashMap<>();
private final Map<String, ScriptEngineFactory> genericSupport = new HashMap<>();
private final Map<String, ScriptEngineFactory> factories = new HashMap<>();
private final ScriptExtensionManager scriptExtensionManager;
private final Set<FactoryChangeListener> listeners = new HashSet<>();
@ -75,11 +74,7 @@ public class ScriptEngineManagerImpl implements ScriptEngineManager {
List<String> 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<String> 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