📄 servlettoolboxmanager.java
字号:
package org.apache.velocity.tools.view.servlet;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import org.apache.commons.digester.RuleSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.tools.view.ToolInfo;
import org.apache.velocity.tools.view.XMLToolboxManager;
import org.apache.velocity.tools.view.context.ViewContext;
/**
* <p>A toolbox manager for the servlet environment.</p>
*
* <p>A toolbox manager is responsible for automatically filling the Velocity
* context with a set of view tools. This class provides the following
* features:</p>
* <ul>
* <li>configurable through an XML-based configuration file</li>
* <li>assembles a set of view tools (the toolbox) on request</li>
* <li>handles different tool scopes (request, session, application)</li>
* <li>supports any class with a public constructor without parameters
* to be used as a view tool</li>
* <li>supports adding primitive data values to the context(String,Number,Boolean)</li>
* </ul>
*
*
* <p><strong>Configuration</strong></p>
* <p>The toolbox manager is configured through an XML-based configuration
* file. The configuration file is passed to the {@link #load(java.io.InputStream input)}
* method. The format is shown in the following example:</p>
* <pre>
* <?xml version="1.0"?>
*
* <toolbox>
* <tool>
* <key>link</key>
* <scope>request</scope>
* <class>org.apache.velocity.tools.view.tools.LinkTool</class>
* </tool>
* <tool>
* <key>date</key>
* <scope>application</scope>
* <class>org.apache.velocity.tools.generic.DateTool</class>
* </tool>
* <data type="number">
* <key>luckynumber</key>
* <value>1.37</value>
* </data>
* <data type="string">
* <key>greeting</key>
* <value>Hello World!</value>
* </data>
* <xhtml>true</xhtml>
* </toolbox>
* </pre>
* <p>The recommended location for the configuration file is the WEB-INF directory of the
* web application.</p>
*
* @author <a href="mailto:sidler@teamup.com">Gabriel Sidler</a>
* @author Nathan Bubna
* @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
* @author <a href="mailto:henning@schmiedehausen.org">Henning P. Schmiedehausen</a>
* @version $Id: ServletToolboxManager.java 488460 2006-12-19 00:00:35Z nbubna $
*/
public class ServletToolboxManager extends XMLToolboxManager
{
// --------------------------------------------------- Properties ---------
public static final String SESSION_TOOLS_KEY =
ServletToolboxManager.class.getName() + ":session-tools";
protected static final Log LOG = LogFactory.getLog(ServletToolboxManager.class);
private ServletContext servletContext;
private Map appTools;
private ArrayList sessionToolInfo;
private ArrayList requestToolInfo;
private boolean createSession;
private static HashMap managersMap = new HashMap();
private static RuleSet servletRuleSet = new ServletToolboxRuleSet();
// --------------------------------------------------- Constructor --------
/**
* Use getInstance(ServletContext,String) instead
* to ensure there is exactly one ServletToolboxManager
* per xml toolbox configuration file.
*/
private ServletToolboxManager(ServletContext servletContext)
{
this.servletContext = servletContext;
appTools = new HashMap();
sessionToolInfo = new ArrayList();
requestToolInfo = new ArrayList();
createSession = true;
}
// -------------------------------------------- Public Methods ------------
/**
* ServletToolboxManager factory method.
* This method will ensure there is exactly one ServletToolboxManager
* per xml toolbox configuration file.
*/
public static synchronized ServletToolboxManager getInstance(ServletContext servletContext,
String toolboxFile)
{
// little fix up
if (!toolboxFile.startsWith("/"))
{
toolboxFile = "/" + toolboxFile;
}
// get the unique key for this toolbox file in this servlet context
String uniqueKey = servletContext.hashCode() + ':' + toolboxFile;
// check if an instance already exists
ServletToolboxManager toolboxManager =
(ServletToolboxManager)managersMap.get(uniqueKey);
if (toolboxManager == null)
{
// if not, build one
InputStream is = null;
try
{
// get the bits
is = servletContext.getResourceAsStream(toolboxFile);
if (is != null)
{
LOG.info("Using config file '" + toolboxFile + "'");
toolboxManager = new ServletToolboxManager(servletContext);
toolboxManager.load(is);
// remember it
managersMap.put(uniqueKey, toolboxManager);
LOG.debug("Toolbox setup complete.");
}
else
{
LOG.debug("No toolbox was found at '" + toolboxFile + "'");
}
}
catch(Exception e)
{
LOG.error("Problem loading toolbox '" + toolboxFile + "'", e);
}
finally
{
try
{
if (is != null)
{
is.close();
}
}
catch(Exception ee) {}
}
}
return toolboxManager;
}
/**
* <p>Sets whether or not to create a new session when none exists for the
* current request and session-scoped tools have been defined for this
* toolbox.</p>
*
* <p>If true, then a call to {@link #getToolbox(Object)} will
* create a new session if none currently exists for this request and
* the toolbox has one or more session-scoped tools designed.</p>
*
* <p>If false, then a call to getToolbox(Object) will never
* create a new session for the current request.
* This effectively means that no session-scoped tools will be added to
* the ToolboxContext for a request that does not have a session object.
* </p>
*
* The default value is true.
*/
public void setCreateSession(boolean b)
{
createSession = b;
LOG.debug("create-session is set to " + b);
}
/**
* <p>Sets an application attribute to tell velocimacros and tools
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -