View Javadoc

1   /*
2    * All Rights Reserved.
3    * Copyright (C) 1999-2005 Tsukuba Bunko.
4    *
5    * Licensed under the BSD License ("the License"); you may not use
6    * this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *       http://www.tsukuba-bunko.org/licenses/LICENSE.txt
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   * $Id: Session.java,v 1.2 2005/07/23 19:06:28 ppoi Exp $
18   */
19  package tsukuba_bunko.peko.session;
20  
21  import	java.io.Serializable;
22  
23  import	java.util.Date;
24  import	java.util.HashMap;
25  import	java.util.HashSet;
26  
27  import	tsukuba_bunko.peko.Logger;
28  
29  import	tsukuba_bunko.peko.scenario.SceneContext;
30  
31  
32  /***
33   * 現在のセッションの情報を格納します。
34   * @author	$Author: ppoi $
35   * @version	$Revision: 1.2 $
36   */
37  public class Session	implements Serializable	{
38  
39  	/***
40  	 * serial version UID
41  	 */
42  	private static final long	serialVersionUID	= 2010059632489065304L;
43  
44  	/***
45  	 * セッションフラグ
46  	 */
47  	protected HashSet	_sessionFlags = null;
48  
49  	/***
50  	 * システムフラグ
51  	 */
52  	protected HashSet	_systemFlags = null;
53  
54  	/***
55  	 * システムフラグのロードタイムスタンプ
56  	 */
57  	protected Date	_timestamp = null;
58  
59  
60  	/***
61  	 * 現在進行中のシーンのコンテクスト
62  	 */
63  	protected SceneContext	_sceneContext = null;
64  
65  	/***
66  	 * その他の属性
67  	 */
68  	protected HashMap	_attributes = new HashMap( 17 );
69  
70  
71  	/***
72  	 * 現在シーンを処理しているスレッド
73  	 */
74  	protected transient Thread	_activeThread = null;
75  
76  
77  	/***
78  	 * <code>Session</code> のインスタンスを生成します。
79  	 */
80  	public Session()
81  	{
82  		super();
83  	}
84  
85  
86  	/***
87  	 * 現在進行中のシーンのコンテクストを設定します。
88  	 * @param	context	シーンコンテクスト
89  	 * @param	processThread	シーンを処理するスレッド
90  	 */
91  	public synchronized void setSceneContext( SceneContext context, Thread processThread )
92  	{
93  		_sceneContext = context;
94  		_activeThread = processThread;
95  	}
96  
97  	/***
98  	 * 現在のスレッドで進行中のシーンのコンテクストを設定します。
99  	 * @param	context	シーンコンテクスト
100 	 */
101 	public void setSceneContext( SceneContext context )
102 	{
103 		setSceneContext( context, Thread.currentThread() );
104 	}
105 
106 	/***
107 	 * セッションで現在進行中のシーンのコンテクストを取得します。
108 	 * @return	シーン名
109 	 */
110 	public SceneContext getSceneContext()
111 	{
112 		return _sceneContext;
113 	}
114 
115 
116 	/***
117 	 * セッションフラグを立てます。
118 	 * @param	flagID	立てるフラグのフラグ ID
119 	 */
120 	public void declareSessionFlag( String flagID )
121 	{
122 		Logger.debug( "[session] declare session flag: " + flagID );
123 		if( isActiveThread() )	{
124 			_sessionFlags.add( flagID );
125 		}
126 		else	{
127 			Logger.debug( "[session] canceled declaring session flag: " + flagID );
128 		}
129 	}
130 
131 	/***
132 	 * セッションフラグを降ろします。
133 	 * @param	flagID	降ろすフラグのフラグ ID
134 	 */
135 	public void undeclareSessionFlag( String flagID )
136 	{
137 		Logger.debug( "[session] undeclare session flag: " + flagID );
138 		if( isActiveThread() )	{
139 			_sessionFlags.remove( flagID );
140 		}
141 		else	{
142 			Logger.debug( "[session] canceled undeclaring session flag: " + flagID );
143 		}
144 	}
145 
146 	/***
147 	 * 指定されたフラグがセッションフラグとして立てられているかどうかを判定します。
148 	 * @param	flagID	判定するフラグ ID
149 	 * @return	フラグが立っている場合 <code>true</code>、それ以外 <code>false</code>
150 	 */
151 	public boolean isDeclaredSessionFlag( String flagID )
152 	{
153 		return _sessionFlags.contains( flagID );
154 	}
155 
156 	/***
157 	 * システムフラグを立てます。
158 	 * @param	flagID	立てるフラグのフラグ ID
159 	 */
160 	public void declareSystemFlag( String flagID )
161 	{
162 		Logger.debug( "[session] declare system flag: " + flagID );
163 		if( isActiveThread() )	{
164 			_systemFlags.add( flagID );
165 		}
166 		else	{
167 			Logger.debug( "[session] canceled declaring system flag: " + flagID );
168 		}
169 	}
170 
171 	/***
172 	 * システムフラグを降ろします。
173 	 * @param	flagID	降ろすフラグのフラグ ID
174 	 */
175 	public void undeclareSystemFlag( String flagID )
176 	{
177 		Logger.debug( "[session] undeclare system flag: " + flagID );
178 		if( isActiveThread() )	{
179 			_systemFlags.remove( flagID );
180 		}
181 		else	{
182 			Logger.debug( "[session] canceled undeclaring system flag: " + flagID );
183 		}
184 	}
185 
186 	/***
187 	 * 指定されたフラグがシステムフラグとして立てられているかどうかを判定します。
188 	 * @param	flagID	判定するフラグ ID
189 	 * @return	フラグが立っている場合 <code>true</code>、それ以外 <code>false</code>
190 	 */
191 	public boolean isDeclaredSystemFlag( String flagID )
192 	{
193 		return _systemFlags.contains( flagID );
194 	}
195 
196 
197 	/***
198 	 * セッションフラグ集合を設定します。
199 	 * @param	flagSet	フラグ集合
200 	 */
201 	public void setSessionFlagSet( HashSet flagSet )
202 	{
203 		_sessionFlags = flagSet;
204 	}
205 
206 	/***
207 	 * セッションフラグ集合を取得します。
208 	 * @return	フラグ集合
209 	 */
210 	public HashSet getSessionFlagSet()
211 	{
212 		return _sessionFlags;
213 	}
214 
215 	/***
216 	 * システムフラグ集合を設定した時刻を取得します。
217 	 * @return	システムフラグ集合を設定した時刻
218 	 */
219 	public Date getTimestamp()
220 	{
221 		return _timestamp;
222 	}
223 
224 	/***
225 	 * システムフラグ集合を設定します。<code>timestamp</code> が、現在のコンテクストに設定されている時刻より古い場合は設定は反映されません。
226 	 * @param	flagSet	フラグ集合
227 	 * @param	timestamp	フラグ集合のタイムスタンプ
228 	 */
229 	public void setSystemFlagSet( HashSet flagSet, Date timestamp )
230 	{
231 		if( (_timestamp == null) || _timestamp.before(timestamp) )	{
232 			_systemFlags = flagSet;
233 			_timestamp = timestamp;
234 		}
235 	}
236 
237 	/***
238 	 * システムフラグ集合を取得します。
239 	 * @return	フラグ集合
240 	 */
241 	public HashSet getSystemFlagSet()
242 	{
243 		return _systemFlags;
244 	}
245 
246 
247 	/***
248 	 * セッションに属性を設定します。
249 	 * @param	name	属性名
250 	 * @param	value	属性値
251 	 */
252 	public void setSessionAttribute( String name, Serializable value )
253 	{
254 		_attributes.put( name, value );
255 	}
256 
257 	/***
258 	 * セッションの属性を取得します。
259 	 * @param	name	属性名
260 	 * @return	属性値
261 	 */
262 	public Object getSessionAttribute( String name )
263 	{
264 		return _attributes.get( name );
265 	}
266 
267 
268 	/***
269 	 * 現在のスレッドがシーン処理中のスレッドかどうかを判定します。
270 	 * @return	現在のスレッドがシーン処理中のスレッドの場合 <code>true</code>、それ以外の場合 <code>false</code>
271 	 */
272 	protected boolean isActiveThread()
273 	{
274 		return (_activeThread == Thread.currentThread());
275 	}
276 }