private static final String INJECTION_CODE = "Object.assign(this, require('openhab'));";
private boolean injectionEnabled = true;
+ /*
+ * Whilst we run in parallel with Nashorn, we use a custom mime-type to avoid
+ * disrupting Nashorn scripts. When Nashorn is removed, we take over the standard
+ * JS runtime.
+ */
+
+ // GraalJSEngineFactory graalJSEngineFactory = new GraalJSEngineFactory();
+ //
+ // scriptTypes.addAll(graalJSEngineFactory.getMimeTypes());
+ // scriptTypes.addAll(graalJSEngineFactory.getExtensions());
+
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 static final List<String> SCRIPT_TYPES = List.of(MIME_TYPE, ALT_MIME_TYPE, ALIAS);
private final JSScriptServiceUtil jsScriptServiceUtil;
private final JSDependencyTracker jsDependencyTracker;
@Override
public List<String> getScriptTypes() {
-
- /*
- * Whilst we run in parallel with Nashorn, we use a custom mime-type to avoid
- * disrupting Nashorn scripts. When Nashorn is removed, we take over the standard
- * JS runtime.
- */
-
- // GraalJSEngineFactory graalJSEngineFactory = new GraalJSEngineFactory();
- //
- // scriptTypes.addAll(graalJSEngineFactory.getMimeTypes());
- // scriptTypes.addAll(graalJSEngineFactory.getExtensions());
-
- return List.of(MIME_TYPE, ALT_MIME_TYPE, ALIAS);
+ return SCRIPT_TYPES;
}
@Override
@Override
public @Nullable ScriptEngine createScriptEngine(String scriptType) {
+ if (!SCRIPT_TYPES.contains(scriptType)) {
+ return null;
+ }
return new DebuggingGraalScriptEngine<>(
new OpenhabGraalJSScriptEngine(injectionEnabled ? INJECTION_CODE : null, jsScriptServiceUtil));
}
@Override
protected void beforeInvocation() {
+ super.beforeInvocation();
+
lock.lock();
if (initialized) {
@Override
protected Object afterInvocation(Object obj) {
lock.unlock();
- return obj;
+ return super.afterInvocation(obj);
}
@Override
protected Exception afterThrowsInvocation(Exception e) {
lock.unlock();
- return e;
+ return super.afterThrowsInvocation(e);
}
@Override
return afterInvocation(super.eval(s, scriptContext));
} catch (ScriptException se) {
throw (ScriptException) afterThrowsInvocation(se);
+ } catch (Exception e) {
+ throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
}
}
return afterInvocation(super.eval(reader, scriptContext));
} catch (ScriptException se) {
throw (ScriptException) afterThrowsInvocation(se);
+ } catch (Exception e) {
+ throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
}
}
return afterInvocation(super.eval(s));
} catch (ScriptException se) {
throw (ScriptException) afterThrowsInvocation(se);
+ } catch (Exception e) {
+ throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
}
}
return afterInvocation(super.eval(reader));
} catch (ScriptException se) {
throw (ScriptException) afterThrowsInvocation(se);
+ } catch (Exception e) {
+ throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
}
}
return afterInvocation(super.eval(s, bindings));
} catch (ScriptException se) {
throw (ScriptException) afterThrowsInvocation(se);
+ } catch (Exception e) {
+ throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
}
}
return afterInvocation(super.eval(reader, bindings));
} catch (ScriptException se) {
throw (ScriptException) afterThrowsInvocation(se);
+ } catch (Exception e) {
+ throw new UndeclaredThrowableException(afterThrowsInvocation(e)); // Wrap and rethrow other exceptions
}
}