| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| tsukuba_bunko.peko.session.Session |
|
|
| 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 | 0 | protected HashSet _sessionFlags = null; |
| 48 | ||
| 49 | /** |
|
| 50 | * システムフラグ |
|
| 51 | */ |
|
| 52 | 0 | protected HashSet _systemFlags = null; |
| 53 | ||
| 54 | /** |
|
| 55 | * システムフラグのロードタイムスタンプ |
|
| 56 | */ |
|
| 57 | 0 | protected Date _timestamp = null; |
| 58 | ||
| 59 | ||
| 60 | /** |
|
| 61 | * 現在進行中のシーンのコンテクスト |
|
| 62 | */ |
|
| 63 | 0 | protected SceneContext _sceneContext = null; |
| 64 | ||
| 65 | /** |
|
| 66 | * その他の属性 |
|
| 67 | */ |
|
| 68 | 0 | protected HashMap _attributes = new HashMap( 17 ); |
| 69 | ||
| 70 | ||
| 71 | /** |
|
| 72 | * 現在シーンを処理しているスレッド |
|
| 73 | */ |
|
| 74 | 0 | protected transient Thread _activeThread = null; |
| 75 | ||
| 76 | ||
| 77 | /** |
|
| 78 | * <code>Session</code> のインスタンスを生成します。 |
|
| 79 | */ |
|
| 80 | public Session() |
|
| 81 | { |
|
| 82 | 0 | super(); |
| 83 | 0 | } |
| 84 | ||
| 85 | ||
| 86 | /** |
|
| 87 | * 現在進行中のシーンのコンテクストを設定します。 |
|
| 88 | * @param context シーンコンテクスト |
|
| 89 | * @param processThread シーンを処理するスレッド |
|
| 90 | */ |
|
| 91 | public synchronized void setSceneContext( SceneContext context, Thread processThread ) |
|
| 92 | { |
|
| 93 | 0 | _sceneContext = context; |
| 94 | 0 | _activeThread = processThread; |
| 95 | 0 | } |
| 96 | ||
| 97 | /** |
|
| 98 | * 現在のスレッドで進行中のシーンのコンテクストを設定します。 |
|
| 99 | * @param context シーンコンテクスト |
|
| 100 | */ |
|
| 101 | public void setSceneContext( SceneContext context ) |
|
| 102 | { |
|
| 103 | 0 | setSceneContext( context, Thread.currentThread() ); |
| 104 | 0 | } |
| 105 | ||
| 106 | /** |
|
| 107 | * セッションで現在進行中のシーンのコンテクストを取得します。 |
|
| 108 | * @return シーン名 |
|
| 109 | */ |
|
| 110 | public SceneContext getSceneContext() |
|
| 111 | { |
|
| 112 | 0 | return _sceneContext; |
| 113 | } |
|
| 114 | ||
| 115 | ||
| 116 | /** |
|
| 117 | * セッションフラグを立てます。 |
|
| 118 | * @param flagID 立てるフラグのフラグ ID |
|
| 119 | */ |
|
| 120 | public void declareSessionFlag( String flagID ) |
|
| 121 | { |
|
| 122 | 0 | Logger.debug( "[session] declare session flag: " + flagID ); |
| 123 | 0 | if( isActiveThread() ) { |
| 124 | 0 | _sessionFlags.add( flagID ); |
| 125 | 0 | } |
| 126 | else { |
|
| 127 | 0 | Logger.debug( "[session] canceled declaring session flag: " + flagID ); |
| 128 | } |
|
| 129 | 0 | } |
| 130 | ||
| 131 | /** |
|
| 132 | * セッションフラグを降ろします。 |
|
| 133 | * @param flagID 降ろすフラグのフラグ ID |
|
| 134 | */ |
|
| 135 | public void undeclareSessionFlag( String flagID ) |
|
| 136 | { |
|
| 137 | 0 | Logger.debug( "[session] undeclare session flag: " + flagID ); |
| 138 | 0 | if( isActiveThread() ) { |
| 139 | 0 | _sessionFlags.remove( flagID ); |
| 140 | 0 | } |
| 141 | else { |
|
| 142 | 0 | Logger.debug( "[session] canceled undeclaring session flag: " + flagID ); |
| 143 | } |
|
| 144 | 0 | } |
| 145 | ||
| 146 | /** |
|
| 147 | * 指定されたフラグがセッションフラグとして立てられているかどうかを判定します。 |
|
| 148 | * @param flagID 判定するフラグ ID |
|
| 149 | * @return フラグが立っている場合 <code>true</code>、それ以外 <code>false</code> |
|
| 150 | */ |
|
| 151 | public boolean isDeclaredSessionFlag( String flagID ) |
|
| 152 | { |
|
| 153 | 0 | return _sessionFlags.contains( flagID ); |
| 154 | } |
|
| 155 | ||
| 156 | /** |
|
| 157 | * システムフラグを立てます。 |
|
| 158 | * @param flagID 立てるフラグのフラグ ID |
|
| 159 | */ |
|
| 160 | public void declareSystemFlag( String flagID ) |
|
| 161 | { |
|
| 162 | 0 | Logger.debug( "[session] declare system flag: " + flagID ); |
| 163 | 0 | if( isActiveThread() ) { |
| 164 | 0 | _systemFlags.add( flagID ); |
| 165 | 0 | } |
| 166 | else { |
|
| 167 | 0 | Logger.debug( "[session] canceled declaring system flag: " + flagID ); |
| 168 | } |
|
| 169 | 0 | } |
| 170 | ||
| 171 | /** |
|
| 172 | * システムフラグを降ろします。 |
|
| 173 | * @param flagID 降ろすフラグのフラグ ID |
|
| 174 | */ |
|
| 175 | public void undeclareSystemFlag( String flagID ) |
|
| 176 | { |
|
| 177 | 0 | Logger.debug( "[session] undeclare system flag: " + flagID ); |
| 178 | 0 | if( isActiveThread() ) { |
| 179 | 0 | _systemFlags.remove( flagID ); |
| 180 | 0 | } |
| 181 | else { |
|
| 182 | 0 | Logger.debug( "[session] canceled undeclaring system flag: " + flagID ); |
| 183 | } |
|
| 184 | 0 | } |
| 185 | ||
| 186 | /** |
|
| 187 | * 指定されたフラグがシステムフラグとして立てられているかどうかを判定します。 |
|
| 188 | * @param flagID 判定するフラグ ID |
|
| 189 | * @return フラグが立っている場合 <code>true</code>、それ以外 <code>false</code> |
|
| 190 | */ |
|
| 191 | public boolean isDeclaredSystemFlag( String flagID ) |
|
| 192 | { |
|
| 193 | 0 | return _systemFlags.contains( flagID ); |
| 194 | } |
|
| 195 | ||
| 196 | ||
| 197 | /** |
|
| 198 | * セッションフラグ集合を設定します。 |
|
| 199 | * @param flagSet フラグ集合 |
|
| 200 | */ |
|
| 201 | public void setSessionFlagSet( HashSet flagSet ) |
|
| 202 | { |
|
| 203 | 0 | _sessionFlags = flagSet; |
| 204 | 0 | } |
| 205 | ||
| 206 | /** |
|
| 207 | * セッションフラグ集合を取得します。 |
|
| 208 | * @return フラグ集合 |
|
| 209 | */ |
|
| 210 | public HashSet getSessionFlagSet() |
|
| 211 | { |
|
| 212 | 0 | return _sessionFlags; |
| 213 | } |
|
| 214 | ||
| 215 | /** |
|
| 216 | * システムフラグ集合を設定した時刻を取得します。 |
|
| 217 | * @return システムフラグ集合を設定した時刻 |
|
| 218 | */ |
|
| 219 | public Date getTimestamp() |
|
| 220 | { |
|
| 221 | 0 | 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 | 0 | if( (_timestamp == null) || _timestamp.before(timestamp) ) { |
| 232 | 0 | _systemFlags = flagSet; |
| 233 | 0 | _timestamp = timestamp; |
| 234 | } |
|
| 235 | 0 | } |
| 236 | ||
| 237 | /** |
|
| 238 | * システムフラグ集合を取得します。 |
|
| 239 | * @return フラグ集合 |
|
| 240 | */ |
|
| 241 | public HashSet getSystemFlagSet() |
|
| 242 | { |
|
| 243 | 0 | return _systemFlags; |
| 244 | } |
|
| 245 | ||
| 246 | ||
| 247 | /** |
|
| 248 | * セッションに属性を設定します。 |
|
| 249 | * @param name 属性名 |
|
| 250 | * @param value 属性値 |
|
| 251 | */ |
|
| 252 | public void setSessionAttribute( String name, Serializable value ) |
|
| 253 | { |
|
| 254 | 0 | _attributes.put( name, value ); |
| 255 | 0 | } |
| 256 | ||
| 257 | /** |
|
| 258 | * セッションの属性を取得します。 |
|
| 259 | * @param name 属性名 |
|
| 260 | * @return 属性値 |
|
| 261 | */ |
|
| 262 | public Object getSessionAttribute( String name ) |
|
| 263 | { |
|
| 264 | 0 | return _attributes.get( name ); |
| 265 | } |
|
| 266 | ||
| 267 | ||
| 268 | /** |
|
| 269 | * 現在のスレッドがシーン処理中のスレッドかどうかを判定します。 |
|
| 270 | * @return 現在のスレッドがシーン処理中のスレッドの場合 <code>true</code>、それ以外の場合 <code>false</code> |
|
| 271 | */ |
|
| 272 | protected boolean isActiveThread() |
|
| 273 | { |
|
| 274 | 0 | return (_activeThread == Thread.currentThread()); |
| 275 | } |
|
| 276 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |