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  1
         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  21
         private ResourceBundle messagesResourceBundle = null;
 48  
 
 49  
         /** メッセージの {@link Map} */
 50  21
         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  21
                         final HttpServletResponse response) {
 65  21
                 this.request = request;
 66  21
                 this.response = response;
 67  21
         }
 68  
 
 69  
         /**
 70  
          * スレッドローカル変数からコンテキストを取得します。
 71  
          * 
 72  
          * @return コンテキスト
 73  
          */
 74  
         private static ThreadContext getContext() {
 75  119
                 final ThreadContext context = THREAD_LOCAL.get();
 76  119
                 if (context == null) {
 77  9
                         throw new IllegalStateException(
 78  
                                         "Could not get context from ThreadLocal. run in context scope.");
 79  
                 }
 80  110
                 return context;
 81  
         }
 82  
 
 83  
         /**
 84  
          * 現在の実行スレッドに関連付けられた要求を取得します。
 85  
          * 
 86  
          * @return 要求
 87  
          */
 88  
         public static HttpServletRequest getRequest() {
 89  21
                 return getContext().request;
 90  
         }
 91  
 
 92  
         /**
 93  
          * 現在の実行スレッドに関連付けられた応答を取得します。
 94  
          * 
 95  
          * @return 応答
 96  
          */
 97  
         public static HttpServletResponse getResponse() {
 98  9
                 return getContext().response;
 99  
         }
 100  
 
 101  
         /**
 102  
          * 現在の実行スレッドに関連付けられた要求に対応するメッセージ用の {@link ResourceBundle} を取得します。
 103  
          * 
 104  
          * @return リソースバンドル
 105  
          */
 106  
         public static ResourceBundle getMessagesResourceBundle() {
 107  85
                 final ThreadContext context = getContext();
 108  85
                 if (context.messagesResourceBundle == null) {
 109  8
                         final MessagesBehaviour messagesBehaviour = getMessagesBehaviour(context);
 110  8
                         context.messagesResourceBundle = messagesBehaviour
 111  
                                         .getBundle(context.request.getLocale());
 112  
                 }
 113  85
                 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  4
                 final ThreadContext context = getContext();
 124  4
                 if (context.messages == null) {
 125  3
                         final ResourceBundle bundle = getMessagesResourceBundle();
 126  3
                         final MessagesBehaviour messagesBehaviour = getMessagesBehaviour(context);
 127  3
                         context.messages = messagesBehaviour.toMap(bundle);
 128  
                 }
 129  4
                 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  11
                 if (context.messagesBehaviour == null) {
 142  8
                         final Container container = ProviderFactory.get(
 143  
                                         ContainerProvider.class).getContainer();
 144  8
                         context.messagesBehaviour = container
 145  
                                         .lookup(MessagesBehaviour.class);
 146  
                 }
 147  11
                 return context.messagesBehaviour;
 148  
         }
 149  
 
 150  
         /**
 151  
          * 指定されたコマンドを新しいコンテキスト内で実行します。
 152  
          * 
 153  
          * @param <T>
 154  
          *            コマンドの戻り値
 155  
          * @param request
 156  
          *            要求
 157  
          * @param response
 158  
          *            応答
 159  
          * @param command
 160  
          *            コンテキスト内で実行するコマンド
 161  
          * @throws Exception
 162  
          *             コマンドの実行中に例外が発生した場合
 163  
          */
 164  
         public static void runInContext(final HttpServletRequest request,
 165  
                         final HttpServletResponse response, final Command command)
 166  
                         throws Exception {
 167  21
                 if (request == null) {
 168  0
                         throw new NullPointerException("request");
 169  
                 }
 170  21
                 if (response == null) {
 171  0
                         throw new NullPointerException("response");
 172  
                 }
 173  21
                 if (command == null) {
 174  0
                         throw new NullPointerException("command");
 175  
                 }
 176  
 
 177  21
                 final ThreadContext previous = THREAD_LOCAL.get();
 178  21
                 final ThreadContext context = new ThreadContext(request, response);
 179  21
                 THREAD_LOCAL.set(context);
 180  
                 try {
 181  21
                         command.execute(request, response);
 182  
                 } finally {
 183  21
                         if (previous == null) {
 184  19
                                 THREAD_LOCAL.remove();
 185  
                         } else {
 186  2
                                 THREAD_LOCAL.set(previous);
 187  
                         }
 188  1
                 }
 189  19
         }
 190  
 
 191  
         /**
 192  
          * コンテキスト内で実行するコマンドのインターフェイスです。
 193  
          * 
 194  
          * @author baba
 195  
          * @param <T>
 196  
          *            コマンドの戻り値
 197  
          */
 198  
         public interface Command {
 199  
 
 200  
                 /**
 201  
                  * コマンドを実行します。
 202  
                  * 
 203  
                  * @param request
 204  
                  *            要求
 205  
                  * @param response
 206  
                  *            応答
 207  
                  * @throws Exception
 208  
                  *             コマンドの実行中に例外が発生した場合
 209  
                  */
 210  
                 void execute(HttpServletRequest request, HttpServletResponse response)
 211  
                                 throws Exception;
 212  
 
 213  
         }
 214  
 
 215  
 }