Examples of BSFManager


Examples of org.apache.bsf.BSFManager

        }

        String location = this.getLocation(modelService);

        // create the manager object and set the classloader
        BSFManager mgr = new BSFManager();
        mgr.setClassLoader(cl);

        mgr.registerBean("dctx", dctx);
        mgr.registerBean("context", context);

        // pre-load the engine to make sure we were called right
        org.apache.bsf.BSFEngine bsfEngine = null;
        try {
            bsfEngine = mgr.loadScriptingEngine(modelService.engineName);
        } catch (BSFException e) {
            throw new GenericServiceException("Problems loading org.apache.bsf.BSFEngine: " + modelService.engineName, e);
        }

        // source the script into a string
        String script = scriptCache.get(localName + "_" + location);

        if (script == null) {
            synchronized (this) {
                script = scriptCache.get(localName + "_" + location);
                if (script == null) {
                    URL scriptUrl = UtilURL.fromResource(location, cl);

                    if (scriptUrl != null) {
                        try {
                            HttpClient http = new HttpClient(scriptUrl);
                            script = http.get();
                        } catch (HttpClientException e) {
                            throw new GenericServiceException("Cannot read script from resource", e);
                        }
                    } else {
                        throw new GenericServiceException("Cannot read script, resource [" + location + "] not found");
                    }
                    if (script == null || script.length() < 2) {
                        throw new GenericServiceException("Null or empty script");
                    }
                    scriptCache.put(localName + "_" + location, script);
                }
            }
        }

        // now invoke the script
        try {
            bsfEngine.exec(location, 0, 0, script);
        } catch (BSFException e) {
            throw new GenericServiceException("Script invocation error", e);
        }

        return mgr.lookupBean("response");
    }
View Full Code Here

Examples of org.apache.bsf.BSFManager

   
    public void setUp() throws Exception {
        super.setUp();
        BSFManager.registerScriptingEngine("ruby", "org.jruby.javasupport.bsf.JRubyEngine", new String[] { "rb" });
       
        manager = new BSFManager();
        String expression = loadScript(RUBY_SCRIPT);
        assertNotNull("Script loaded from " + RUBY_SCRIPT + " should exist", expression);
        manager.exec("ruby", "(java)", 1, 1, expression);
    }
View Full Code Here

Examples of org.apache.bsf.BSFManager

    protected void setUp() throws Exception {
        LOGGER.log(Level.FINEST, SCRIPT);
        BSFManager.registerScriptingEngine("ruby",
                "org.jruby.javasupport.bsf.JRubyEngine",
                new String[] { "rb" });
        manager_ = new BSFManager();
        manager_.exec("ruby", "(java)", 1, 1, SCRIPT);
    }
View Full Code Here

Examples of org.apache.bsf.BSFManager

    public BSFTestElement() {
        super();
    }

    protected BSFManager getManager() throws BSFException {
        BSFManager mgr = new BSFManager();
        initManager(mgr);
        return mgr;
    }
View Full Code Here

Examples of org.apache.bsf.BSFManager

        res.setSampleLabel(label);
        InputStream 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 BufferedInputStream(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

Examples of org.apache.bsf.BSFManager

    private static final long serialVersionUID = 234L;

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

Examples of org.apache.bsf.BSFManager

    private static final long serialVersionUID = 232L;

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

Examples of org.apache.bsf.BSFManager

    private static final long serialVersionUID = 232L;

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

Examples of org.apache.bsf.BSFManager

    /** {@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 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

Examples of org.apache.bsf.BSFManager

    private static final long serialVersionUID = 234L;

    @Override
    public void sampleOccurred(SampleEvent event) {
        BSFManager mgr =null;
        try {
            mgr = getManager();
            if (mgr == null) {
                log.error("Problem creating BSF manager");
                return;
            }
            mgr.declareBean("sampleEvent", event, SampleEvent.class);
            SampleResult result = event.getResult();
            mgr.declareBean("sampleResult", result, SampleResult.class);
            processFileOrScript(mgr);
        } catch (BSFException e) {
            log.warn("Problem in BSF script "+e);
        } finally {
            if (mgr != null) {
                mgr.terminate();
            }
        }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.