Skip to content

Commit

Permalink
[jsscripting] Implement javax.script.Compilable
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Hotze <[email protected]>
  • Loading branch information
florian-h05 committed Jun 30, 2024
1 parent 9fea1c5 commit 41cab0d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
*/
package org.openhab.automation.jsscripting.internal;

import static org.openhab.core.automation.module.script.ScriptTransformationService.OPENHAB_TRANSFORMATION_SCRIPT;

import java.util.Arrays;
import java.util.stream.Collectors;

import javax.script.Compilable;
import javax.script.Invocable;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
Expand All @@ -25,15 +28,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.openhab.core.automation.module.script.ScriptTransformationService.OPENHAB_TRANSFORMATION_SCRIPT;

/**
* Wraps ScriptEngines provided by Graal to provide error messages and stack traces for scripts.
*
* @author Jonathan Gilbert - Initial contribution
* @author Florian Hotze - Improve logger name, Fix memory leak caused by exception logging
*/
class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoCloseable>
class DebuggingGraalScriptEngine<T extends ScriptEngine & Invocable & AutoCloseable & Compilable>
extends InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T> {

private static final int STACK_TRACE_LENGTH = 5;
Expand Down Expand Up @@ -93,8 +94,7 @@ private void initializeLogger() {
identifier = ruleUID.toString();
} else if (ohEngineIdentifier != null) {
if (ohEngineIdentifier.toString().startsWith(OPENHAB_TRANSFORMATION_SCRIPT)) {
identifier = ohEngineIdentifier.toString().replaceAll(OPENHAB_TRANSFORMATION_SCRIPT,
"transformation.");
identifier = ohEngineIdentifier.toString().replaceAll(OPENHAB_TRANSFORMATION_SCRIPT, "transformation.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.io.Reader;

import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.Invocable;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
Expand All @@ -29,8 +31,8 @@
*
* @author Jonathan Gilbert - Initial contribution
*/
public abstract class DelegatingScriptEngineWithInvocableAndAutocloseable<T extends ScriptEngine & Invocable & AutoCloseable>
implements ScriptEngine, Invocable, AutoCloseable {
public abstract class DelegatingScriptEngineWithInvocableAndAutocloseable<T extends ScriptEngine & Invocable & AutoCloseable & Compilable>
implements ScriptEngine, Invocable, AutoCloseable, Compilable {
protected @NonNull T delegate;

public DelegatingScriptEngineWithInvocableAndAutocloseable(@NonNull T delegate) {
Expand Down Expand Up @@ -132,4 +134,14 @@ public <T> T getInterface(Object o, Class<T> aClass) {
public void close() throws Exception {
delegate.close();
}

@Override
public CompiledScript compile(String s) throws ScriptException {
return delegate.compile(s);
}

@Override
public CompiledScript compile(Reader reader) throws ScriptException {
return delegate.compile(reader);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.lang.reflect.UndeclaredThrowableException;

import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.Invocable;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
Expand All @@ -28,7 +30,7 @@
* @param <T> The delegate class
* @author Jonathan Gilbert - Initial contribution
*/
public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T extends ScriptEngine & Invocable & AutoCloseable>
public abstract class InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable<T extends ScriptEngine & Invocable & AutoCloseable & Compilable>
extends DelegatingScriptEngineWithInvocableAndAutocloseable<T> {

public InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable(T delegate) {
Expand Down Expand Up @@ -155,4 +157,28 @@ public Object invokeFunction(String s, Object... objects)
throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
}
}

@Override
public CompiledScript compile(String s) throws ScriptException {
try {
beforeInvocation();
return (CompiledScript) afterInvocation(super.compile(s));
} catch (ScriptException se) {
throw (ScriptException) afterThrowsInvocation(se);
} catch (Exception e) {
throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
}
}

@Override
public CompiledScript compile(Reader reader) throws ScriptException {
try {
beforeInvocation();
return (CompiledScript) afterInvocation(super.compile(reader));
} catch (ScriptException se) {
throw (ScriptException) afterThrowsInvocation(se);
} catch (Exception e) {
throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
}
}
}

0 comments on commit 41cab0d

Please sign in to comment.