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

📄 servlettoolboxmanager.java

📁 一个用于java web页面开发的开源包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * (especially the LinkTool) whether they should output XHTML or HTML.</p>
     *
     * @see ViewContext#XHTML
     * @since VelocityTools 1.1
     */
    public void setXhtml(Boolean value)
    {
        servletContext.setAttribute(ViewContext.XHTML, value);
        LOG.info(ViewContext.XHTML + " is set to " + value);
    }


    // ------------------------------ XMLToolboxManager Overrides -------------

    /**
     * <p>Retrieves the rule set Digester should use to parse and load
     * the toolbox for this manager.</p>
     *
     * <p>The DTD corresponding to the ServletToolboxRuleSet is:
     * <pre>
     *  &lt;?xml version="1.0"?&gt;
     *  &lt;!ELEMENT toolbox (create-session?,xhtml?,tool*,data*,#PCDATA)&gt;
     *  &lt;!ELEMENT create-session (#CDATA)&gt;
     *  &lt;!ELEMENT xhtml          (#CDATA)&gt;
     *  &lt;!ELEMENT tool           (key,scope?,class,parameter*,#PCDATA)&gt;
     *  &lt;!ELEMENT data           (key,value)&gt;
     *      &lt;!ATTLIST data type (string|number|boolean) "string"&gt;
     *  &lt;!ELEMENT key            (#CDATA)&gt;
     *  &lt;!ELEMENT scope          (#CDATA)&gt;
     *  &lt;!ELEMENT class          (#CDATA)&gt;
     *  &lt;!ELEMENT parameter (EMPTY)&gt;
     *      &lt;!ATTLIST parameter name CDATA #REQUIRED&gt;
     *      &lt;!ATTLIST parameter value CDATA #REQUIRED&gt;
     *  &lt;!ELEMENT value          (#CDATA)&gt;
     * </pre></p>
     *
     * @since VelocityTools 1.1
     */
    protected RuleSet getRuleSet()
    {
        return servletRuleSet;
    }

    /**
     * Ensures that application-scoped tools do not have request path
     * restrictions set for them, as those will not be enforced.
     *
     * @param info a ToolInfo object
     * @return true if the ToolInfo is valid
     * @since VelocityTools 1.3
     */
    protected boolean validateToolInfo(ToolInfo info)
    {
        if (!super.validateToolInfo(info))
        {
            return false;
        }
        if (info instanceof ServletToolInfo)
        {
            ServletToolInfo sti = (ServletToolInfo)info;
            if (sti.getRequestPath() != null &&
                !ViewContext.REQUEST.equalsIgnoreCase(sti.getScope()))
            {
                LOG.error(sti.getKey() + " must be a request-scoped tool to have a request path restriction!");
                return false;
            }
        }
        return true;
    }


    /**
     * Overrides XMLToolboxManager to separate tools by scope.
     * For this to work, we obviously override getToolbox(Object) as well.
     */
    public void addTool(ToolInfo info)
    {
        if (validateToolInfo(info))
        {
            if (info instanceof ServletToolInfo)
            {
                ServletToolInfo sti = (ServletToolInfo)info;

                if (ViewContext.REQUEST.equalsIgnoreCase(sti.getScope()))
                {
                    requestToolInfo.add(sti);
                    return;
                }
                else if (ViewContext.SESSION.equalsIgnoreCase(sti.getScope()))
                {
                    sessionToolInfo.add(sti);
                    return;
                }
                else if (ViewContext.APPLICATION.equalsIgnoreCase(sti.getScope()))
                {
                    /* add application scoped tools to appTools and
                     * initialize them with the ServletContext */
                    appTools.put(sti.getKey(), sti.getInstance(servletContext));
                    return;
                }
                else
                {
                    LOG.warn("Unknown scope '" + sti.getScope() + "' - " +
                            sti.getKey() + " will be request scoped.");

                    //default is request scope
                    requestToolInfo.add(info);
                }
            }
            else
            {
                //default is request scope
                requestToolInfo.add(info);
            }
        }
    }

    /**
     * Overrides XMLToolboxManager to put data into appTools map
     */
    public void addData(ToolInfo info)
    {
        if (validateToolInfo(info))
        {
            appTools.put(info.getKey(), info.getInstance(null));
        }
    }

    /**
     * Overrides XMLToolboxManager to handle the separate
     * scopes.
     *
     * Application scope tools were initialized when the toolbox was loaded.
     * Session scope tools are initialized once per session and stored in a
     * map in the session attributes.
     * Request scope tools are initialized on every request.
     *
     * @param initData the {@link ViewContext} for the current servlet request
     */
    public Map getToolbox(Object initData)
    {
        //we know the initData is a ViewContext
        ViewContext ctx = (ViewContext)initData;
        String requestPath = ServletUtils.getPath(ctx.getRequest());

        //create the toolbox map with the application tools in it
        Map toolbox = new HashMap(appTools);

        if (!sessionToolInfo.isEmpty())
        {
            HttpSession session = ctx.getRequest().getSession(createSession);
            if (session != null)
            {
                // allow only one thread per session at a time
                synchronized(getMutex(session))
                {
                    // get the session tools
                    Map stmap = (Map)session.getAttribute(SESSION_TOOLS_KEY);
                    if (stmap == null)
                    {
                        // init and store session tools map
                        stmap = new HashMap(sessionToolInfo.size());
                        Iterator i = sessionToolInfo.iterator();
                        while(i.hasNext())
                        {
                            ServletToolInfo sti = (ServletToolInfo)i.next();
                            stmap.put(sti.getKey(), sti.getInstance(ctx));
                        }
                        session.setAttribute(SESSION_TOOLS_KEY, stmap);
                    }
                    // add them to the toolbox
                    toolbox.putAll(stmap);
                }
            }
        }

        //add and initialize request tools
        Iterator i = requestToolInfo.iterator();
        while(i.hasNext())
        {
            ToolInfo info = (ToolInfo)i.next();
            if (info instanceof ServletToolInfo)
            {
                ServletToolInfo sti = (ServletToolInfo)info;
                if (!sti.allowsRequestPath(requestPath))
                {
                    continue;
                }
            }
            toolbox.put(info.getKey(), info.getInstance(ctx));
        }

        return toolbox;
    }


    /**
     * Returns a mutex (lock object) unique to the specified session
     * to allow for reliable synchronization on the session.
     */
    protected Object getMutex(HttpSession session)
    {
        // yes, this uses double-checked locking, but it is safe here
        // since partial initialization of the lock is not an issue
        Object lock = session.getAttribute("session.mutex");
        if (lock == null)
        {
            // one thread per toolbox manager at a time
            synchronized(this)
            {
                // in case another thread already came thru
                lock = session.getAttribute("session.mutex");
                if (lock == null)
                {
                    // use a Boolean because it is serializable and small
                    lock = new Boolean(true);
                    session.setAttribute("session.mutex", lock);
                }
            }
        }
        return lock;
    }

}

⌨️ 快捷键说明

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