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 "conversation" scope, beans under this scope live until a method annotated with
31   * 
32   * @ConvEnd is executed
33   * @author Urubatan
34   */
35  public class ConversationScope implements Scope {
36  	private final String	SCOPE_KEY	= ConversationScope.class.getName() + ".SCOPEDMAP";
37  //TODO document
38  	@SuppressWarnings("unchecked")
39  	private HashMap<String, Object> getScopedValues() {
40  		HashMap<String, Object> scopedObj = (HashMap<String, Object>) RequestContextHolder.currentRequestAttributes().getAttribute(this.SCOPE_KEY, RequestAttributes.SCOPE_SESSION);
41  		if (scopedObj == null) {
42  			scopedObj = new HashMap<String, Object>();
43  			RequestContextHolder.currentRequestAttributes().setAttribute(this.SCOPE_KEY, scopedObj, RequestAttributes.SCOPE_SESSION);
44  		}
45  		return scopedObj;
46  	}
47  	//TODO document
48  	public Object get(final String name, final ObjectFactory objectFactory) {
49  		Object scopedObject = this.getScopedValues().get(name);
50  		if ((scopedObject == null) && (objectFactory != null)) {
51  			scopedObject = objectFactory.getObject();
52  			this.getScopedValues().put(name, scopedObject);
53  		}
54  		return scopedObject;
55  	}
56  	//TODO document
57  	public Object remove(final String name) {
58  		final Object scopedObject = this.getScopedValues().get(name);
59  		if (scopedObject != null) {
60  			this.getScopedValues().remove(name);
61  			return scopedObject;
62  		}
63  		return null;
64  	}
65  	//TODO document
66  	public void clearScope() {
67  		this.getScopedValues().clear();
68  	}
69  	//TODO document
70  	public void set(String name, Object value) {
71  		this.getScopedValues().put(name, value);
72  	}
73  	//TODO document
74  	public String getConversationId() {
75  		return "conversation";
76  	}
77  	//TODO document
78  	public void registerDestructionCallback(String arg0, Runnable arg1) {
79  		// TODO Auto-generated method stub
80  	}
81  }