use of org.apache.bsf.BSFManager in project jmeter by apache.
the class BSFTimer method delay.
/** {@inheritDoc} */
@Override
public long delay() {
long delay = 0;
BSFManager mgr = null;
try {
mgr = getManager();
Object o = evalFileOrScript(mgr);
if (o == null) {
log.warn("Script did not return a value");
return 0;
}
delay = Long.parseLong(o.toString());
} catch (NumberFormatException | BSFException e) {
if (log.isWarnEnabled()) {
log.warn("Problem in BSF script. {}", e.toString());
}
} finally {
if (mgr != null) {
mgr.terminate();
}
}
return delay;
}
use of org.apache.bsf.BSFManager in project opennms by OpenNMS.
the class BSFClient method connect.
/**
* {@inheritDoc}
*/
@Override
public void connect(final InetAddress address, final int port, final int timeout) throws IOException, Exception {
m_results = new HashMap<String, String>();
BSFManager bsfManager = new BSFManager();
File file = new File(m_fileName);
Map<String, Object> map = getParametersMap();
try {
if (m_langClass == null) {
m_langClass = BSFManager.getLangFromFilename(m_fileName);
}
if (m_bsfEngine != null && m_langClass != null && m_fileExtensions.length > 0) {
BSFManager.registerScriptingEngine(m_langClass, m_bsfEngine, m_fileExtensions);
}
if (file.exists() && file.canRead()) {
String code = IOUtils.getStringFromReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
// Declare some beans that can be used inside the script
bsfManager.declareBean("map", map, Map.class);
bsfManager.declareBean("ip_addr", address.getHostAddress(), String.class);
// TODO: I'm not sure how to deal with it on detectors. Is the node exists before running detectors? If so, I need NodeDao here.
// bsfManager.declareBean("node_id",svc.getNodeId(),int.class );
// bsfManager.declareBean("node_label", svc.getNodeLabel(), String.class);
bsfManager.declareBean("svc_name", m_serviceName, String.class);
bsfManager.declareBean("results", m_results, Map.class);
bsfManager.declareBean("port", port, Integer.class);
bsfManager.declareBean("timeout", timeout, Integer.class);
for (final Entry<String, Object> entry : map.entrySet()) {
bsfManager.declareBean(entry.getKey(), entry.getValue(), String.class);
}
LOG.info("Executing {} for {}", m_langClass, file.getAbsoluteFile());
if ("eval".equals(m_runType)) {
m_results.put("status", bsfManager.eval(m_langClass, "BSFDetector", 0, 0, code).toString());
} else if ("exec".equals(m_runType)) {
bsfManager.exec(m_langClass, "BSFDetector", 0, 0, code);
} else {
LOG.warn("Invalid run-type parameter value '{}' for service '{}'. Only 'eval' and 'exec' are supported.", m_runType, m_serviceName);
throw new RuntimeException("Invalid run-type '" + m_runType + "'");
}
if ("exec".equals(m_runType) && !m_results.containsKey("status")) {
LOG.warn("The exec script '{}' for service '{}' never put a 'status' entry in the 'results' bean. Exec scripts should put this entry with a value of 'OK' for up.", m_fileName, m_serviceName);
}
} else {
LOG.warn("Cannot locate or read BSF script file '{}'. Marking service '{}' down.", m_fileName, m_serviceName);
}
} catch (BSFException e) {
m_results.clear();
LOG.warn("BSFDetector poll for service '{}' failed with BSFException: {}", m_serviceName, e.getMessage(), e);
} catch (FileNotFoundException e) {
m_results.clear();
LOG.warn("Could not find BSF script file '{}'. Marking service '{}' down.", m_fileName, m_serviceName);
} catch (IOException e) {
m_results.clear();
LOG.warn("BSFDetector poll for service '{}' failed with IOException: {}", m_serviceName, e.getMessage(), e);
} catch (Throwable e) {
m_results.clear();
LOG.warn("BSFDetector poll for service '{}' failed with unexpected throwable: {}", m_serviceName, e.getMessage(), e);
} finally {
BSFManagerTerminator.terminate(bsfManager);
}
}
use of org.apache.bsf.BSFManager in project groovy-core by groovy.
the class BSFSpecTest method testApply.
@Test
public void testApply() throws BSFException {
// tag::bsf_apply[]
BSFManager manager = new BSFManager();
Vector<String> ignoreParamNames = null;
Vector<Integer> args = new Vector<Integer>();
args.add(2);
args.add(5);
args.add(1);
Integer actual = (Integer) manager.apply("groovy", "applyTest", 0, 0, "def summer = { a, b, c -> a * 100 + b * 10 + c }", ignoreParamNames, args);
assertEquals(251, actual.intValue());
// end::bsf_apply[]
}
use of org.apache.bsf.BSFManager in project groovy by apache.
the class BSFSpecTest method testApply.
@Test
public void testApply() throws BSFException {
// tag::bsf_apply[]
BSFManager manager = new BSFManager();
Vector<String> ignoreParamNames = null;
Vector<Integer> args = new Vector<Integer>();
args.add(2);
args.add(5);
args.add(1);
Integer actual = (Integer) manager.apply("groovy", "applyTest", 0, 0, "def summer = { a, b, c -> a * 100 + b * 10 + c }", ignoreParamNames, args);
assertEquals(251, actual.intValue());
// end::bsf_apply[]
}
use of org.apache.bsf.BSFManager in project groovy by apache.
the class BSFSpecTest method testVariablePassing.
@Test
public void testVariablePassing() throws BSFException {
// tag::bsf_variable_passing[]
BSFManager manager = new BSFManager();
manager.declareBean("xyz", 4, Integer.class);
Object answer = manager.eval("groovy", "test.groovy", 0, 0, "xyz + 1");
assertEquals(5, answer);
// end::bsf_variable_passing[]
}
Aggregations