KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > utils > SessionTempFileDeleter


1 /*
2  * Copyright (c) 1971-2003 TONBELLER AG, Bensheim.
3  * All rights reserved.
4  */

5 package com.tonbeller.wcf.utils;
6
7 import java.io.File JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.LinkedList JavaDoc;
10 import java.util.List JavaDoc;
11
12 import javax.servlet.http.HttpSession JavaDoc;
13 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
14 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
15
16 import org.apache.log4j.Logger;
17
18 /**
19  * deletes fiels on session timeout
20  * @author av
21  * @since Jan 4, 2006
22  */

23 public class SessionTempFileDeleter implements TempFileDeleter, HttpSessionBindingListener JavaDoc {
24   List JavaDoc files = new LinkedList JavaDoc();
25   private static final String JavaDoc 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 JavaDoc 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 JavaDoc f) {
41     files.add(f);
42     if (logger.isInfoEnabled())
43       logger.info("registering file for deletion: " + f);
44   }
45
46   public void valueBound(HttpSessionBindingEvent JavaDoc arg0) {
47   }
48
49   public void valueUnbound(HttpSessionBindingEvent JavaDoc arg0) {
50     for (Iterator JavaDoc it = files.iterator(); it.hasNext(); ) {
51       File JavaDoc f = (File JavaDoc) it.next();
52       try {
53         f.delete();
54       } catch (Exception JavaDoc e) {
55         logger.error(null, e);
56       }
57     }
58   }
59 }
60
Popular Tags