Coverage Report - org.seasar.cubby.internal.controller.ThreadContext
 
Classes in this File Line Coverage Branch Coverage Complexity
ThreadContext
93%
40/43
81%
13/16
0
ThreadContext$Command
N/A
N/A
0
 
 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.internal.controller;
 17  
 
 18  
 import java.util.Map;
 19  
 import java.util.ResourceBundle;
 20  
 
 21  
 import javax.servlet.http.HttpServletRequest;
 22  
 import javax.servlet.http.HttpServletResponse;
 23  
 
 24  
 import org.seasar.cubby.controller.MessagesBehaviour;
 25  
 import org.seasar.cubby.spi.ContainerProvider;
 26  
 import org.seasar.cubby.spi.ProviderFactory;
 27  
 import org.seasar.cubby.spi.container.Container;
 28  
 
 29  
 /**
 30  
  * 実行スレッドのコンテキスト情報です。
 31  
  * 
 32  
  * @author baba
 33  
  * @since 1.0.0
 34  
  */
 35  
 public class ThreadContext {
 36  
 
 37  
         /** ThreadContext を保存するスレッドローカル。 */
 38  3
         private static final ThreadLocal<ThreadContext> THREAD_LOCAL = new ThreadLocal<ThreadContext>();
 39  
 
 40  
         /** 要求。 */
 41  
         private final HttpServletRequest request;
 42  
 
 43  
         /** 応答 */
 44  
         private final HttpServletResponse response;
 45  
 
 46  
         /** メッセージのリソースバンドル。 */
 47  63
         private ResourceBundle messagesResourceBundle = null;
 48  
 
 49  
         /** メッセージの {@link Map} */
 50  63
         private Map<String, Object> messages = null;
 51  
 
 52  
         /** メッセージ表示用リソースバンドルの振る舞い。 */
 53  
         private MessagesBehaviour messagesBehaviour;
 54  
 
 55  
         /**
 56  
          * インスタンス化します。
 57  
          * 
 58  
          * @param request
 59  
          *            要求
 60  
          * @param response
 61  
          *            応答
 62  
          */
 63  
         private ThreadContext(final HttpServletRequest request,
 64  63
                         final HttpServletResponse response) {
 65  63
                 this.request = request;
 66  63
                 this.response = response;
 67  63
         }
 68  
 
 69  
         /**
 70  
          * スレッドローカル変数からコンテキストを取得します。
 71  
          * 
 72  
          * @return コンテキスト
 73  
          */
 74  
         private static ThreadContext getContext() {
 75  357
                 final ThreadContext context = THREAD_LOCAL.get();
 76  357
                 if (context == null) {
 77  27
                         throw new IllegalStateException(
 78  
                                         "Could not get context from ThreadLocal. run in context scope.");
 79  
                 }
 80  330
                 return context;
 81  
         }
 82  
 
 83  
         /**
 84  
          * 現在の実行スレッドに関連付けられた要求を取得します。
 85  
          * 
 86  
          * @return 要求
 87  
          */
 88  
         public static HttpServletRequest getRequest() {
 89  63
                 return getContext().request;
 90  
         }
 91  
 
 92  
         /**
 93  
          * 現在の実行スレッドに関連付けられた応答を取得します。
 94  
          * 
 95  
          * @return 応答
 96  
          */
 97  
         public static HttpServletResponse getResponse() {
 98  27
                 return getContext().response;
 99  
         }
 100  
 
 101  
         /**
 102  
          * 現在の実行スレッドに関連付けられた要求に対応するメッセージ用の {@link ResourceBundle} を取得します。
 103  
          * 
 104  
          * @return リソースバンドル
 105  
          */
 106  
         public static ResourceBundle getMessagesResourceBundle() {
 107  255
                 final ThreadContext context = getContext();
 108  255
                 if (context.messagesResourceBundle == null) {
 109  24
                         final MessagesBehaviour messagesBehaviour = getMessagesBehaviour(context);
 110  24
                         context.messagesResourceBundle = messagesBehaviour
 111  
                                         .getBundle(context.request.getLocale());
 112  
                 }
 113  255
                 return context.messagesResourceBundle;
 114  
         }
 115  
 
 116  
         /**
 117  
          * {@link #getMessagesResourceBundle()} で取得できる {@link ResourceBundle} を変換した
 118  
          * {@link Map} を取得します。
 119  
          * 
 120  
          * @return メッセージの {@link Map}
 121  
          */
 122  
         public static Map<String, Object> getMessagesMap() {
 123  12
                 final ThreadContext context = getContext();
 124  12
                 if (context.messages == null) {
 125  9
                         final ResourceBundle bundle = getMessagesResourceBundle();
 126  9
                         final MessagesBehaviour messagesBehaviour = getMessagesBehaviour(context);
 127  9
                         context.messages = messagesBehaviour.toMap(bundle);
 128  
                 }
 129  12
                 return context.messages;
 130  
         }
 131  
 
 132  
         /**
 133  
          * メッセージ表示用リソースバンドルの振る舞いを取得します。
 134  
          * 
 135  
          * @param context
 136  
          *            実行スレッドのコンテキスト情報
 137  
          * @return メッセージ表示用リソースバンドルの振る舞い
 138  
          */
 139  
         private static MessagesBehaviour getMessagesBehaviour(
 140  
                         final ThreadContext context) {
 141  33
                 if (context.messagesBehaviour == null) {
 142  24
                         final Container container = ProviderFactory.get(
 143  
                                         ContainerProvider.class).getContainer();
 144  24
                         context.messagesBehaviour = container
 145  
                                         .lookup(MessagesBehaviour.class);
 146  
                 }
 147  33
                 return context.messagesBehaviour;
 148  
         }
 149  
 
 150  
         /**
 151  
          * 指定されたコマンドを新しいコンテキスト内で実行します。
 152  
          * 
 153  
          * @param request
 154  
          *            要求
 155  
          * @param response
 156  
          *            応答
 157  
          * @param command
 158  
          *            コンテキスト内で実行するコマンド
 159  
          * @throws Exception
 160  
          *             コマンドの実行中に例外が発生した場合
 161  
          */
 162  
         public static void runInContext(final HttpServletRequest request,
 163  
                         final HttpServletResponse response, final Command command)
 164  
                         throws Exception {
 165  63
                 if (request == null) {
 166  0
                         throw new NullPointerException("request");
 167  
                 }
 168  63
                 if (response == null) {
 169  0
                         throw new NullPointerException("response");
 170  
                 }
 171  63
                 if (command == null) {
 172  0
                         throw new NullPointerException("command");
 173  
                 }
 174  
 
 175  63
                 final ThreadContext previous = THREAD_LOCAL.get();
 176  63
                 final ThreadContext context = new ThreadContext(request, response);
 177  63
                 THREAD_LOCAL.set(context);
 178  
                 try {
 179  63
                         command.execute(request, response);
 180  
                 } finally {
 181  63
                         if (previous == null) {
 182  57
                                 THREAD_LOCAL.remove();
 183  
                         } else {
 184  6
                                 THREAD_LOCAL.set(previous);
 185  
                         }
 186  3
                 }
 187  57
         }
 188  
 
 189  
         /**
 190  
          * コンテキスト内で実行するコマンドのインターフェイスです。
 191  
          * 
 192  
          * @author baba
 193  
          */
 194  
         public interface Command {
 195  
 
 196  
                 /**
 197  
                  * コマンドを実行します。
 198  
                  * 
 199  
                  * @param request
 200  
                  *            要求
 201  
                  * @param response
 202  
                  *            応答
 203  
                  * @throws Exception
 204  
                  *             コマンドの実行中に例外が発生した場合
 205  
                  */
 206  
                 void execute(HttpServletRequest request, HttpServletResponse response)
 207  
                                 throws Exception;
 208  
 
 209  
         }
 210  
 
 211  
 }