/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.parallax.server.common.cloudsession.service.impl;

import com.google.inject.Inject;
import com.parallax.server.common.cloudsession.service.AuthenticationTokenService;
import com.parallax.server.common.cloudsession.service.DatabaseMaintenanceService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Michel
 */
public class DatabaseMaintenanceServiceImpl implements DatabaseMaintenanceService, Runnable {

    private final Logger log = LoggerFactory.getLogger(DatabaseMaintenanceServiceImpl.class);

    private ScheduledExecutorService service;

//    private UserDao userDao;
    private AuthenticationTokenService authenticationTokenService;

//    @Inject
//    public void setUserDao(UserDao userDao) {
//        this.userDao = userDao;
//        setup();
//    }
    @Inject
    public void setAuthenticationTokenService(AuthenticationTokenService authenticationTokenService) {
        this.authenticationTokenService = authenticationTokenService;
        setup();
    }

    public void setup() {
        this.service = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setDaemon(true);
                //thread.setDaemon(false);
                return thread;
            }
        });
        this.service.scheduleAtFixedRate(this, 5, 5, TimeUnit.MINUTES);
    }

    @Override
    public void run() {
        //  log.info("Keep db active: {}", userDao.count());
        try {
            log.info("Clean authentication tokens and keep db active: removed {}", authenticationTokenService.cleanExpiredAutheticationTokens());
        } catch (Throwable t) {
            log.error("ALERT: Problem cleaning authentication tokens and keeping db active", t);
        }
    }

}

@@ -274,16 +274,162 @@
 Inject;%0A
+import com.parallax.server.common.cloudsession.db.utils.DataSourceSetup;%0Aimport com.parallax.server.common.cloudsession.db.utils.NeedsDataSource;%0A
 import c
@@ -499,24 +499,24 @@
 kenService;%0A
-
 import com.p
@@ -578,32 +578,60 @@
 tenanceService;%0A
+import java.sql.Connection;%0A
 import java.util
@@ -788,16 +788,45 @@
 meUnit;%0A
+import javax.sql.DataSource;%0A
 import o
@@ -986,16 +986,33 @@
 Service,
+ NeedsDataSource,
  Runnabl
@@ -1253,24 +1253,168 @@
 enService;%0A%0A
+    private DataSource dataSource;%0A%0A    public DatabaseMaintenanceServiceImpl() %7B%0A        DataSourceSetup.registerDataSourceUsers(this);%0A    %7D%0A%0A
 //    @Injec
@@ -1729,24 +1729,138 @@
 p();%0A    %7D%0A%0A
+    @Override%0A    public void setDataSource(DataSource dataSource) %7B%0A        this.dataSource = dataSource;%0A    %7D%0A%0A
     public v
@@ -2586,24 +2586,24 @@
 owable t) %7B%0A
-
             
@@ -2693,21 +2693,344 @@
 ;%0A        %7D%0A
+        if (this.dataSource != null) %7B%0A            try %7B%0A                Connection connection = dataSource.getConnection();%0A                connection.prepareStatement(%22SELECT 1%22).executeQuery();%0A            %7D catch (Throwable t) %7B%0A                log.error(%22ALERT: Problem keeping db active%22, t);%0A            %7D%0A        %7D%0A
     %7D%0A%0A%7D%0A

Fix problems with db