001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019:
020: package org.openharmonise.rm.sessions;
021:
022: import java.sql.*;
023: import java.util.logging.*;
024:
025: import org.openharmonise.commons.dsi.*;
026: import org.openharmonise.commons.dsi.dml.*;
027: import org.openharmonise.rm.config.*;
028:
029: /**
030: * This class runs a in a thread to clean expired sessions from the DB.
031: *
032: * @author Matt Treanor
033: * @version $Revision: 1.1 $
034: *
035: */
036: public class SessionCleaner implements Runnable {
037: private static SessionCleaner m_instance = null;
038: public static String SESSIONCLEANER_SLEEPTIME_PNAME = "SESSIONCLEANER_SLEEPTIME";
039: public static String DEFAULT_SESSIONCLEANER_SLEEPTIME = "60"; //in minutes
040: private AbstractDataStoreInterface m_dbinterf = null;
041: private boolean m_bKeepRunning = true;
042: private int m_nCounter = 0;
043:
044: /**
045: * Logger for this class.
046: */
047: private static final Logger m_logger = Logger
048: .getLogger(SessionCleaner.class.getName());
049:
050: /**
051: * Creates a new instance of the session cleaner.
052: *
053: * @param dbinterf
054: */
055: private SessionCleaner(AbstractDataStoreInterface dbinterf) {
056: m_dbinterf = dbinterf;
057: }
058:
059: /**
060: * Returns the singleton instance of the session cleaner.
061: *
062: * @param dbinterf
063: * @return
064: */
065: public synchronized static SessionCleaner getInstance(
066: AbstractDataStoreInterface dbinterf) {
067: if (m_instance == null) {
068: m_instance = new SessionCleaner(dbinterf);
069: }
070:
071: return m_instance;
072: }
073:
074: /* (non-Javadoc)
075: * @see java.lang.Runnable#run()
076: */
077: public void run() {
078: while (m_bKeepRunning) {
079: try {
080: int nSleepTime = ConfigSettings.getIntProperty(
081: SESSIONCLEANER_SLEEPTIME_PNAME,
082: DEFAULT_SESSIONCLEANER_SLEEPTIME);
083: Thread.sleep(nSleepTime * 60 * 1000);
084:
085: removeTimedOutSessions();
086: } catch (Exception e) {
087: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
088: }
089:
090: m_nCounter++;
091: }
092: }
093:
094: /**
095: * Returns <code>true</code> while thread is runnning.
096: *
097: * @return
098: */
099: public boolean isRunning() {
100: return m_bKeepRunning;
101: }
102:
103: /**
104: * Stops thread from running.
105: *
106: */
107: public void stopRunning() {
108: m_bKeepRunning = false;
109: }
110:
111: /**
112: * Returns a count of how many iterations of the cleaner have run since start up.
113: *
114: * @return
115: */
116: public int getCounterValue() {
117: return m_nCounter;
118: }
119:
120: /**
121: * Removes expired session from database.
122: *
123: * @throws SQLException
124: */
125: private void removeTimedOutSessions() throws DataStoreException,
126: SessionException, SQLException {
127: ResultSet rs = null;
128: try {
129: SelectStatement select = new SelectStatement();
130: ColumnRef cr = new Session(m_dbinterf)
131: .getInstanceColumnRef(Session.CLMN_SESSION_ID);
132: select.addSelectColumn(cr);
133: cr = new Session(m_dbinterf)
134: .getInstanceColumnRef(Session.CLMN_SESSION_TIMEOUT);
135: select.addWhereCondition(cr, "<", new java.util.Date());
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: }
|