Package org.apache.bsf

Examples of org.apache.bsf.BSFManager


    /** {@inheritDoc} */
    public long delay() {
        long delay = 0;
        try {
            BSFManager mgr = getManager();
            Object o = evalFileOrScript(mgr);
            if (o == null) {
                log.warn("Script did not return a value");
                return 0;
            }
View Full Code Here


  public static void init()
  {
    try
    {
      // Initialize the engine for loading Jython scripts
      _bsf = new BSFManager();
      // Execution of all the scripts placed in data/jscript
      // inside the DataPack directory

      String dataPackDirForwardSlashes = Config.DATAPACK_ROOT.getPath().replaceAll("\\\\","/");
      String loadingScript =
View Full Code Here

        }
    }

    public void parsePackages()
    {
        BSFManager context = new BSFManager();
        try
        {
            context.eval("beanshell", "core", 0, 0, "double log1p(double d) { return Math.log1p(d); }");
            context.eval("beanshell", "core", 0, 0,
                         "double pow(double d, double p) { return Math.pow(d,p); }");

            for (ScriptDocument script : _scripts)
            {
                parseScript(script, context);
View Full Code Here

    public static Object eval(String lang, String code)
    {
        try
        {
            return new BSFManager().eval(lang, "eval", 0, 0, code);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
View Full Code Here

            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
             throws Exception {

        BSFManager bsfManager = new BSFManager();

        String scriptName = null;
        try {
            scriptName = parseScriptName(mapping.getParameter(), bsfManager);
        } catch (Exception ex) {
            LOG.error("Unable to parse " + mapping.getParameter(), ex);
            throw new Exception("Unable to parse " + mapping.getParameter());
        }
        if (scriptName == null) {
            LOG.error("No script specified in the parameter attribute");
            throw new Exception("No script specified");
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("Executing script: " + scriptName);
        }

        HttpSession session = request.getSession();
        ServletContext application = getServlet().getServletContext();

        Script script = loadScript(scriptName, application);

        bsfManager.declareBean("request", request,
                HttpServletRequest.class);

        bsfManager.declareBean("response", response,
                HttpServletResponse.class);

        if (session == null) {
            LOG.debug("HTTP session is null");
        } else {
            bsfManager.declareBean("session", session, HttpSession.class);
        }

        bsfManager.declareBean("application", application,
                ServletContext.class);

        bsfManager.declareBean("log", LOG, Log.class);
        StrutsInfo struts = new StrutsInfo(this, mapping, form,
                getResources(request));
        bsfManager.declareBean("struts", struts, StrutsInfo.class);

        for (int x = 0; x < filters.length; x++) {
            filters[x].apply(bsfManager);
        }

        bsfManager.exec(script.lang, script.file.getCanonicalPath(), 0, 0,
                script.string);

        ActionForward af = struts.getForward();
        return af;
    }
View Full Code Here

            if (useDynamicClasses && clazz != null)
            {
                return clazz.newInstance();
            }

            BSFManager bsfman = new BSFManager();

            try
            {
                WebContext context = WebContextFactory.get();
                bsfman.declareBean("context", context, context.getClass());
            }
            catch (BSFException ex)
            {
                log.warn("Failed to register WebContext with scripting engine: " + ex.getMessage());
            }
            String languageToUse = language != null ? language : BSFManager.getLangFromFilename(scriptPath);
            return bsfman.eval(languageToUse, (null == scriptPath ? "dwr.xml" : scriptPath), 0, 0, getScript());
        }
        catch (Exception ex)
        {
            throw new IllegalArgumentException("Failed to getInstance", ex);
        }
View Full Code Here

            throw createBuildException(be);
        }
    }

    private BSFManager prepareManager() throws BSFException {
        final BSFManager manager = new BSFManager();
        for (Iterator i = fBeans.keySet().iterator(); i.hasNext();) {
            final String key = (String) i.next();
            final Object value = fBeans.get(key);
            if (value != null) {
                manager.declareBean(key, value, value.getClass());
            } else {
                // BSF uses a hashtable to store values
                // so cannot declareBean with a null value
                // So need to remove any bean of this name as
                // that bean should not be visible
                manager.undeclareBean(key);
            }
        }
        return manager;
    }
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 (UtilValidate.isEmpty(event.path)) {
                // 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 discardFromPool(Object object)
    {
        BSFManager manager = (BSFManager)object;
       
        manager.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.