TimerRepositoryStatefulThread.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.pdd.timers;

  21. import java.sql.Connection;

  22. import org.openspcoop2.core.commons.dao.DAOFactory;
  23. import org.openspcoop2.core.commons.dao.DAOFactoryProperties;
  24. import org.openspcoop2.generic_project.utils.ServiceManagerProperties;
  25. import org.openspcoop2.pdd.config.DBTransazioniManager;
  26. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  27. import org.openspcoop2.pdd.config.Resource;
  28. import org.openspcoop2.pdd.core.handlers.HandlerException;
  29. import org.openspcoop2.pdd.core.transazioni.GestoreTransazioniStateful;
  30. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  31. import org.openspcoop2.utils.threads.BaseThread;
  32. import org.slf4j.Logger;

  33. /**    
  34.  * TimerRepositoryStatefulThread
  35.  *
  36.  * @author Poli Andrea (poli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public class TimerRepositoryStatefulThread extends BaseThread{

  41.     private static TimerState STATE = TimerState.OFF; // abilitato in OpenSPCoop2Startup al momento dell'avvio
  42.     public static TimerState getSTATE() {
  43.         return STATE;
  44.     }
  45.     public static void setSTATE(TimerState sTATE) {
  46.         STATE = sTATE;
  47.     }
  48.    
  49.     public static final String ID_MODULO = "TimerRepositoryStateful";
  50.    
  51.    
  52.     /** Logger utilizzato per debug. */
  53.     private Logger log = null;
  54.     private Logger logSql = null;

  55.     /** Indicazione se deve essere effettuato il log delle query */
  56.     private boolean debug = false;  
  57.        
  58.     /** Database */
  59.     private String tipoDatabaseRuntime = null; //tipoDatabase
  60.    
  61.     private DAOFactory daoFactory = null;
  62.     private ServiceManagerProperties daoFactoryServiceManagerPropertiesTransazioni = null;
  63.     private Logger daoFactoryLoggerTransazioni = null;
  64.    
  65.     /**
  66.      * OpenSPCoop2Properties resources
  67.      */
  68.     private OpenSPCoop2Properties openspcoopProperties = null;
  69.    
  70.     /** Gestore */
  71.     private GestoreTransazioniStateful gestore = null;
  72.    
  73.    
  74.    
  75.     /** Costruttore */
  76.     public TimerRepositoryStatefulThread() throws Exception{
  77.    
  78.         this.openspcoopProperties = OpenSPCoop2Properties.getInstance();
  79.        
  80.         this.setTimeout(this.openspcoopProperties.getTransazioniStatefulTimerIntervalSeconds());
  81.        
  82.         this.debug = this.openspcoopProperties.isTransazioniStatefulDebug();

  83.         this.log = OpenSPCoop2Logger.getLoggerOpenSPCoopTransazioniStateful(this.debug);
  84.         this.logSql = OpenSPCoop2Logger.getLoggerOpenSPCoopTransazioniStatefulSql(this.debug);
  85.        
  86.         // DB
  87.         try{
  88.             this.tipoDatabaseRuntime = this.openspcoopProperties.getDatabaseType();
  89.             if(this.tipoDatabaseRuntime==null){
  90.                 throw new Exception("Tipo Database non definito");
  91.             }

  92.             DAOFactoryProperties daoFactoryProperties = null;
  93.             this.daoFactoryLoggerTransazioni = this.logSql;
  94.             this.daoFactory = DAOFactory.getInstance(this.daoFactoryLoggerTransazioni);
  95.             daoFactoryProperties = DAOFactoryProperties.getInstance(this.daoFactoryLoggerTransazioni);
  96.             this.daoFactoryServiceManagerPropertiesTransazioni = daoFactoryProperties.getServiceManagerProperties(org.openspcoop2.core.transazioni.utils.ProjectInfo.getInstance());
  97.             this.daoFactoryServiceManagerPropertiesTransazioni.setShowSql(this.debug);  
  98.             this.daoFactoryServiceManagerPropertiesTransazioni.setDatabaseType(DBTransazioniManager.getInstance().getTipoDatabase());

  99.         }catch(Exception e){
  100.             throw new HandlerException("Errore durante l'inizializzazione delle risorse per l'accesso al database: "+e.getMessage(),e);
  101.         }
  102.                
  103.         try{
  104.             this.gestore = new GestoreTransazioniStateful(this.log, this.logSql,
  105.                     this.tipoDatabaseRuntime, this.debug);
  106.         }catch(Exception e){
  107.             throw new Exception("Errore durante l'inizializzazione del gestore: "+e.getMessage(),e);
  108.         }
  109.        
  110.        
  111.     }
  112.    
  113.     @Override
  114.     public boolean initialize(){
  115.         if(this.gestore==null){
  116.             this.log.error("Gestore non correttamente inizializzato");
  117.             return false;
  118.         }
  119.         return true;
  120.     }
  121.     @Override
  122.     public void process(){
  123.         if(TimerState.ENABLED.equals(STATE)) {
  124.            
  125.             DBTransazioniManager dbManager = null;
  126.             Resource r = null;
  127.             try{
  128.                 dbManager = DBTransazioniManager.getInstance();
  129.                 r = dbManager.getResource(this.openspcoopProperties.getIdentitaPortaDefaultWithoutProtocol(), ID_MODULO, null);
  130.                 if(r==null){
  131.                     throw new Exception("Risorsa al database non disponibile");
  132.                 }
  133.                 Connection con = (Connection) r.getResource();
  134.                 if(con == null)
  135.                     throw new Exception("Connessione non disponibile");
  136.                
  137.                 if(this.debug){
  138.                     this.log.debug("Esecuzione thread per gestione delle transazioni stateful....");
  139.                 }
  140.                
  141.                 this.gestore.verificaOggettiPresentiRepository(this.daoFactory,this.daoFactoryServiceManagerPropertiesTransazioni, this.daoFactoryLoggerTransazioni, con);
  142.                 if(this.debug){
  143.                     this.log.debug("Esecuzione thread per gestione delle transazioni stateful terminata");
  144.                 }
  145.                
  146.             }catch(Exception e){
  147.                 this.log.error("Errore durante la gestione delle transazioni stateful: "+e.getMessage(),e);
  148.             }finally{
  149.                 try{
  150.                     if(r!=null)
  151.                         dbManager.releaseResource(this.openspcoopProperties.getIdentitaPortaDefaultWithoutProtocol(), ID_MODULO, r);
  152.                 }catch(Exception eClose){
  153.                     // ignore
  154.                 }
  155.             }
  156.            
  157.         }
  158.         else {
  159.             this.log.info("Timer "+ID_MODULO+" disabilitato");
  160.         }
  161.     }
  162. }