Skip to content

Commit c023801

Browse files
committed
[jsscripting] Implement javax.script.Compilable (openhab#16970)
* [jsscripting] Restructure & Comment POM * [jsscripting] Use OPENHAB_TRANSFORMATION_SCRIPT constant from core Signed-off-by: Florian Hotze <[email protected]> (cherry picked from commit 405f402)
1 parent ef3ffff commit c023801

6 files changed

+69
-26
lines changed

bundles/org.openhab.automation.jsscripting/pom.xml

+12-8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
<build>
2929
<plugins>
30+
<!-- exclude META-INF/services/com.oracle.truffle.api.TruffleLanguage$Provider when unpacking dependencies -->
3031
<plugin>
3132
<groupId>org.apache.maven.plugins</groupId>
3233
<artifactId>maven-dependency-plugin</artifactId>
@@ -42,6 +43,7 @@
4243
</execution>
4344
</executions>
4445
</plugin>
46+
<!-- bundle the openhab-js library -->
4547
<plugin>
4648
<groupId>com.github.eirslett</groupId>
4749
<artifactId>frontend-maven-plugin</artifactId>
@@ -112,6 +114,7 @@
112114
</execution>
113115
</executions>
114116
</plugin>
117+
<!-- run SAT -->
115118
<plugin>
116119
<groupId>org.openhab.tools.sat</groupId>
117120
<artifactId>sat-plugin</artifactId>
@@ -123,32 +126,33 @@
123126
</build>
124127

125128
<dependencies>
129+
<dependency>
130+
<groupId>org.graalvm.sdk</groupId>
131+
<artifactId>graal-sdk</artifactId>
132+
<version>${graal.version}</version>
133+
</dependency>
126134
<dependency>
127135
<groupId>org.graalvm.truffle</groupId>
128136
<artifactId>truffle-api</artifactId>
129137
<version>${graal.version}</version>
130138
</dependency>
139+
<!-- Graal JavaScript ScriptEngine JSR 223 support -->
131140
<dependency>
132141
<groupId>org.graalvm.js</groupId>
133142
<artifactId>js-scriptengine</artifactId>
134143
<version>${graal.version}</version>
135144
</dependency>
136-
<dependency>
137-
<groupId>org.graalvm.sdk</groupId>
138-
<artifactId>graal-sdk</artifactId>
139-
<version>${graal.version}</version>
140-
</dependency>
145+
<!-- Graal TRegex engine (internally used by Graal JavaScript engine) -->
141146
<dependency>
142147
<groupId>org.graalvm.regex</groupId>
143148
<artifactId>regex</artifactId>
144149
<version>${graal.version}</version>
145150
</dependency>
146-
<dependency> <!-- this must come AFTER the regex lib -->
151+
<!-- Graal JavaScript engine (depends on Graal TRegex engine, must be added after it) -->
152+
<dependency>
147153
<groupId>org.graalvm.js</groupId>
148154
<artifactId>js</artifactId>
149155
<version>${graal.version}</version>
150156
</dependency>
151-
<!-- GraalJS changelog says that com.ibm.icu/icu4j is not required for GraalJS >= 22.0.0 as it moved to org.graalvm.truffle;
152-
but GraalJS >= 22.2.0 requires it, so we'll need to add it when we upgrade -->
153157
</dependencies>
154158
</project>

bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/DebuggingGraalScriptEngine.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@
1212
*/
1313
package org.openhab.automation.jsscripting.internal;
1414

15+
import static org.openhab.core.automation.module.script.ScriptTransformationService.OPENHAB_TRANSFORMATION_SCRIPT;
16+
1517
import java.util.Arrays;
1618
import java.util.stream.Collectors;
1719

20+
import javax.script.Compilable;
1821
import javax.script.Invocable;
1922
import javax.script.ScriptContext;
2023
import javax.script.ScriptEngine;
2124

2225
import org.eclipse.jdt.annotation.Nullable;
2326
import org.graalvm.polyglot.PolyglotException;
24-
import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable;
27+
import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable;
2528
import org.slf4j.Logger;
2629
import org.slf4j.LoggerFactory;
2730

@@ -31,10 +34,9 @@
3134
* @author Jonathan Gilbert - Initial contribution
3235
* @author Florian Hotze - Improve logger name, Fix memory leak caused by exception logging
3336
*/
34-
class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoCloseable>
35-
extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T> {
37+
class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoCloseable & Compilable>
38+
extends InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable<T> {
3639

37-
private static final String SCRIPT_TRANSFORMATION_ENGINE_IDENTIFIER = "openhab-transformation-script-";
3840
private static final int STACK_TRACE_LENGTH = 5;
3941

4042
private @Nullable Logger logger;
@@ -91,9 +93,8 @@ private void initializeLogger() {
9193
} else if (ruleUID != null) {
9294
identifier = ruleUID.toString();
9395
} else if (ohEngineIdentifier != null) {
94-
if (ohEngineIdentifier.toString().startsWith(SCRIPT_TRANSFORMATION_ENGINE_IDENTIFIER)) {
95-
identifier = ohEngineIdentifier.toString().replaceAll(SCRIPT_TRANSFORMATION_ENGINE_IDENTIFIER,
96-
"transformation.");
96+
if (ohEngineIdentifier.toString().startsWith(OPENHAB_TRANSFORMATION_SCRIPT)) {
97+
identifier = ohEngineIdentifier.toString().replaceAll(OPENHAB_TRANSFORMATION_SCRIPT, "transformation.");
9798
}
9899
}
99100

bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/OpenhabGraalJSScriptEngine.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
import org.openhab.automation.jsscripting.internal.fs.PrefixedSeekableByteChannel;
5151
import org.openhab.automation.jsscripting.internal.fs.ReadOnlySeekableByteArrayChannel;
5252
import org.openhab.automation.jsscripting.internal.fs.watch.JSDependencyTracker;
53-
import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable;
53+
import org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable;
5454
import org.openhab.core.automation.module.script.ScriptExtensionAccessor;
5555
import org.openhab.core.items.Item;
5656
import org.openhab.core.library.types.QuantityType;
@@ -69,7 +69,7 @@
6969
* {@link Lock} for multi-thread synchronization; globals and openhab-js injection code caching
7070
*/
7171
public class OpenhabGraalJSScriptEngine
72-
extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<GraalJSScriptEngine> {
72+
extends InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable<GraalJSScriptEngine> {
7373

7474
private static final Logger LOGGER = LoggerFactory.getLogger(OpenhabGraalJSScriptEngine.class);
7575
private static final Source GLOBAL_SOURCE;
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.io.Reader;
1616

1717
import javax.script.Bindings;
18+
import javax.script.Compilable;
19+
import javax.script.CompiledScript;
1820
import javax.script.Invocable;
1921
import javax.script.ScriptContext;
2022
import javax.script.ScriptEngine;
@@ -29,11 +31,11 @@
2931
*
3032
* @author Jonathan Gilbert - Initial contribution
3133
*/
32-
public abstract class DelegatingScriptEngineWithInvocableAndAutocloseable<T extends ScriptEngine & Invocable & AutoCloseable>
33-
implements ScriptEngine, Invocable, AutoCloseable {
34+
public abstract class DelegatingScriptEngineWithInvocableAndCompilableAndAutocloseable<T extends ScriptEngine & Invocable & Compilable & AutoCloseable>
35+
implements ScriptEngine, Invocable, Compilable, AutoCloseable {
3436
protected @NonNull T delegate;
3537

36-
public DelegatingScriptEngineWithInvocableAndAutocloseable(@NonNull T delegate) {
38+
public DelegatingScriptEngineWithInvocableAndCompilableAndAutocloseable(@NonNull T delegate) {
3739
this.delegate = delegate;
3840
}
3941

@@ -128,6 +130,16 @@ public <T> T getInterface(Object o, Class<T> aClass) {
128130
return delegate.getInterface(o, aClass);
129131
}
130132

133+
@Override
134+
public CompiledScript compile(String s) throws ScriptException {
135+
return delegate.compile(s);
136+
}
137+
138+
@Override
139+
public CompiledScript compile(Reader reader) throws ScriptException {
140+
return delegate.compile(reader);
141+
}
142+
131143
@Override
132144
public void close() throws Exception {
133145
delegate.close();
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@
1616
import java.lang.reflect.UndeclaredThrowableException;
1717

1818
import javax.script.Bindings;
19+
import javax.script.Compilable;
20+
import javax.script.CompiledScript;
1921
import javax.script.Invocable;
2022
import javax.script.ScriptContext;
2123
import javax.script.ScriptEngine;
2224
import javax.script.ScriptException;
2325

2426
/**
25-
* Delegate allowing AOP-style interception of calls, either before Invocation, or upon a {@link ScriptException}.
26-
* being thrown.
27+
* Delegate allowing AOP-style interception of calls, either before Invocation, or upon a {@link ScriptException} being
28+
* thrown.
2729
*
2830
* @param <T> The delegate class
2931
* @author Jonathan Gilbert - Initial contribution
3032
*/
31-
public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T extends ScriptEngine & Invocable & AutoCloseable>
32-
extends DelegatingScriptEngineWithInvocableAndAutocloseable<T> {
33+
public abstract class InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable<T extends ScriptEngine & Invocable & Compilable & AutoCloseable>
34+
extends DelegatingScriptEngineWithInvocableAndCompilableAndAutocloseable<T> {
3335

34-
public InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable(T delegate) {
36+
public InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable(T delegate) {
3537
super(delegate);
3638
}
3739

@@ -155,4 +157,28 @@ public Object invokeFunction(String s, Object... objects)
155157
throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
156158
}
157159
}
160+
161+
@Override
162+
public CompiledScript compile(String s) throws ScriptException {
163+
try {
164+
beforeInvocation();
165+
return (CompiledScript) afterInvocation(super.compile(s));
166+
} catch (ScriptException se) {
167+
throw (ScriptException) afterThrowsInvocation(se);
168+
} catch (Exception e) {
169+
throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
170+
}
171+
}
172+
173+
@Override
174+
public CompiledScript compile(Reader reader) throws ScriptException {
175+
try {
176+
beforeInvocation();
177+
return (CompiledScript) afterInvocation(super.compile(reader));
178+
} catch (ScriptException se) {
179+
throw (ScriptException) afterThrowsInvocation(se);
180+
} catch (Exception e) {
181+
throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
182+
}
183+
}
158184
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Please check here how to add suppressions https://maven.apache.org/plugins/maven-pmd-plugin/examples/violation-exclusions.html
22
org.openhab.automation.jsscripting.internal.OpenhabGraalJSScriptEngine=UnusedPrivateField
3-
org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable=AvoidThrowingNullPointerException,AvoidCatchingNPE
3+
org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndCompilableAndAutoCloseable=AvoidThrowingNullPointerException,AvoidCatchingNPE

0 commit comments

Comments
 (0)