Coverage Report - org.seasar.cubby.action.impl.FlashMapImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
FlashMapImpl
95%
40/42
62%
5/8
1.278
 
 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  2
 public class FlashMapImpl implements FlashMap {
 35  
 
 36  
         /** セッションの属性に格納する {@link Map} のキー。 */
 37  1
         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  0
                 this(ThreadContext.getRequest());
 54  0
         }
 55  
 
 56  
         /**
 57  
          * インスタンス化します。
 58  
          * 
 59  
          * @param request
 60  
          *            要求
 61  
          */
 62  3
         public FlashMapImpl(final HttpServletRequest request) {
 63  3
                 this.request = request;
 64  3
                 this.map = buildMap(request);
 65  3
         }
 66  
 
 67  
         private Map<String, Object> buildMap(final HttpServletRequest request) {
 68  3
                 final HttpSession session = request.getSession(false);
 69  3
                 if (session != null) {
 70  1
                         final Map<String, Object> map = getAttribute(session,
 71  
                                         ATTRIBUTE_NAME);
 72  1
                         if (map != null) {
 73  1
                                 return map;
 74  
                         }
 75  
                 }
 76  2
                 return createMap();
 77  
         }
 78  
 
 79  
         protected Map<String, Object> createMap() {
 80  2
                 return new ConcurrentHashMap<String, Object>();
 81  
         }
 82  
 
 83  
         private void export(final HttpSession session) {
 84  8
                 session.setAttribute(ATTRIBUTE_NAME, this.map);
 85  8
         }
 86  
 
 87  
         /**
 88  
          * {@inheritDoc}
 89  
          */
 90  
         public int size() {
 91  8
                 return map.size();
 92  
         }
 93  
 
 94  
         /**
 95  
          * {@inheritDoc}
 96  
          */
 97  
         public boolean isEmpty() {
 98  8
                 return map.isEmpty();
 99  
         }
 100  
 
 101  
         /**
 102  
          * {@inheritDoc}
 103  
          */
 104  
         public boolean containsKey(final Object key) {
 105  8
                 return map.containsKey(key);
 106  
         }
 107  
 
 108  
         /**
 109  
          * {@inheritDoc}
 110  
          */
 111  
         public boolean containsValue(final Object value) {
 112  8
                 return map.containsValue(value);
 113  
         }
 114  
 
 115  
         /**
 116  
          * {@inheritDoc}
 117  
          */
 118  
         public Object get(final Object key) {
 119  6
                 return map.get(key);
 120  
         }
 121  
 
 122  
         /**
 123  
          * {@inheritDoc}
 124  
          */
 125  
         public Object put(final String key, final Object value) {
 126  2
                 final Object previousValue = map.put(key, value);
 127  2
                 export(request.getSession());
 128  2
                 return previousValue;
 129  
         }
 130  
 
 131  
         /**
 132  
          * {@inheritDoc}
 133  
          */
 134  
         public Object remove(final Object key) {
 135  2
                 final Object removedValue = map.remove(key);
 136  2
                 final HttpSession session = request.getSession(false);
 137  2
                 if (session != null) {
 138  2
                         export(session);
 139  
                 }
 140  2
                 return removedValue;
 141  
         }
 142  
 
 143  
         /**
 144  
          * {@inheritDoc}
 145  
          */
 146  
         public void putAll(final Map<? extends String, ? extends Object> t) {
 147  2
                 map.putAll(t);
 148  2
                 export(request.getSession());
 149  2
         }
 150  
 
 151  
         /**
 152  
          * {@inheritDoc}
 153  
          */
 154  
         public void clear() {
 155  2
                 map.clear();
 156  2
                 final HttpSession session = request.getSession(false);
 157  2
                 if (session != null) {
 158  2
                         export(session);
 159  
                 }
 160  2
         }
 161  
 
 162  
         /**
 163  
          * {@inheritDoc}
 164  
          */
 165  
         public Set<String> keySet() {
 166  8
                 return map.keySet();
 167  
         }
 168  
 
 169  
         /**
 170  
          * {@inheritDoc}
 171  
          */
 172  
         public Collection<Object> values() {
 173  8
                 return map.values();
 174  
         }
 175  
 
 176  
         /**
 177  
          * {@inheritDoc}
 178  
          */
 179  
         public Set<Entry<String, Object>> entrySet() {
 180  8
                 return map.entrySet();
 181  
         }
 182  
 
 183  
         @SuppressWarnings("unchecked")
 184  
         private static <T> T getAttribute(final HttpSession session,
 185  
                         final String name) {
 186  1
                 return (T) session.getAttribute(name);
 187  
         }
 188  
 
 189  
 }