1 16 package org.apache.commons.chain.web.servlet; 17 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Enumeration ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import javax.servlet.http.HttpSession ; 28 import org.apache.commons.chain.web.MapEntry; 29 30 31 38 39 final class ServletSessionScopeMap implements Map { 40 41 42 public ServletSessionScopeMap(HttpSession session) { 43 this.session = session; 44 } 45 46 47 private HttpSession session = null; 48 49 50 public void clear() { 51 Iterator keys = keySet().iterator(); 52 while (keys.hasNext()) { 53 session.removeAttribute((String ) keys.next()); 54 } 55 } 56 57 58 public boolean containsKey(Object key) { 59 return (session.getAttribute(key(key)) != null); 60 } 61 62 63 public boolean containsValue(Object value) { 64 if (value == null) { 65 return (false); 66 } 67 Enumeration keys = session.getAttributeNames(); 68 while (keys.hasMoreElements()) { 69 Object next = session.getAttribute((String ) keys.nextElement()); 70 if (next == value) { 71 return (true); 72 } 73 } 74 return (false); 75 } 76 77 78 public Set entrySet() { 79 Set set = new HashSet (); 80 Enumeration keys = session.getAttributeNames(); 81 String key; 82 while (keys.hasMoreElements()) { 83 key = (String ) keys.nextElement(); 84 set.add(new MapEntry(key, session.getAttribute(key), true)); 85 } 86 return (set); 87 } 88 89 90 public boolean equals(Object o) { 91 return (session.equals(o)); 92 } 93 94 95 public Object get(Object key) { 96 return (session.getAttribute(key(key))); 97 } 98 99 100 public int hashCode() { 101 return (session.hashCode()); 102 } 103 104 105 public boolean isEmpty() { 106 return (size() < 1); 107 } 108 109 110 public Set keySet() { 111 Set set = new HashSet (); 112 Enumeration keys = session.getAttributeNames(); 113 while (keys.hasMoreElements()) { 114 set.add(keys.nextElement()); 115 } 116 return (set); 117 } 118 119 120 public Object put(Object key, Object value) { 121 if (value == null) { 122 return (remove(key)); 123 } 124 String skey = key(key); 125 Object previous = session.getAttribute(skey); 126 session.setAttribute(skey, value); 127 return (previous); 128 } 129 130 131 public void putAll(Map map) { 132 Iterator keys = map.keySet().iterator(); 133 while (keys.hasNext()) { 134 String key = (String ) keys.next(); 135 session.setAttribute(key, map.get(key)); 136 } 137 } 138 139 140 public Object remove(Object key) { 141 String skey = key(key); 142 Object previous = session.getAttribute(skey); 143 session.removeAttribute(skey); 144 return (previous); 145 } 146 147 148 public int size() { 149 int n = 0; 150 Enumeration keys = session.getAttributeNames(); 151 while (keys.hasMoreElements()) { 152 keys.nextElement(); 153 n++; 154 } 155 return (n); 156 } 157 158 159 public Collection values() { 160 List list = new ArrayList (); 161 Enumeration keys = session.getAttributeNames(); 162 while (keys.hasMoreElements()) { 163 list.add(session.getAttribute((String ) keys.nextElement())); 164 } 165 return (list); 166 } 167 168 169 private String key(Object key) { 170 if (key == null) { 171 throw new IllegalArgumentException (); 172 } else if (key instanceof String ) { 173 return ((String ) key); 174 } else { 175 return (key.toString()); 176 } 177 } 178 179 180 } 181 | Popular Tags |