TimerStatisticheThread.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.monitor.engine.statistic;

  21. import java.sql.Connection;
  22. import java.sql.DriverManager;
  23. import java.util.Properties;

  24. import javax.sql.DataSource;

  25. import org.openspcoop2.core.commons.dao.DAOFactory;
  26. import org.openspcoop2.monitor.engine.config.MonitorProperties;
  27. import org.openspcoop2.monitor.engine.constants.CostantiConfigurazione;
  28. import org.openspcoop2.monitor.engine.exceptions.EngineException;
  29. import org.openspcoop2.utils.Utilities;
  30. import org.openspcoop2.utils.resources.GestoreJNDI;

  31. /**
  32.  * TimerStatisticheThread
  33.  *
  34.  * @author Poli Andrea (apoli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */
  38. public class TimerStatisticheThread extends Thread{

  39.     /**
  40.      * Timeout che definisce la cadenza di avvio di questo timer.
  41.      */
  42.     private long timeout = 10; // ogni 10 secondi avvio il Thread

  43.     /** Configurazione */
  44.     private StatisticsConfig statisticsConfig;
  45.    
  46.     /** DataSource */
  47.     private DataSource ds;
  48.    
  49.     /** Connection */
  50.     private Connection connection;
  51.    
  52.     /** DAOFactory */
  53.     private DAOFactory daoFactory;
  54.     private org.openspcoop2.core.statistiche.dao.IServiceManager statisticheSM = null;
  55.     private org.openspcoop2.core.transazioni.dao.IServiceManager transazioniSM = null;
  56.     private org.openspcoop2.monitor.engine.config.statistiche.dao.IServiceManager pluginsStatisticheSM = null;
  57.     private org.openspcoop2.core.plugins.dao.IServiceManager pluginsBaseSM;
  58.     private org.openspcoop2.core.commons.search.dao.IServiceManager utilsSM;
  59.     private org.openspcoop2.monitor.engine.config.transazioni.dao.IServiceManager pluginsTransazioniSM;
  60.    
  61.     // VARIABILE PER STOP
  62.     private boolean stop = false;
  63.    
  64.     public boolean isStop() {
  65.         return this.stop;
  66.     }

  67.     public void setStop(boolean stop) {
  68.         this.stop = stop;
  69.     }
  70.    
  71.    
  72.     /** Costruttore Datasource */
  73.     public TimerStatisticheThread(DataSource ds, StatisticsConfig statisticsConfig) throws EngineException{
  74.         this(statisticsConfig);
  75.         this.ds = ds;
  76.     }
  77.     public TimerStatisticheThread(String ds,Properties dsContext, StatisticsConfig statisticsConfig) throws EngineException{
  78.         this(statisticsConfig);
  79.         GestoreJNDI jndi = new GestoreJNDI(dsContext);
  80.         try{
  81.             this.ds = (DataSource) jndi.lookup(ds);
  82.         }catch(Exception e){
  83.             throw new EngineException(e.getMessage(),e);
  84.         }
  85.     }
  86.    
  87.     /** Costruttore Connection */
  88.     public TimerStatisticheThread(Connection connection, StatisticsConfig statisticsConfig) throws EngineException{
  89.         this(statisticsConfig);
  90.         this.connection = connection;
  91.     }
  92.     public TimerStatisticheThread(String connectionUrl,String driverJDBC,String username, String password, StatisticsConfig statisticsConfig) throws EngineException{
  93.         this(statisticsConfig);
  94.         try{
  95.             Class.forName(driverJDBC);
  96.             this.connection = DriverManager.getConnection(connectionUrl,username,password);
  97.         }catch(Exception e){
  98.             throw new EngineException(e.getMessage(),e);
  99.         }
  100.     }
  101.    
  102.     /** Costruttore */
  103.     public TimerStatisticheThread(StatisticsConfig statisticsConfig) throws EngineException{
  104.    
  105.         try{
  106.        
  107.             MonitorProperties props = MonitorProperties.getInstance(statisticsConfig.getLogCore());
  108.     //      
  109.     //      this.tipoDatabase = props.getProperty(CostantiConfigurazione.PROP_DBTYPE);
  110.     //      if (!"postgresql".equals(this.tipoDatabase) && !"mysql".equals(this.tipoDatabase) && !"oracle".equals(this.tipoDatabase))
  111.     //          throw new FrameworkCoreException("Tipo di database non valido indicato in configurazione");
  112.            
  113.             try {
  114.                 this.timeout = Integer.parseInt(props.getProperty(CostantiConfigurazione.STAT_TIMEOUT, "10", true));
  115.             } catch (NumberFormatException e) {
  116.                 this.timeout=10;
  117.             }
  118.            
  119.             this.statisticsConfig = statisticsConfig;
  120.            
  121.             this.daoFactory = DAOFactory.getInstance(this.statisticsConfig.getLogSql());
  122.            
  123.         }catch(Exception e){
  124.             throw new EngineException(e.getMessage(),e);
  125.         }
  126.     }
  127.    
  128.     /**
  129.      * Metodo che fa partire il Thread.
  130.      *
  131.      */
  132.     @Override
  133.     public void run(){
  134.        
  135.         this.initResources();
  136.        
  137.         while(this.stop == false){
  138.                    
  139.             StatisticsLibrary sLibrary = new StatisticsLibrary(this.statisticsConfig, this.statisticheSM, this.transazioniSM,
  140.                     this.pluginsStatisticheSM, this.pluginsBaseSM, this.utilsSM, this.pluginsTransazioniSM);
  141.            
  142.             sLibrary.generateStatisticaOraria();
  143.            
  144.             sLibrary.generateStatisticaGiornaliera();
  145.            
  146.             sLibrary.generateStatisticaSettimanale();
  147.            
  148.             sLibrary.generateStatisticaMensile();
  149.                    
  150.             // CheckInterval
  151.             if(this.stop==false){
  152.                 int i=0;
  153.                 while(i<this.timeout){
  154.                     Utilities.sleep(1000);      
  155.                     if(this.stop){
  156.                         break; // thread terminato, non lo devo far piu' dormire
  157.                     }
  158.                     i++;
  159.                 }
  160.             }
  161.         }
  162.        
  163.         this.statisticsConfig.getLogCore().info("Thread per la generazione delle statistiche terminato");

  164.     }
  165.    
  166.     private void initResources(){
  167.        
  168.         try{
  169.            
  170.             if(this.ds!=null){
  171.                 this.statisticheSM = (org.openspcoop2.core.statistiche.dao.IServiceManager) this.daoFactory.getServiceManager(
  172.                         org.openspcoop2.core.statistiche.utils.ProjectInfo.getInstance(),
  173.                         this.ds);
  174.             }
  175.             else if(this.connection!=null){
  176.                 this.statisticheSM = (org.openspcoop2.core.statistiche.dao.IServiceManager) this.daoFactory.getServiceManager(
  177.                         org.openspcoop2.core.statistiche.utils.ProjectInfo.getInstance(),
  178.                         this.connection);
  179.             }
  180.             else{
  181.                 this.statisticheSM = (org.openspcoop2.core.statistiche.dao.IServiceManager) this.daoFactory.getServiceManager(
  182.                         org.openspcoop2.core.statistiche.utils.ProjectInfo.getInstance());
  183.             }
  184.            
  185.         }catch(Exception e){
  186.             this.statisticsConfig.getLogCore().error("Errore durante l'inizializzazione del Service Manager per le statistiche: "+e.getMessage(),e);
  187.         }
  188.        
  189.        
  190.         try{
  191.            
  192.             if(this.ds!=null){
  193.                 this.transazioniSM = (org.openspcoop2.core.transazioni.dao.IServiceManager) this.daoFactory.getServiceManager(
  194.                         org.openspcoop2.core.transazioni.utils.ProjectInfo.getInstance(),
  195.                         this.ds);
  196.             }
  197.             else if(this.connection!=null){
  198.                 this.transazioniSM = (org.openspcoop2.core.transazioni.dao.IServiceManager) this.daoFactory.getServiceManager(
  199.                         org.openspcoop2.core.transazioni.utils.ProjectInfo.getInstance(),
  200.                         this.connection);
  201.             }
  202.             else{
  203.                 this.transazioniSM = (org.openspcoop2.core.transazioni.dao.IServiceManager) this.daoFactory.getServiceManager(
  204.                         org.openspcoop2.core.transazioni.utils.ProjectInfo.getInstance());
  205.             }
  206.            
  207.         }catch(Exception e){
  208.             this.statisticsConfig.getLogCore().error("Errore durante l'inizializzazione del Service Manager per le transazioni: "+e.getMessage(),e);
  209.         }
  210.        
  211.        
  212.         if(this.statisticsConfig.isGenerazioneStatisticheCustom()){
  213.             try{
  214.                
  215.                 if(this.ds!=null){
  216.                     this.pluginsStatisticheSM = (org.openspcoop2.monitor.engine.config.statistiche.dao.IServiceManager) this.daoFactory.getServiceManager(
  217.                             org.openspcoop2.monitor.engine.config.statistiche.utils.ProjectInfo.getInstance(),
  218.                             this.ds);
  219.                 }
  220.                 else if(this.connection!=null){
  221.                     this.pluginsStatisticheSM = (org.openspcoop2.monitor.engine.config.statistiche.dao.IServiceManager) this.daoFactory.getServiceManager(
  222.                             org.openspcoop2.monitor.engine.config.statistiche.utils.ProjectInfo.getInstance(),
  223.                             this.connection);
  224.                 }
  225.                 else{
  226.                     this.pluginsStatisticheSM = (org.openspcoop2.monitor.engine.config.statistiche.dao.IServiceManager) this.daoFactory.getServiceManager(
  227.                             org.openspcoop2.monitor.engine.config.statistiche.utils.ProjectInfo.getInstance());
  228.                 }
  229.                
  230.                 if(this.ds!=null){
  231.                     this.pluginsBaseSM = (org.openspcoop2.core.plugins.dao.IServiceManager) this.daoFactory.getServiceManager(
  232.                             org.openspcoop2.core.plugins.utils.ProjectInfo.getInstance(),
  233.                             this.ds);
  234.                 }
  235.                 else if(this.connection!=null){
  236.                     this.pluginsBaseSM = (org.openspcoop2.core.plugins.dao.IServiceManager) this.daoFactory.getServiceManager(
  237.                             org.openspcoop2.core.plugins.utils.ProjectInfo.getInstance(),
  238.                             this.connection);
  239.                 }
  240.                 else{
  241.                     this.pluginsBaseSM = (org.openspcoop2.core.plugins.dao.IServiceManager) this.daoFactory.getServiceManager(
  242.                             org.openspcoop2.core.plugins.utils.ProjectInfo.getInstance());
  243.                 }
  244.                
  245.                 if(this.ds!=null){
  246.                     this.utilsSM = (org.openspcoop2.core.commons.search.dao.IServiceManager) this.daoFactory.getServiceManager(
  247.                             org.openspcoop2.core.commons.search.utils.ProjectInfo.getInstance(),
  248.                             this.ds);
  249.                 }
  250.                 else if(this.connection!=null){
  251.                     this.utilsSM = (org.openspcoop2.core.commons.search.dao.IServiceManager) this.daoFactory.getServiceManager(
  252.                             org.openspcoop2.core.commons.search.utils.ProjectInfo.getInstance(),
  253.                             this.connection);
  254.                 }
  255.                 else{
  256.                     this.utilsSM = (org.openspcoop2.core.commons.search.dao.IServiceManager) this.daoFactory.getServiceManager(
  257.                             org.openspcoop2.core.commons.search.utils.ProjectInfo.getInstance());
  258.                 }
  259.                
  260.                 if(this.statisticsConfig.isAnalisiTransazioniCustom()){
  261.                     if(this.ds!=null){
  262.                         this.pluginsTransazioniSM = (org.openspcoop2.monitor.engine.config.transazioni.dao.IServiceManager) this.daoFactory.getServiceManager(
  263.                                 org.openspcoop2.monitor.engine.config.transazioni.utils.ProjectInfo.getInstance(),
  264.                                 this.ds);
  265.                     }
  266.                     else if(this.connection!=null){
  267.                         this.pluginsTransazioniSM = (org.openspcoop2.monitor.engine.config.transazioni.dao.IServiceManager) this.daoFactory.getServiceManager(
  268.                                 org.openspcoop2.monitor.engine.config.transazioni.utils.ProjectInfo.getInstance(),
  269.                                 this.connection);
  270.                     }
  271.                     else{
  272.                         this.pluginsTransazioniSM = (org.openspcoop2.monitor.engine.config.transazioni.dao.IServiceManager) this.daoFactory.getServiceManager(
  273.                                 org.openspcoop2.monitor.engine.config.transazioni.utils.ProjectInfo.getInstance());
  274.                     }
  275.                 }
  276.                
  277.             }catch(Exception e){
  278.                 this.statisticsConfig.getLogCore().error("Errore durante l'inizializzazione del Service Manager per i plugins: "+e.getMessage(),e);
  279.             }
  280.         }
  281.     }
  282. }