use of org.apache.bsf.BSFManager in project groovy by apache.
the class BSFSpecTest method testSimpleIntegration.
@Test
public void testSimpleIntegration() throws BSFException {
// tag::bsf_simple[]
String myScript = "println('Hello World')\n return [1, 2, 3]";
BSFManager manager = new BSFManager();
List answer = (List) manager.eval("groovy", "myScript.groovy", 0, 0, myScript);
assertEquals(3, answer.size());
// end::bsf_simple[]
}
use of org.apache.bsf.BSFManager in project groovy by apache.
the class BSFSpecTest method testAccess.
@Test
public void testAccess() throws BSFException {
// tag::bsf_access[]
BSFManager manager = new BSFManager();
BSFEngine bsfEngine = manager.loadScriptingEngine("groovy");
manager.declareBean("myvar", "hello", String.class);
Object myvar = manager.lookupBean("myvar");
String result = (String) bsfEngine.call(myvar, "reverse", new Object[0]);
assertEquals("olleh", result);
// end::bsf_access[]
}
use of org.apache.bsf.BSFManager in project groovy by apache.
the class CacheBSFTest method setUp.
protected void setUp() throws Exception {
// override standard engine with caching one
BSFManager.registerScriptingEngine("groovy", CACHING_ENGINE.getName(), new String[] { "groovy", "gy" });
manager = new BSFManager();
}
use of org.apache.bsf.BSFManager in project jmeter by apache.
the class BSFSampler method sample.
@Override
public // Entry tends to be ignored ...
SampleResult sample(// Entry tends to be ignored ...
Entry e) {
final String label = getName();
final String request = getScript();
final String fileName = getFilename();
log.debug("{} {}", label, fileName);
SampleResult res = new SampleResult();
res.setSampleLabel(label);
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.
// $NON-NLS-1$
res.setResponseCode("200");
// $NON-NLS-1$
res.setResponseMessage("OK");
res.setSuccessful(true);
// Default (can be overridden by the script)
res.setDataType(SampleResult.TEXT);
res.sampleStart();
try {
initManager(mgr);
// $NON-NLS-1$
mgr.declareBean("SampleResult", res, res.getClass());
// 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);
try (FileInputStream fis = new FileInputStream(fileName);
BufferedInputStream is = new BufferedInputStream(fis)) {
bsfOut = bsfEngine.eval(fileName, 0, 0, IOUtils.toString(is, Charset.defaultCharset()));
}
} else {
res.setSamplerData(request);
bsfOut = bsfEngine.eval("script", 0, 0, request);
}
if (bsfOut != null) {
res.setResponseData(bsfOut.toString(), null);
}
} catch (BSFException ex) {
log.warn("BSF error", ex);
res.setSuccessful(false);
// $NON-NLS-1$
res.setResponseCode("500");
res.setResponseMessage(ex.toString());
} catch (Exception ex) {
// Catch evaluation errors
log.warn("Problem evaluating the script", ex);
res.setSuccessful(false);
// $NON-NLS-1$
res.setResponseCode("500");
res.setResponseMessage(ex.toString());
} finally {
res.sampleEnd();
mgr.terminate();
}
return res;
}
use of org.apache.bsf.BSFManager in project jmeter by apache.
the class BSFPreProcessor method process.
@Override
public void process() {
BSFManager mgr = null;
try {
mgr = getManager();
if (mgr == null) {
return;
}
processFileOrScript(mgr);
} catch (BSFException e) {
if (log.isWarnEnabled()) {
log.warn("Problem in BSF script. {}", e.toString());
}
} finally {
if (mgr != null) {
mgr.terminate();
}
}
}
Aggregations