View Javadoc

1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.internal.controller.impl;
18  
19  import java.util.Collection;
20  import java.util.Map;
21  import java.util.Set;
22  import java.util.concurrent.ConcurrentHashMap;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpSession;
26  
27  import org.seasar.cubby.action.FlashMap;
28  import org.seasar.cubby.internal.controller.ThreadContext;
29  
30  /**
31   * {@link FlashMap} の実装です。
32   * 
33   * @author baba
34   */
35  class FlashMapImpl implements FlashMap {
36  
37  	/** セッションの属性に格納する {@link Map} のキー。 */
38  	private static final String ATTRIBUTE_NAME = FlashMapImpl.class.getName()
39  			+ ".MAP";
40  
41  	/** 要求。 */
42  	private final HttpServletRequest request;
43  
44  	/** 実際に値を格納する {@link Map}。 */
45  	private final Map<String, Object> map;
46  
47  	/**
48  	 * インスタンス化します。
49  	 * <p>
50  	 * 内部で使用する要求は {@link ThreadContext} から取得します。
51  	 * </p>
52  	 */
53  	public FlashMapImpl() {
54  		this(ThreadContext.getCurrentContext().getRequest());
55  	}
56  
57  	/**
58  	 * インスタンス化します。
59  	 * 
60  	 * @param request
61  	 *            要求
62  	 */
63  	public FlashMapImpl(final HttpServletRequest request) {
64  		this.request = request;
65  		this.map = buildMap(request);
66  	}
67  
68  	private Map<String, Object> buildMap(final HttpServletRequest request) {
69  		final HttpSession session = request.getSession(false);
70  		if (session != null) {
71  			final Map<String, Object> map = getAttribute(session,
72  					ATTRIBUTE_NAME);
73  			if (map != null) {
74  				return map;
75  			}
76  		}
77  		return createMap();
78  	}
79  
80  	protected Map<String, Object> createMap() {
81  		return new ConcurrentHashMap<String, Object>();
82  	}
83  
84  	private void export(final HttpSession session) {
85  		session.setAttribute(ATTRIBUTE_NAME, this.map);
86  	}
87  
88  	/**
89  	 * {@inheritDoc}
90  	 */
91  	public int size() {
92  		return map.size();
93  	}
94  
95  	/**
96  	 * {@inheritDoc}
97  	 */
98  	public boolean isEmpty() {
99  		return map.isEmpty();
100 	}
101 
102 	/**
103 	 * {@inheritDoc}
104 	 */
105 	public boolean containsKey(final Object key) {
106 		return map.containsKey(key);
107 	}
108 
109 	/**
110 	 * {@inheritDoc}
111 	 */
112 	public boolean containsValue(final Object value) {
113 		return map.containsValue(value);
114 	}
115 
116 	/**
117 	 * {@inheritDoc}
118 	 */
119 	public Object get(final Object key) {
120 		return map.get(key);
121 	}
122 
123 	/**
124 	 * {@inheritDoc}
125 	 */
126 	public Object put(final String key, final Object value) {
127 		final Object previousValue = map.put(key, value);
128 		export(request.getSession());
129 		return previousValue;
130 	}
131 
132 	/**
133 	 * {@inheritDoc}
134 	 */
135 	public Object remove(final Object key) {
136 		final Object removedValue = map.remove(key);
137 		final HttpSession session = request.getSession(false);
138 		if (session != null) {
139 			export(session);
140 		}
141 		return removedValue;
142 	}
143 
144 	/**
145 	 * {@inheritDoc}
146 	 */
147 	public void putAll(final Map<? extends String, ? extends Object> t) {
148 		map.putAll(t);
149 		export(request.getSession());
150 	}
151 
152 	/**
153 	 * {@inheritDoc}
154 	 */
155 	public void clear() {
156 		map.clear();
157 		final HttpSession session = request.getSession(false);
158 		if (session != null) {
159 			export(session);
160 		}
161 	}
162 
163 	/**
164 	 * {@inheritDoc}
165 	 */
166 	public Set<String> keySet() {
167 		return map.keySet();
168 	}
169 
170 	/**
171 	 * {@inheritDoc}
172 	 */
173 	public Collection<Object> values() {
174 		return map.values();
175 	}
176 
177 	/**
178 	 * {@inheritDoc}
179 	 */
180 	public Set<Entry<String, Object>> entrySet() {
181 		return map.entrySet();
182 	}
183 
184 	@SuppressWarnings("unchecked")
185 	private static <T> T getAttribute(final HttpSession session,
186 			final String name) {
187 		return (T) session.getAttribute(name);
188 	}
189 
190 }