use of org.apache.bsf.BSFException 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.BSFException 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();
}
}
}
use of org.apache.bsf.BSFException 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.BSFException in project opennms by OpenNMS.
the class BSFMonitor method executeScript.
private synchronized PollStatus executeScript(MonitoredService svc, Map<String, Object> map) {
PollStatus pollStatus = PollStatus.unavailable();
String fileName = ParameterMap.getKeyedString(map, "file-name", null);
String lang = ParameterMap.getKeyedString(map, "lang-class", null);
String langEngine = ParameterMap.getKeyedString(map, "bsf-engine", null);
String[] langExtensions = ParameterMap.getKeyedString(map, "file-extensions", "").split(",");
String runType = ParameterMap.getKeyedString(map, "run-type", "eval");
File file = new File(fileName);
try {
if (lang == null)
lang = BSFManager.getLangFromFilename(fileName);
if (langEngine != null && lang != null && langExtensions.length > 0) {
// We register the scripting engine again no matter what since
// BSFManager doesn't let us know what engine is currently registered
// for this language and it might not be the same as what we want.
LOG.debug("Registering scripting engine '{}' for '{}'", langEngine, lang);
BSFManager.registerScriptingEngine(lang, langEngine, langExtensions);
}
if (file.exists() && file.canRead()) {
String code = IOUtils.getStringFromReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
HashMap<String, String> results = new HashMap<String, String>();
LinkedHashMap<String, Number> times = new LinkedHashMap<String, Number>();
// Declare some beans that can be used inside the script
m_bsfManager.declareBean("map", map, Map.class);
m_bsfManager.declareBean("ip_addr", svc.getIpAddr(), String.class);
m_bsfManager.declareBean("node_id", svc.getNodeId(), int.class);
m_bsfManager.declareBean("node_label", svc.getNodeLabel(), String.class);
m_bsfManager.declareBean("svc_name", svc.getSvcName(), String.class);
m_bsfManager.declareBean("bsf_monitor", this, BSFMonitor.class);
m_bsfManager.declareBean("results", results, Map.class);
m_bsfManager.declareBean("times", times, Map.class);
for (final Entry<String, Object> entry : map.entrySet()) {
m_bsfManager.declareBean(entry.getKey(), entry.getValue(), String.class);
}
pollStatus = PollStatus.unknown("The script did not update the service status");
long startTime = System.currentTimeMillis();
if ("eval".equals(runType)) {
LOG.debug("m_bsfManager's hashCode is " + (m_bsfManager == null ? "null" : m_bsfManager.hashCode()) + "; lang is " + lang + "; code's hashCode is " + (code == null ? "null" : code.hashCode()));
results.put("status", m_bsfManager.eval(lang, "BSFMonitor", 0, 0, code).toString());
} else if ("exec".equals(runType)) {
m_bsfManager.exec(lang, "BSFMonitor", 0, 0, code);
} else {
LOG.warn("Invalid run-type parameter value '{}' for service '{}'. Only 'eval' and 'exec' are supported.", runType, svc.getSvcName());
throw new RuntimeException("Invalid run-type '" + runType + "'");
}
long endTime = System.currentTimeMillis();
if (!times.containsKey(PollStatus.PROPERTY_RESPONSE_TIME)) {
times.put(PollStatus.PROPERTY_RESPONSE_TIME, endTime - startTime);
}
if (STATUS_UNKNOWN.equals(results.get("status"))) {
pollStatus = PollStatus.unknown(results.get("reason"));
} else if (STATUS_UNRESPONSIVE.equals(results.get("status"))) {
pollStatus = PollStatus.unresponsive(results.get("reason"));
} else if (STATUS_AVAILABLE.equals(results.get("status"))) {
pollStatus = PollStatus.available();
} else if (STATUS_UNAVAILABLE.equals(results.get("status"))) {
pollStatus = PollStatus.unavailable(results.get("reason"));
} else {
// Fall through to the old default of treating any other non-OK
// code as meaning unavailable and also carrying the reason code
pollStatus = PollStatus.unavailable(results.get("status"));
}
LOG.debug("Setting {} times for service '{}'", times.size(), svc.getSvcName());
pollStatus.setProperties(times);
if ("exec".equals(runType) && !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.", fileName, svc.getSvcName());
}
} else {
LOG.warn("Cannot locate or read BSF script file '{}'. Marking service '{}' down.", fileName, svc.getSvcName());
pollStatus = PollStatus.unavailable("Cannot locate or read BSF script file: " + fileName);
}
} catch (BSFException e) {
LOG.warn("BSFMonitor poll for service '{}' failed with BSFException: {}", svc.getSvcName(), e.getMessage(), e);
pollStatus = PollStatus.unavailable(e.getMessage());
} catch (FileNotFoundException e) {
LOG.warn("Could not find BSF script file '{}'. Marking service '{}' down.", fileName, svc.getSvcName());
pollStatus = PollStatus.unavailable("Could not find BSF script file: " + fileName);
} catch (IOException e) {
pollStatus = PollStatus.unavailable(e.getMessage());
LOG.warn("BSFMonitor poll for service '{}' failed with IOException: {}", svc.getSvcName(), e.getMessage(), e);
} catch (Throwable e) {
// Catch any RuntimeException throws
pollStatus = PollStatus.unavailable(e.getMessage());
LOG.warn("BSFMonitor poll for service '{}' failed with unexpected throwable: {}", svc.getSvcName(), e.getMessage(), e);
} finally {
// remove the beans we've declared so the manager is ready for the next poll
undeclareBeans(map);
}
return pollStatus;
}
use of org.apache.bsf.BSFException in project opennms by OpenNMS.
the class BSFNorthbounder method initializeBSFEngine.
/**
* Initialize BSF engine.
*
* @throws Exception the exception
*/
private void initializeBSFEngine() throws Exception {
if (!BSFManager.isLanguageRegistered(m_engine.getLanguage())) {
LOG.debug("registering BSF language {} using {}", m_engine.getLanguage(), m_engine.getClassName());
BSFManager.registerScriptingEngine(m_engine.getLanguage(), m_engine.getClassName(), m_engine.getExtensions().split(","));
}
m_manager.registerBean("log", LOG);
if (m_engine.getOnStart() != null) {
LOG.debug("running start script for BSF engine {}", getName());
try {
m_manager.exec(m_engine.getLanguage(), "", 0, 0, m_engine.getOnStart());
} catch (BSFException e) {
throw new NorthbounderException("Cannot execute start script", e);
}
}
}
Aggregations