1 24 package org.riotfamily.common.web.util; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 30 import javax.servlet.http.HttpSession ; 31 import javax.servlet.http.HttpSessionBindingEvent ; 32 import javax.servlet.http.HttpSessionBindingListener ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 47 public class SessionReferenceRemover implements HttpSessionBindingListener { 48 49 private static final Log log = LogFactory 50 .getLog(SessionReferenceRemover.class); 51 52 private static final String SESSION_KEY = 53 SessionReferenceRemover.class.getName(); 54 55 private ArrayList references = new ArrayList (); 56 57 58 private SessionReferenceRemover() { 59 } 60 61 private void addReference(Collection collection, Object value) { 62 references.add(new SessionReference(collection, value)); 63 } 64 65 public void valueBound(HttpSessionBindingEvent event) { 66 } 67 68 public void valueUnbound(HttpSessionBindingEvent event) { 69 log.info("Session invalidated - removing " + references.size() 70 + " references."); 71 72 Iterator it = references.iterator(); 73 while (it.hasNext()) { 74 SessionReference ref = (SessionReference) it.next(); 75 ref.remove(); 76 } 77 references.clear(); 78 } 79 80 84 public static void removeFromCollectionOnInvalidation( 85 HttpSession session, Collection collection, Object 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 collection; 100 101 private Object value; 102 103 public SessionReference(Collection collection, Object 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 |