View Javadoc

1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.internal.controller.impl;
17  
18  import java.util.Collection;
19  import java.util.Map;
20  import java.util.Set;
21  import java.util.concurrent.ConcurrentHashMap;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpSession;
25  
26  class FlashMap<K, V> implements Map<K, V> {
27  
28  	private static final String ATTRIBUTE_NAME = FlashMap.class.getName()
29  			+ ".MAP";
30  
31  	private final HttpServletRequest request;
32  
33  	private final Map<K, V> map;
34  
35  	FlashMap(final HttpServletRequest request) {
36  		this.request = request;
37  		this.map = buildMap(request);
38  	}
39  
40  	private Map<K, V> buildMap(final HttpServletRequest request) {
41  		final HttpSession session = request.getSession(false);
42  		if (session != null) {
43  			final Map<K, V> map = getAttribute(session, ATTRIBUTE_NAME);
44  			if (map != null) {
45  				return map;
46  			}
47  		}
48  		return new ConcurrentHashMap<K, V>();
49  	}
50  
51  	private void export(final HttpSession session) {
52  		session.setAttribute(ATTRIBUTE_NAME, this.map);
53  	}
54  
55  	public int size() {
56  		return map.size();
57  	}
58  
59  	public boolean isEmpty() {
60  		return map.isEmpty();
61  	}
62  
63  	public boolean containsKey(final Object key) {
64  		return map.containsKey(key);
65  	}
66  
67  	public boolean containsValue(final Object value) {
68  		return map.containsValue(value);
69  	}
70  
71  	public V get(final Object key) {
72  		return map.get(key);
73  	}
74  
75  	public V put(final K key, final V value) {
76  		final V previousValue = map.put(key, value);
77  		export(request.getSession());
78  		return previousValue;
79  	}
80  
81  	public V remove(final Object key) {
82  		final V removedValue = map.remove(key);
83  		final HttpSession session = request.getSession(false);
84  		if (session != null) {
85  			export(session);
86  		}
87  		return removedValue;
88  	}
89  
90  	public void putAll(final Map<? extends K, ? extends V> t) {
91  		map.putAll(t);
92  		export(request.getSession());
93  	}
94  
95  	public void clear() {
96  		map.clear();
97  		final HttpSession session = request.getSession(false);
98  		if (session != null) {
99  			export(session);
100 		}
101 	}
102 
103 	public Set<K> keySet() {
104 		return map.keySet();
105 	}
106 
107 	public Collection<V> values() {
108 		return map.values();
109 	}
110 
111 	public Set<Entry<K, V>> entrySet() {
112 		return map.entrySet();
113 	}
114 
115 	@SuppressWarnings("unchecked")
116 	private static <T> T getAttribute(final HttpSession session,
117 			final String name) {
118 		return (T) session.getAttribute(name);
119 	}
120 
121 }