Skip to content

Commit

Permalink
Cleanups and simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Holger Hees committed Mar 8, 2024
1 parent ba06c0d commit eecfbf7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 56 deletions.
Expand Up @@ -14,11 +14,12 @@

import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.script.ScriptEngine;

Expand All @@ -36,93 +37,84 @@
*
* @author Scott Rushworth - Initial contribution
* @author Wouter Born - Initial contribution
* @author Holger Hees - Further development
*/
@Component(service = ScriptEngineFactory.class)
@NonNullByDefault
public class JythonScriptEngineFactory extends AbstractScriptEngineFactory {

private static final String PYTHON_CACHEDIR = "python.cachedir";
private static final String PYTHON_HOME = "python.home";
private static final String PYTHON_HOME_PATH = JythonScriptEngineFactory.class.getProtectionDomain().getCodeSource()
.getLocation().toString().replace("file:", "");

private static final String PYTHON_PATH = "python.path";
private static final String PYTHON_DEFAULT_PATH = Paths.get(OpenHAB.getConfigFolder(), "automation", "python")
.toString();

private static final String PYTHON_CACHEDIR = "python.cachedir";
private static final String PYTHON_CACHEDIR_PATH = Paths
.get(OpenHAB.getUserDataFolder(), "cache", JythonScriptEngineFactory.class.getPackageName(), "cachedir")
.toString();

private static final String DEFAULT_PYTHON_PATH = Paths
.get(OpenHAB.getConfigFolder(), "automation", "lib", "python").toString();
private static final org.python.jsr223.PyScriptEngineFactory factory = new org.python.jsr223.PyScriptEngineFactory();

private static final String SCRIPT_TYPE = "py";
private static final javax.script.ScriptEngineManager ENGINE_MANAGER = new javax.script.ScriptEngineManager();
private final List<String> scriptTypes = (List<String>) Stream.of(factory.getExtensions(), factory.getMimeTypes())
.flatMap(List::stream) //
.collect(Collectors.toUnmodifiableList());

@Activate
public JythonScriptEngineFactory() {
logger.debug("Loading JythonScriptEngineFactory");

String pythonHome = JythonScriptEngineFactory.class.getProtectionDomain().getCodeSource().getLocation()
.toString().replace("file:", "");
System.setProperty(PYTHON_HOME, pythonHome);
System.setProperty(PYTHON_HOME, PYTHON_HOME_PATH);

Set<String> pythonPathList = new TreeSet<>(Arrays.asList(PYTHON_DEFAULT_PATH));
String existingPythonPath = System.getProperty(PYTHON_PATH);
if (existingPythonPath == null || existingPythonPath.isEmpty()) {
System.setProperty(PYTHON_PATH, DEFAULT_PYTHON_PATH);
} else if (!existingPythonPath.contains(DEFAULT_PYTHON_PATH)) {
Set<String> newPythonPathList = new TreeSet<>(Arrays.asList(existingPythonPath.split(File.pathSeparator)));
newPythonPathList.add(DEFAULT_PYTHON_PATH);
System.setProperty(PYTHON_PATH, String.join(File.pathSeparator, newPythonPathList));
if (existingPythonPath != null && !existingPythonPath.isEmpty()) {
pythonPathList.addAll(Arrays.asList(existingPythonPath.split(File.pathSeparator)));
}
System.setProperty(PYTHON_PATH, String.join(File.pathSeparator, pythonPathList));

System.setProperty(PYTHON_CACHEDIR, Paths
.get(OpenHAB.getUserDataFolder(), "cache", JythonScriptEngineFactory.class.getPackageName(), "cachedir")
.toString());
System.setProperty(PYTHON_CACHEDIR, PYTHON_CACHEDIR_PATH);

logPythonPaths();
}

private void logPythonPaths() {
logger.trace("{}: {}, {}: {}, {}: {}", //
PYTHON_HOME, System.getProperty(PYTHON_HOME), //
PYTHON_PATH, System.getProperty(PYTHON_PATH), //
PYTHON_CACHEDIR, System.getProperty(PYTHON_CACHEDIR));
@Deactivate
public void cleanupJythonScriptEngine() {
logger.debug("Unloading JythonScriptEngineFactory");

System.clearProperty(PYTHON_HOME);

String existingPythonPath = System.getProperty(PYTHON_PATH);
if (existingPythonPath != null && !existingPythonPath.isEmpty()) {
Set<String> newPythonPathList = new TreeSet<>(Arrays.asList(existingPythonPath.split(File.pathSeparator)));
newPythonPathList.remove(PYTHON_DEFAULT_PATH);
System.setProperty(PYTHON_PATH, String.join(File.pathSeparator, newPythonPathList));
}

System.clearProperty(PYTHON_CACHEDIR);

logPythonPaths();
}

@Override
public List<String> getScriptTypes() {
List<String> scriptTypes = new ArrayList<>();

for (javax.script.ScriptEngineFactory factory : ENGINE_MANAGER.getEngineFactories()) {
List<String> extensions = factory.getExtensions();

if (extensions.contains(SCRIPT_TYPE)) {
scriptTypes.addAll(extensions);
scriptTypes.addAll(factory.getMimeTypes());
}
}
return scriptTypes;
}

@Override
public @Nullable ScriptEngine createScriptEngine(String scriptType) {
ScriptEngine scriptEngine = ENGINE_MANAGER.getEngineByExtension(scriptType);
if (scriptEngine == null) {
scriptEngine = ENGINE_MANAGER.getEngineByMimeType(scriptType);
}
if (scriptEngine == null) {
scriptEngine = ENGINE_MANAGER.getEngineByName(scriptType);
if (!scriptTypes.contains(scriptType)) {
return null;
}
return scriptEngine;
return factory.getScriptEngine();
}

@Deactivate
public void removePythonPath() {
logger.debug("Unloading JythonScriptEngineFactory");

String existingPythonPath = System.getProperty(PYTHON_PATH);
if (existingPythonPath != null && existingPythonPath.contains(DEFAULT_PYTHON_PATH)) {
Set<String> newPythonPathList = new TreeSet<>(Arrays.asList(existingPythonPath.split(File.pathSeparator)));
newPythonPathList.remove(DEFAULT_PYTHON_PATH);
System.setProperty(PYTHON_PATH, String.join(File.pathSeparator, newPythonPathList));
}

System.clearProperty(PYTHON_HOME);
System.clearProperty(PYTHON_CACHEDIR);

logPythonPaths();
private void logPythonPaths() {
logger.trace("{}: {}, {}: {}, {}: {}", //
PYTHON_HOME, System.getProperty(PYTHON_HOME), //
PYTHON_PATH, System.getProperty(PYTHON_PATH), //
PYTHON_CACHEDIR, System.getProperty(PYTHON_CACHEDIR));
}
}
Expand Up @@ -17,4 +17,5 @@
* Additional information for the Jython Scripting package
*
* @author Wouter Born - Initial contribution
* @author Holger Hees - Further development
*/
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="https://openhab.org/schemas/addon/v1.0.0 https://openhab.org/schemas/addon-1.0.0.xsd">

<type>automation</type>
<name>Jython Scripting (DEPRECATED)</name>
<name>Jython Scripting</name>
<description>This adds a Jython script engine.</description>
<connection>none</connection>

Expand Down

0 comments on commit eecfbf7

Please sign in to comment.