Package org.apache.bsf

Examples of org.apache.bsf.BSFManager


   *
   * @return the interpreter or null, if there was an error.
   */
  protected BSFManager createInterpreter() throws BSFException
  {
    final BSFManager interpreter = new BSFManager();
    initializeInterpreter(interpreter);
    return interpreter;
  }
View Full Code Here


    final WrapperExpressionRuntime runtimeWrapper = new WrapperExpressionRuntime();
    runtimeWrapper.update(null, getRuntime());
    legacyDataRowWrapper.setParent(getDataRow());
    try
    {
      final BSFManager interpreter = new BSFManager();
      interpreter.declareBean("chartExpression", this, getClass()); //$NON-NLS-1$
      interpreter.declareBean("chart", originalChart, JFreeChart.class); //$NON-NLS-1$
      interpreter.declareBean("runtime", runtimeWrapper, ExpressionRuntime.class); //$NON-NLS-1$
      interpreter.declareBean("dataRow", legacyDataRowWrapper, DataRow.class); //$NON-NLS-1$
      final Object o = interpreter.eval
          (postProcessingLanguage, "expression", 1, 1, postProcessingScript); //$NON-NLS-1$
      if (o instanceof JFreeChart)
      {
        return (JFreeChart) o;
      }
View Full Code Here

    }
  }

  public Message process(Message message) throws ActionProcessingException
  {
    BSFManager bsf = new BSFManager();
    try
    {
      bsf.declareBean( "message", message, message.getClass() );
      bsf.declareBean( "config", config, config.getClass() );
      bsf.declareBean( "payloadProxy", payloadProxy, payloadProxy.getClass() );
      bsf.declareBean( "logger", logger, logger.getClass() );
      // NOTE: cannot use eval here since it does not work for all engines (jython in particular)
      bsf.exec( language, source, 0, 0, getScript(message) );
    }
    catch (BSFException bsfe)
    {
      final String error = "Exception caught while processing script: '" + source + "'" ;
      if (logger.isDebugEnabled())
      {
        logger.debug(error, bsfe) ;
      }
      throw new ActionProcessingException(error, bsfe);
    }
    finally
    {
      bsf.terminate();
    }
    return message;
  }
View Full Code Here

            throw new EventHandlerException("Problem getting ServletContext");
        }

        try {
            // create the BSF manager
            BSFManager bsfManager = new BSFManager();
            bsfManager.setClassLoader(cl);

            // expose the event objects to the script
            bsfManager.declareBean("request", request, HttpServletRequest.class);
            bsfManager.declareBean("response", response, HttpServletResponse.class);

            // get the script type
            String scriptType = BSFManager.getLangFromFilename(event.invoke);

            // load the script
            InputStream scriptStream = null;
            String scriptString = null;
            String cacheName = null;
            if (event.path == null || event.path.length() == 0) {
                // we are a resource to be loaded off the classpath
                cacheName = event.invoke;
                scriptString = eventCache.get(cacheName);
                if (scriptString == null) {
                    synchronized(eventCache) {
                        if (scriptString == null) {
                            if (Debug.verboseOn()) {
                                Debug.logVerbose("Loading BSF Script at location: " + cacheName, module);
                            }
                            URL scriptUrl = FlexibleLocation.resolveLocation(cacheName);
              if (scriptUrl == null) {
                throw new EventHandlerException("BSF script not found at location [" + cacheName + "]");
              }
                            scriptStream = scriptUrl.openStream();
              scriptString = IOUtils.getStringFromReader(new InputStreamReader(scriptStream));
                            scriptStream.close();
              eventCache.put(cacheName, scriptString);
                        }
                    }
                }
            } else {
                // we are a script in the webapp - load by resource
                cacheName = context.getServletContextName() + ":" + event.path + event.invoke;
                scriptString = eventCache.get(cacheName);
                if (scriptString == null) {
                    synchronized(eventCache) {
                        if (scriptString == null) {
                            scriptStream = context.getResourceAsStream(event.path + event.invoke);
                            if (scriptStream == null) {
                                throw new EventHandlerException("Could not find BSF script file in webapp context: " + event.path + event.invoke);
                            }
                            scriptString = IOUtils.getStringFromReader(new InputStreamReader(scriptStream));
                            scriptStream.close();
                            eventCache.put(cacheName, scriptString);
                        }
                    }
                }
            }

            // execute the script
            Object result = bsfManager.eval(scriptType, cacheName, 0, 0, scriptString);

            // check the result
            if (result != null && !(result instanceof String)) {
                throw new EventHandlerException("Event did not return a String result, it returned a " + result.getClass().getName());
            }
View Full Code Here

        }

        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

   
    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

    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

    public BSFTestElement() {
        super();
    }

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

        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

    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

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.