[jsscripting] Add an alternative MIME type/alias (#13851)

The alternative MIME type text/javascript is also used by Nashorn, therefore we add the same extension as for the main MIME type.
The alias is introduced to simplify the usage in SCRIPT transformation profiles.

* Fix syntax problems in the bndtools file
* Add null check to avoid potential NPE
  In some cases during testing, a NPE occured, this should solve it.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
This commit is contained in:
Florian Hotze 2022-12-05 18:40:15 +01:00 committed by GitHub
parent 52b7b7981f
commit 54b2772df4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 4 deletions

View File

@ -1,9 +1,10 @@
Bundle-SymbolicName: ${project.artifactId}
DynamicImport-Package: *
Import-Package: org.openhab.core.automation.module.script,javax.management,javax.script,javax.xml.datatype,javax.xml.stream;version="[1.0,2)",org.osgi.framework;version="[1.8,2)",org.slf4j;version="[1.7,2)"
Require-Capability: osgi.extender;
Require-Capability:
osgi.extender:=
filter:="(osgi.extender=osgi.serviceloader.processor)",
osgi.serviceloader;
osgi.serviceloader:=
filter:="(osgi.serviceloader=org.graalvm.polyglot.impl.AbstractPolyglotImpl)";
cardinality:=multiple

View File

@ -40,11 +40,14 @@ import org.osgi.service.component.annotations.Reference;
public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
private static final String CFG_INJECTION_ENABLED = "injectionEnabled";
private static final String INJECTION_CODE = "Object.assign(this, require('openhab'));";
private final JSDependencyTracker jsDependencyTracker;
private boolean injectionEnabled = true;
public static final String MIME_TYPE = "application/javascript;version=ECMAScript-2021";
private static final String ALT_MIME_TYPE = "text/javascript;version=ECMAScript-2021";
private static final String ALIAS = "graaljs";
private final JSScriptServiceUtil jsScriptServiceUtil;
private final JSDependencyTracker jsDependencyTracker;
@Activate
public GraalJSScriptEngineFactory(final @Reference JSScriptServiceUtil jsScriptServiceUtil,
@ -68,7 +71,7 @@ public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
// scriptTypes.addAll(graalJSEngineFactory.getMimeTypes());
// scriptTypes.addAll(graalJSEngineFactory.getExtensions());
return List.of(MIME_TYPE);
return List.of(MIME_TYPE, ALT_MIME_TYPE, ALIAS);
}
@Override

View File

@ -179,6 +179,9 @@ public class OpenhabGraalJSScriptEngine
}
ScriptContext ctx = delegate.getContext();
if (ctx == null) {
throw new IllegalStateException("Failed to retrieve script context");
}
// these are added post-construction, so we need to fetch them late
this.engineIdentifier = (String) ctx.getAttribute(CONTEXT_KEY_ENGINE_IDENTIFIER);