KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > sessions > SessionCleaner


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19
20
21 package org.openharmonise.rm.sessions;
22
23 import java.sql.*;
24 import java.util.logging.*;
25
26 import org.openharmonise.commons.dsi.*;
27 import org.openharmonise.commons.dsi.dml.*;
28 import org.openharmonise.rm.config.*;
29
30
31 /**
32  * This class runs a in a thread to clean expired sessions from the DB.
33  *
34  * @author Matt Treanor
35  * @version $Revision: 1.1 $
36  *
37  */

38 public class SessionCleaner implements Runnable JavaDoc {
39     private static SessionCleaner m_instance = null;
40     public static String JavaDoc SESSIONCLEANER_SLEEPTIME_PNAME =
41             "SESSIONCLEANER_SLEEPTIME";
42     public static String JavaDoc DEFAULT_SESSIONCLEANER_SLEEPTIME = "60"; //in minutes
43
private AbstractDataStoreInterface m_dbinterf = null;
44     private boolean m_bKeepRunning = true;
45     private int m_nCounter = 0;
46     
47     /**
48      * Logger for this class.
49      */

50     private static final Logger m_logger = Logger.getLogger(SessionCleaner.class.getName());
51
52     /**
53      * Creates a new instance of the session cleaner.
54      *
55      * @param dbinterf
56      */

57     private SessionCleaner(AbstractDataStoreInterface dbinterf) {
58         m_dbinterf = dbinterf;
59     }
60
61     /**
62      * Returns the singleton instance of the session cleaner.
63      *
64      * @param dbinterf
65      * @return
66      */

67     public synchronized static SessionCleaner getInstance(AbstractDataStoreInterface dbinterf) {
68         if (m_instance == null) {
69             m_instance = new SessionCleaner(dbinterf);
70         }
71
72         return m_instance;
73     }
74
75     /* (non-Javadoc)
76      * @see java.lang.Runnable#run()
77      */

78     public void run() {
79         while (m_bKeepRunning) {
80             try {
81                 int nSleepTime = ConfigSettings
82                                                   .getIntProperty(SESSIONCLEANER_SLEEPTIME_PNAME,
83                                                                   DEFAULT_SESSIONCLEANER_SLEEPTIME);
84                 Thread.sleep(nSleepTime * 60 * 1000);
85
86                 removeTimedOutSessions();
87             } catch (Exception JavaDoc e) {
88                 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
89             }
90
91             m_nCounter++;
92         }
93     }
94
95     /**
96      * Returns <code>true</code> while thread is runnning.
97      *
98      * @return
99      */

100     public boolean isRunning() {
101         return m_bKeepRunning;
102     }
103
104     /**
105      * Stops thread from running.
106      *
107      */

108     public void stopRunning() {
109         m_bKeepRunning = false;
110     }
111
112     /**
113      * Returns a count of how many iterations of the cleaner have run since start up.
114      *
115      * @return
116      */

117     public int getCounterValue() {
118         return m_nCounter;
119     }
120
121     /**
122      * Removes expired session from database.
123      *
124      * @throws SQLException
125      */

126     private void removeTimedOutSessions() throws DataStoreException, SessionException, SQLException {
127         ResultSet rs = null;
128         try {
129             SelectStatement select = new SelectStatement();
130             ColumnRef cr = new Session(m_dbinterf).getInstanceColumnRef(
131                                    Session.CLMN_SESSION_ID);
132             select.addSelectColumn(cr);
133             cr = new Session(m_dbinterf).getInstanceColumnRef(
134                          Session.CLMN_SESSION_TIMEOUT);
135             select.addWhereCondition(cr, "<", new java.util.Date JavaDoc());
136
137             rs = m_dbinterf.executeQuery(select);
138
139             while (rs.next()) {
140                 new Session(m_dbinterf, rs.getString(1)).delete();
141             }
142         } finally {
143             if (rs != null) {
144                 rs.close();
145             }
146         }
147     }
148 }
Popular Tags