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