Coverage Report - org.seasar.cubby.controller.ThreadContext
 
Classes in this File Line Coverage Branch Coverage Complexity
ThreadContext
100%
24/24
83%
5/6
0
ThreadContext$1
100%
2/2
N/A
0
 
 1  
 /*
 2  
  * Copyright 2004-2008 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.controller;
 17  
 
 18  
 import static org.seasar.cubby.CubbyConstants.RES_MESSAGES;
 19  
 
 20  
 import java.util.Locale;
 21  
 import java.util.Map;
 22  
 import java.util.ResourceBundle;
 23  
 
 24  
 import javax.servlet.http.HttpServletRequest;
 25  
 
 26  
 import org.seasar.framework.container.SingletonS2Container;
 27  
 import org.seasar.framework.util.ResourceBundleUtil;
 28  
 
 29  2
 public class ThreadContext {
 30  
 
 31  1
         private static final ThreadLocal<ThreadContext> CONTEXT = new ThreadLocal<ThreadContext>() {
 32  
 
 33  
                 @Override
 34  3
                 protected ThreadContext initialValue() {
 35  2
                         return new ThreadContext();
 36  
                 }
 37  
 
 38  
         };
 39  
 
 40  
         public static void remove() {
 41  1
                 CONTEXT.remove();
 42  1
         }
 43  
 
 44  
         public static HttpServletRequest getRequest() {
 45  12
                 return CONTEXT.get().request;
 46  
         }
 47  
 
 48  
         public static void setRequest(final HttpServletRequest request) {
 49  6
                 CONTEXT.get().request = request;
 50  6
         }
 51  
 
 52  
         public static CubbyConfiguration getConfiguration() {
 53  76
                 return SingletonS2Container.getComponent(CubbyConfiguration.class);
 54  
         }
 55  
 
 56  
         public static ResourceBundle getMessagesResourceBundle() {
 57  12
                 final ThreadContext context = CONTEXT.get();
 58  12
                 if (context.resourceBundle == null) {
 59  
                         final Locale locale;
 60  2
                         if (context.request == null) {
 61  1
                                 locale = Locale.getDefault();
 62  
                         } else {
 63  1
                                 locale = context.request.getLocale();
 64  
                         }
 65  2
                         context.resourceBundle = ResourceBundleUtil.getBundle(RES_MESSAGES,
 66  
                                         locale);
 67  
                 }
 68  12
                 return context.resourceBundle;
 69  
         }
 70  
 
 71  
         public static Map<?, ?> getMessagesMap() {
 72  1
                 final ThreadContext context = CONTEXT.get();
 73  1
                 if (context.messages == null) {
 74  1
                         final ResourceBundle resourceBundle = getMessagesResourceBundle();
 75  1
                         context.messages = ResourceBundleUtil.convertMap(resourceBundle);
 76  
                 }
 77  1
                 return context.messages;
 78  
         }
 79  
 
 80  2
         private ThreadContext() {
 81  2
         }
 82  
 
 83  
         private HttpServletRequest request;
 84  
 
 85  2
         private ResourceBundle resourceBundle = null;
 86  
 
 87  2
         private Map<?, ?> messages = null;
 88  
 
 89  
 }