View Javadoc

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  /**
30   * 実行スレッドのコンテキスト情報です。
31   * 
32   * @author baba
33   * @since 1.0.0
34   */
35  public class ThreadContext {
36  
37  	/** ThreadContext を保存するスレッドローカル。 */
38  	private static final ThreadLocal<ThreadContext> CONTEXT = new ThreadLocal<ThreadContext>() {
39  
40  		@Override
41  		protected ThreadContext initialValue() {
42  			return new ThreadContext();
43  		}
44  
45  	};
46  
47  	/**
48  	 * スレッドローカルから現在の実行スレッドに関する情報を削除します。
49  	 */
50  	public static void remove() {
51  		CONTEXT.remove();
52  	}
53  
54  	/**
55  	 * 現在の実行スレッドに関連付けられたリクエストを取得します。
56  	 * 
57  	 * @return リクエスト
58  	 */
59  	public static HttpServletRequest getRequest() {
60  		return CONTEXT.get().request;
61  	}
62  
63  	/**
64  	 * 現在の実行スレッドに指定されたリクエストを関連付けます。
65  	 * 
66  	 * @param request
67  	 *            リクエスト
68  	 */
69  	public static void setRequest(final HttpServletRequest request) {
70  		CONTEXT.get().request = request;
71  	}
72  
73  	/**
74  	 * Cubby の全体的な設定情報を取得します。
75  	 * 
76  	 * @return Cubby の全体的な設定情報
77  	 */
78  	public static CubbyConfiguration getConfiguration() {
79  		return SingletonS2Container.getComponent(CubbyConfiguration.class);
80  	}
81  
82  	/**
83  	 * 現在の実行スレッドに関連付けられたリクエストに対応するメッセージ用の {@link ResourceBundle} を取得します。
84  	 * 
85  	 * @return リソースバンドル
86  	 */
87  	public static ResourceBundle getMessagesResourceBundle() {
88  		final ThreadContext context = CONTEXT.get();
89  		if (context.resourceBundle == null) {
90  			final Locale locale;
91  			if (context.request == null) {
92  				locale = Locale.getDefault();
93  			} else {
94  				locale = context.request.getLocale();
95  			}
96  			context.resourceBundle = ResourceBundleUtil.getBundle(RES_MESSAGES,
97  					locale);
98  		}
99  		return context.resourceBundle;
100 	}
101 
102 	/**
103 	 * {@link #getMessagesResourceBundle()} で取得できる {@link ResourceBundle} を変換した
104 	 * {@link Map} を取得します。
105 	 * 
106 	 * @return メッセージの {@link Map}
107 	 */
108 	public static Map<?, ?> getMessagesMap() {
109 		final ThreadContext context = CONTEXT.get();
110 		if (context.messages == null) {
111 			final ResourceBundle resourceBundle = getMessagesResourceBundle();
112 			context.messages = ResourceBundleUtil.convertMap(resourceBundle);
113 		}
114 		return context.messages;
115 	}
116 
117 	/**
118 	 * インスタンス化します。
119 	 */
120 	private ThreadContext() {
121 	}
122 
123 	/** リクエスト。 */
124 	private HttpServletRequest request;
125 
126 	/** リソースバンドル。 */
127 	private ResourceBundle resourceBundle = null;
128 
129 	/** メッセージの {@link Map} */
130 	private Map<?, ?> messages = null;
131 
132 }