1 5 package com.tonbeller.wcf.utils; 6 7 import java.io.File ; 8 import java.util.Iterator ; 9 import java.util.LinkedList ; 10 import java.util.List ; 11 12 import javax.servlet.http.HttpSession ; 13 import javax.servlet.http.HttpSessionBindingEvent ; 14 import javax.servlet.http.HttpSessionBindingListener ; 15 16 import org.apache.log4j.Logger; 17 18 23 public class SessionTempFileDeleter implements TempFileDeleter, HttpSessionBindingListener { 24 List files = new LinkedList (); 25 private static final String WEBKEY = SessionTempFileDeleter.class.getName(); 26 private static final Logger logger = Logger.getLogger(SessionTempFileDeleter.class); 27 28 private SessionTempFileDeleter () { 29 } 30 31 public static TempFileDeleter instance(HttpSession session) { 32 TempFileDeleter tfd = (TempFileDeleter) session.getAttribute(WEBKEY); 33 if (tfd == null) { 34 tfd = new SessionTempFileDeleter(); 35 session.setAttribute(WEBKEY, tfd); 36 } 37 return tfd; 38 } 39 40 public void register(File f) { 41 files.add(f); 42 if (logger.isInfoEnabled()) 43 logger.info("registering file for deletion: " + f); 44 } 45 46 public void valueBound(HttpSessionBindingEvent arg0) { 47 } 48 49 public void valueUnbound(HttpSessionBindingEvent arg0) { 50 for (Iterator it = files.iterator(); it.hasNext(); ) { 51 File f = (File ) it.next(); 52 try { 53 f.delete(); 54 } catch (Exception e) { 55 logger.error(null, e); 56 } 57 } 58 } 59 } 60 | Popular Tags |