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 ServletContextStateMap implements StateMap { 34 35 protected ServletContext context = null; 37 private String NULL = "~Null~"; 38 39 44 public ServletContextStateMap(ServletContext icontext) { 45 context = icontext; 46 } 47 48 54 public void putState(Object key, Object val) { 55 if (key==null) key = NULL; 56 if (val==null) val = NULL; 57 context.setAttribute(key.toString(),val); 58 } 59 60 66 public Object getState(Object key) { 67 if (key==null) key = NULL; 68 Object val = context.getAttribute(key.toString()); 69 return (val.equals(NULL) ? null : val); 70 } 71 72 78 public Object removeState(Object key) { 79 if (key==null) key = NULL; 80 Object val = context.getAttribute(key.toString()); 81 context.removeAttribute(key.toString()); 82 return (val.equals(NULL) ? null : val); 83 } 84 85 91 public List getStateKeys() { 92 Enumeration e = context.getAttributeNames(); 93 List list = new ArrayList(); 94 while (e.hasMoreElements()) { 95 list.add(e.nextElement()); 96 } 97 return list; 98 } 99 100 107 public Map getStateValues() { 108 Map map = new HashMap(); 109 List keys = getStateKeys(); 111 Iterator it = keys.iterator(); 112 while (it.hasNext()) { 113 Object key = it.next(); 114 Object val = context.getAttribute(key.toString()); 115 map.put(key,val); 116 } 117 return map; 118 } 119 120 124 public void clearState() { 125 List keys = getStateKeys(); 126 Iterator it = keys.iterator(); 127 while (it.hasNext()) { 128 Object key = it.next(); 129 removeState(key); 130 } 131 } 132 133 138 public ServletContext getContext() { 139 return context; 140 } 141 } 142 | Popular Tags |