Coverage Report - org.seasar.cubby.internal.controller.ThreadContext
 
Classes in this File Line Coverage Branch Coverage Complexity
ThreadContext
92%
26/28
80%
8/10
2.143
 
 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;
 18  
 
 19  
 import javax.servlet.http.HttpServletRequest;
 20  
 import javax.servlet.http.HttpServletResponse;
 21  
 
 22  
 /**
 23  
  * 実行スレッドのコンテキスト情報です。
 24  
  * 
 25  
  * @author baba
 26  
  */
 27  
 public class ThreadContext {
 28  
 
 29  
         /** ThreadContext を保存するスレッドローカル。 */
 30  1
         private static final ThreadLocal<ThreadContext> THREAD_LOCAL = new ThreadLocal<ThreadContext>();
 31  
 
 32  1
         private static final ThreadLocal<ThreadContext> PREVIOUS = new ThreadLocal<ThreadContext>();
 33  
 
 34  
         /** 要求。 */
 35  
         private final HttpServletRequest request;
 36  
 
 37  
         /** 応答 */
 38  
         private final HttpServletResponse response;
 39  
 
 40  
         /**
 41  
          * インスタンス化します。
 42  
          * 
 43  
          * @param request
 44  
          *            要求
 45  
          * @param response
 46  
          *            応答
 47  
          */
 48  
         private ThreadContext(final HttpServletRequest request,
 49  18
                         final HttpServletResponse response) {
 50  18
                 this.request = request;
 51  18
                 this.response = response;
 52  18
         }
 53  
 
 54  
         /**
 55  
          * スレッドローカル変数からコンテキストを取得します。
 56  
          * 
 57  
          * @return コンテキスト
 58  
          */
 59  
         public static ThreadContext getCurrentContext() {
 60  110
                 final ThreadContext context = THREAD_LOCAL.get();
 61  110
                 if (context == null) {
 62  8
                         throw new IllegalStateException(
 63  
                                         "Could not get context from ThreadLocal. run in context scope.");
 64  
                 }
 65  102
                 return context;
 66  
         }
 67  
 
 68  
         /**
 69  
          * 現在の実行スレッドに関連付けられた要求を取得します。
 70  
          * 
 71  
          * @return 要求
 72  
          */
 73  
         public HttpServletRequest getRequest() {
 74  95
                 return request;
 75  
         }
 76  
 
 77  
         /**
 78  
          * 現在の実行スレッドに関連付けられた応答を取得します。
 79  
          * 
 80  
          * @return 応答
 81  
          */
 82  
         public HttpServletResponse getResponse() {
 83  8
                 return response;
 84  
         }
 85  
 
 86  
         /**
 87  
          * コンテキストに入ります。
 88  
          * 
 89  
          * @param request
 90  
          *            要求
 91  
          * @param response
 92  
          *            応答
 93  
          */
 94  
         public static void enter(final HttpServletRequest request,
 95  
                         final HttpServletResponse response) {
 96  18
                 if (request == null) {
 97  0
                         throw new NullPointerException("request");
 98  
                 }
 99  18
                 if (response == null) {
 100  0
                         throw new NullPointerException("response");
 101  
                 }
 102  
 
 103  18
                 final ThreadContext previous = THREAD_LOCAL.get();
 104  18
                 if (previous != null) {
 105  4
                         PREVIOUS.set(previous);
 106  
                 }
 107  18
                 final ThreadContext context = new ThreadContext(request, response);
 108  18
                 THREAD_LOCAL.set(context);
 109  18
         }
 110  
 
 111  
         /**
 112  
          * コンテキストを抜けます。
 113  
          */
 114  
         public static void exit() {
 115  18
                 final ThreadContext previous = PREVIOUS.get();
 116  18
                 if (previous != null) {
 117  8
                         THREAD_LOCAL.set(previous);
 118  
                 }
 119  18
         }
 120  
 
 121  
         /**
 122  
          * コンテキストを削除します。
 123  
          */
 124  
         public static void remove() {
 125  14
                 THREAD_LOCAL.remove();
 126  14
         }
 127  
 
 128  
 }