1 5 package com.opensymphony.webwork.dispatcher; 6 7 import javax.servlet.http.HttpServletRequest ; 8 import javax.servlet.http.HttpSession ; 9 import java.io.Serializable ; 10 import java.util.*; 11 12 13 21 public class SessionMap extends AbstractMap implements Serializable { 22 24 protected HttpSession session; 25 protected Set entries; 26 protected HttpServletRequest request; 27 28 30 36 public SessionMap(HttpServletRequest request) { 37 this.request = request; 41 this.session = request.getSession(false); 42 } 43 44 46 49 public void clear() { 50 if (session == null) { 51 return; 52 } 53 54 synchronized (session) { 55 entries = null; 56 } 57 } 58 59 64 public Set entrySet() { 65 if (session == null) { 66 return Collections.EMPTY_SET; 67 } 68 69 synchronized (session) { 70 if (entries == null) { 71 entries = new HashSet(); 72 73 Enumeration enumeration = session.getAttributeNames(); 74 75 while (enumeration.hasMoreElements()) { 76 final String key = enumeration.nextElement().toString(); 77 final Object value = session.getAttribute(key); 78 entries.add(new Map.Entry() { 79 public boolean equals(Object obj) { 80 Map.Entry entry = (Map.Entry) obj; 81 82 return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue())); 83 } 84 85 public int hashCode() { 86 return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode()); 87 } 88 89 public Object getKey() { 90 return key; 91 } 92 93 public Object getValue() { 94 return value; 95 } 96 97 public Object setValue(Object obj) { 98 session.setAttribute(key.toString(), obj); 99 100 return value; 101 } 102 }); 103 } 104 } 105 } 106 107 return entries; 108 } 109 110 116 public Object get(Object key) { 117 if (session == null) { 118 return null; 119 } 120 121 synchronized (session) { 122 return session.getAttribute(key.toString()); 123 } 124 } 125 126 133 public Object put(Object key, Object value) { 134 synchronized (this) { 135 if (session == null) { 136 session = request.getSession(true); 137 } 138 } 139 140 synchronized (session) { 141 entries = null; 142 session.setAttribute(key.toString(), value); 143 144 return get(key); 145 } 146 } 147 148 154 public Object remove(Object key) { 155 if (session == null) { 156 return null; 157 } 158 159 synchronized (session) { 160 entries = null; 161 162 Object value = get(key); 163 session.removeAttribute(key.toString()); 164 165 return value; 166 } 167 } 168 } 169 | Popular Tags |