KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > terracotta > session > util > DefaultLifecycleEventMgr


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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