KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > servlets > LZBindingListener


1 /* *****************************************************************************
2  * LZBindingListener.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.servlets;
11
12 import java.io.File JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import javax.servlet.ServletContext JavaDoc;
16 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
17 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
18 import org.apache.log4j.*;
19
20 /**
21  * This class implements an HttpSessionBindingListener and is used to remove
22  * temporary session files.<p>
23  */

24 public class LZBindingListener
25     implements HttpSessionBindingListener JavaDoc
26 {
27     /**
28      * Logger
29      */

30     private static Logger mLogger = Logger.getLogger(LZBindingListener.class);
31
32     /**
33      * Store temporary filename to remove during valueUnbound().
34      */

35     HashSet JavaDoc mTempFileNameSet;
36
37     /**
38      * Constructor.
39      */

40     public LZBindingListener(String JavaDoc fileName)
41     {
42         mTempFileNameSet = new HashSet JavaDoc();
43         addTempFile(fileName);
44     }
45
46     /**
47      * At the moment, don't do anything when value is bound, aside from loggin
48      * session id.
49      */

50     public void valueBound(HttpSessionBindingEvent JavaDoc event)
51     {
52         mLogger.info("SessId bound " + event.getSession().getId());
53     }
54
55     /**
56      * Remove the file when session value is unbound.
57      */

58     public synchronized void valueUnbound(HttpSessionBindingEvent JavaDoc event)
59     {
60         mLogger.info("SessID unbound " + event.getSession().getId());
61
62         // Remove all temporary files.
63
Iterator JavaDoc iter = mTempFileNameSet.iterator();
64         while (iter.hasNext()) {
65             String JavaDoc fileName = (String JavaDoc)iter.next();
66             File JavaDoc file = new File JavaDoc(fileName);
67             file.delete();
68         }
69         mTempFileNameSet.clear();
70     }
71
72
73     /**
74      * Add a temporary file name to remove when unbinding.
75      */

76     public synchronized void addTempFile(String JavaDoc fileName)
77     {
78         if (fileName == null || fileName.length() == 0)
79             return;
80
81         if (! mTempFileNameSet.contains(fileName))
82             mTempFileNameSet.add(fileName);
83     }
84
85
86     /**
87      * Deletes a requested temporary file.
88      */

89     public synchronized boolean delTempFile(String JavaDoc fileName)
90     {
91         if (fileName != null && mTempFileNameSet.contains(fileName)) {
92             mTempFileNameSet.remove(fileName);
93             File JavaDoc file = new File JavaDoc(fileName);
94             return file.delete();
95         }
96
97         return false;
98     }
99
100 }
101
Popular Tags