]> git.basschouten.com Git - openhab-addons.git/commitdiff
[groovyscripting] Add Groovy scripting support (#8772)
authorWouter Born <github@maindrain.net>
Sat, 17 Oct 2020 21:32:14 +0000 (23:32 +0200)
committerGitHub <noreply@github.com>
Sat, 17 Oct 2020 21:32:14 +0000 (23:32 +0200)
* Add Groovy scripting support

This makes it really easy to start using Groovy 3.x scripts with openHAB!

Signed-off-by: Wouter Born <github@maindrain.net>
CODEOWNERS
bom/openhab-addons/pom.xml
bundles/org.openhab.automation.groovyscripting/NOTICE [new file with mode: 0644]
bundles/org.openhab.automation.groovyscripting/README.md [new file with mode: 0644]
bundles/org.openhab.automation.groovyscripting/pom.xml [new file with mode: 0644]
bundles/org.openhab.automation.groovyscripting/src/main/feature/feature.xml [new file with mode: 0644]
bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java [new file with mode: 0644]
bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/package-info.java [new file with mode: 0644]
bundles/pom.xml

index 94f3ed94d29f122a935773b10d7884c6034fd5e0..68a951f270df806802896dc8da29d5e2b0079238 100644 (file)
@@ -5,6 +5,7 @@
 *       @openhab/add-ons-maintainers
 
 # Add-on maintainers:
+/bundles/org.openhab.automation.groovyscripting/ @wborn
 /bundles/org.openhab.binding.adorne/ @theiding
 /bundles/org.openhab.binding.airquality/ @kubawolanin
 /bundles/org.openhab.binding.airvisualnode/ @3cky
index e300dce9a2a67efa794c253ce37f19fa0f5f687c..3d23653f938aed5f0c5e93506865e9250acdf5c9 100644 (file)
   <name>openHAB Add-ons :: BOM :: openHAB Add-ons</name>
 
   <dependencies>
+    <dependency>
+      <groupId>org.openhab.addons.bundles</groupId>
+      <artifactId>org.openhab.automation.groovyscripting</artifactId>
+      <version>${project.version}</version>
+    </dependency>
     <dependency>
       <groupId>org.openhab.addons.bundles</groupId>
       <artifactId>org.openhab.binding.adorne</artifactId>
diff --git a/bundles/org.openhab.automation.groovyscripting/NOTICE b/bundles/org.openhab.automation.groovyscripting/NOTICE
new file mode 100644 (file)
index 0000000..38d625e
--- /dev/null
@@ -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/openhab-addons
diff --git a/bundles/org.openhab.automation.groovyscripting/README.md b/bundles/org.openhab.automation.groovyscripting/README.md
new file mode 100644 (file)
index 0000000..d23cb9a
--- /dev/null
@@ -0,0 +1,3 @@
+# Groovy Scripting
+
+This add-on provides support for [Groovy](https://groovy-lang.org/) 3.x scripts in openHAB.
diff --git a/bundles/org.openhab.automation.groovyscripting/pom.xml b/bundles/org.openhab.automation.groovyscripting/pom.xml
new file mode 100644 (file)
index 0000000..4bcb169
--- /dev/null
@@ -0,0 +1,43 @@
+<?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-4.0.0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.openhab.addons.bundles</groupId>
+    <artifactId>org.openhab.addons.reactor.bundles</artifactId>
+    <version>3.0.0-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>org.openhab.automation.groovyscripting</artifactId>
+
+  <name>openHAB Add-ons :: Automation :: Groovy Scripting</name>
+
+  <properties>
+    <bnd.importpackage>
+      com.ibm.icu.*;resolution:=optional,
+      groovy.runtime.metaclass;resolution:=optional,
+      groovyjarjarantlr4.stringtemplate;resolution:=optional,
+      org.abego.treelayout.*;resolution:=optional,
+      org.apache.ivy.*;resolution:=optional,
+      org.stringtemplate.v4.*;resolution:=optional</bnd.importpackage>
+    <groovy.version>3.0.6</groovy.version>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.codehaus.groovy</groupId>
+      <artifactId>groovy</artifactId>
+      <version>${groovy.version}</version>
+      <scope>compile</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.groovy</groupId>
+      <artifactId>groovy-jsr223</artifactId>
+      <version>${groovy.version}</version>
+      <scope>compile</scope>
+    </dependency>
+  </dependencies>
+
+</project>
diff --git a/bundles/org.openhab.automation.groovyscripting/src/main/feature/feature.xml b/bundles/org.openhab.automation.groovyscripting/src/main/feature/feature.xml
new file mode 100644 (file)
index 0000000..ae04075
--- /dev/null
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<features name="org.openhab.automation.groovyscripting-${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/${ohc.version}/xml/features</repository>
+
+       <feature name="openhab-automation-groovyscripting" description="Groovy Scripting" version="${project.version}">
+               <feature>openhab-runtime-base</feature>
+               <bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.automation.groovyscripting/${project.version}</bundle>
+       </feature>
+</features>
diff --git a/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java b/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java
new file mode 100644 (file)
index 0000000..7dae3d6
--- /dev/null
@@ -0,0 +1,51 @@
+/**
+ * Copyright (c) 2010-2020 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.groovyscripting.internal;
+
+import java.util.List;
+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.automation.module.script.AbstractScriptEngineFactory;
+import org.openhab.core.automation.module.script.ScriptEngineFactory;
+import org.osgi.service.component.annotations.Component;
+
+/**
+ * This is an implementation of a {@link ScriptEngineFactory} for Groovy.
+ *
+ * @author Wouter Born - Initial contribution
+ */
+@Component(service = ScriptEngineFactory.class)
+@NonNullByDefault
+public class GroovyScriptEngineFactory extends AbstractScriptEngineFactory {
+
+    private final org.codehaus.groovy.jsr223.GroovyScriptEngineFactory factory = new org.codehaus.groovy.jsr223.GroovyScriptEngineFactory();
+
+    private final List<String> scriptTypes = (List<String>) Stream.of(factory.getExtensions(), factory.getMimeTypes())
+            .flatMap(List::stream) //
+            .collect(Collectors.toUnmodifiableList());
+
+    @Override
+    public List<String> getScriptTypes() {
+        return scriptTypes;
+    }
+
+    @Override
+    public @Nullable ScriptEngine createScriptEngine(String scriptType) {
+        return scriptTypes.contains(scriptType) ? factory.getScriptEngine() : null;
+    }
+}
diff --git a/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/package-info.java b/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/package-info.java
new file mode 100644 (file)
index 0000000..6be4121
--- /dev/null
@@ -0,0 +1,21 @@
+/**
+ * Copyright (c) 2010-2020 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
+ */
+
+@org.osgi.annotation.bundle.Header(name = org.osgi.framework.Constants.DYNAMICIMPORT_PACKAGE, value = "*")
+package org.openhab.automation.groovyscripting.internal;
+
+/**
+ * Additional information for the Groovy Scripting package
+ *
+ * @author Wouter Born - Initial contribution
+ */
index f7af5daa79b1d36d397646a0201f46e81bc73609..e0c7e2eb72f84c62aa218ae77e1302e5701c8394 100644 (file)
@@ -17,6 +17,8 @@
   <name>openHAB Add-ons :: Bundles</name>
 
   <modules>
+    <!-- automation -->
+    <module>org.openhab.automation.groovyscripting</module>
     <!-- io -->
     <module>org.openhab.io.homekit</module>
     <module>org.openhab.io.hueemulation</module>