1 20 package org.enhydra.barracuda.plankton.data; 21 22 import java.io.*; 23 import java.util.*; 24 import javax.servlet.*; 25 import javax.servlet.http.*; 26 27 33 public class HttpSessionStateMap implements StateMap { 34 35 protected HttpSession session = null; 37 private String NULL = "~Null~"; 38 39 44 public HttpSessionStateMap(HttpSession isession) { 45 session = isession; 46 } 47 48 54 public void putState(Object key, Object val) { 55 if (key==null) key = NULL; 56 if (val==null) val = NULL; 57 session.setAttribute(key.toString(),val); 58 } 59 60 66 public Object getState(Object key) { 67 if (key==null) key = NULL; 68 Object val = session.getAttribute(key.toString()); 69 return ((val==null || val.equals(NULL)) ? null : val); 70 } 71 72 85 public Object removeState(Object key) { 86 try { if (key!=null && (key instanceof String ) && ((String ) key).endsWith("*")) { 90 Map removed = new HashMap(); 91 String keystr = (String ) key; 92 String targetstr = keystr.substring(0,keystr.length()-1); 93 List keys = getStateKeys(); 94 Iterator it = keys.iterator(); 95 while (it.hasNext()) { 96 Object okey = it.next(); 97 if ((okey instanceof String ) && ((String ) okey).startsWith(targetstr)) { 98 Object oval = session.getAttribute(okey.toString()); 99 removed.put(okey, oval); 100 session.removeAttribute(okey.toString()); 101 } 102 } 103 return (removed.size()>0 ? removed : null); 104 } else { 105 if (key==null) key = NULL; 107 Object val = session.getAttribute(key.toString()); 108 session.removeAttribute(key.toString()); 109 return ((val==null || val.equals(NULL)) ? null : val); } 112 113 } catch (NullPointerException e) { return null; 117 } 118 } 119 120 126 public List getStateKeys() { 127 Enumeration e = session.getAttributeNames(); 128 List list = new ArrayList(); 129 while (e.hasMoreElements()) { 130 list.add(e.nextElement()); 131 } 132 return list; 133 } 134 135 142 public Map getStateValues() { 143 Map map = new HashMap(); 144 List keys = getStateKeys(); 145 Iterator it = keys.iterator(); 146 while (it.hasNext()) { 147 Object key = it.next(); 148 Object val = session.getAttribute(key.toString()); 149 map.put(key,val); 150 } 151 return map; 152 } 153 154 158 public void clearState() { 159 List keys = getStateKeys(); 160 Iterator it = keys.iterator(); 161 while (it.hasNext()) { 162 Object key = it.next(); 163 removeState(key); 164 } 165 } 166 167 172 public HttpSession getSession() { 173 return session; 174 } 175 } 176 | Popular Tags |