1 4 package com.opensymphony.webwork.portlet; 5 6 import javax.portlet.PortletRequest; 7 import javax.portlet.PortletSession; 8 import java.io.Serializable ; 9 import java.util.*; 10 11 12 16 public class SessionMap extends AbstractMap implements Serializable { 17 19 PortletSession session; 20 Set entries; 21 22 24 30 public SessionMap(PortletRequest request) { 31 this.session = request.getPortletSession(true); 32 } 33 34 36 39 public void clear() { 40 synchronized (session) { 41 entries = null; 42 session.invalidate(); 43 } 44 } 45 46 51 public Set entrySet() { 52 synchronized (session) { 53 if (entries == null) { 54 entries = new HashSet(); 55 56 Enumeration enumeration = session.getAttributeNames(); 57 58 while (enumeration.hasMoreElements()) { 59 final String key = enumeration.nextElement().toString(); 60 final Object value = session.getAttribute(key); 61 entries.add(new Map.Entry() { 62 public boolean equals(Object obj) { 63 Map.Entry entry = (Map.Entry) obj; 64 65 return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue())); 66 } 67 68 public int hashCode() { 69 return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode()); 70 } 71 72 public Object getKey() { 73 return key; 74 } 75 76 public Object getValue() { 77 return value; 78 } 79 80 public Object setValue(Object obj) { 81 session.setAttribute(key.toString(), obj); 82 83 return value; 84 } 85 }); 86 } 87 } 88 } 89 90 return entries; 91 } 92 93 99 public Object get(Object key) { 100 synchronized (session) { 101 return session.getAttribute(key.toString()); 102 } 103 } 104 105 112 public Object put(Object key, Object value) { 113 synchronized (session) { 114 entries = null; 115 session.setAttribute(key.toString(), value); 116 117 return get(key); 118 } 119 } 120 121 127 public Object remove(Object key) { 128 synchronized (session) { 129 entries = null; 130 131 Object value = get(key); 132 session.removeAttribute(key.toString()); 133 134 return value; 135 } 136 } 137 } 138 | Popular Tags |