diff --git a/core/src/main/java/org/apache/struts2/dispatcher/SessionMap.java b/core/src/main/java/org/apache/struts2/dispatcher/SessionMap.java index 6d0b20f35..be0b2ca1a 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/SessionMap.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/SessionMap.java @@ -21,19 +21,23 @@ package org.apache.struts2.dispatcher; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.io.Serializable; -import java.util.*; +import java.util.AbstractMap; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Set; /** * A simple implementation of the {@link java.util.Map} interface to handle a collection of HTTP session * attributes. The {@link #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. */ -public class SessionMap extends AbstractMap implements Serializable { +public class SessionMap extends AbstractMap implements Serializable { private static final long serialVersionUID = 4678843241638046854L; protected HttpSession session; - protected Set> entries; + protected Set> entries; protected HttpServletRequest request; @@ -43,7 +47,7 @@ public class SessionMap extends AbstractMap implements Serializable * * @param request the http servlet request object. */ - public SessionMap(HttpServletRequest request) { + public SessionMap(final 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 // "execAndWait" interceptor @@ -70,7 +74,6 @@ public class SessionMap extends AbstractMap implements Serializable * Removes all attributes from the session as well as clears entries in this * map. */ - @SuppressWarnings("unchecked") public void clear() { if (session == null) { return; @@ -91,8 +94,7 @@ public class SessionMap extends AbstractMap implements Serializable * * @return a Set of attributes from the http session. */ - @SuppressWarnings("unchecked") - public Set> entrySet() { + public Set> entrySet() { if (session == null) { return Collections.emptySet(); } @@ -106,32 +108,12 @@ public class SessionMap extends AbstractMap implements Serializable 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) { - if (!(obj instanceof Map.Entry)) { - return false; - } - 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 K getKey() { - return (K) key; - } - - public V getValue() { - return (V) value; - } - - public V setValue(Object obj) { + entries.add(new StringObjectEntry(key, value) { + @Override + public Object setValue(Object obj) { session.setAttribute(key, obj); - return (V) value; + return value; } }); } @@ -147,14 +129,13 @@ public class SessionMap extends AbstractMap implements Serializable * @param key the name of the session attribute. * @return the session attribute or null if it doesn't exist. */ - @SuppressWarnings("unchecked") - public V get(Object key) { + public Object get(final String key) { if (session == null) { return null; } synchronized (session.getId().intern()) { - return (V) session.getAttribute(key.toString()); + return session.getAttribute(key.toString()); } } @@ -165,14 +146,14 @@ public class SessionMap extends AbstractMap implements Serializable * @param value the value to set. * @return the object that was just set. */ - public V put(K key, V value) { + public Object put(final String key, final Object value) { synchronized (this) { if (session == null) { session = request.getSession(true); } } synchronized (session.getId().intern()) { - V oldValue = get(key); + Object oldValue = get(key); entries = null; session.setAttribute(key.toString(), value); return oldValue; @@ -185,7 +166,7 @@ public class SessionMap extends AbstractMap implements Serializable * @param key the name of the attribute to remove. * @return the value that was removed or null if the value was not found (and hence, not removed). */ - public V remove(Object key) { + public Object remove(final String key) { if (session == null) { return null; } @@ -193,7 +174,7 @@ public class SessionMap extends AbstractMap implements Serializable synchronized (session.getId().intern()) { entries = null; - V value = get(key); + Object value = get(key); session.removeAttribute(key.toString()); return value; @@ -207,7 +188,7 @@ public class SessionMap extends AbstractMap implements Serializable * @param key the name of the session attribute. * @return true if the session attribute exits or false if it doesn't exist. */ - public boolean containsKey(Object key) { + public boolean containsKey(final String key) { if (session == null) { return false; }