@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
throws InvalidVariableException {
JMeterContext jmctx = JMeterContextService.getContext();
JMeterVariables vars = jmctx.getVariables();
String script = ((CompoundVariable) values[0]).execute();
// Allow variable to be omitted
String varName = values.length < 2 ? null : ((CompoundVariable) values[1]).execute().trim();
String resultStr = "";
Context cx = Context.enter();
try {
Scriptable scope = cx.initStandardObjects(null);
// Set up some objects for the script to play with
scope.put("log", scope, log); //$NON-NLS-1$
scope.put("ctx", scope, jmctx); //$NON-NLS-1$
scope.put("vars", scope, vars); //$NON-NLS-1$
scope.put("props", scope, JMeterUtils.getJMeterProperties()); //$NON-NLS-1$
// Previously mis-spelt as theadName
scope.put("threadName", scope, Thread.currentThread().getName()); //$NON-NLS-1$
scope.put("sampler", scope, currentSampler); //$NON-NLS-1$
scope.put("sampleResult", scope, previousResult); //$NON-NLS-1$
Object result = cx.evaluateString(scope, script, "<cmd>", 1, null); //$NON-NLS-1$
resultStr = Context.toString(result);
if (varName != null && vars != null) {// vars can be null if run from TestPlan
vars.put(varName, resultStr);
}
} catch (RhinoException e) {
log.error("Error processing Javascript: [" + script + "]\n", e);
throw new InvalidVariableException();