1 12 13 package com.openedit.util; 14 15 import java.util.AbstractMap ; 16 import java.util.AbstractSet ; 17 import java.util.Enumeration ; 18 import java.util.Iterator ; 19 import java.util.Set ; 20 21 import javax.servlet.http.HttpSession ; 22 23 24 30 public class SessionMap extends AbstractMap 31 { 32 protected HttpSession fieldSession; 33 34 public SessionMap(HttpSession inSession) 35 { 36 fieldSession = inSession; 37 } 38 39 42 public boolean isEmpty() 43 { 44 return getSession().getAttributeNames().hasMoreElements(); 45 } 46 47 52 public HttpSession getSession() 53 { 54 return fieldSession; 55 } 56 57 60 public boolean containsKey(Object key) 61 { 62 return (getSession().getAttribute((String ) key) != null); 63 } 64 65 68 public Set entrySet() 69 { 70 return new EntrySet(); 71 } 72 73 76 public Object get(Object key) 77 { 78 return getSession().getAttribute((String ) key); 79 } 80 81 84 public Object put(Object key, Object value) 85 { 86 Object oldValue = getSession().getAttribute((String ) key); 87 getSession().setAttribute((String ) key, value); 88 89 return oldValue; 90 } 91 92 95 public Object remove(Object key) 96 { 97 Object oldValue = getSession().getAttribute((String ) key); 98 99 if (oldValue != null) 100 { 101 getSession().removeAttribute((String ) key); 102 } 103 104 return oldValue; 105 } 106 107 110 public int size() 111 { 112 int size = 0; 113 114 for (Enumeration e = getSession().getAttributeNames(); e.hasMoreElements();) 115 { 116 e.nextElement(); 117 size++; 118 } 119 120 return size; 121 } 122 123 126 protected class EntrySet extends AbstractSet 127 { 128 public EntrySet() 129 { 130 } 131 132 137 public boolean isEmpty() 138 { 139 return SessionMap.this.isEmpty(); 140 } 141 142 147 public Iterator iterator() 148 { 149 return new EntryIterator(); 150 } 151 152 155 public boolean remove(Object o) 156 { 157 return (SessionMap.this.remove(o) != null); 158 } 159 160 165 public int size() 166 { 167 return SessionMap.this.size(); 168 } 169 170 protected class EntryIterator implements Iterator 171 { 172 protected Enumeration fieldEnumeration = getSession().getAttributeNames(); 173 protected String fieldCurrentName; 174 175 180 public boolean hasNext() 181 { 182 return fieldEnumeration.hasMoreElements(); 183 } 184 185 190 public Object next() 191 { 192 fieldCurrentName = (String ) fieldEnumeration.nextElement(); 193 194 if (fieldCurrentName == null) 195 { 196 return null; 197 } 198 else 199 { 200 return new com.openedit.util.SimpleEntry( 201 fieldCurrentName, getSession().getAttribute(fieldCurrentName)); 202 } 203 } 204 205 208 public void remove() 209 { 210 getSession().removeAttribute(fieldCurrentName); 211 } 212 } 213 } 214 } 215 | Popular Tags |