TimerUtils.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 java.util.ArrayList;
  23. import java.util.List;

  24. import javax.rmi.PortableRemoteObject;

  25. import org.openspcoop2.pdd.config.DBManager;
  26. import org.openspcoop2.pdd.config.DBStatisticheManager;
  27. import org.openspcoop2.pdd.config.DBTransazioniManager;
  28. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  29. import org.openspcoop2.pdd.config.Resource;
  30. import org.openspcoop2.pdd.core.GestoreMessaggi;
  31. import org.openspcoop2.utils.TipiDatabase;
  32. import org.openspcoop2.utils.id.serial.InfoStatistics;
  33. import org.openspcoop2.utils.resources.GestoreJNDI;
  34. import org.openspcoop2.utils.semaphore.Semaphore;
  35. import org.openspcoop2.utils.semaphore.SemaphoreConfiguration;
  36. import org.openspcoop2.utils.semaphore.SemaphoreMapping;
  37. import org.slf4j.Logger;

  38. /**
  39.  * Libreria contenente metodi utili per lo smistamento delle buste
  40.  * all'interno dei moduli di openspcoop realizzati tramite servizi.
  41.  *
  42.  * @author Poli Andrea (apoli@link.it)
  43.  * @author $Author$
  44.  * @version $Rev$, $Date$
  45.  */

  46. public class TimerUtils {
  47.    
  48.  
  49.    

  50.     /**
  51.      * Metodo che si occupa di ottenere il EJB utile alla gestione dei Riscontri Scaduti
  52.      *
  53.      * @return un EJB utile alla gestione dei riscontri scaduti {@link org.openspcoop2.pdd.timers.TimerGestoreBusteNonRiscontrate}.
  54.      *
  55.      */
  56.     public static TimerGestoreBusteNonRiscontrate createTimerGestoreBusteNonRiscontrate() throws Exception {
  57.        
  58.         OpenSPCoop2Properties properties = OpenSPCoop2Properties.getInstance();
  59.             GestoreJNDI jndi = null;
  60.             if(properties.getJNDIContext_TimerEJB()==null)
  61.                 jndi = new GestoreJNDI();
  62.             else
  63.                 jndi = new GestoreJNDI(properties.getJNDIContext_TimerEJB());
  64.        
  65.             String nomeJNDI = properties.getJNDITimerEJBName().get(TimerGestoreBusteNonRiscontrate.ID_MODULO);
  66.             Object objref = jndi.lookup(nomeJNDI);
  67.             TimerGestoreBusteNonRiscontrateHome timerHome =
  68.                 (TimerGestoreBusteNonRiscontrateHome) PortableRemoteObject.narrow(objref,TimerGestoreBusteNonRiscontrateHome.class);
  69.             TimerGestoreBusteNonRiscontrate timerDiServizio = timerHome.create();  
  70.        
  71.             return timerDiServizio;
  72.        
  73.        
  74.     }




  75.     /**
  76.      * Metodo che si occupa di ottenere il EJB utile alla gestione dei Messaggi
  77.      *
  78.      * @return un EJB utile alla gestione dei messaggi {@link org.openspcoop2.pdd.timers.TimerGestoreMessaggi}.
  79.      *
  80.      */
  81.     public static TimerGestoreMessaggi createTimerGestoreMessaggi() throws Exception {
  82.        
  83.         OpenSPCoop2Properties properties = OpenSPCoop2Properties.getInstance();
  84.             GestoreJNDI jndi = null;
  85.             if(properties.getJNDIContext_TimerEJB()==null)
  86.                 jndi = new GestoreJNDI();
  87.             else
  88.                 jndi = new GestoreJNDI(properties.getJNDIContext_TimerEJB());
  89.            
  90.         String nomeJNDI = properties.getJNDITimerEJBName().get(TimerGestoreMessaggi.ID_MODULO);
  91.         Object objref = jndi.lookup(nomeJNDI);
  92.         TimerGestoreMessaggiHome timerHome =
  93.         (TimerGestoreMessaggiHome) PortableRemoteObject.narrow(objref,TimerGestoreMessaggiHome.class);
  94.         TimerGestoreMessaggi timerDiServizio = timerHome.create();  
  95.  
  96.             return timerDiServizio;
  97.        
  98.        
  99.     }
  100.    
  101.    
  102.     /**
  103.      * Metodo che si occupa di ottenere il EJB utile alla gestione dei Messaggi anomali
  104.      *
  105.      * @return un EJB utile alla gestione dei messaggi {@link org.openspcoop2.pdd.timers.TimerGestorePuliziaMessaggiAnomali}.
  106.      *
  107.      */
  108.     public static TimerGestorePuliziaMessaggiAnomali createTimerGestorePuliziaMessaggiAnomali() throws Exception {
  109.        
  110.         OpenSPCoop2Properties properties = OpenSPCoop2Properties.getInstance();
  111.         GestoreJNDI jndi = null;
  112.         if(properties.getJNDIContext_TimerEJB()==null)
  113.             jndi = new GestoreJNDI();
  114.         else
  115.             jndi = new GestoreJNDI(properties.getJNDIContext_TimerEJB());
  116.            
  117.         String nomeJNDI = properties.getJNDITimerEJBName().get(TimerGestorePuliziaMessaggiAnomali.ID_MODULO);
  118.         Object objref = jndi.lookup(nomeJNDI);
  119.         TimerGestorePuliziaMessaggiAnomaliHome timerHome =
  120.         (TimerGestorePuliziaMessaggiAnomaliHome) PortableRemoteObject.narrow(objref,TimerGestorePuliziaMessaggiAnomaliHome.class);
  121.         TimerGestorePuliziaMessaggiAnomali timerDiServizio = timerHome.create();    
  122.  
  123.         return timerDiServizio;
  124.        
  125.        
  126.     }

  127.    
  128.     /**
  129.      * Metodo che si occupa di ottenere il EJB utile alla gestione delle buste
  130.      *
  131.      * @return un EJB utile alla gestione delle buste {@link org.openspcoop2.pdd.timers.TimerGestoreRepositoryBuste}.
  132.      *
  133.      */
  134.     public static TimerGestoreRepositoryBuste createTimerGestoreRepositoryBuste() throws Exception {
  135.        
  136.             OpenSPCoop2Properties properties = OpenSPCoop2Properties.getInstance();
  137.             GestoreJNDI jndi = null;
  138.             if(properties.getJNDIContext_TimerEJB()==null)
  139.                 jndi = new GestoreJNDI();
  140.             else
  141.                 jndi = new GestoreJNDI(properties.getJNDIContext_TimerEJB());
  142.             String nomeJNDI = properties.getJNDITimerEJBName().get(TimerGestoreRepositoryBuste.ID_MODULO);
  143.             Object objref = jndi.lookup(nomeJNDI);
  144.             TimerGestoreRepositoryBusteHome timerHome =
  145.                 (TimerGestoreRepositoryBusteHome) PortableRemoteObject.narrow(objref,TimerGestoreRepositoryBusteHome.class);
  146.             TimerGestoreRepositoryBuste timerDiServizio = timerHome.create();  
  147.  
  148.             return timerDiServizio;
  149.        
  150.     }
  151.    

  152.     public static boolean createEmptyLockTimers(OpenSPCoop2Properties propertiesReader, String ID_MODULO, Logger logCore, boolean logErrorConnection) {
  153.         return _lockTimers(propertiesReader, ID_MODULO, logCore, logErrorConnection, false);
  154.     }
  155.     public static void relaseLockTimers(OpenSPCoop2Properties propertiesReader, String ID_MODULO, Logger logCore, boolean logErrorConnection) {
  156.         _lockTimers(propertiesReader, ID_MODULO, logCore, logErrorConnection, true);
  157.     }
  158.     public static boolean _lockTimers(OpenSPCoop2Properties propertiesReader, String ID_MODULO, Logger logCore, boolean logErrorConnection, boolean release) {
  159.        
  160.         boolean ok = true;
  161.        
  162.         if(propertiesReader!=null && propertiesReader.isTimerLockByDatabase() && propertiesReader.getDatabaseType()!=null) {
  163.            
  164.             // RILASCIO LOCK SUL RUNTIME
  165.            
  166.             List<TipoLock> tipiStatistiche = new ArrayList<TipoLock>();
  167.             tipiStatistiche.add(TipoLock.GENERAZIONE_STATISTICHE_ORARIE);
  168.             tipiStatistiche.add(TipoLock.GENERAZIONE_STATISTICHE_GIORNALIERE);
  169.             tipiStatistiche.add(TipoLock.GENERAZIONE_STATISTICHE_SETTIMANALI);
  170.             tipiStatistiche.add(TipoLock.GENERAZIONE_STATISTICHE_MENSILI);
  171.            
  172.             List<TipoLock> tipiRuntime = new ArrayList<TipoLock>();
  173.             tipiRuntime.add(TipoLock.GESTIONE_BUSTE_NON_RISCONTRATE);
  174.             tipiRuntime.add(TipoLock.GESTIONE_CORRELAZIONE_APPLICATIVA);
  175.             tipiRuntime.add(TipoLock.GESTIONE_PULIZIA_MESSAGGI_ANOMALI);
  176.             if(propertiesReader.isMsgGiaInProcessamentoUseLock()) {
  177.                 tipiRuntime.add(TipoLock._getLockGestioneRepositoryMessaggi());
  178.             }
  179.             else {
  180.                 tipiRuntime.add(TipoLock.GESTIONE_PULIZIA_REPOSITORY_MESSAGGI);
  181.                 tipiRuntime.add(TipoLock.GESTIONE_PULIZIA_REPOSITORY_BUSTE);
  182.             }
  183.             if(propertiesReader.isStatisticheGenerazioneEnabled()){
  184.                 if(propertiesReader.isStatisticheUsePddRuntimeDatasource()) {
  185.                     tipiRuntime.addAll(tipiStatistiche);
  186.                 }
  187.             }

  188.             if(tipiRuntime!=null && tipiRuntime.size()>0) {
  189.                 Resource resource = null;
  190.                 DBManager dbManager = DBManager.getInstance();
  191.                 try {
  192.                     resource = dbManager.getResource(propertiesReader.getIdentitaPortaDefaultWithoutProtocol(), ID_MODULO, null, false);
  193.                    
  194.                     for (TipoLock tipoLock : tipiRuntime) {
  195.                         _lock(propertiesReader, (Connection)resource.getResource(), tipoLock, logCore, release);
  196.                     }
  197.                    
  198.                     if(propertiesReader.isServerJ2EE()!=null && propertiesReader.isServerJ2EE()==false){
  199.                         if(propertiesReader.isTimerConsegnaContenutiApplicativiAbilitato()){
  200.                             List<String> code = propertiesReader.getTimerConsegnaContenutiApplicativiCode();
  201.                             if(code!=null && !code.isEmpty()) {
  202.                                 for (String coda : code) {
  203.                                     boolean result = _lock(propertiesReader, (Connection)resource.getResource(), TimerLock.getIdLockConsegnaNotifica(coda), logCore, release);
  204.                                     if(!result) {
  205.                                         ok = false; // l'errore è registrato all'interno del metodo _lock
  206.                                     }
  207.                                 }
  208.                             }
  209.                         }
  210.                     }
  211.                    
  212.                 }catch(Exception e){
  213.                     ok = false;
  214.                     if(logErrorConnection) {
  215.                         logCore.error("Riscontrato errore durante il "+(release?"il rilascio":"la creazione")+" dei lock: "+e.getMessage(),e);
  216.                     }
  217.                     else {
  218.                         logCore.debug("Riscontrato errore durante il "+(release?"il rilascio":"la creazione")+" dei lock, in fase di shutdown: "+e.getMessage(),e);
  219.                     }
  220.                 }
  221.                 finally {
  222.                     if(dbManager!=null) {
  223.                         dbManager.releaseResource(propertiesReader.getIdentitaPortaDefaultWithoutProtocol(), ID_MODULO, resource, false);
  224.                     }
  225.                 }
  226.             }
  227.            
  228.             // RILASCIO LOCK STATISTICHE SE NON SONO SUL RUNTIME
  229.             if(propertiesReader.isStatisticheGenerazioneEnabled() && !propertiesReader.isStatisticheUsePddRuntimeDatasource()){
  230.                
  231.                 if(tipiStatistiche!=null && tipiStatistiche.size()>0) {
  232.                     Resource resource = null;
  233.                     DBStatisticheManager dbStatisticheManager = null;
  234.                     DBTransazioniManager dbTransazioniManager = null;
  235.                     if(propertiesReader.isStatisticheUseTransazioniDatasource()) {
  236.                         dbTransazioniManager = DBTransazioniManager.getInstance();
  237.                     }
  238.                     else {
  239.                         dbStatisticheManager = DBStatisticheManager.getInstance();
  240.                     }
  241.                     try {
  242.                         if(dbTransazioniManager!=null) {
  243.                             resource = dbTransazioniManager.getResource(propertiesReader.getIdentitaPortaDefaultWithoutProtocol(), ID_MODULO, null, false);
  244.                         }
  245.                         else if(dbStatisticheManager!=null){
  246.                             resource = dbStatisticheManager.getResource(propertiesReader.getIdentitaPortaDefaultWithoutProtocol(), ID_MODULO, null, false);
  247.                         }
  248.                        
  249.                         for (TipoLock tipoLock : tipiStatistiche) {
  250.                             if(resource!=null) {
  251.                                 boolean result = _lock(propertiesReader, (Connection)resource.getResource(), tipoLock, logCore, release);
  252.                                 if(!result) {
  253.                                     ok = false; // l'errore è registrato all'interno del metodo _lock
  254.                                 }
  255.                             }
  256.                             else {
  257.                                 ok = false;
  258.                                 logCore.error("Timer Resource is null?");
  259.                             }
  260.                         }
  261.                     }catch(Exception e){
  262.                         ok = false;
  263.                         if(logErrorConnection) {
  264.                             logCore.error("Riscontrato errore durante il "+(release?"il rilascio":"la creazione")+" dei lock: "+e.getMessage(),e);
  265.                         }
  266.                         else {
  267.                             logCore.debug("Riscontrato errore durante il "+(release?"il rilascio":"la creazione")+" dei lock, in fase di shutdown: "+e.getMessage(),e);
  268.                         }
  269.                     }
  270.                     finally {
  271.                         if(dbTransazioniManager!=null) {
  272.                             dbTransazioniManager.releaseResource(propertiesReader.getIdentitaPortaDefaultWithoutProtocol(), ID_MODULO, resource, false);
  273.                         }
  274.                         else if(dbStatisticheManager!=null){
  275.                             dbStatisticheManager.releaseResource(propertiesReader.getIdentitaPortaDefaultWithoutProtocol(), ID_MODULO, resource, false);
  276.                         }
  277.                     }
  278.                 }
  279.                
  280.             }
  281.         }
  282.        
  283.         return ok;
  284.     }
  285.    
  286.     private static boolean _lock(OpenSPCoop2Properties propertiesReader, Connection connection, TipoLock tipoLock, Logger logCore, boolean release) {
  287.         return _lock(propertiesReader,connection,tipoLock.getTipo(),logCore, release);
  288.     }
  289.     private static boolean _lock(OpenSPCoop2Properties propertiesReader, Connection connection, String idLock, Logger logCore, boolean release) {
  290.         boolean ok = true;
  291.         try{
  292.             InfoStatistics semaphore_statistics = new InfoStatistics();
  293.             SemaphoreConfiguration config = GestoreMessaggi.newSemaphoreConfiguration(propertiesReader.getTimerConsegnaContenutiApplicativiLockMaxLife(),
  294.                     propertiesReader.getTimerConsegnaContenutiApplicativiLockIdleTime());
  295.             //config.setSerializableLevel(true); // in modo da non avere il lock esclusivo sulla tabella e far funzionare anche se l'entry non esiste proprio
  296.             TipiDatabase databaseType = TipiDatabase.toEnumConstant(propertiesReader.getDatabaseType());
  297.             Semaphore semaphore = new Semaphore(semaphore_statistics, SemaphoreMapping.newInstance(idLock),
  298.                     config, databaseType, logCore);
  299.             if(release) {
  300.                 boolean unlock = semaphore.releaseLock(connection, "Riavvio application server");
  301.                 logCore.debug("Lock di tipo '"+idLock+"' "+(unlock?"rilasciato" :"non rilasciato"));
  302.             }
  303.             else {
  304.                 boolean created = semaphore.createEmptyLock(connection, false);
  305.                 logCore.debug("Lock di tipo '"+idLock+"' "+(created?"creato" :"non creato"));
  306.             }
  307.         }catch(Exception e){
  308.             ok = false;
  309.             logCore.error("Riscontrato errore durante "+(release?"il rilascio":"la creazione")+" del lock di tipo '"+idLock+"': "+e.getMessage(),e);
  310.         }
  311.         return ok;
  312.     }
  313. }