package at.gv.egovernment.moa.id.auth; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import at.gv.egovernment.moa.id.config.auth.AuthConfiguration; import at.gv.egovernment.moa.id.storage.IAuthenticationSessionStoreage; import at.gv.egovernment.moa.id.storage.ITransactionStorage; import at.gv.egovernment.moa.id.util.MOAIDMessageProvider; import at.gv.egovernment.moa.logging.Logger; /** * Thread cleaning the AuthenticationServer session store * and authentication data store from garbage. * * @author Paul Ivancsics * @version $Id$ */ @Service("AuthenticationSessionCleaner") public class AuthenticationSessionCleaner implements Runnable { @Autowired private IAuthenticationSessionStoreage authenticationSessionStorage; @Autowired private ITransactionStorage transactionStorage; @Autowired protected AuthConfiguration authConfig; /** interval the AuthenticationSessionCleaner is run in */ private static final long SESSION_CLEANUP_INTERVAL = 5 * 60; // 5 min /** * Runs the thread. Cleans the AuthenticationServer session store * and authentication data store from garbage, then sleeps for given interval, and restarts. * * Cleans up expired session and authentication data stores. * */ public void run() { while (true) { try { Logger.debug("AuthenticationSessionCleaner run"); Date now = new Date(); try { int sessionTimeOutCreated = authConfig.getSSOCreatedTimeOut() * 1000; int sessionTimeOutUpdated = authConfig.getSSOUpdatedTimeOut() * 1000; int authDataTimeOut = authConfig.getTransactionTimeOut() * 1000; //clean AuthenticationSessionStore authenticationSessionStorage.clean(now, sessionTimeOutCreated, sessionTimeOutUpdated); //clean TransactionStorage transactionStorage.clean(now, authDataTimeOut); } catch (Exception e) { Logger.error("Session cleanUp FAILED!" , e); } } catch (Exception e) { Logger.error(MOAIDMessageProvider.getInstance().getMessage("cleaner.01", null), e); } try { Thread.sleep(SESSION_CLEANUP_INTERVAL * 1000); } catch (InterruptedException e) { } } } /** * start the sessionCleaner */ public static void start(Runnable clazz) { // start the session cleanup thread Thread sessionCleaner = new Thread(clazz, "AuthenticationSessionCleaner"); sessionCleaner.setName("SessionCleaner"); sessionCleaner.setDaemon(true); sessionCleaner.setPriority(Thread.MIN_PRIORITY); sessionCleaner.start(); } }