Skip to content

Commit

Permalink
Add JythonScriptEngineFactory
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Rushworth <openhab@5iver.com>
  • Loading branch information
Scott Rushworth committed Mar 25, 2020
1 parent 12b2190 commit 5db9d8f
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 0 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @openhab/2-x-add-ons-maintainers

# Add-on maintainers:
/bundles/org.openhab.automation.scriptenginefactory.jython/ @openhab-5iver
/bundles/org.openhab.binding.airquality/ @kubawolanin
/bundles/org.openhab.binding.airvisualnode/ @3cky
/bundles/org.openhab.binding.allplay/ @dominicdesu
Expand Down
5 changes: 5 additions & 0 deletions bom/openhab-addons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<name>openHAB Add-ons :: BOM :: openHAB Add-ons</name>

<dependencies>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.automation.scriptenginefactory.jython</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.airquality</artifactId>
Expand Down
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 bundles/org.openhab.automation.scriptenginefactory.jython/.project
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 bundles/org.openhab.automation.scriptenginefactory.jython/NOTICE
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
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.
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 bundles/org.openhab.automation.scriptenginefactory.jython/pom.xml
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>
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>
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"));
}
}
2 changes: 2 additions & 0 deletions bundles/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<name>openHAB Add-ons :: Bundles</name>

<modules>
<!-- automation -->
<module>org.openhab.automation.scriptenginefactory.jython</module>
<!-- io -->
<!-- temporarily disabled due to malfunction -->
<!--module>org.openhab.io.azureiothub</module-->
Expand Down

0 comments on commit 5db9d8f

Please # to comment.