|
10 | 10 | *
|
11 | 11 | * SPDX-License-Identifier: EPL-2.0
|
12 | 12 | */
|
13 |
| -package org.openhab.automation.jythonscripting; |
| 13 | +package org.openhab.automation.jythonscripting.internal; |
14 | 14 |
|
15 | 15 | import java.io.File;
|
16 | 16 | import java.nio.file.Paths;
|
17 |
| -import java.util.ArrayList; |
18 | 17 | import java.util.Arrays;
|
19 | 18 | import java.util.List;
|
20 | 19 | import java.util.Set;
|
21 | 20 | import java.util.TreeSet;
|
| 21 | +import java.util.stream.Stream; |
22 | 22 |
|
23 | 23 | import javax.script.ScriptEngine;
|
24 | 24 |
|
|
36 | 36 | *
|
37 | 37 | * @author Scott Rushworth - Initial contribution
|
38 | 38 | * @author Wouter Born - Initial contribution
|
| 39 | + * @author Holger Hees - Further development |
39 | 40 | */
|
40 | 41 | @Component(service = ScriptEngineFactory.class)
|
41 | 42 | @NonNullByDefault
|
42 | 43 | public class JythonScriptEngineFactory extends AbstractScriptEngineFactory {
|
43 | 44 |
|
44 |
| - private static final String PYTHON_CACHEDIR = "python.cachedir"; |
45 | 45 | private static final String PYTHON_HOME = "python.home";
|
| 46 | + private static final String PYTHON_HOME_PATH = JythonScriptEngineFactory.class.getProtectionDomain().getCodeSource() |
| 47 | + .getLocation().toString().replace("file:", ""); |
| 48 | + |
46 | 49 | private static final String PYTHON_PATH = "python.path";
|
| 50 | + private static final String PYTHON_DEFAULT_PATH = Paths |
| 51 | + .get(OpenHAB.getConfigFolder(), "automation", "jython", "lib").toString(); |
| 52 | + |
| 53 | + private static final String PYTHON_CACHEDIR = "python.cachedir"; |
| 54 | + private static final String PYTHON_CACHEDIR_PATH = Paths |
| 55 | + .get(OpenHAB.getUserDataFolder(), "cache", JythonScriptEngineFactory.class.getPackageName(), "cachedir") |
| 56 | + .toString(); |
47 | 57 |
|
48 |
| - private static final String DEFAULT_PYTHON_PATH = Paths |
49 |
| - .get(OpenHAB.getConfigFolder(), "automation", "lib", "python").toString(); |
| 58 | + private static final org.python.jsr223.PyScriptEngineFactory factory = new org.python.jsr223.PyScriptEngineFactory(); |
50 | 59 |
|
51 |
| - private static final String SCRIPT_TYPE = "py"; |
52 |
| - private static final javax.script.ScriptEngineManager ENGINE_MANAGER = new javax.script.ScriptEngineManager(); |
| 60 | + private final List<String> scriptTypes = (List<String>) Stream.of(factory.getExtensions(), factory.getMimeTypes()) |
| 61 | + .flatMap(List::stream) // |
| 62 | + .toList(); |
53 | 63 |
|
54 | 64 | @Activate
|
55 | 65 | public JythonScriptEngineFactory() {
|
56 | 66 | logger.debug("Loading JythonScriptEngineFactory");
|
57 | 67 |
|
58 |
| - String pythonHome = JythonScriptEngineFactory.class.getProtectionDomain().getCodeSource().getLocation() |
59 |
| - .toString().replace("file:", ""); |
60 |
| - System.setProperty(PYTHON_HOME, pythonHome); |
| 68 | + System.setProperty(PYTHON_HOME, PYTHON_HOME_PATH); |
61 | 69 |
|
| 70 | + Set<String> pythonPathList = new TreeSet<>(Arrays.asList(PYTHON_DEFAULT_PATH)); |
62 | 71 | String existingPythonPath = System.getProperty(PYTHON_PATH);
|
63 |
| - if (existingPythonPath == null || existingPythonPath.isEmpty()) { |
64 |
| - System.setProperty(PYTHON_PATH, DEFAULT_PYTHON_PATH); |
65 |
| - } else if (!existingPythonPath.contains(DEFAULT_PYTHON_PATH)) { |
66 |
| - Set<String> newPythonPathList = new TreeSet<>(Arrays.asList(existingPythonPath.split(File.pathSeparator))); |
67 |
| - newPythonPathList.add(DEFAULT_PYTHON_PATH); |
68 |
| - System.setProperty(PYTHON_PATH, String.join(File.pathSeparator, newPythonPathList)); |
| 72 | + if (existingPythonPath != null && !existingPythonPath.isEmpty()) { |
| 73 | + pythonPathList.addAll(Arrays.asList(existingPythonPath.split(File.pathSeparator))); |
69 | 74 | }
|
| 75 | + System.setProperty(PYTHON_PATH, String.join(File.pathSeparator, pythonPathList)); |
70 | 76 |
|
71 |
| - System.setProperty(PYTHON_CACHEDIR, Paths |
72 |
| - .get(OpenHAB.getUserDataFolder(), "cache", JythonScriptEngineFactory.class.getPackageName(), "cachedir") |
73 |
| - .toString()); |
| 77 | + System.setProperty(PYTHON_CACHEDIR, PYTHON_CACHEDIR_PATH); |
74 | 78 |
|
75 | 79 | logPythonPaths();
|
76 | 80 | }
|
77 | 81 |
|
78 |
| - private void logPythonPaths() { |
79 |
| - logger.trace("{}: {}, {}: {}, {}: {}", // |
80 |
| - PYTHON_HOME, System.getProperty(PYTHON_HOME), // |
81 |
| - PYTHON_PATH, System.getProperty(PYTHON_PATH), // |
82 |
| - PYTHON_CACHEDIR, System.getProperty(PYTHON_CACHEDIR)); |
| 82 | + @Deactivate |
| 83 | + public void cleanup() { |
| 84 | + logger.debug("Unloading JythonScriptEngineFactory"); |
| 85 | + |
| 86 | + System.clearProperty(PYTHON_HOME); |
| 87 | + |
| 88 | + String existingPythonPath = System.getProperty(PYTHON_PATH); |
| 89 | + if (existingPythonPath != null && !existingPythonPath.isEmpty()) { |
| 90 | + Set<String> newPythonPathList = new TreeSet<>(Arrays.asList(existingPythonPath.split(File.pathSeparator))); |
| 91 | + newPythonPathList.remove(PYTHON_DEFAULT_PATH); |
| 92 | + System.setProperty(PYTHON_PATH, String.join(File.pathSeparator, newPythonPathList)); |
| 93 | + } |
| 94 | + |
| 95 | + System.clearProperty(PYTHON_CACHEDIR); |
| 96 | + |
| 97 | + logPythonPaths(); |
83 | 98 | }
|
84 | 99 |
|
85 | 100 | @Override
|
86 | 101 | public List<String> getScriptTypes() {
|
87 |
| - List<String> scriptTypes = new ArrayList<>(); |
88 |
| - |
89 |
| - for (javax.script.ScriptEngineFactory factory : ENGINE_MANAGER.getEngineFactories()) { |
90 |
| - List<String> extensions = factory.getExtensions(); |
91 |
| - |
92 |
| - if (extensions.contains(SCRIPT_TYPE)) { |
93 |
| - scriptTypes.addAll(extensions); |
94 |
| - scriptTypes.addAll(factory.getMimeTypes()); |
95 |
| - } |
96 |
| - } |
97 | 102 | return scriptTypes;
|
98 | 103 | }
|
99 | 104 |
|
100 | 105 | @Override
|
101 | 106 | public @Nullable ScriptEngine createScriptEngine(String scriptType) {
|
102 |
| - ScriptEngine scriptEngine = ENGINE_MANAGER.getEngineByExtension(scriptType); |
103 |
| - if (scriptEngine == null) { |
104 |
| - scriptEngine = ENGINE_MANAGER.getEngineByMimeType(scriptType); |
105 |
| - } |
106 |
| - if (scriptEngine == null) { |
107 |
| - scriptEngine = ENGINE_MANAGER.getEngineByName(scriptType); |
| 107 | + if (!scriptTypes.contains(scriptType)) { |
| 108 | + return null; |
108 | 109 | }
|
109 |
| - return scriptEngine; |
| 110 | + return factory.getScriptEngine(); |
110 | 111 | }
|
111 | 112 |
|
112 |
| - @Deactivate |
113 |
| - public void removePythonPath() { |
114 |
| - logger.debug("Unloading JythonScriptEngineFactory"); |
115 |
| - |
116 |
| - String existingPythonPath = System.getProperty(PYTHON_PATH); |
117 |
| - if (existingPythonPath != null && existingPythonPath.contains(DEFAULT_PYTHON_PATH)) { |
118 |
| - Set<String> newPythonPathList = new TreeSet<>(Arrays.asList(existingPythonPath.split(File.pathSeparator))); |
119 |
| - newPythonPathList.remove(DEFAULT_PYTHON_PATH); |
120 |
| - System.setProperty(PYTHON_PATH, String.join(File.pathSeparator, newPythonPathList)); |
121 |
| - } |
122 |
| - |
123 |
| - System.clearProperty(PYTHON_HOME); |
124 |
| - System.clearProperty(PYTHON_CACHEDIR); |
125 |
| - |
126 |
| - logPythonPaths(); |
| 113 | + private void logPythonPaths() { |
| 114 | + logger.trace("{}: {}, {}: {}, {}: {}", // |
| 115 | + PYTHON_HOME, System.getProperty(PYTHON_HOME), // |
| 116 | + PYTHON_PATH, System.getProperty(PYTHON_PATH), // |
| 117 | + PYTHON_CACHEDIR, System.getProperty(PYTHON_CACHEDIR)); |
127 | 118 | }
|
128 | 119 | }
|
0 commit comments