Search in sources :

Example 6 with BSFManager

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

Example 7 with BSFManager

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

Example 8 with BSFManager

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

Example 9 with 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;
}
Also used : BSFEngine(org.apache.bsf.BSFEngine) BSFException(org.apache.bsf.BSFException) BufferedInputStream(java.io.BufferedInputStream) SampleResult(org.apache.jmeter.samplers.SampleResult) BSFManager(org.apache.bsf.BSFManager) FileInputStream(java.io.FileInputStream) BSFException(org.apache.bsf.BSFException)

Example 10 with BSFManager

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

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