1 16 17 package org.apache.jetspeed.services.statemanager; 19 20 import java.util.Iterator ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 import java.util.Set ; 25 import java.util.Collections ; 26 import javax.servlet.ServletConfig ; 27 import javax.servlet.http.HttpSession ; 28 29 import org.apache.turbine.services.TurbineBaseService; 30 import org.apache.turbine.services.InitializationException; 31 import org.apache.turbine.util.RunData; 32 33 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 34 import org.apache.jetspeed.services.logging.JetspeedLogger; 35 import org.apache.jetspeed.services.statemanager.StateManagerService; 36 import org.apache.jetspeed.services.statemanager.SessionStateBindingListener; 37 38 50 public abstract class BaseStateManagerService 51 extends TurbineBaseService 52 implements StateManagerService 53 { 54 57 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BaseStateManagerService.class.getName()); 58 59 60 protected Map m_httpSessions = null; 61 62 65 66 69 protected abstract void initStates(); 70 71 74 protected abstract void shutdownStates(); 75 76 81 protected abstract Map getState( String key ); 82 83 88 protected abstract void addState( String key, Map state ); 89 90 94 protected abstract void removeState( String key ); 95 96 101 protected abstract String [] getStateKeys( String start ); 102 103 108 protected void retireAttributes( String key, Map state ) 109 { 110 if (state == null) return; 111 112 Set attributes = state.entrySet(); 113 synchronized (state) 114 { 115 Iterator i = attributes.iterator(); 116 while (i.hasNext()) 117 { 118 Map.Entry attribute = (Map.Entry ) i.next(); 119 unBindAttributeValue(key, (String )attribute.getKey(), attribute.getValue()); 120 } 121 } 122 123 state.clear(); 125 126 } 128 134 protected void unBindAttributeValue( String stateKey, String attributeName, Object attribute ) 135 { 136 if ((attribute != null) && (attribute instanceof SessionStateBindingListener)) 138 { 139 try 140 { 141 ((SessionStateBindingListener)attribute) 142 .valueUnbound(stateKey, attributeName); 143 } 144 catch (Throwable e) 145 { 146 logger.warn("JetspeedStateManagerService.unBindAttributeValue: unbinding exception: ", e); 147 } 148 } 149 150 } 152 158 protected void bindAttributeValue( String stateKey, String attributeName, Object attribute ) 159 { 160 if ((attribute != null) && (attribute instanceof SessionStateBindingListener)) 162 { 163 try 164 { 165 ((SessionStateBindingListener)attribute) 166 .valueBound(stateKey, attributeName); 167 } 168 catch (Throwable e) 169 { 170 logger.warn("JetspeedStateManagerService.bindAttributeValue: unbinding exception: ", e); 171 } 172 } 173 174 } 176 179 180 188 public void init( ServletConfig config ) 189 throws InitializationException 190 { 191 super.init(config); 192 193 } 195 202 public void init( RunData data ) 203 throws InitializationException 204 { 205 super.init(data); 206 207 } 209 219 public void init() 220 throws InitializationException 221 { 222 super.init(); 223 224 m_httpSessions = Collections.synchronizedMap(new HashMap ()); 226 227 initStates(); 229 230 } 232 238 public void shutdown() 239 { 240 m_httpSessions.clear(); 241 m_httpSessions = null; 242 shutdownStates(); 243 super.shutdown(); 244 245 } 247 250 251 257 public Object getAttribute ( String key, String name ) 258 { 259 Map state = getState(key); 260 if (state == null) return null; 261 return state.get(name); 262 263 } 265 271 public void setAttribute( String key, String name, Object value ) 272 { 273 Map state = getState(key); 274 if (state == null) 275 { 276 state = Collections.synchronizedMap(new HashMap ()); 278 addState(key, state); 279 } 280 281 Object old = getAttribute(key, name); 283 284 state.put(name, value); 286 287 if (old != null) 289 { 290 unBindAttributeValue(key, name, old); 291 } 292 293 bindAttributeValue(key, name, value); 295 296 } 298 303 public void removeAttribute( String key, String name ) 304 { 305 Map state = getState(key); 306 if (state == null) return; 307 308 Object old = getAttribute(key, name); 310 311 state.remove(name); 313 314 if (state.isEmpty()) 316 { 317 removeState(key); 318 } 319 320 if (old != null) 322 { 323 unBindAttributeValue(key, name, old); 324 } 325 326 } 328 332 public void clear( String key ) 333 { 334 Map state = getState(key); 335 if (state == null) return; 336 337 retireAttributes(key, state); 339 340 removeState(key); 342 343 } 345 350 public String [] getAttributeNames( String key ) 351 { 352 Map state = (Map ) getState(key); 353 if (state == null) return null; 354 if (state.size() == 0) return null; 355 356 return (String []) state.keySet().toArray(new String [state.size()]); 358 359 } 361 366 public SessionState getSessionState( String key ) 367 { 368 return new MySessionState(key, this); 369 370 } 372 377 public SessionState getCurrentSessionState() 378 { 379 HttpSession session = (HttpSession ) m_httpSessions.get(Thread.currentThread()); 380 if (session == null) return null; 381 382 return getSessionState(session.getId()); 383 384 } 386 391 public SessionState getCurrentSessionState( String key ) 392 { 393 HttpSession session = (HttpSession ) m_httpSessions.get(Thread.currentThread()); 394 if (session == null) return null; 395 396 return getSessionState(session.getId() + key); 397 398 } 400 404 public synchronized void retireState( String keyStart ) 405 { 406 String keys[] = getStateKeys(keyStart); 408 if (keys == null) return; 409 410 for (int i = 0; i < keys.length; i++) 412 { 413 clear(keys[i]); 414 } 415 416 } 418 424 public void setCurrentContext( HttpSession session ) 425 { 426 m_httpSessions.put(Thread.currentThread(), session); 428 429 } 431 435 public void clearCurrentContext() 436 { 437 m_httpSessions.remove(Thread.currentThread()); 439 440 } 442 445 446 449 private class MySessionState 450 implements SessionState 451 { 452 453 private String m_key = null; 454 455 456 private BaseStateManagerService m_service = null; 457 458 463 public MySessionState( String key, 464 BaseStateManagerService service) 465 { 466 m_key = key; 467 m_service = service; 468 469 } 471 476 public Object getAttribute( String name ) 477 { 478 return m_service.getAttribute(m_key, name); 479 480 } 482 487 public void setAttribute( String name, Object value ) 488 { 489 m_service.setAttribute(m_key, name, value); 490 491 } 493 497 public void removeAttribute( String name ) 498 { 499 m_service.removeAttribute(m_key, name); 500 501 } 503 506 public void clear() 507 { 508 m_service.clear(m_key); 509 510 } 512 516 public String [] getAttributeNames() 517 { 518 return m_service.getAttributeNames(m_key); 519 520 } 522 526 public String getKey() 527 { 528 return m_key; 529 530 } 532 535 public void retire() 536 { 537 m_service.retireState(m_key); 538 539 } 541 } 543 } 545 550 551 | Popular Tags |