Skip to content

Commit c5cfed5

Browse files
committed
[jsscripting] Implement javax.script.Compilable
Signed-off-by: Florian Hotze <[email protected]>
1 parent 8e790cc commit c5cfed5

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
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;
@@ -25,15 +28,13 @@
2528
import org.slf4j.Logger;
2629
import org.slf4j.LoggerFactory;
2730

28-
import static org.openhab.core.automation.module.script.ScriptTransformationService.OPENHAB_TRANSFORMATION_SCRIPT;
29-
3031
/**
3132
* Wraps ScriptEngines provided by Graal to provide error messages and stack traces for scripts.
3233
*
3334
* @author Jonathan Gilbert - Initial contribution
3435
* @author Florian Hotze - Improve logger name, Fix memory leak caused by exception logging
3536
*/
36-
class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoCloseable>
37+
class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoCloseable & Compilable>
3738
extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T> {
3839

3940
private static final int STACK_TRACE_LENGTH = 5;
@@ -93,8 +94,7 @@ private void initializeLogger() {
9394
identifier = ruleUID.toString();
9495
} else if (ohEngineIdentifier != null) {
9596
if (ohEngineIdentifier.toString().startsWith(OPENHAB_TRANSFORMATION_SCRIPT)) {
96-
identifier = ohEngineIdentifier.toString().replaceAll(OPENHAB_TRANSFORMATION_SCRIPT,
97-
"transformation.");
97+
identifier = ohEngineIdentifier.toString().replaceAll(OPENHAB_TRANSFORMATION_SCRIPT, "transformation.");
9898
}
9999
}
100100

bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/DelegatingScriptEngineWithInvocableAndAutocloseable.java

+14-2
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,8 +31,8 @@
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 DelegatingScriptEngineWithInvocableAndAutocloseable<T extends ScriptEngine & Invocable & AutoCloseable & Compilable>
35+
implements ScriptEngine, Invocable, AutoCloseable, Compilable {
3436
protected @NonNull T delegate;
3537

3638
public DelegatingScriptEngineWithInvocableAndAutocloseable(@NonNull T delegate) {
@@ -132,4 +134,14 @@ public <T> T getInterface(Object o, Class<T> aClass) {
132134
public void close() throws Exception {
133135
delegate.close();
134136
}
137+
138+
@Override
139+
public CompiledScript compile(String s) throws ScriptException {
140+
return delegate.compile(s);
141+
}
142+
143+
@Override
144+
public CompiledScript compile(Reader reader) throws ScriptException {
145+
return delegate.compile(reader);
146+
}
135147
}

bundles/org.openhab.automation.jsscripting/src/main/java/org/openhab/automation/jsscripting/internal/scriptengine/InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
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;
@@ -28,7 +30,7 @@
2830
* @param <T> The delegate class
2931
* @author Jonathan Gilbert - Initial contribution
3032
*/
31-
public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T extends ScriptEngine & Invocable & AutoCloseable>
33+
public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T extends ScriptEngine & Invocable & AutoCloseable & Compilable>
3234
extends DelegatingScriptEngineWithInvocableAndAutocloseable<T> {
3335

3436
public InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable(T delegate) {
@@ -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
}

0 commit comments

Comments
 (0)