Package org.apache.bsf

Examples of org.apache.bsf.BSFManager


    private static final long serialVersionUID = 4;

    /** {@inheritDoc} */
    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 e) {
            log.warn("Problem in BSF script "+e);
        } catch (BSFException e) {
            log.warn("Problem in BSF script "+e);
        } finally {
            if(mgr != null) {
                mgr.terminate();
            }
        }
        return delay;
    }
View Full Code Here


        res.setSampleLabel(label);
        FileInputStream is = null;
        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.
        res.setResponseCode("200"); // $NON-NLS-1$
        res.setResponseMessage("OK"); // $NON-NLS-1$
        res.setSuccessful(true);
        res.setDataType(SampleResult.TEXT); // Default (can be overridden by the script)

        res.sampleStart();
        try {
            initManager(mgr);
            mgr.declareBean("SampleResult", res, res.getClass()); // $NON-NLS-1$

            // These are not useful yet, as have not found how to get updated values back
            //mgr.declareBean("ResponseCode", "200", String.class); // $NON-NLS-1$
            //mgr.declareBean("ResponseMessage", "OK", String.class); // $NON-NLS-1$
            //mgr.declareBean("IsSuccess", Boolean.TRUE, Boolean.class); // $NON-NLS-1$

            // 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);
                is = new FileInputStream(fileName);
                bsfOut = bsfEngine.eval(fileName, 0, 0, IOUtils.toString(is));
            } 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);
            res.setResponseCode("500"); // $NON-NLS-1$
            res.setResponseMessage(ex.toString());
        } catch (Exception ex) {// Catch evaluation errors
            log.warn("Problem evaluating the script", ex);
            res.setSuccessful(false);
            res.setResponseCode("500"); // $NON-NLS-1$
            res.setResponseMessage(ex.toString());
        } finally {
            res.sampleEnd();
            IOUtils.closeQuietly(is);
// Will be done by mgr.terminate() anyway
//          if (bsfEngine != null) {
//              bsfEngine.terminate();
//          }
            mgr.terminate();
        }

        return res;
    }
View Full Code Here

    private static final long serialVersionUID = 232L;

    public void process(){
        try {
            BSFManager mgr = getManager();
            processFileOrScript(mgr);
            mgr.terminate();
        } catch (BSFException e) {
            log.warn("Problem in BSF script "+e);
        }
    }
View Full Code Here

    private static final long serialVersionUID = 234L;

    public AssertionResult getResult(SampleResult response) {
        AssertionResult result = new AssertionResult(getName());
        try {
            BSFManager mgr = getManager();
            mgr.declareBean("SampleResult", response, SampleResult.class);
            mgr.declareBean("AssertionResult", result, AssertionResult.class);
            processFileOrScript(mgr);
            mgr.terminate();
            result.setError(false);
        } catch (BSFException e) {
            log.warn("Problem in BSF script "+e);
            result.setFailure(true);
            result.setError(true);
View Full Code Here

public class BSFTest extends TestCase {

    protected BSFManager manager;

    protected void setUp() throws Exception {
        manager = new BSFManager();
    }
View Full Code Here

    private static final Class CACHING_ENGINE = CachingGroovyEngine.class;

    protected void setUp() throws Exception {
        // override standard engine with caching one
        BSFManager.registerScriptingEngine("groovy", CACHING_ENGINE.getName(), new String[]{"groovy", "gy"});
        manager = new BSFManager();
    }
View Full Code Here

     * Test of terminate method, of class JRubyEngine.
     */
    @Test
    public void testTerminate() throws BSFException {
        logger1.info("terminate");
        BSFManager manager = new BSFManager();
        JRubyEngine instance = new JRubyEngine();
        instance.initialize(manager, "jruby", null);
        instance.terminate();
    }
View Full Code Here

    @Test
    public void testPathTyp() throws BSFException {
        logger1.info("PathType");
        BSFManager.registerScriptingEngine("jruby", "org.jruby.embed.bsf.JRubyEngine", new String[] {"rb"});
        BSFManager manager = new BSFManager();
        JRubyEngine instance = (JRubyEngine) manager.loadScriptingEngine("jruby");
        Object receiver = instance.eval("org/jruby/embed/ruby/radioactive_decay.rb", 0, 0, PathType.CLASSPATH);
        String method = "amount_after_years";
        Object[] args = new Object[2];
        args[0] = 10.0; args[1] = 1000;

        // Plutonium
        manager.declareBean("h", 24100, Long.class);
        Object result = instance.call(receiver, method, args);
        assertEquals(9.716, (Double)result, 0.001);
    }
View Full Code Here

    @Test
    public void testRubyVersion() throws BSFException {
        logger1.info("RubyVersion");
        BSFManager.registerScriptingEngine("jruby", "org.jruby.embed.bsf.JRubyEngine", new String[] {"rb"});
        BSFManager manager = new BSFManager();
        JRubyEngine instance = (JRubyEngine) manager.loadScriptingEngine("jruby");
        Object result = instance.eval("org/jruby/embed/ruby/block-param-scope.rb", 0, 0, PathType.CLASSPATH);
        String expResult = "cat";
        assertEquals(expResult, ((String)result).trim());

        // Ruby 1.9 mode is somehow broken in 1.5.0dev
        BSFManager.registerScriptingEngine("jruby19", "org.jruby.embed.bsf.JRubyEngine", new String[] {"rb"});
        instance = (JRubyEngine) manager.loadScriptingEngine("jruby19");
        result = instance.eval("org/jruby/embed/ruby/block-param-scope.rb", 0, 0, PathType.CLASSPATH);
        expResult = "bear";
        assertEquals(expResult, ((String)result).trim());
    }
View Full Code Here

    @Test
    public void testLVar() throws BSFException {
        BSFManager.registerScriptingEngine("jruby",
                "org.jruby.embed.bsf.JRubyEngine", new String[]{"rb"});
        BSFManager bsf = new BSFManager();
        bsf.eval("jruby", "(java)", 1, 1, "$x='GVar'");
        bsf.eval("jruby", "(java)", 1, 1, "@gvar = \"$x = #{$x}\"");

        bsf.eval("jruby", "(java)", 1, 1, "x='LVar'");
        bsf.eval("jruby", "(java)", 1, 1, "at_exit { @result =  \"#{x} and #{$x} in an at_exit block\" }");
        Object ret = bsf.eval("jruby", "(java)", 1, 1, "@lvar = \"x = #{x}\";return @gvar, @lvar");
        List<String> expResult = Arrays.asList("$x = GVar", "x = LVar");
        assertEquals(expResult, ret);
        logger1.info(ret.toString());
        bsf.terminate();
    }
View Full Code Here

TOP

Related Classes of org.apache.bsf.BSFManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.