diff --git a/bundles/org.openhab.automation.jsscriptingnashorn/NOTICE b/bundles/org.openhab.automation.jsscriptingnashorn/NOTICE new file mode 100644 index 0000000000000..38d625e349232 --- /dev/null +++ b/bundles/org.openhab.automation.jsscriptingnashorn/NOTICE @@ -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.jsscriptingnashorn/README.md b/bundles/org.openhab.automation.jsscriptingnashorn/README.md new file mode 100644 index 0000000000000..4cfb17e86607f --- /dev/null +++ b/bundles/org.openhab.automation.jsscriptingnashorn/README.md @@ -0,0 +1,43 @@ +# Groovy Scripting + +This add-on provides support for [Groovy](https://groovy-lang.org/) 3.0.9 that can be used as a scripting language within automation rules and which eliminates the need to manually install Groovy. + +## Creating Groovy Scripts + +When this add-on is installed, you can select Groovy as a scripting language when creating a script action within the rule editor of the UI. + +Alternatively, you can create scripts in the `automation/jsr223` configuration directory. +If you create an empty file called `test.groovy`, you will see a log line with information similar to: + +```text + ... [INFO ] [.a.m.s.r.i.l.ScriptFileWatcher:150 ] - Loading script 'test.groovy' +``` + +To enable debug logging, use the [console logging]({{base}}/administration/logging.html) commands to enable debug logging for the automation functionality: + +```text +log:set DEBUG org.openhab.core.automation +``` + +For more information on the available APIs in scripts see the [JSR223 Scripting]({{base}}/configuration/jsr223.html) documentation. + +## Script Examples + +Groovy scripts provide access to almost all the functionality in an openHAB runtime environment. +As a simple example, the following script logs "Hello, World!". +Note that `System.out.println` will usually not work since the output has no terminal to display the text. +The openHAB server uses the [SLF4J](https://www.slf4j.org/) library for logging. + +```groovy +import org.slf4j.LoggerFactory + +LoggerFactory.getLogger("org.openhab.core.automation.examples").info("Hello world!") +``` + +Depending on the openHAB logging configuration, you may need to prefix logger names with `org.openhab.core.automation` for them to show up in the log file (or you modify the logging configuration). + +The script uses the [LoggerFactory](https://www.slf4j.org/apidocs/org/slf4j/Logger.html) to obtain a named logger and then logs a message like: + +```text + ... [INFO ] [.openhab.core.automation.examples:-2 ] - Hello world! +``` diff --git a/bundles/org.openhab.automation.jsscriptingnashorn/pom.xml b/bundles/org.openhab.automation.jsscriptingnashorn/pom.xml new file mode 100644 index 0000000000000..a515b76a3a3a5 --- /dev/null +++ b/bundles/org.openhab.automation.jsscriptingnashorn/pom.xml @@ -0,0 +1,57 @@ + + + + 4.0.0 + + + org.openhab.addons.bundles + org.openhab.addons.reactor.bundles + 3.2.0-SNAPSHOT + + + org.openhab.automation.jsscriptingnashorn + + openHAB Add-ons :: Bundles :: Automation :: JSScripting Nashorn + + + jdk.dynalink.*;resolution:=optional + 7.3.1 + + + + + + org.openjdk.nashorn + nashorn-core + 15.3 + compile + + + org.ow2.asm + asm + ${asm.version} + + + org.ow2.asm + asm-analysis + ${asm.version} + + + org.ow2.asm + asm-commons + ${asm.version} + + + org.ow2.asm + asm-tree + ${asm.version} + + + org.ow2.asm + asm-util + ${asm.version} + + + + diff --git a/bundles/org.openhab.automation.jsscriptingnashorn/src/main/feature/feature.xml b/bundles/org.openhab.automation.jsscriptingnashorn/src/main/feature/feature.xml new file mode 100644 index 0000000000000..7a9757d4af6f8 --- /dev/null +++ b/bundles/org.openhab.automation.jsscriptingnashorn/src/main/feature/feature.xml @@ -0,0 +1,9 @@ + + + mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features + + + openhab-runtime-base + mvn:org.openhab.addons.bundles/org.openhab.automation.jsscriptingnashorn/${project.version} + + diff --git a/bundles/org.openhab.automation.jsscriptingnashorn/src/main/java/org/openhab/automation/jsscriptingnashorn/internal/NashornScriptEngineFactory.java b/bundles/org.openhab.automation.jsscriptingnashorn/src/main/java/org/openhab/automation/jsscriptingnashorn/internal/NashornScriptEngineFactory.java new file mode 100644 index 0000000000000..2ad5c43c5d8d3 --- /dev/null +++ b/bundles/org.openhab.automation.jsscriptingnashorn/src/main/java/org/openhab/automation/jsscriptingnashorn/internal/NashornScriptEngineFactory.java @@ -0,0 +1,76 @@ +/** + * Copyright (c) 2010-2021 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.jsscriptingnashorn.internal; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import javax.script.ScriptEngine; +import javax.script.ScriptException; + +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 Nashorn. + * + * @author Wouter Born - Initial contribution + */ +@Component(service = ScriptEngineFactory.class) +@NonNullByDefault +public class NashornScriptEngineFactory extends AbstractScriptEngineFactory { + + private final org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory factory = new org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory(); + + private final List scriptTypes = (List) Stream.of(factory.getExtensions(), factory.getMimeTypes()) + .flatMap(List::stream) // + .collect(Collectors.toUnmodifiableList()); + + @Override + public List getScriptTypes() { + System.out.println(scriptTypes.stream().collect(Collectors.joining(", "))); + return scriptTypes; + } + + @Override + public void scopeValues(ScriptEngine scriptEngine, Map scopeValues) { + Set expressions = new HashSet<>(); + + for (Entry entry : scopeValues.entrySet()) { + scriptEngine.put(entry.getKey(), entry.getValue()); + if (entry.getValue() instanceof Class) { + expressions.add(String.format("%s = % org.openhab.automation.groovyscripting org.openhab.automation.jsscripting + org.openhab.automation.jsscriptingnashorn org.openhab.automation.jythonscripting org.openhab.automation.pidcontroller org.openhab.automation.pwm