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