KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > util > SessionReferenceRemover


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.util;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import javax.servlet.http.HttpSession JavaDoc;
31 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
32 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 /**
38  * Utility class that removes a value from a collection when the HTTP session
39  * is invalidated.
40  * <p>
41  * Use this class if you need to keep a global reference to an object that is
42  * bound to a user's session.
43  * </p>
44  * @author Felix Gnass [fgnass at neteye dot de]
45  * @since 6.4
46  */

47 public class SessionReferenceRemover implements HttpSessionBindingListener JavaDoc {
48
49     private static final Log log = LogFactory
50             .getLog(SessionReferenceRemover.class);
51     
52     private static final String JavaDoc SESSION_KEY =
53             SessionReferenceRemover.class.getName();
54     
55     private ArrayList JavaDoc references = new ArrayList JavaDoc();
56     
57     
58     private SessionReferenceRemover() {
59     }
60     
61     private void addReference(Collection JavaDoc collection, Object JavaDoc value) {
62         references.add(new SessionReference(collection, value));
63     }
64
65     public void valueBound(HttpSessionBindingEvent JavaDoc event) {
66     }
67
68     public void valueUnbound(HttpSessionBindingEvent JavaDoc event) {
69         log.info("Session invalidated - removing " + references.size()
70                 + " references.");
71         
72         Iterator JavaDoc it = references.iterator();
73         while (it.hasNext()) {
74             SessionReference ref = (SessionReference) it.next();
75             ref.remove();
76         }
77         references.clear();
78     }
79     
80     /**
81      * Removes the given value from the collection when the session is
82      * invalidated.
83      */

84     public static void removeFromCollectionOnInvalidation(
85             HttpSession JavaDoc session, Collection JavaDoc collection, Object JavaDoc value) {
86         
87         SessionReferenceRemover remover = (SessionReferenceRemover)
88                 session.getAttribute(SESSION_KEY);
89         
90         if (remover == null) {
91             remover = new SessionReferenceRemover();
92             session.setAttribute(SESSION_KEY, remover);
93         }
94         remover.addReference(collection, value);
95     }
96     
97     private static class SessionReference {
98
99         private Collection JavaDoc collection;
100         
101         private Object JavaDoc value;
102
103         public SessionReference(Collection JavaDoc collection, Object JavaDoc value) {
104             this.collection = collection;
105             this.value = value;
106         }
107         
108         public void remove() {
109             collection.remove(value);
110         }
111         
112     }
113     
114 }
115
Popular Tags