Yes you can call java from GEL, and you can either choose to do that directly with your jar - since it's placed in either clarity/lib or clarity/customlib it is normally automatically added to the classpath when the app/bg services start.
What you do with it would not be supported; it's your own code after all and nobody elses, and generally speaking whilst things like this might work in the SaaS environments, calling scripts would be deeply frowned upon and discouraged.
Since you put the jar files in clarity/lib though, I will assume you're on-premises and not SaaS.
I don't have Clarity installed on Linux currently to demo right now, but I can do it from Windows and it's pretty much the same deal in other OS's so long as you use appropriate script names and contents. It should go without saying that these ought to be non-interactive scripts that cannot stop and prompt for any inputs, or else they may get stuck in limbo and cause the process engine to time out over the long-running (stuck) script, which will then bring down the process engine for everything.
With the disclaimers out of the way, if I have a script like this called external_script.bat (and we assume for now it's placed in the clarity/bin folder although I'd suggest putting it some place else):
@echo off
echo Called by a gel script > logfile.txt
echo Shhh, don't tell anyone the %1 >> logfile.txt
echo Do I talk too much? Well you said it was ok right? %2 >> logfile.txt
If I create a GEL script like this and run it:
<gel:script
xmlns:core="jelly:core"
xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
>
<core:set var="dbPwd" value="secret" />
<core:set var="scriptName" value="external_script.bat" />
<core:set var="verbosity" value="--verbose" />
<core:invokeStatic var="stringClass" className="java.lang.Class" method="forName">
<core:arg type="java.lang.String" value="java.lang.String"/>
</core:invokeStatic>
<core:invokeStatic var="stringArray" className="java.lang.reflect.Array" method="newInstance">
<core:arg type="java.lang.Class" value="${stringClass}"/>
<core:arg type="int" value="${3}"/>
</core:invokeStatic>
<core:invokeStatic className="java.lang.reflect.Array" method="set">
<core:arg type="java.lang.Object" value="${stringArray}" />
<core:arg type="int" value="${0}" />
<core:arg type="java.lang.Object" value="${scriptName}" />
</core:invokeStatic>
<core:invokeStatic className="java.lang.reflect.Array" method="set">
<core:arg type="java.lang.Object" value="${stringArray}" />
<core:arg type="int" value="${1}" />
<core:arg type="java.lang.Object" value="${dbPwd}" />
</core:invokeStatic>
<core:invokeStatic className="java.lang.reflect.Array" method="set">
<core:arg type="java.lang.Object" value="${stringArray}" />
<core:arg type="int" value="${2}" />
<core:arg type="java.lang.Object" value="${verbosity}" />
</core:invokeStatic>
<core:new className="java.lang.ProcessBuilder" var="builder">
<core:arg value="${stringArray}" />
</core:new>
<core:invoke on="${builder}" method="start" var="proc" />
</gel:script>
I get a logfile.txt created with the following contents:
Called by a gel script
Shhh, don't tell anyone the secret
Do I talk too much? Well you said it was ok right? --verbose
The tricky part of this, if there is to be one, is that ProcessBuilder's constructor takes either a List<String> or String... args list; and constructing those in GEL as you can see takes a disproportionate amount of effort for the results (compared to plain java equivalents).
Reference:
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ProcessBuilder.html
Other packages and classes can be more simply called, where the <core:arg> value can either be accepted directly from a GEL variable or maybe just coerced by setting the type="..." attribute.
A lot (but not all) of the jelly commons core library tags work as documented, so for more variations such as needing to call static methods or similar, check it out.
Reference:
http://commons.apache.org/proper/commons-jelly/tags.html
Make sure that all the files (scripts, jars, etc.) have the appropriate permissions set, so that the OS user running the bg services where the process engine is running will be able to find and execute what it needs.
Original Message:
Sent: 08-16-2019 12:49 AM
From: Pavan Bugata
Subject: How to invoke custom Java class from GEL script
Hi,
I have a use case - a sequence of three shell scripts to be executed on Linux box. I got the java jar deployed under clarity/lib which accepts the parameters. So I need to pass DB password from properties.xml and shell script name/s (as an ArrayList). Is there a way to invoke the Java class with parameters from GEL script?
[Edited]
My goal is to run the shell scripts in sequence on Linux box. If we cannot invoke Java class from GEL, please let me know if we can run shell scripts from GEL directly.
Thanks,
PB