1 13 package com.tonbeller.wcf.param; 14 15 import java.util.Collection ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import javax.servlet.http.HttpSession ; 22 import javax.servlet.jsp.PageContext ; 23 24 import org.apache.log4j.Logger; 25 26 import com.tonbeller.tbutils.res.Resources; 27 28 68 69 public class SessionParamPool implements Map { 70 private static final String SQL_VALUE_MAP = "sqlValueMap"; 71 private static final String PARAM_POOL = "paramPool"; 72 73 private static final Logger logger = Logger.getLogger(SessionParamPool.class); 74 private SqlValueMap sqlValueMap; 75 private Map map = new HashMap (); 76 77 protected SessionParamPool() { 78 sqlValueMap = new SqlValueMap(this); 79 } 80 81 84 static SessionParamPool instance() { 85 return createInstance(); 86 } 87 88 91 public static SessionParamPool instance(HttpSession session) { 92 if (session == null) 94 return createInstance(); 95 SessionParamPool p = (SessionParamPool) session.getAttribute(PARAM_POOL); 97 if (p == null) { 98 p = createInstance(); 99 session.setAttribute(PARAM_POOL, p); 100 session.setAttribute(SQL_VALUE_MAP, p.getSqlValueMap()); 101 } 102 return p; 103 } 104 105 108 protected static SessionParamPool createInstance() { 109 String clazz = SessionParamPool.class.getName(); 110 clazz = Resources.instance().getOptionalString(clazz, clazz); 111 try { 112 return (SessionParamPool) Class.forName(clazz).newInstance(); 113 } catch (InstantiationException e) { 114 logger.error(null, e); 115 throw new IllegalArgumentException (clazz); 116 } catch (IllegalAccessException e) { 117 logger.error(null, e); 118 throw new IllegalArgumentException (clazz); 119 } catch (ClassNotFoundException e) { 120 logger.error(null, e); 121 throw new IllegalArgumentException (clazz); 122 } 123 } 124 125 128 public static SessionParamPool instance(PageContext pageContext) { 129 SessionParamPool p = (SessionParamPool) pageContext.findAttribute(PARAM_POOL); 130 if (p == null) 131 return instance(pageContext.getSession()); 132 return p; 133 } 134 135 public SessionParam getParam(String name) { 136 return (SessionParam) map.get(name); 137 } 138 139 public SessionParam setParam(SessionParam p) { 140 return (SessionParam) map.put(p.getName(), p); 141 } 142 143 149 public Map pushParams(Collection c) { 150 Map memento = new HashMap (); 151 for (Iterator it = c.iterator(); it.hasNext();) { 152 SessionParam param = (SessionParam) it.next(); 153 SessionParam prev = setParam(param); 154 String name = param.getName(); 155 if (!memento.containsKey(name)) 156 memento.put(name, prev); 157 } 158 return memento; 159 } 160 161 167 public void popParams(Map memento) { 168 for (Iterator it = memento.entrySet().iterator(); it.hasNext();) { 169 Map.Entry e = (Entry) it.next(); 170 SessionParam p = (SessionParam) e.getValue(); 171 if (p == null) 172 removeParam((String ) e.getKey()); 173 else 174 setParam(p); 175 } 176 } 177 178 public void removeParam(SessionParam p) { 179 map.remove(p.getName()); 180 } 181 182 public SessionParam removeParam(String name) { 183 return (SessionParam) map.remove(name); 184 } 185 186 189 public Map getSqlValueMap() { 190 return sqlValueMap; 191 } 192 193 public int size() { 194 return map.size(); 195 } 196 197 public void clear() { 198 map.clear(); 199 } 200 201 public boolean isEmpty() { 202 return map.isEmpty(); 203 } 204 205 public boolean containsKey(Object key) { 206 return map.containsKey(key); 207 } 208 209 public boolean containsValue(Object value) { 210 return map.containsValue(value); 211 } 212 213 public Collection values() { 214 return map.values(); 215 } 216 217 public void putAll(Map t) { 218 map.putAll(t); 219 } 220 221 public Set entrySet() { 222 return map.entrySet(); 223 } 224 225 public Set keySet() { 226 return map.keySet(); 227 } 228 229 public Object get(Object key) { 230 return map.get(key); 231 } 232 233 public Object remove(Object key) { 234 return map.remove(key); 235 } 236 237 public Object put(Object key, Object value) { 238 return map.put(key, value); 239 } 240 241 } | Popular Tags |