forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JythonScriptEngineFactory.java
120 lines (98 loc) · 4.44 KB
/
JythonScriptEngineFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Copyright (c) 2010-2024 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.automation.jythonscripting;
import java.io.File;
import java.nio.file.Paths;
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;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.OpenHAB;
import org.openhab.core.automation.module.script.AbstractScriptEngineFactory;
import org.openhab.core.automation.module.script.ScriptEngineFactory;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
/**
* This is an implementation of {@link ScriptEngineFactory} for Jython.
*
* @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_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 org.python.jsr223.PyScriptEngineFactory factory = new org.python.jsr223.PyScriptEngineFactory();
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");
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()) {
pythonPathList.addAll(Arrays.asList(existingPythonPath.split(File.pathSeparator)));
}
System.setProperty(PYTHON_PATH, String.join(File.pathSeparator, pythonPathList));
System.setProperty(PYTHON_CACHEDIR, PYTHON_CACHEDIR_PATH);
logPythonPaths();
}
@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() {
return scriptTypes;
}
@Override
public @Nullable ScriptEngine createScriptEngine(String scriptType) {
if (!scriptTypes.contains(scriptType)) {
return null;
}
return factory.getScriptEngine();
}
private void logPythonPaths() {
logger.trace("{}: {}, {}: {}, {}: {}", //
PYTHON_HOME, System.getProperty(PYTHON_HOME), //
PYTHON_PATH, System.getProperty(PYTHON_PATH), //
PYTHON_CACHEDIR, System.getProperty(PYTHON_CACHEDIR));
}
}