-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Scott Rushworth <openhab@5iver.com>
- Loading branch information
Scott Rushworth
committed
Mar 25, 2020
1 parent
12b2190
commit 5db9d8f
Showing
11 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
bundles/org.openhab.automation.scriptenginefactory.jython/.classpath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" output="target/classes" path="src/main/java"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
<attribute name="test" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="output" path="target/classes"/> | ||
</classpath> |
23 changes: 23 additions & 0 deletions
23
bundles/org.openhab.automation.scriptenginefactory.jython/.project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>org.openhab.automation.scriptenginefactory.jython</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.m2e.core.maven2Builder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
13 changes: 13 additions & 0 deletions
13
bundles/org.openhab.automation.scriptenginefactory.jython/NOTICE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
This content is produced and maintained by the openHAB project. | ||
|
||
* Project home: https://www.openhab.org | ||
|
||
== Declared Project Licenses | ||
|
||
This program and the accompanying materials are made available under the terms | ||
of the Eclipse Public License 2.0 which is available at | ||
https://www.eclipse.org/legal/epl-2.0/. | ||
|
||
== Source Code | ||
|
||
https://github.com/openhab/openhab2-addons |
3 changes: 3 additions & 0 deletions
3
bundles/org.openhab.automation.scriptenginefactory.jython/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Jython ScriptEngineFactory | ||
|
||
This addon provides a Jython ScriptEngineFactory for use with scripted automation. |
6 changes: 6 additions & 0 deletions
6
bundles/org.openhab.automation.scriptenginefactory.jython/bnd.bnd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Bundle-SymbolicName: ${project.artifactId} | ||
DynamicImport-Package: * | ||
Import-Package: org.openhab.core.automation.module.script | ||
-includeresource: @jython-standalone-2.7.[0-9a-z]*.jar; lib:=true | ||
-fixupmessages: "Classes found in the wrong directory",\ | ||
"The default package '.' is not permitted by the Import-Package syntax"; restrict:=error; is:=warning |
31 changes: 31 additions & 0 deletions
31
bundles/org.openhab.automation.scriptenginefactory.jython/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-v4_0_0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.openhab.addons.bundles</groupId> | ||
<artifactId>org.openhab.addons.reactor.bundles</artifactId> | ||
<version>2.5.4-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>org.openhab.automation.scriptenginefactory.jython</artifactId> | ||
|
||
<name>openHAB Add-ons :: Automation :: Jython ScriptEngineFactory</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.openhab.core.bundles</groupId> | ||
<artifactId>org.openhab.core.automation.module.script</artifactId> | ||
<version>2.5.0</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/org.python/jython-standalone --> | ||
<dependency> | ||
<groupId>org.python</groupId> | ||
<artifactId>jython-standalone</artifactId> | ||
<version>2.7.2rc1</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
10 changes: 10 additions & 0 deletions
10
bundles/org.openhab.automation.scriptenginefactory.jython/src/main/feature/feature.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<features name="org.openhab.automation.scriptenginefactory.jython-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0"> | ||
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/2.5.0/xml/features</repository> | ||
|
||
<feature name="openhab-scriptenginefactory-jython" description="Jython ScriptEngineFactory" version="${project.version}"> | ||
<feature>openhab-runtime-base</feature> | ||
<feature>openhab-core-automation</feature> | ||
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.automation.scriptenginefactory.jython/${project.version}</bundle> | ||
</feature> | ||
</features> |
114 changes: 114 additions & 0 deletions
114
...ain/java/org/openhab/automation/scriptenginefactory/jython/JythonScriptEngineFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* Copyright (c) 2010-2019 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.scriptenginefactory.jython; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.TreeSet; | ||
|
||
import javax.script.ScriptEngine; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.eclipse.smarthome.config.core.ConfigConstants; | ||
import org.openhab.core.automation.module.script.AbstractScriptEngineFactory; | ||
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 | ||
*/ | ||
@Component(service = org.openhab.core.automation.module.script.ScriptEngineFactory.class) | ||
@NonNullByDefault | ||
public class JythonScriptEngineFactory extends AbstractScriptEngineFactory { | ||
|
||
private static final String SCRIPT_TYPE = "py"; | ||
private static javax.script.ScriptEngineManager ENGINE_MANAGER = new javax.script.ScriptEngineManager(); | ||
private static final String DEFAULT_PYTHON_PATH = new StringBuilder(ConfigConstants.getConfigFolder()) | ||
.append(File.separator).append("automation").append(File.separator).append("lib").append(File.separator) | ||
.append("python").toString(); | ||
|
||
@Activate | ||
public JythonScriptEngineFactory() { | ||
logger.debug("Loading JythonScriptEngineFactory"); | ||
String home = JythonScriptEngineFactory.class.getProtectionDomain().getCodeSource().getLocation().toString() | ||
.replace("file:", ""); | ||
System.setProperty("python.home", home); | ||
|
||
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)) { | ||
TreeSet<String> newPythonPathList = new TreeSet<>( | ||
new ArrayList<String>(Arrays.asList(existingPythonPath.split(File.pathSeparator)))); | ||
newPythonPathList.add(DEFAULT_PYTHON_PATH); | ||
String newPythonPath = String.join(File.pathSeparator, newPythonPathList); | ||
System.setProperty("python.path", newPythonPath); | ||
} | ||
|
||
StringBuilder newPythonCachedir = new StringBuilder(System.getenv("OPENHAB_USERDATA")).append(File.separator) | ||
.append("tmp"); | ||
System.setProperty("python.cachedir", newPythonCachedir.toString()); | ||
|
||
logger.trace("python.home [{}], python.path [{}], python.cachdir [{}]", System.getProperty("python.home"), | ||
System.getProperty("python.path"), System.getProperty("python.cachedir")); | ||
} | ||
|
||
@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 Collections.unmodifiableList(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); | ||
} | ||
return scriptEngine; | ||
} | ||
|
||
@Deactivate | ||
public void deactivate() { | ||
logger.debug("Unloading JythonScriptEngineFactory"); | ||
String existingPythonPath = System.getProperty("python.path"); | ||
if (existingPythonPath.contains(DEFAULT_PYTHON_PATH)) { | ||
TreeSet<String> newPythonPathList = new TreeSet<>( | ||
new ArrayList<String>(Arrays.asList(existingPythonPath.split(File.pathSeparator)))); | ||
newPythonPathList.remove(DEFAULT_PYTHON_PATH); | ||
String newPythonPath = String.join(File.pathSeparator, newPythonPathList); | ||
System.setProperty("python.path", newPythonPath); | ||
} | ||
logger.trace("python.home [{}], python.path [{}], python.cachdir [{}]", System.getProperty("python.home"), | ||
System.getProperty("python.path"), System.getProperty("python.cachedir")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters