1 21 22 package org.opensubsystems.core.util; 23 24 import java.security.Principal ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import java.util.Stack ; 28 29 import org.opensubsystems.core.data.DataObject; 30 31 45 public class CallContext 46 { 47 49 52 private static final String IMPL_LOCK = "IMPL_LOCK"; 53 54 56 59 private static CallContext s_defaultInstance; 60 61 63 76 private ThreadLocal m_currentUser; 77 78 82 private ThreadLocal m_currentSession; 83 84 88 private ThreadLocal m_messages; 89 90 93 private ThreadLocal m_cache; 94 95 97 102 public static CallContext getInstance( 103 ) 104 { 105 if (s_defaultInstance == null) 106 { 107 synchronized (IMPL_LOCK) 110 { 111 if (s_defaultInstance == null) 112 { 113 setInstance(new CallContext()); 114 } 115 } 116 } 117 118 return s_defaultInstance; 119 } 120 121 128 public static void setInstance( 129 CallContext defaultInstance 130 ) 131 { 132 if (GlobalConstants.ERROR_CHECKING) 133 { 134 135 assert defaultInstance != null : "Default instance cannot be null"; 136 } 137 138 synchronized (IMPL_LOCK) 139 { 140 s_defaultInstance = defaultInstance; 141 } 142 } 143 144 146 149 public CallContext( 150 ) 151 { 152 m_currentUser = new InheritableThreadLocal () 155 { 156 protected Object childValue( 157 Object parentValue 158 ) 159 { 160 Stack childStack = null; 161 162 if ((parentValue != null) 163 && (!((Stack )parentValue).isEmpty())) 164 { 165 childStack = new Stack (); 167 childStack.addAll((Stack )parentValue); 168 } 169 170 return childStack; 171 } 172 }; 173 m_currentSession = new InheritableThreadLocal () 174 { 175 protected Object childValue( 176 Object parentValue 177 ) 178 { 179 Stack childStack = null; 180 181 if ((parentValue != null) 182 && (!((Stack )parentValue).isEmpty())) 183 { 184 childStack = new Stack (); 186 childStack.addAll((Stack )parentValue); 187 } 188 189 return childStack; 190 } 191 }; 192 m_messages = new ThreadLocal (); 197 m_cache = new ThreadLocal (); 198 } 199 200 202 207 public Principal getCurrentUser( 208 ) 209 { 210 Stack userStack = (Stack )m_currentUser.get(); 211 212 if ((userStack == null) || (userStack.isEmpty())) 213 { 214 return null; 215 } 216 217 return (Principal )userStack.peek(); 218 } 219 220 227 public String getCurrentSession( 228 ) 229 { 230 Stack sessionStack = (Stack )m_currentSession.get(); 231 232 if ((sessionStack == null) || (sessionStack.isEmpty())) 233 { 234 return null; 235 } 236 237 return (String )sessionStack.peek(); 238 } 239 240 245 public int getCurrentUserId( 246 ) 247 { 248 return DataObject.NEW_ID; 249 } 250 251 258 public void setCurrentUserAndSession( 259 Principal user, 260 String session 261 ) 262 { 263 Stack userStack = (Stack )(m_currentUser.get()); 264 if (userStack == null) 265 { 266 m_currentUser.set(new Stack ()); 267 userStack = (Stack )(m_currentUser.get()); 268 } 269 userStack.push(user); 270 271 Stack sessionStack = (Stack )(m_currentSession.get()); 272 if (sessionStack == null) 273 { 274 m_currentSession.set(new Stack ()); 275 sessionStack = (Stack )(m_currentSession.get()); 276 } 277 sessionStack.push(session); 278 } 279 280 284 public void resetCurrentUserAndSession( 285 ) 286 { 287 Stack userStack = (Stack )(m_currentUser.get()); 288 if ((userStack != null) && (!userStack.isEmpty())) 289 { 290 userStack.pop(); 291 } 292 Stack sessionStack = (Stack )(m_currentSession.get()); 293 if ((sessionStack != null) && (!sessionStack.isEmpty())) 294 { 295 sessionStack.pop(); 296 } 297 } 298 299 306 public int getCurrentDomainId( 307 ) 308 { 309 return DataObject.NEW_ID; 310 } 311 312 318 public Messages getMessages( 319 ) 320 { 321 Messages msgs = null; 322 323 msgs = (Messages)m_messages.get(); 324 325 if (msgs == null) 326 { 327 msgs = new Messages(); 328 setMessages(msgs); 329 } 330 331 return msgs; 332 } 333 334 340 protected void setMessages( 341 Messages msgs 342 ) 343 { 344 m_messages.set(msgs); 345 } 346 347 351 public void resetMessages( 352 ) 353 { 354 m_messages.set(null); 355 } 356 357 364 public Map getCache( 365 String strCacheKey 366 ) 367 { 368 Map cache; 369 Map customcache; 370 371 cache = (Map )m_cache.get(); 372 373 if (cache == null) 374 { 375 cache = new HashMap (); 376 setCache(cache); 377 } 378 customcache = (Map )cache.get(strCacheKey); 379 if (customcache == null) 380 { 381 customcache = new HashMap (); 382 cache.put(strCacheKey, customcache); 383 } 384 385 return customcache; 386 } 387 388 393 protected void setCache( 394 Map cache 395 ) 396 { 397 m_cache.set(cache); 398 } 399 400 403 public void resetCache( 404 ) 405 { 406 m_cache.set(null); 407 } 408 409 412 public void reset( 413 ) 414 { 415 resetCurrentUserAndSession(); 416 resetMessages(); 417 resetCache(); 418 } 419 } 420 | Popular Tags |