001 /* 002 * Copyright 1999-2001,2004 The Apache Software Foundation. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.apache.commons.workflow.web; 018 019 020 import java.util.ArrayList; 021 import java.util.Collection; 022 import java.util.Enumeration; 023 import java.util.HashSet; 024 import java.util.Iterator; 025 import java.util.Map; 026 import java.util.Set; 027 import javax.servlet.ServletRequest; 028 import org.apache.commons.workflow.base.BaseScope; 029 import org.apache.commons.workflow.util.MapEntry; 030 031 032 /** 033 * <strong>ServletRequestScope</strong> is a specialized <code>Scope</code> 034 * implementation corresponding the the attributes of a specified 035 * <code>ServletRequest</code>. 036 * 037 * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $ 038 * @author Craig R. McClanahan 039 */ 040 041 public class ServletRequestScope extends BaseScope { 042 043 044 // ----------------------------------------------------------- Constructors 045 046 047 /** 048 * Construct a new ServletRequestScope with no attached ServletRequest. 049 */ 050 public ServletRequestScope() { 051 052 super(); 053 054 } 055 056 057 /** 058 * Construct a ServletRequestScope associated with the specified 059 * ServletRequest. 060 * 061 * @param servletRequest The associated ServletRequest 062 */ 063 public ServletRequestScope(ServletRequest servletRequest) { 064 065 super(); 066 setServletRequest(servletRequest); 067 068 } 069 070 071 // ------------------------------------------------------------- Properties 072 073 074 /** 075 * The servlet request with which we are associated. 076 */ 077 protected ServletRequest servletRequest = null; 078 079 public ServletRequest getServletRequest() { 080 return (this.servletRequest); 081 } 082 083 public void setServletRequest(ServletRequest servletRequest) { 084 this.servletRequest = servletRequest; 085 } 086 087 088 // ------------------------------------------------------------ Map Methods 089 090 091 /** 092 * Remove all beans from this Map and call <code>scopeCleared() on 093 * all registered <code>ScopeListeners</code>. 094 */ 095 public void clear() { 096 097 // Accumulate a list of the elements to be cleared 098 Enumeration names = servletRequest.getAttributeNames(); 099 ArrayList list = new ArrayList(); 100 while (names.hasMoreElements()) { 101 list.add((String) names.nextElement()); 102 } 103 104 // Erase the accumulated elements 105 int n = list.size(); 106 for (int i = 0; i < n; i++) { 107 servletRequest.removeAttribute((String) list.get(i)); 108 } 109 support.fireScopeCleared(); 110 111 } 112 113 114 /** 115 * Return <code>true</code> if this map contains the specified key. 116 * 117 * @param key Key to be looked up 118 */ 119 public boolean containsKey(Object key) { 120 121 return (servletRequest.getAttribute((String) key) != null); 122 123 } 124 125 126 /** 127 * Return <code>true</code> if this map contains the specified value. 128 * 129 * @param value Value to be looked up 130 */ 131 public boolean containsValue(Object value) { 132 133 // Check all existing attributes for a match 134 Enumeration names = servletRequest.getAttributeNames(); 135 while (names.hasMoreElements()) { 136 String name = (String) names.nextElement(); 137 if (value.equals(servletRequest.getAttribute(name))) 138 return (true); 139 } 140 return (false); 141 142 } 143 144 145 /** 146 * Return a set view of the mappings contained in this map. 147 */ 148 public Set entrySet() { 149 150 HashSet results = new HashSet(); 151 Enumeration names = servletRequest.getAttributeNames(); 152 while (names.hasMoreElements()) { 153 String name = (String) names.nextElement(); 154 results.add(new MapEntry(name, servletRequest.getAttribute(name))); 155 } 156 return (results); 157 158 } 159 160 161 /** 162 * Compare the specified object with this map for equality. 163 * 164 * @param object Object to be compared 165 */ 166 public boolean equals(Object object) { 167 168 return (servletRequest.equals(object)); 169 170 } 171 172 173 /** 174 * Return the value to which this map maps the specified key. 175 * 176 * @param key Key to be looked up 177 */ 178 public Object get(Object key) { 179 180 return (get((String) key)); 181 182 } 183 184 185 /** 186 * Return the value to which this map maps the specified key. 187 * 188 * @param key Key to be looked up 189 */ 190 public Object get(String key) { 191 192 return (servletRequest.getAttribute(key)); 193 194 } 195 196 197 /** 198 * Return the hash code value for this map. 199 */ 200 public int hashCode() { 201 202 return (servletRequest.hashCode()); 203 204 } 205 206 207 /** 208 * Return <code>true</code> if this map is empty. 209 */ 210 public boolean isEmpty() { 211 212 // Check all existing attributes 213 Enumeration names = servletRequest.getAttributeNames(); 214 while (names.hasMoreElements()) 215 return (true); 216 return (false); 217 218 } 219 220 221 /** 222 * Return a set view of the keys contained in this map. 223 */ 224 public Set keySet() { 225 226 HashSet results = new HashSet(); 227 Enumeration names = servletRequest.getAttributeNames(); 228 while (names.hasMoreElements()) 229 results.add(names.nextElement()); 230 return (results); 231 232 } 233 234 235 /** 236 * Add or replace the bean associated with the specified key. 237 * 238 * @param key Key with which the new value should be associated 239 * (cannot be null) 240 * @param bean Bean to be associated with this key (cannot be null) 241 */ 242 public Object put(Object key, Object bean) { 243 244 return (put((String) key, bean)); 245 246 } 247 248 249 /** 250 * Add the specified bean, associated with the specified key, to this 251 * scope and replace any previous bean associated with this key. If 252 * the bean was added, call <code>beanAdded()</code> on all registered 253 * listeners after the add is done. If an old bean was replaced, 254 * call <code>beanReplaced()</code> (passing the old value in the event) 255 * on all registered <code>ScopeListeners</code> after the removal 256 * is done. If a bean was replaced, the old value is also returned; 257 * otherwise <code>null</code> is returned. 258 * 259 * @param key Key with which the new value should be associated 260 * (cannot be null) 261 * @param bean Bean to be associated with this key (cannot be null) 262 * 263 * @exception IllegalArgumentException if <code>key</code> or 264 * <code>bean</code> is null 265 */ 266 public Object put(String key, Object bean) { 267 268 if (key == null) 269 throw new IllegalArgumentException("Key cannot be null"); 270 if (bean == null) 271 throw new IllegalArgumentException("Value cannot be null"); 272 273 Object old = servletRequest.getAttribute(key); 274 if (old != null) { 275 servletRequest.setAttribute(key, bean); 276 support.fireBeanReplaced(key, old); 277 } else { 278 servletRequest.setAttribute(key, bean); 279 support.fireBeanAdded(key, bean); 280 } 281 return (old); 282 283 } 284 285 286 /** 287 * Copy all of the mappings from the specified map into this map, 288 * firing appropriate <code>beanAdded()</code> and 289 * <code>beanReplaced()</code> events along the way. 290 * 291 * @param in Map whose contents are to be added 292 */ 293 public void putAll(Map in) { 294 295 Iterator keys = in.keySet().iterator(); 296 while (keys.hasNext()) { 297 Object key = keys.next(); 298 put(key, in.get(key)); 299 } 300 301 } 302 303 304 /** 305 * Remove the bean associated with the specified key (if any), and return 306 * the old value if removed. 307 * 308 * @param key Key of the bean to remove (cannot be null) 309 */ 310 public Object remove(Object key) { 311 312 return (remove((String) key)); 313 314 } 315 316 317 318 /** 319 * Remove the bean associated with the specified key (if any). If such 320 * a bean is found and removed, call <code>beanRemoved()</code> on all 321 * registered <code>ScopeListeners</code> after the removal is done. 322 * Return the old value (if any); otherwise return <code>null</code>. 323 * 324 * @param key Key of the bean to remove (cannot be null) 325 * 326 * @exception IllegalArgumentException if <code>key</code> is null 327 */ 328 public Object remove(String key) { 329 330 Object old = servletRequest.getAttribute(key); 331 if (old != null) { 332 support.fireBeanRemoved(key, old); 333 return (old); 334 } 335 return (null); 336 337 } 338 339 340 /** 341 * Return the number of key-value mappings in this map. 342 */ 343 public int size() { 344 345 Enumeration names = servletRequest.getAttributeNames(); 346 int n = 0; 347 while (names.hasMoreElements()) { 348 n++; 349 } 350 return (n); 351 352 } 353 354 355 /** 356 * Return a Collection view of the values contained in this map. 357 */ 358 public Collection values() { 359 360 ArrayList results = new ArrayList(); 361 Enumeration names = servletRequest.getAttributeNames(); 362 while (names.hasMoreElements()) { 363 String name = (String) names.nextElement(); 364 results.add(servletRequest.getAttribute(name)); 365 } 366 return (results); 367 368 } 369 370 371 }