[jsscriptingnashorn] JavaScript Scripting Nashorn Automation (#14013)

* [jsscriptingnashorn] JavaScript Scripting Nashorn Automation

This add-on allows you to use your older JavaScript (ECMAScript 5.1) rules on newer Java versions until they are migrated to JavaScript (ECMAScript 2021+).
The add-on uses a standalone [Nashorn Engine](https://github.com/openjdk/nashorn) which was part of Java until it was removed in Java 15.

* Update parent to 3.4.0-SNAPSHOT and nashorn-core to 15.4

For the Nashorn changelog, see:

https://github.com/openjdk/nashorn/blob/main/CHANGELOG.md

* Update parent to 4.0.0-SNAPSHOT
* Remove removeUnsupportedNashornArgs
* Update scriptTypes
* Add CODEOWNERS entry
* Recycle ScriptScopeOSGiTest.java

It got removed in openhab/openhab-core#2994

* Remove redundant new line from pom.xml

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born
2022-12-26 15:27:03 +01:00
committed by GitHub
parent 605574b600
commit 88c0b720c6
17 changed files with 652 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<features name="org.openhab.automation.jsscriptingnashorn-${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-jsscriptingnashorn" description="JavaScript Scripting (Nashorn)" version="${project.version}">
<feature>openhab-runtime-base</feature>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.automation.jsscriptingnashorn/${project.version}</bundle>
</feature>
</features>

View File

@@ -0,0 +1,84 @@
/**
* Copyright (c) 2010-2022 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<String> scriptTypes = createScriptTypes();
private List<String> createScriptTypes() {
List<String> extensions = List.of("nashornjs");
String mimeTypeVersion = ";version=ECMAScript-5.1";
List<String> mimeTypes = factory.getMimeTypes().stream().map(mimeType -> mimeType + mimeTypeVersion)
.collect(Collectors.toUnmodifiableList());
return Stream.of(extensions, mimeTypes).flatMap(List::stream).collect(Collectors.toUnmodifiableList());
}
@Override
public List<String> getScriptTypes() {
return scriptTypes;
}
@Override
public void scopeValues(ScriptEngine scriptEngine, Map<String, Object> scopeValues) {
Set<String> expressions = new HashSet<>();
for (Entry<String, Object> entry : scopeValues.entrySet()) {
scriptEngine.put(entry.getKey(), entry.getValue());
if (entry.getValue() instanceof Class) {
expressions.add(String.format("%s = %<s.static;", entry.getKey()));
}
}
String scriptToEval = String.join("\n", expressions);
try {
scriptEngine.eval(scriptToEval);
} catch (ScriptException ex) {
logger.error("ScriptException while importing scope: {}", ex.getMessage());
}
}
@Override
public @Nullable ScriptEngine createScriptEngine(String scriptType) {
return scriptTypes.contains(scriptType)
? factory.getScriptEngine(NashornScriptEngineFactory.class.getClassLoader())
: null;
}
}

View File

@@ -0,0 +1,20 @@
/**
* Copyright (c) 2010-2022 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.jsscriptingnashorn.internal;
/**
* Additional information for the JavaScript Scripting Nashorn package
*
* @author Wouter Born - Initial contribution
*/

View File

@@ -0,0 +1,43 @@
/**
* Copyright (c) 2010-2022 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;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.automation.jsscriptingnashorn.internal.NashornScriptEngineFactory;
/**
* Tests {@link NashornScriptEngineFactory}.
*
* @author Wouter Born - Initial contribution
*/
@NonNullByDefault
public class NashornScriptEngineFactoryTest {
@Test
public void scriptTypesAreNashornSpecific() {
List<String> scriptTypes = new NashornScriptEngineFactory().getScriptTypes();
assertThat(scriptTypes,
contains("nashornjs", "application/javascript;version=ECMAScript-5.1",
"application/ecmascript;version=ECMAScript-5.1", "text/javascript;version=ECMAScript-5.1",
"text/ecmascript;version=ECMAScript-5.1"));
assertThat(scriptTypes.size(), is(5));
}
}