View Javadoc

1   /*
2    Spring-Annotation, an easy way to configre Spring-Framework without all that XML.
3    Copyright (C) 2007 Spring-Annotation Development Team (http://www.urubatan.com.br)
4   
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14  
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  
19   <a href="http://www.gnu.org/licenses/lgpl.html">http://www.gnu.org/licenses/lgpl.html</a>
20   */
21  package net.sourceforge.sannotations.scopes;
22  
23  import java.util.HashMap;
24  import org.springframework.beans.factory.ObjectFactory;
25  import org.springframework.beans.factory.config.Scope;
26  import org.springframework.web.context.request.RequestAttributes;
27  import org.springframework.web.context.request.RequestContextHolder;
28  
29  /***
30   * Bean that implements the "flash" Scope, beans under this scope live from the end of an HTTP request to the begining of the next one.
31   * 
32   * @author Urubatan
33   */
34  public class FlashScope implements Scope {
35  	private final String	SCOPE_KEY	= FlashScope.class.getName() + ".SCOPEDMAP";
36  	//TODO document
37  	@SuppressWarnings("unchecked")
38  	private HashMap<String, Object> getScopedValues() {
39  		HashMap<String, Object> scopedObj = (HashMap<String, Object>) RequestContextHolder.currentRequestAttributes().getAttribute(this.SCOPE_KEY, RequestAttributes.SCOPE_SESSION);
40  		if (scopedObj == null) {
41  			scopedObj = new HashMap<String, Object>();
42  			RequestContextHolder.currentRequestAttributes().setAttribute(this.SCOPE_KEY, scopedObj, RequestAttributes.SCOPE_SESSION);
43  		}
44  		return scopedObj;
45  	}
46  	//TODO document
47  	public Object get(final String name, final ObjectFactory objectFactory) {
48  		Object scopedObject = this.getScopedValues().get(name);
49  		if ((scopedObject == null) && (objectFactory != null)) {
50  			scopedObject = objectFactory.getObject();
51  			this.getScopedValues().put(name, scopedObject);
52  		}
53  		return scopedObject;
54  	}
55  	//TODO document
56  	public Object remove(final String name) {
57  		final Object scopedObject = this.getScopedValues().get(name);
58  		if (scopedObject != null) {
59  			this.getScopedValues().remove(name);
60  			return scopedObject;
61  		}
62  		return null;
63  	}
64  	//TODO document
65  	public void clearScope() {
66  		this.getScopedValues().clear();
67  	}
68  	//TODO document
69  	public void set(String name, Object value) {
70  		this.getScopedValues().put(name, value);
71  	}
72  	//TODO document
73  	public String getConversationId() {
74  		return "flash";
75  	}
76  	//TODO document
77  	public void registerDestructionCallback(String arg0, Runnable arg1) {
78  		// TODO Auto-generated method stub
79  	}
80  }