1 5 package com.terracotta.session.util; 6 7 import com.terracotta.session.Session; 8 9 import javax.servlet.http.HttpSessionAttributeListener ; 10 import javax.servlet.http.HttpSessionBindingEvent ; 11 import javax.servlet.http.HttpSessionBindingListener ; 12 import javax.servlet.http.HttpSessionEvent ; 13 import javax.servlet.http.HttpSessionListener ; 14 15 public class DefaultLifecycleEventMgr implements LifecycleEventMgr { 16 17 private final HttpSessionAttributeListener [] attributeListeners; 18 private final HttpSessionListener [] sessionListeners; 19 20 public static LifecycleEventMgr makeInstance(ConfigProperties cp) { 21 Assert.pre(cp != null); 22 final HttpSessionAttributeListener [] attrListeners = cp.getSessionAttributeListeners(); 23 final HttpSessionListener [] sessListeners = cp.getSessionListeners(); 24 return new DefaultLifecycleEventMgr(attrListeners, sessListeners); 25 } 26 27 public DefaultLifecycleEventMgr(HttpSessionAttributeListener [] attributeListeners, 28 HttpSessionListener [] sessionListeners) { 29 this.attributeListeners = (attributeListeners == null) ? new HttpSessionAttributeListener [0] : attributeListeners; 30 this.sessionListeners = (sessionListeners == null) ? new HttpSessionListener [0] : sessionListeners; 31 } 32 33 public void fireSessionCreatedEvent(Session s) { 34 Assert.pre(s != null); 35 HttpSessionEvent e = new HttpSessionEvent (s); 36 for (int i = 0; i < sessionListeners.length; i++) { 37 HttpSessionListener l = sessionListeners[i]; 38 if (l != null) l.sessionCreated(e); 39 } 40 } 41 42 public void fireSessionDestroyedEvent(Session session) { 43 Assert.pre(session != null); 44 HttpSessionEvent e = new HttpSessionEvent (session); 45 for (int i = 0; i < sessionListeners.length; i++) { 46 HttpSessionListener l = sessionListeners[i]; 47 if (l != null) l.sessionDestroyed(e); 48 } 49 } 50 51 public void bindAttribute(Session sess, String name, Object val) { 52 if (val instanceof HttpSessionBindingListener ) { 53 Assert.pre(sess != null); 54 HttpSessionBindingEvent e = new HttpSessionBindingEvent (sess, name, val); 55 ((HttpSessionBindingListener ) val).valueBound(e); 56 } 57 } 58 59 public void unbindAttribute(Session sess, String name, Object val) { 60 if (val instanceof HttpSessionBindingListener ) { 61 Assert.pre(sess != null); 62 HttpSessionBindingEvent e = new HttpSessionBindingEvent (sess, name, val); 63 ((HttpSessionBindingListener ) val).valueUnbound(e); 64 } 65 } 66 67 public void removeAttribute(Session sess, String name, Object val) { 68 Assert.pre(sess != null); 69 HttpSessionBindingEvent e = new HttpSessionBindingEvent (sess, name, val); 70 for (int i = 0; i < attributeListeners.length; i++) { 71 HttpSessionAttributeListener l = attributeListeners[i]; 72 if (l != null) l.attributeRemoved(e); 73 } 74 } 75 76 public void replaceAttribute(Session sess, String name, Object oldVal, Object newVal) { 77 Assert.pre(sess != null); 78 Assert.pre(oldVal != null); 79 HttpSessionBindingEvent e = new HttpSessionBindingEvent (sess, name, oldVal); 80 for (int i = 0; i < attributeListeners.length; i++) { 81 HttpSessionAttributeListener l = attributeListeners[i]; 82 if (l != null) l.attributeReplaced(e); 83 } 84 } 85 86 public void setAttribute(Session sess, String name, Object val) { 87 Assert.pre(sess != null); 88 HttpSessionBindingEvent e = new HttpSessionBindingEvent (sess, name, val); 89 for (int i = 0; i < attributeListeners.length; i++) { 90 HttpSessionAttributeListener l = attributeListeners[i]; 91 if (l != null) l.attributeAdded(e); 92 } 93 } 94 } 95 | Popular Tags |