res.setSampleLabel(label);
FileInputStream is = null;
BSFEngine bsfEngine = null;
// There's little point saving the manager between invocations
// as we need to reset most of the beans anyway
BSFManager mgr = new BSFManager();
// TODO: find out how to retrieve these from the script
// At present the script has to use SampleResult methods to set them.
res.setResponseCode("200"); // $NON-NLS-1$
res.setResponseMessage("OK"); // $NON-NLS-1$
res.setSuccessful(true);
res.setDataType(SampleResult.TEXT); // Default (can be overridden by the script)
res.sampleStart();
try {
initManager(mgr);
mgr.declareBean("SampleResult", res, res.getClass()); // $NON-NLS-1$
// These are not useful yet, as have not found how to get updated values back
//mgr.declareBean("ResponseCode", "200", String.class); // $NON-NLS-1$
//mgr.declareBean("ResponseMessage", "OK", String.class); // $NON-NLS-1$
//mgr.declareBean("IsSuccess", Boolean.TRUE, Boolean.class); // $NON-NLS-1$
// N.B. some engines (e.g. Javascript) cannot handle certain declareBean() calls
// after the engine has been initialised, so create the engine last
bsfEngine = mgr.loadScriptingEngine(getScriptLanguage());
Object bsfOut = null;
if (fileName.length()>0) {
res.setSamplerData("File: "+fileName);
is = new FileInputStream(fileName);
bsfOut = bsfEngine.eval(fileName, 0, 0, IOUtils.toString(is));
} else {
res.setSamplerData(request);
bsfOut = bsfEngine.eval("script", 0, 0, request);
}
if (bsfOut != null) {
res.setResponseData(bsfOut.toString().getBytes());
}
} catch (BSFException ex) {
log.warn("BSF error", ex);
res.setSuccessful(false);
res.setResponseCode("500"); // $NON-NLS-1$
res.setResponseMessage(ex.toString());
} catch (Exception ex) {// Catch evaluation errors
log.warn("Problem evaluating the script", ex);
res.setSuccessful(false);
res.setResponseCode("500"); // $NON-NLS-1$
res.setResponseMessage(ex.toString());
} finally {
res.sampleEnd();
IOUtils.closeQuietly(is);
// Will be done by mgr.terminate() anyway
// if (bsfEngine != null) {
// bsfEngine.terminate();
// }
mgr.terminate();
}
return res;
}