org.apache.struts2.dispatcher
public class: SessionMap [javadoc |
source]
java.lang.Object
java.util.AbstractMap
org.apache.struts2.dispatcher.SessionMap
All Implemented Interfaces:
Serializable, Map
A simple implementation of the
java.util.Map interface to handle a collection of HTTP session
attributes. The
#entrySet() method enumerates over all session attributes and creates a Set of entries.
Note, this will occur lazily - only when the entry set is asked for.
| Field Summary |
|---|
| protected HttpSession | session | |
| protected Set | entries | |
| protected HttpServletRequest | request | |
| Constructor: |
public SessionMap(HttpServletRequest request) {
// note, holding on to this request and relying on lazy session initalization will not work
// if you are running your action invocation in a background task, such as using the
// "exec-and-wait" interceptor
this.request = request;
this.session = request.getSession(false);
}
Creates a new session map given a http servlet request. Note, ths enumeration of request
attributes will occur when the map entries are asked for. Parameters:
request - the http servlet request object.
|
| Methods from java.util.AbstractMap: |
|---|
|
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, toString, values |
| Method from org.apache.struts2.dispatcher.SessionMap Detail: |
public void clear() {
if (session == null ) {
return;
}
synchronized (session) {
entries = null;
Enumeration< String > attributeNamesEnum = session.getAttributeNames();
while(attributeNamesEnum.hasMoreElements()) {
session.removeAttribute(attributeNamesEnum.nextElement());
}
}
}
Removes all attributes from the session as well as clears entries in this
map. |
public Set entrySet() {
if (session == null) {
return Collections.EMPTY_SET;
}
synchronized (session) {
if (entries == null) {
entries = new HashSet< Object >();
Enumeration enumeration = session.getAttributeNames();
while (enumeration.hasMoreElements()) {
final String key = enumeration.nextElement().toString();
final Object value = session.getAttribute(key);
entries.add(new Map.Entry() {
public boolean equals(Object obj) {
Map.Entry entry = (Map.Entry) obj;
return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
}
public int hashCode() {
return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
}
public Object getKey() {
return key;
}
public Object getValue() {
return value;
}
public Object setValue(Object obj) {
session.setAttribute(key.toString(), obj);
return value;
}
});
}
}
}
return entries;
}
Returns a Set of attributes from the http session. |
public Object get(Object key) {
if (session == null) {
return null;
}
synchronized (session) {
return session.getAttribute(key.toString());
}
}
Returns the session attribute associated with the given key or null if it doesn't exist. |
public void invalidate() {
if (session == null) {
return;
}
synchronized (session) {
session.invalidate();
session = null;
entries = null;
}
}
Invalidate the http session. |
public Object put(Object key,
Object value) {
synchronized (this) {
if (session == null) {
session = request.getSession(true);
}
}
synchronized (session) {
entries = null;
session.setAttribute(key.toString(), value);
return get(key);
}
}
Saves an attribute in the session. |
public Object remove(Object key) {
if (session == null) {
return null;
}
synchronized (session) {
entries = null;
Object value = get(key);
session.removeAttribute(key.toString());
return value;
}
}
Removes the specified session attribute. |