Search in sources :

Example 11 with BSFManager

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;
}
Also used : BSFException(org.apache.bsf.BSFException) BSFManager(org.apache.bsf.BSFManager)

Example 12 with BSFManager

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) FileNotFoundException(java.io.FileNotFoundException) BSFManager(org.apache.bsf.BSFManager) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BSFException(org.apache.bsf.BSFException) File(java.io.File)

Example 13 with 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[]
}
Also used : BSFManager(org.apache.bsf.BSFManager) Vector(java.util.Vector) Test(org.junit.Test)

Example 14 with BSFManager

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[]
}
Also used : BSFManager(org.apache.bsf.BSFManager) Vector(java.util.Vector) Test(org.junit.Test)

Example 15 with BSFManager

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[]
}
Also used : BSFManager(org.apache.bsf.BSFManager) Test(org.junit.Test)

Aggregations

BSFManager (org.apache.bsf.BSFManager)21 BSFException (org.apache.bsf.BSFException)8 Test (org.junit.Test)8 BSFEngine (org.apache.bsf.BSFEngine)3 FileInputStream (java.io.FileInputStream)2 List (java.util.List)2 Vector (java.util.Vector)2 SampleResult (org.apache.jmeter.samplers.SampleResult)2 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 StringTokenizer (java.util.StringTokenizer)1 LogPreservingThreadFactory (org.opennms.core.concurrent.LogPreservingThreadFactory)1 Engine (org.opennms.netmgt.config.scriptd.Engine)1 StartScript (org.opennms.netmgt.config.scriptd.StartScript)1