001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.terracotta.session.util;
006:
007: import com.terracotta.session.Session;
008:
009: import javax.servlet.http.HttpSessionAttributeListener;
010: import javax.servlet.http.HttpSessionBindingEvent;
011: import javax.servlet.http.HttpSessionBindingListener;
012: import javax.servlet.http.HttpSessionEvent;
013: import javax.servlet.http.HttpSessionListener;
014:
015: public class DefaultLifecycleEventMgr implements LifecycleEventMgr {
016:
017: private final HttpSessionAttributeListener[] attributeListeners;
018: private final HttpSessionListener[] sessionListeners;
019:
020: public static LifecycleEventMgr makeInstance(ConfigProperties cp) {
021: Assert.pre(cp != null);
022: final HttpSessionAttributeListener[] attrListeners = cp
023: .getSessionAttributeListeners();
024: final HttpSessionListener[] sessListeners = cp
025: .getSessionListeners();
026: return new DefaultLifecycleEventMgr(attrListeners,
027: sessListeners);
028: }
029:
030: public DefaultLifecycleEventMgr(
031: HttpSessionAttributeListener[] attributeListeners,
032: HttpSessionListener[] sessionListeners) {
033: this .attributeListeners = (attributeListeners == null) ? new HttpSessionAttributeListener[0]
034: : attributeListeners;
035: this .sessionListeners = (sessionListeners == null) ? new HttpSessionListener[0]
036: : sessionListeners;
037: }
038:
039: public void fireSessionCreatedEvent(Session s) {
040: Assert.pre(s != null);
041: HttpSessionEvent e = new HttpSessionEvent(s);
042: for (int i = 0; i < sessionListeners.length; i++) {
043: HttpSessionListener l = sessionListeners[i];
044: if (l != null)
045: l.sessionCreated(e);
046: }
047: }
048:
049: public void fireSessionDestroyedEvent(Session session) {
050: Assert.pre(session != null);
051: HttpSessionEvent e = new HttpSessionEvent(session);
052: for (int i = 0; i < sessionListeners.length; i++) {
053: HttpSessionListener l = sessionListeners[i];
054: if (l != null)
055: l.sessionDestroyed(e);
056: }
057: }
058:
059: public void bindAttribute(Session sess, String name, Object val) {
060: if (val instanceof HttpSessionBindingListener) {
061: Assert.pre(sess != null);
062: HttpSessionBindingEvent e = new HttpSessionBindingEvent(
063: sess, name, val);
064: ((HttpSessionBindingListener) val).valueBound(e);
065: }
066: }
067:
068: public void unbindAttribute(Session sess, String name, Object val) {
069: if (val instanceof HttpSessionBindingListener) {
070: Assert.pre(sess != null);
071: HttpSessionBindingEvent e = new HttpSessionBindingEvent(
072: sess, name, val);
073: ((HttpSessionBindingListener) val).valueUnbound(e);
074: }
075: }
076:
077: public void removeAttribute(Session sess, String name, Object val) {
078: Assert.pre(sess != null);
079: HttpSessionBindingEvent e = new HttpSessionBindingEvent(sess,
080: name, val);
081: for (int i = 0; i < attributeListeners.length; i++) {
082: HttpSessionAttributeListener l = attributeListeners[i];
083: if (l != null)
084: l.attributeRemoved(e);
085: }
086: }
087:
088: public void replaceAttribute(Session sess, String name,
089: Object oldVal, Object newVal) {
090: Assert.pre(sess != null);
091: Assert.pre(oldVal != null);
092: HttpSessionBindingEvent e = new HttpSessionBindingEvent(sess,
093: name, oldVal);
094: for (int i = 0; i < attributeListeners.length; i++) {
095: HttpSessionAttributeListener l = attributeListeners[i];
096: if (l != null)
097: l.attributeReplaced(e);
098: }
099: }
100:
101: public void setAttribute(Session sess, String name, Object val) {
102: Assert.pre(sess != null);
103: HttpSessionBindingEvent e = new HttpSessionBindingEvent(sess,
104: name, val);
105: for (int i = 0; i < attributeListeners.length; i++) {
106: HttpSessionAttributeListener l = attributeListeners[i];
107: if (l != null)
108: l.attributeAdded(e);
109: }
110: }
111: }
|