📄 servlettoolboxmanager.java
字号:
* (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>
* <?xml version="1.0"?>
* <!ELEMENT toolbox (create-session?,xhtml?,tool*,data*,#PCDATA)>
* <!ELEMENT create-session (#CDATA)>
* <!ELEMENT xhtml (#CDATA)>
* <!ELEMENT tool (key,scope?,class,parameter*,#PCDATA)>
* <!ELEMENT data (key,value)>
* <!ATTLIST data type (string|number|boolean) "string">
* <!ELEMENT key (#CDATA)>
* <!ELEMENT scope (#CDATA)>
* <!ELEMENT class (#CDATA)>
* <!ELEMENT parameter (EMPTY)>
* <!ATTLIST parameter name CDATA #REQUIRED>
* <!ATTLIST parameter value CDATA #REQUIRED>
* <!ELEMENT value (#CDATA)>
* </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 + -