⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scriptaction.java

📁 structs源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     *
     *@param  url            The script url consisting of a path and optional
     *      parameters
     *@param  manager        The BSF manager to declare new parameters in
     *@return                The name of the script to execute
     *@exception  Exception  If something goes wrong
     */
    protected String parseScriptName(String url, BSFManager manager)
             throws Exception {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Parsing " + url);
        }
        String name = null;
        if (url != null) {
            String[] parsed = split(url, "?");
            name = parsed[0];
            if (parsed.length == 2) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Found a query string");
                }
                String[] args = split(parsed[1], "&");
                for (int x = 0; x < args.length; x++) {
                    String[] param = split(args[x], "=");
                    Object o = manager.lookupBean(param[0]);
                    if (o != null) {
                        LOG.warn("BSF variable " + param[0]
                                 + " already exists");
                        param[0] = "_" + param[0];
                    }
                    manager.declareBean(param[0], param[1], String.class);
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Registering param " + param[0]
                                 + " with value " + param[1]);
                    }
                }
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("No query string:" + parsed.length);
                }
            }
        }
        return name;
    }


    /**
     *  Loads the script from cache if possible. Reloads if the script has been
     *  recently modified.
     *
     *@param  name     The name of the script
     *@param  context  The servlet context
     *@return          The script object
     */
    protected Script loadScript(String name, ServletContext context) {

        Script script = (Script) scripts.get(name);
        if (script == null) {
            script = new Script();
            script.file = new File(context.getRealPath(name));
            try {
                script.lang =
                        BSFManager.getLangFromFilename(script.file.getName());
            } catch (BSFException ex) {
                LOG.warn(ex, ex);
            }
        }

        boolean reloadScript = false;
        long scriptLastModified = script.file.lastModified();
        if (scriptLastModified > script.timeLastLoaded) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Loading updated or new script: "
                        + script.file.getName());
            }
            reloadScript = true;
        }

        if (reloadScript || script.string == null) {
            synchronized (this) {
                script.timeLastLoaded = System.currentTimeMillis();
                FileReader reader = null;
                try {
                    reader = new FileReader(script.file);
                    script.string = IOUtils.getStringFromReader(reader);
                } catch (IOException ex) {
                    LOG.error("Unable to load script: " + script.file, ex);
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException ex) {
                            LOG.debug(ex, ex);
                        }
                    }
                }
            }
        }

        return script;
    }


    /**
     *  Loads and initializes the filters.
     *
     *@param  props  The properties defining the filters
     *@return        An array of the loaded filters
     */
    protected static BSFManagerFilter[] loadFilters(Properties props) {
        ArrayList list = new ArrayList();
        for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
            String prop = (String) e.nextElement();
            if (prop.startsWith(FILTERS_BASE) && prop.endsWith("class")) {
                String type = prop.substring(FILTERS_BASE.length(),
                        prop.indexOf(".", FILTERS_BASE.length()));
                String claz = props.getProperty(prop);
                try {
                    Class cls = Class.forName(claz);
                    BSFManagerFilter f = (BSFManagerFilter) cls.newInstance();
                    f.init(type, props);
                    list.add(f);
                    if (LOG.isInfoEnabled()) {
                        LOG.info("Loaded " + type + " filter: " + claz);
                    }
                } catch (Exception ex) {
                    LOG.error("Unable to load " + type + " filter: " + claz);
                }
            }
        }
        BSFManagerFilter[] filters = new BSFManagerFilter[list.size()];
        filters = (BSFManagerFilter[]) list.toArray(filters);
        return filters;
    }


    /**
     *  Splits a line with the given delimiter.
     *
     *@param  line       The line to split
     *@param  delimiter  The string to split with
     *@return            An array of substrings
     */
    protected static String[] split(String line, String delimiter) {
        if (line == null || "".equals(line)) {
            return new String[]{};
        }

        List lst = new ArrayList();
        for (Enumeration e = new StringTokenizer(line, delimiter);
            e.hasMoreElements();) {
            lst.add(e.nextElement());
        }
        String[] ret = new String[lst.size()];
        return (String[]) lst.toArray(ret);
    }


    // These methods seem necessary as some scripting engines are not able to
    // access Action's protected methods.  Ugly? yes... any suggestions?

    /**
     *  Saves a token.
     *
     *@param  req  The request object
     */
    public void saveToken(HttpServletRequest req) {
        super.saveToken(req);
    }


    /**
     *  Checks to see if the request is cancelled.
     *
     *@param  req  The request object
     *@return      True if cancelled
     */
    public boolean isCancelled(HttpServletRequest req) {
        return super.isCancelled(req);
    }


    /**
     *  Checks to see if the token is valid.
     *
     *@param  req  The request object
     *@return      True if valid
     */
    public boolean isTokenValid(HttpServletRequest req) {
        return super.isTokenValid(req);
    }


    /**
     *  Resets the token.
     *
     *@param  req  The request object
     */
    public void resetToken(HttpServletRequest req) {
        super.resetToken(req);
    }


    /**
     *  Gets the locale.
     *
     *@param  req  The request object
     *@return      The locale value
     */
    public Locale getLocale(HttpServletRequest req) {
        return super.getLocale(req);
    }


    /**
     *  Saves the messages to the request.
     *
     *@param  req  The request object
     *@param  mes  The action messages
     */
    public void saveMessages(HttpServletRequest req, ActionMessages mes) {
        super.saveMessages(req, mes);
    }


    /**
     *  Saves the errors to the request.
     *
     *@param  req    The request object
     *@param  errs   The action errors
     *@deprecated    Use saveErrors(HttpServletRequest, ActionMessages) instead.
     *      This will be removed after Struts 1.2.
     */
    public void saveErrors(HttpServletRequest req, ActionErrors errs) {
        super.saveErrors(req, errs);
    }


    /**  Represents a saved script. */
    class Script {

        /**  The script file. */
        public File file;

        /**  The language the script is in. */
        public String lang = null;

        /**  The time when the script was last used. */
        public long timeLastLoaded = 0;

        /**  The contents of the script file. */
        public String string = null;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -