1 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 38 public class SessionCleaner implements Runnable { 39 private static SessionCleaner m_instance = null; 40 public static String SESSIONCLEANER_SLEEPTIME_PNAME = 41 "SESSIONCLEANER_SLEEPTIME"; 42 public static String DEFAULT_SESSIONCLEANER_SLEEPTIME = "60"; private AbstractDataStoreInterface m_dbinterf = null; 44 private boolean m_bKeepRunning = true; 45 private int m_nCounter = 0; 46 47 50 private static final Logger m_logger = Logger.getLogger(SessionCleaner.class.getName()); 51 52 57 private SessionCleaner(AbstractDataStoreInterface dbinterf) { 58 m_dbinterf = dbinterf; 59 } 60 61 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 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 e) { 88 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 89 } 90 91 m_nCounter++; 92 } 93 } 94 95 100 public boolean isRunning() { 101 return m_bKeepRunning; 102 } 103 104 108 public void stopRunning() { 109 m_bKeepRunning = false; 110 } 111 112 117 public int getCounterValue() { 118 return m_nCounter; 119 } 120 121 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 ()); 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 |