📄 applicationcontext.java
字号:
* @param exception Exception to be reported
* @param message Message to be written
*
* @deprecated As of Java Servlet API 2.1, use
* <code>log(String, Throwable)</code> instead
*/
public void log(Exception exception, String message) {
Logger logger = context.getLogger();
if (logger != null)
logger.log(exception, context.logName() + message);
}
/**
* Writes the specified message and exception to a servlet log file.
*
* @param message Message to be written
* @param throwable Exception to be reported
*/
public void log(String message, Throwable throwable) {
Logger logger = context.getLogger();
if (logger != null)
logger.log(context.logName() + message, throwable);
}
/**
* Remove the context attribute with the specified name, if any.
*
* @param name Name of the context attribute to be removed
*/
public void removeAttribute(String name) {
Object value = null;
boolean found = false;
// Remove the specified attribute
synchronized (attributes) {
// Check for read only attribute
if (readOnlyAttributes.containsKey(name))
return;
found = attributes.containsKey(name);
if (found) {
value = attributes.get(name);
attributes.remove(name);
} else {
return;
}
}
// Notify interested application event listeners
Object listeners[] = context.getApplicationEventListeners();
if ((listeners == null) || (listeners.length == 0))
return;
ServletContextAttributeEvent event =
new ServletContextAttributeEvent(context.getServletContext(),
name, value);
for (int i = 0; i < listeners.length; i++) {
if (!(listeners[i] instanceof ServletContextAttributeListener))
continue;
ServletContextAttributeListener listener =
(ServletContextAttributeListener) listeners[i];
try {
context.fireContainerEvent("beforeContextAttributeRemoved",
listener);
listener.attributeRemoved(event);
context.fireContainerEvent("afterContextAttributeRemoved",
listener);
} catch (Throwable t) {
context.fireContainerEvent("afterContextAttributeRemoved",
listener);
// FIXME - should we do anything besides log these?
log(sm.getString("applicationContext.attributeEvent"), t);
}
}
}
/**
* Bind the specified value with the specified context attribute name,
* replacing any existing value for that name.
*
* @param name Attribute name to be bound
* @param value New attribute value to be bound
*/
public void setAttribute(String name, Object value) {
// Name cannot be null
if (name == null)
throw new IllegalArgumentException
(sm.getString("applicationContext.setAttribute.namenull"));
// Null value is the same as removeAttribute()
if (value == null) {
removeAttribute(name);
return;
}
Object oldValue = null;
boolean replaced = false;
// Add or replace the specified attribute
synchronized (attributes) {
// Check for read only attribute
if (readOnlyAttributes.containsKey(name))
return;
oldValue = attributes.get(name);
if (oldValue != null)
replaced = true;
attributes.put(name, value);
}
// Notify interested application event listeners
Object listeners[] = context.getApplicationEventListeners();
if ((listeners == null) || (listeners.length == 0))
return;
ServletContextAttributeEvent event = null;
if (replaced)
event =
new ServletContextAttributeEvent(context.getServletContext(),
name, oldValue);
else
event =
new ServletContextAttributeEvent(context.getServletContext(),
name, value);
for (int i = 0; i < listeners.length; i++) {
if (!(listeners[i] instanceof ServletContextAttributeListener))
continue;
ServletContextAttributeListener listener =
(ServletContextAttributeListener) listeners[i];
try {
if (replaced) {
context.fireContainerEvent
("beforeContextAttributeReplaced", listener);
listener.attributeReplaced(event);
context.fireContainerEvent("afterContextAttributeReplaced",
listener);
} else {
context.fireContainerEvent("beforeContextAttributeAdded",
listener);
listener.attributeAdded(event);
context.fireContainerEvent("afterContextAttributeAdded",
listener);
}
} catch (Throwable t) {
if (replaced)
context.fireContainerEvent("afterContextAttributeReplaced",
listener);
else
context.fireContainerEvent("afterContextAttributeAdded",
listener);
// FIXME - should we do anything besides log these?
log(sm.getString("applicationContext.attributeEvent"), t);
}
}
}
// -------------------------------------------------------- Package Methods
/**
* Clear all application-created attributes.
*/
void clearAttributes() {
// Create list of attributes to be removed
ArrayList list = new ArrayList();
synchronized (attributes) {
Iterator iter = attributes.keySet().iterator();
while (iter.hasNext()) {
list.add(iter.next());
}
}
// Remove application originated attributes
// (read only attributes will be left in place)
Iterator keys = list.iterator();
while (keys.hasNext()) {
String key = (String) keys.next();
removeAttribute(key);
}
}
/**
* Return the facade associated with this ApplicationContext.
*/
protected ServletContext getFacade() {
return (this.facade);
}
/**
* Set an attribute as read only.
*/
void setAttributeReadOnly(String name) {
synchronized (attributes) {
if (attributes.containsKey(name))
readOnlyAttributes.put(name, name);
}
}
// -------------------------------------------------------- Private Methods
/**
* Return a context-relative path, beginning with a "/", that represents
* the canonical version of the specified path after ".." and "." elements
* are resolved out. If the specified path attempts to go outside the
* boundaries of the current context (i.e. too many ".." path elements
* are present), return <code>null</code> instead.
*
* @param path Path to be normalized
*/
private String normalize(String path) {
String normalized = path;
// Normalize the slashes and add leading slash if necessary
if (normalized.indexOf('\\') >= 0)
normalized = normalized.replace('\\', '/');
// Resolve occurrences of "/../" in the normalized path
while (true) {
int index = normalized.indexOf("/../");
if (index < 0)
break;
if (index == 0)
return (null); // Trying to go outside our context
int index2 = normalized.lastIndexOf('/', index - 1);
normalized = normalized.substring(0, index2) +
normalized.substring(index + 3);
}
// Return the normalized path that we have completed
return (normalized);
}
/**
* Merge the context initialization parameters specified in the application
* deployment descriptor with the application parameters described in the
* server configuration, respecting the <code>override</code> property of
* the application parameters appropriately.
*/
private void mergeParameters() {
if (parameters != null)
return;
HashMap results = new HashMap();
String names[] = context.findParameters();
for (int i = 0; i < names.length; i++)
results.put(names[i], context.findParameter(names[i]));
ApplicationParameter params[] =
context.findApplicationParameters();
for (int i = 0; i < params.length; i++) {
if (params[i].getOverride()) {
if (results.get(params[i].getName()) == null)
results.put(params[i].getName(), params[i].getValue());
} else {
results.put(params[i].getName(), params[i].getValue());
}
}
parameters = results;
}
/**
* List resource paths (recursively), and store all of them in the given
* Set.
*/
private static void listPaths(Set set, DirContext resources, String path)
throws NamingException {
Enumeration childPaths = resources.listBindings(path);
while (childPaths.hasMoreElements()) {
Binding binding = (Binding) childPaths.nextElement();
String name = binding.getName();
String childPath = path + "/" + name;
set.add(childPath);
Object object = binding.getObject();
if (object instanceof DirContext) {
listPaths(set, resources, childPath);
}
}
}
/**
* List resource paths (recursively), and store all of them in the given
* Set.
*/
private static void listCollectionPaths
(Set set, DirContext resources, String path)
throws NamingException {
Enumeration childPaths = resources.listBindings(path);
while (childPaths.hasMoreElements()) {
Binding binding = (Binding) childPaths.nextElement();
String name = binding.getName();
StringBuffer childPath = new StringBuffer(path);
if (!"/".equals(path) && !path.endsWith("/"))
childPath.append("/");
childPath.append(name);
Object object = binding.getObject();
if (object instanceof DirContext) {
childPath.append("/");
}
set.add(childPath.toString());
}
}
/**
* Get full path, based on the host name and the context path.
*/
private static String getJNDIUri(String hostName, String path) {
if (!path.startsWith("/"))
return "/" + hostName + "/" + path;
else
return "/" + hostName + path;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -