Rework Java223ScriptEngine.invokeFunction() #27
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is how it works:
openhab/openhab-docs#2567 ⇔ https://deploy-preview-2567--openhab-docs-preview.netlify.app/docs/configuration/jsr223.html#the-scriptloaded-and-scriptunloaded-functions shows how scriptLoaded() and scriptUnloaded() work in different OH JSR223 languages. In particular in Groovy the functions are not executed, if they are
private, or ifscriptUnloaded()is defined with at least one parameter, or ifscriptLoaded()does not have exactly one parameter of type String. This is how now it is in Java223.The other OH-JSR223 add-ons do not implement the loadScript/undloadScript functionality as helper library service, with the exception of JRuby (see .script_loaded - no parameter - and .script_unloaded at https://openhab.github.io/openhab-jruby/main/OpenHAB/Core/ScriptHandling.html ; and on_load and on_unload at https://openhab.github.io/openhab-jruby/main/OpenHAB/DSL/Rules/BuilderDSL.html) - which does not pass the filename parameter of scriptLoaded(). Implementing the functionality of scriptUnloaded() in a helper library should be easy using
lifecycleTracker, so there is no special handling for this necessary here.In any case, as there is no more a class
ScriptUnloadedTriggerthe system cannot complain anymore about "NoClassDefFoundError: java223/common/ScriptUnloadedTrigger" when the Java223 bundle is reloaded: https://community.openhab.org/t/java223-scripting-script-with-java-on-openhab-5-0-1-0-6-0-0-0/159853/25 .So back to Groovy, which has a very thin layer in the openhab-addons, to integrate JSR223 with openHAB and the scriptLoaded(String) and scriptUnloaded() function calls just work out of the box. The other OH Automation JSR223 languages are similar - they are not much lines of code, handle invokeFunction() and do nothing special about it, yet still work.
In Groovy, if a file contains all logic between
class {and}, then this is called a Groovy Class, otherwise it is a Groovy Script. Groovy Scripts are implicitly converted to Groovy Classes. The equivalent Groovy class for the example from https://deploy-preview-2567--openhab-docs-preview.netlify.app/docs/configuration/jsr223.html#the-scriptloaded-and-scriptunloaded-functions is:The above does indeed work as the Groovy example from https://deploy-preview-2567--openhab-docs-preview.netlify.app/docs/configuration/jsr223.html#the-scriptloaded-and-scriptunloaded-functions when put in a automation/jsr223/.groovy file. As Groovy and Java are very similar, I think the next excerpt from the example above should also work in Java, the modifications from the above were to add
publicon three places, addmain, add semi-colons, and do not use formatted string in scriptLoaded, but appendfilename.That works now in Java223!
After
} catch (IllegalAccessException | InvocationTargetException e) {there is no need to log a message, when thescriptLoaded()orscriptUnloaded()function is private. The reason is that scriptLoaded() is called from org.openhab.core.automation.module.script.internal.ScriptEngineManager.scriptLoaded() and it does log the received exceptions. Likewise forscriptUnloaded()called from ScriptEngineManager.removeEngine(). Okay, the latter logs ScriptException.toMessage() instead of scriptException.getCause().toMessage(), but as nobody noticed a problem so far, this should be fine. These are the only two places in openhab-core, which call invokeFunction().In any case, if I make the functions loadScript() or unloadScript()
privateopenhab.log by default does say nothing.In
the last
@Nullableis wrong,argsis always an array and cannot be null. But I cannot get the annotations working then.