}
public SampleResult sample(Entry e)// Entry tends to be ignored ...
{
//log.info(getLabel()+" "+getFilename());
SampleResult res = new SampleResult();
boolean isSuccessful = false;
res.setSampleLabel(getLabel());
res.sampleStart();
try
{
String request=getScript();
String fileName=getFilename();
if (fileName.length() == 0) {
res.setSamplerData(request);
} else {
res.setSamplerData(fileName);
}
// Has to be done after construction, otherwise fails serialisation check
bshInterpreter.set("log",log); //$NON-NLS-1$
bshInterpreter.set("Label",getLabel()); //$NON-NLS-1$
bshInterpreter.set("FileName",getFilename()); //$NON-NLS-1$
bshInterpreter.set("SampleResult",res); //$NON-NLS-1$
bshInterpreter.set("Parameters",getParameters());// as a single line//$NON-NLS-1$
bshInterpreter.set("bsh.args",JOrphanUtils.split(getParameters()," "));
// Set default values
bshInterpreter.set("ResponseCode","200"); //$NON-NLS-1$
bshInterpreter.set("ResponseMessage","OK");//$NON-NLS-1$
bshInterpreter.set("IsSuccess",true);//$NON-NLS-1$
Object bshOut;
if (fileName.length() == 0){
bshOut = bshInterpreter.eval(request);
} else {
bshOut = bshInterpreter.source(fileName);
}
String out;
if (bshOut == null) {// Script did not return anything...
out="";
} else {
out = bshOut.toString();
}
res.setResponseData(out.getBytes());
res.setDataType(SampleResult.TEXT);
res.setResponseCode(bshInterpreter.get("ResponseCode").toString());//$NON-NLS-1$
res.setResponseMessage(bshInterpreter.get("ResponseMessage").toString());//$NON-NLS-1$
isSuccessful = Boolean.valueOf(bshInterpreter.get("IsSuccess") //$NON-NLS-1$
.toString()).booleanValue();
}
/*
* To avoid class loading problems when bsh,jar is missing,
* we don't try to catch this error separately
* catch (bsh.EvalError ex)
{
log.debug("",ex);
res.setResponseCode("500");//$NON-NLS-1$
res.setResponseMessage(ex.toString());
}
*/
// but we do trap this error to make tests work better
catch(NoClassDefFoundError ex){
log.error("BeanShell Jar missing? "+ex.toString());
res.setResponseCode("501");//$NON-NLS-1$
res.setResponseMessage(ex.toString());
res.setStopThread(true); // No point continuing
}
catch (IOException ex)
{
log.warn(ex.toString());
res.setResponseCode("500");//$NON-NLS-1$
res.setResponseMessage(ex.toString());
}
catch (Exception ex) // Mainly for bsh.EvalError
{
log.warn(ex.toString());
res.setResponseCode("500");//$NON-NLS-1$
res.setResponseMessage(ex.toString());
}
res.sampleEnd();
// Set if we were successful or not
res.setSuccessful(isSuccessful);
return res;
}