Overview
The Nashorn plugin provides support for the Nashorn JavaScript engine for IdPs of version 4.1 and beyond. This the scripting engine that was shipped with Java between versions 8 to 14 (it is not present as of JDK15). The plugin uses the Nashorn scripting engine provided by the OpenJDK project.
Installation
Starting with IdP 4.2 you can the install the latest plugin version supported on your IdP version with.\plugin.sh -I net.shibboleth.idp.plugin.nashorn
PluginId |
---|
net.shibboleth.idp.plugin.nashorn |
Configuration
This plugin requires no configuration and does not expose any Modules. Its sole purpose is to add a new scripting language to the IdP. The plugin is actually a JSR-223 implementation which works alongside the IdP to make these language strings available:
nashorn
Nashorn
js
JS
JavaScript
javascript
ECMAScript
ecmascript
Tips
This is a collection point for any “generic” scripting tips or examples that don’t pertain to specific use cases but just illustrate harder or non-obvious ways to use Javascript in the Java environment.
Setting ‘nashorn.args’
In JDK11 you set arguments to the Nashorn scripting engine via the nashorn.args
system property
-Dnashorn.args=--no-deprecation-warning
It appears that this mechanism has changed in new versions and you need an extra level of indirection
-Dnashorn.args=-D--no-deprecation-warning
Debugging issues with the Nashorn Scripting Engine
The engine can fail (and indeed fail to load) silently. To debug this (not your script itself) you can set the nashorn.debug
system property to “true”.
Accessing Classes or Static Methods
Many of the APIs in our software rely on class objects or static methods, which are simple to use in Java itself but less obvious in Javascript.
Calling a static method on a class looks like this. The method name in this example is bar and it takes no arguments.
var type = Java.type("org.example.package.ClassName"); var foo = type.bar();
Accessing a class object is slightly different:
var claz = Java.type("org.example.package.ClassName").class; var classname = claz.getName();