1 48 49 package com.caucho.portal.generic; 50 51 import javax.servlet.http.HttpSession ; 52 import javax.servlet.http.HttpSessionActivationListener ; 53 import javax.servlet.http.HttpSessionBindingEvent ; 54 import javax.servlet.http.HttpSessionBindingListener ; 55 import javax.servlet.http.HttpSessionEvent ; 56 import java.lang.ref.WeakReference ; 57 import java.util.LinkedHashMap ; 58 59 public class SessionPreferences 60 extends LinkedHashMap <String , String []> 61 implements java.io.Serializable , 62 HttpSessionBindingListener , HttpSessionActivationListener 63 { 64 transient WeakReference _sessionRef; 65 transient private boolean _isUpdateNeeded; 66 67 private String _sessionAttributeName; 68 69 public SessionPreferences() 70 { 71 } 72 73 public void valueBound(HttpSessionBindingEvent event) 74 { 75 if (_sessionRef == null) { 76 _sessionRef = new WeakReference (event.getSession()); 77 _sessionAttributeName = event.getName(); 78 } 79 } 80 81 public void valueUnbound(HttpSessionBindingEvent event) 82 { 83 _sessionRef = null; 84 _sessionAttributeName = null; 85 } 86 87 public void sessionDidActivate(HttpSessionEvent event) 88 { 89 _sessionRef = new WeakReference (event.getSession()); 90 } 91 92 public void sessionWillPassivate(HttpSessionEvent event) 93 { 94 } 95 96 public String [] put(String key, String [] value) 97 { 98 _isUpdateNeeded = true; 99 return super.put(key, value); 100 } 101 102 void updateSessionIfNeeded() 103 { 104 if (_isUpdateNeeded) { 105 if (_sessionRef != null) { 106 HttpSession session = (HttpSession ) _sessionRef.get(); 107 108 if (session != null) { 109 synchronized (session) { 110 session.setAttribute(_sessionAttributeName, this); 111 _isUpdateNeeded = false; 112 } 113 } 114 } 115 } 116 } 117 } 118 119 | Popular Tags |