cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptTask, JRuby cannot access execution variable

andyb
Champ in-the-making
Champ in-the-making
Hi, as stated in the subject header, I am trying to write a simple script task in jruby, but am running into a problem wherein the script does not recognize the execution variable's existence in its scope. I'm wondering if any issue like this has been noted before and a solution exists, or whether I'm just doing something stupid.

The test case I'm running is quite simple. I have a form attached to a start activity that requests a username variable. Then in my script I try execution.getVariable('username'), but no luck. I get an error:
      problem evaluation script: org.jruby.embed.EvalFailedException: (NameError) undefined local variable `execution' for mainSmiley Surprisedbject

When I try the same script with groovy I have no problem. My guess then, is that this is a compatibility issue, and indeed looking through the source code, it does appear groovy is given a certain degree of preferential treatment, but I haven't been able to pinpoint in the source just how I would make this work for jruby.

Any explanation would be immensely helpful, as I've spent a long time exhausting a lot of avenues of possibilities for this error, thanks.
2 REPLIES 2

jbarrez
Star Contributor
Star Contributor
It's been a long time since I tried Jruby … but in theory if it implements the same JSR interface, it should behave the same.

I did try your example, and had the same error Smiley Sad Normally, the scripting engine asks the script binding (https://github.com/Activiti/Activiti/blob/master/modules/activiti-engine/src/main/java/org/activiti/..., nor in https://github.com/Activiti/Activiti/blob/master/modules/activiti-engine/src/main/java/org/activiti/...) for a variable if it doesn't know about it. But that doesn't happen here. I see quite a bit of keys being tried, but not the execution. So it makes me wonder if JRuby still implements JSR 233 the same as the other scripting engines do.

(googling around found this: https://github.com/jruby/jruby/wiki/Embedding-with-JSR-223 … which seems to use the same mechanism we use … but different, cause the objects are put directly into the bindings there …)

andyb
Champ in-the-making
Champ in-the-making
Okay, so running the following code in a groovy script allows you to fetch strings from a form and then manipulate them in jruby. It's an odd solution though.

import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleBindings;

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("jruby");
Bindings bindings = new SimpleBindings();

def message = execution.getVariable('username');
def quote = "\"";
message = "$quote$message$quote";
bindings.put("message", message);
engine.eval("puts $message");

For some reason you have to add extra quotes around the string, otherwise groovy interprets it as a variable name instead of a string and just tells you it doesn't have access to variable '(your form input string)'.

This is messy though, and fraught with what I would consider bad coding practices. But hey, it works.