NotificatoreEventi.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.core.controllo_traffico;

  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.sql.Connection;
  26. import java.text.SimpleDateFormat;
  27. import java.util.Date;
  28. import java.util.HashMap;
  29. import java.util.Iterator;
  30. import java.util.Map;
  31. import java.util.zip.ZipEntry;
  32. import java.util.zip.ZipFile;
  33. import java.util.zip.ZipOutputStream;

  34. import javax.activation.FileDataSource;

  35. import org.openspcoop2.core.commons.CoreException;
  36. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  37. import org.openspcoop2.core.controllo_traffico.utils.PolicyUtilities;
  38. import org.openspcoop2.core.eventi.Evento;
  39. import org.openspcoop2.core.eventi.constants.CodiceEventoControlloTraffico;
  40. import org.openspcoop2.core.eventi.constants.TipoEvento;
  41. import org.openspcoop2.core.eventi.constants.TipoSeverita;
  42. import org.openspcoop2.core.eventi.utils.SeveritaConverter;
  43. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  44. import org.openspcoop2.pdd.core.eventi.GestoreEventi;
  45. import org.openspcoop2.protocol.basic.Costanti;
  46. import org.openspcoop2.utils.SemaphoreLock;
  47. import org.openspcoop2.utils.Utilities;
  48. import org.openspcoop2.utils.UtilsException;
  49. import org.openspcoop2.utils.beans.WriteToSerializerType;
  50. import org.openspcoop2.utils.date.DateManager;
  51. import org.openspcoop2.utils.date.DateUtils;
  52. import org.openspcoop2.utils.io.ZipUtilities;
  53. import org.openspcoop2.utils.resources.FileSystemUtilities;
  54. import org.slf4j.Logger;

  55. /**    
  56.  * NotificatoreEventi
  57.  *
  58.  * @author Poli Andrea (poli@link.it)
  59.  * @author $Author$
  60.  * @version $Rev$, $Date$
  61.  */
  62. public class NotificatoreEventi {

  63.     private static NotificatoreEventi staticInstance = null;
  64.     private static synchronized void initialize() throws DriverConfigurazioneException {
  65.         if(staticInstance==null){
  66.             staticInstance = new NotificatoreEventi();
  67.         }
  68.     }
  69.     public static NotificatoreEventi getInstance() throws DriverConfigurazioneException {
  70.         if(staticInstance==null){
  71.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  72.             synchronized (NotificatoreEventi.class) {
  73.                 initialize();
  74.             }
  75.         }
  76.         return staticInstance;
  77.     }

  78.     private GestoreEventi gestoreEventi;
  79.        
  80.     private NotificatoreEventi() throws DriverConfigurazioneException {
  81.         this.gestoreEventi = GestoreEventi.getInstance();
  82.     }
  83.    
  84.     /**
  85.      * In memoria vengono collezionate (viene salvata la data):
  86.      * Eventi causati durante la gestione di un thread (denominati TH_EVENTO)
  87.      * - TH_lastMaxRequests. ultimo scarto di una richiesta per raggiungimento MaxThreads
  88.      * - TH_lastPddCongestionata. ultimo evento di rilevamento congestione della PdD
  89.      * - TH_lastPolicyViolated. ultimo scarto per violazione di una policy attiva
  90.      * - TH_lastPolicyViolated_warningOnly. ultimo di scarto per violazione di una policy attiva configurata in warning only
  91.      *
  92.      * Inoltre in memoria vengono collezionati gli ultimi eventi emessi realmente emessi sul database degli eventi
  93.      * (viene salvato la tipologia dell'evento cioè se l'evento occorre 'INGRESSO', ad es. violazione,  
  94.      * o se l'evento termina 'USCITA', ad es. violazione-risolta)
  95.      * per i quattro tipi sopra indicati (denominati con DB_EVENTO):
  96.      * - DB_lastMaxRequests. ultimo evento emesso su db per raggiungimento (o ritorno nei livelli soglia) da MaxThreads
  97.      * - DB_lastPddCongestionata.  ultimo evento di rilevamento congestione (fine congestione) della PdD
  98.      * - DB_lastPolicyViolated. ultimo evento emesso per scarto per violazione (o ritorno nei livelli soglia) di una policy attiva
  99.      * - DB_lastPolicyViolated_warningOnly. ultimo evento emesso per scarto per violazione (o ritorno nei livelli soglia) di una policy attiva in warning only
  100.      *
  101.      * Esistera un timer che gira ogni x minuti.
  102.      * Per ogni TH_<tipo> effettua il seguente algoritmo (dove tipo e' uno dei quattro descritti in precedenza).
  103.      * if( esiste TH_<tipo> ultimo_intervallo_x_minuti ){
  104.      *      if( esiste DB_<tipo> ){
  105.      *          tipologia = DB_<tipo>.tipologia
  106.      *          if ( tipologia == "USCITA" ){
  107.      *              // emetto evento di ingresso, ho una variazione di stato
  108.      *              DB_<tipo> = emettoEventoSuDatabase(<tipo>,"INGRESSO")
  109.      *          }
  110.      *      }
  111.      *      else{
  112.      *          // primo evento
  113.      *          DB_<tipo> = emettoEventoSuDatabase(<tipo>,"INGRESSO")
  114.      *      }
  115.      * }
  116.      * else{
  117.      *      if( esiste DB_<tipo> ){
  118.      *          tipologia = DB_<tipo>.tipologia
  119.      *          if ( tipologia == "INGRESSO" ){
  120.      *              // emetto evento di uscita, ho una variazione di stato
  121.      *              DB_<tipo> = emettoEventoSuDatabase(<tipo>,"USCITA")
  122.      *          }
  123.      *      }  
  124.      * }
  125.      *
  126.      * NOTA: Non ci interessa la visione di dettaglio. E' chiaro che se rilevo un evento di scarto nell'intervallo di 5 minuti
  127.      *       poi puo' darsi che nell'ultimo minuto il problema sia rientrato. Pero' io vorrei segnalare che c'e' stato il problema
  128.      *       Poi nel prossimo controllo, indichero' che il problema e' rientrato, pero' almeno so' che nella finestra temporale dei 5 minuti e' accaduto un errore.
  129.      *      
  130.      * NOTA: L'analisi di dettaglio puo' poi essere effettuata sulle transazioni ricercando le transazioni che contengono quell'evento.
  131.      */
  132.    
  133.     //private Boolean semaphore = true; // per gestire l'accesso alle risorse dal thread
  134.     private static final org.openspcoop2.utils.Semaphore lock = new org.openspcoop2.utils.Semaphore("NotificatoreEventi");
  135.    
  136.     // TH_EVENTO
  137.     private DatiEventoGenerico inMemoryLastMaxRequests = new DatiEventoGenerico();
  138.     private DatiEventoGenerico inMemoryLastMaxRequestsWarningOnly = new DatiEventoGenerico();
  139.     private DatiEventoGenerico inMemoryLastPddCongestionata = new DatiEventoGenerico();
  140.     private Map<String, DatiEventoGenerico> inMemoryLastPolicyGlobaliViolated = new HashMap<>();
  141.     private Map<String, DatiEventoGenerico> inMemoryLastPolicyGlobaliViolatedWarningOnly = new HashMap<>();
  142.     private Map<String, DatiEventoGenerico> inMemoryLastPolicyAPIViolated = new HashMap<>();
  143.     private Map<String, DatiEventoGenerico> inMemoryLastPolicyAPIViolatedWarningOnly = new HashMap<>();
  144.     private Map<String, DatiEventoGenerico> inMemoryLastTimeoutConnessione = new HashMap<>();
  145.     private Map<String, DatiEventoGenerico> inMemoryLastTimeoutRichiesta = new HashMap<>();
  146.     private Map<String, DatiEventoGenerico> inMemoryLastTimeoutRisposta = new HashMap<>();
  147.        
  148.     // DB_EVENTO
  149.     private DatabaseDatiEventoGenerico dbLastMaxRequests = new DatabaseDatiEventoGenerico();
  150.     private DatabaseDatiEventoGenerico dbLastMaxRequestsWarningOnly = new DatabaseDatiEventoGenerico();
  151.     private DatabaseDatiEventoGenerico dbLastPddCongestionata = new DatabaseDatiEventoGenerico();
  152.     private Map<String, DatabaseDatiEventoGenerico> dbLastPolicyGlobaliViolated = new HashMap<>();
  153.     private Map<String, DatabaseDatiEventoGenerico> dbLastPolicyGlobaliViolatedWarningOnly = new HashMap<>();
  154.     private Map<String, DatabaseDatiEventoGenerico> dbLastPolicyAPIViolated = new HashMap<>();
  155.     private Map<String, DatabaseDatiEventoGenerico> dbLastPolicyAPIViolatedWarningOnly = new HashMap<>();
  156.     private Map<String, DatabaseDatiEventoGenerico> dbLastTimeoutConnessione = new HashMap<>();
  157.     private Map<String, DatabaseDatiEventoGenerico> dbLastTimeoutRichiesta = new HashMap<>();
  158.     private Map<String, DatabaseDatiEventoGenerico> dbLastTimeoutRisposta = new HashMap<>();
  159.    
  160.     private static final String ID_POLICY_NON_FORNITA = "IdPolicy non fornita";
  161.    
  162.     public void log(CategoriaEventoControlloTraffico evento, Date date, String descrizione) throws CoreException,UtilsException{
  163.         log(evento, null, null, date, descrizione);
  164.     }
  165.     public void log(CategoriaEventoControlloTraffico evento, String idPolicy, String configurazione, Date date, String descrizione) throws CoreException,UtilsException{
  166.        
  167.         if(evento==null){
  168.             throw new CoreException("Evento non definito");
  169.         }
  170.        
  171.         switch (evento) {
  172.        
  173.         case LIMITE_GLOBALE_RICHIESTE_SIMULTANEE:{
  174.             SemaphoreLock slock = lock.acquire("log_"+evento.name());
  175.             try {
  176.                 if(this.inMemoryLastMaxRequests.datiConsumatiThread){
  177.                     if(date==null)
  178.                         this.inMemoryLastMaxRequests.data = DateManager.getDate();
  179.                     else
  180.                         this.inMemoryLastMaxRequests.data = date;
  181.                     this.inMemoryLastMaxRequests.descrizione = descrizione;
  182.                     this.inMemoryLastMaxRequests.configurazione = configurazione;
  183.                     /**System.out.println("@@LOG AGGIORNO DATA LIMITE_RICHIESTE_SIMULTANEE_VIOLAZIONE ["+
  184.                         newSimpleDateFormat().format(this.inMemoryLastMaxRequests.data)+"]");*/
  185.                     this.inMemoryLastMaxRequests.datiConsumatiThread=false;
  186.                 }
  187.             }finally {
  188.                 lock.release(slock, "log_"+evento.name());
  189.             }
  190.             break;
  191.         }
  192.         case LIMITE_GLOBALE_RICHIESTE_SIMULTANEE_WARNING_ONLY:{
  193.             SemaphoreLock slock = lock.acquire("log_"+evento.name());
  194.             try {
  195.                 if(this.inMemoryLastMaxRequestsWarningOnly.datiConsumatiThread){
  196.                     if(date==null)
  197.                         this.inMemoryLastMaxRequestsWarningOnly.data = DateManager.getDate();
  198.                     else
  199.                         this.inMemoryLastMaxRequestsWarningOnly.data = date;
  200.                     this.inMemoryLastMaxRequestsWarningOnly.descrizione = descrizione;
  201.                     this.inMemoryLastMaxRequestsWarningOnly.configurazione = configurazione;
  202.                     /**System.out.println("@@LOG AGGIORNO DATA LIMITE_RICHIESTE_SIMULTANEE_VIOLAZIONE WARNING_ONLY ["+
  203.                         newSimpleDateFormat().format(this.inMemoryLastMaxRequestsWarningOnly.data)+"]");*/
  204.                     this.inMemoryLastMaxRequestsWarningOnly.datiConsumatiThread=false;
  205.                 }
  206.             }finally {
  207.                 lock.release(slock, "log_"+evento.name());
  208.             }
  209.             break;
  210.         }
  211.         case CONGESTIONE_PORTA_DOMINIO:{
  212.             SemaphoreLock slock = lock.acquire("log_"+evento.name());
  213.             try {
  214.                 if(this.inMemoryLastPddCongestionata.datiConsumatiThread){
  215.                     if(date==null)
  216.                         this.inMemoryLastPddCongestionata.data = DateManager.getDate();
  217.                     else
  218.                         this.inMemoryLastPddCongestionata.data = date;
  219.                     this.inMemoryLastPddCongestionata.descrizione = descrizione;
  220.                     this.inMemoryLastPddCongestionata.configurazione = configurazione;
  221.                     /**System.out.println("@@LOG AGGIORNO DATA CONGESTIONE PDD ["+
  222.                         newSimpleDateFormat().format(this.inMemoryLastPddCongestionata.data)+"]");*/
  223.                     this.inMemoryLastPddCongestionata.datiConsumatiThread=false;
  224.                 }
  225.             }finally {
  226.                 lock.release(slock, "log_"+evento.name());
  227.             }
  228.             break;
  229.         }
  230.         case POLICY_GLOBALE:{
  231.             if(idPolicy==null){
  232.                 throw new CoreException(ID_POLICY_NON_FORNITA);
  233.             }
  234.             SemaphoreLock slock = lock.acquire("log_"+evento.name());
  235.             try {
  236.                 if(!this.inMemoryLastPolicyGlobaliViolated.containsKey(idPolicy)){
  237.                     this.inMemoryLastPolicyGlobaliViolated.put(idPolicy, new DatiEventoGenerico());
  238.                 }
  239.                 if(!this.dbLastPolicyGlobaliViolated.containsKey(idPolicy)){
  240.                     this.dbLastPolicyGlobaliViolated.put(idPolicy, new DatabaseDatiEventoGenerico());
  241.                 }
  242.                 DatiEventoGenerico inMemoryLastPolicyGlobaleViolatedPolicy = this.inMemoryLastPolicyGlobaliViolated.get(idPolicy);
  243.                
  244.                 if(inMemoryLastPolicyGlobaleViolatedPolicy.datiConsumatiThread){
  245.                     if(date==null)
  246.                         inMemoryLastPolicyGlobaleViolatedPolicy.data = DateManager.getDate();
  247.                     else
  248.                         inMemoryLastPolicyGlobaleViolatedPolicy.data = date;
  249.                     inMemoryLastPolicyGlobaleViolatedPolicy.descrizione = descrizione;
  250.                     inMemoryLastPolicyGlobaleViolatedPolicy.configurazione = configurazione;
  251.                     /**System.out.println("@@LOG AGGIORNO DATA POLICY VIOLAZIONE ["+
  252.                             newSimpleDateFormat().format(inMemoryLastPolicyGlobaleViolatedPolicy.data)+"]");*/
  253.                     inMemoryLastPolicyGlobaleViolatedPolicy.datiConsumatiThread=false;
  254.                 }
  255.             }finally {
  256.                 lock.release(slock, "log_"+evento.name());
  257.             }
  258.             break;
  259.         }
  260.         case POLICY_GLOBALE_WARNING_ONLY:{
  261.             if(idPolicy==null){
  262.                 throw new CoreException(ID_POLICY_NON_FORNITA);
  263.             }
  264.             SemaphoreLock slock = lock.acquire("log_"+evento.name());
  265.             try {
  266.                 if(!this.inMemoryLastPolicyGlobaliViolatedWarningOnly.containsKey(idPolicy)){
  267.                     this.inMemoryLastPolicyGlobaliViolatedWarningOnly.put(idPolicy, new DatiEventoGenerico());
  268.                 }
  269.                 if(!this.dbLastPolicyGlobaliViolatedWarningOnly.containsKey(idPolicy)){
  270.                     this.dbLastPolicyGlobaliViolatedWarningOnly.put(idPolicy, new DatabaseDatiEventoGenerico());
  271.                 }
  272.                 DatiEventoGenerico inMemoryLastPolicyGlobaleViolatedWarningOnlyPolicy = this.inMemoryLastPolicyGlobaliViolatedWarningOnly.get(idPolicy);
  273.                
  274.                 if(inMemoryLastPolicyGlobaleViolatedWarningOnlyPolicy.datiConsumatiThread){
  275.                     if(date==null)
  276.                         inMemoryLastPolicyGlobaleViolatedWarningOnlyPolicy.data = DateManager.getDate();
  277.                     else
  278.                         inMemoryLastPolicyGlobaleViolatedWarningOnlyPolicy.data = date;
  279.                     inMemoryLastPolicyGlobaleViolatedWarningOnlyPolicy.descrizione = descrizione;
  280.                     inMemoryLastPolicyGlobaleViolatedWarningOnlyPolicy.configurazione = configurazione;
  281.                     /**System.out.println("@@LOG AGGIORNO DATA POLICY VIOLAZIONE WARNING_ONLY ["+
  282.                             newSimpleDateFormat().format(inMemoryLastPolicyGlobaleViolatedWarningOnlyPolicy.data)+"]");*/
  283.                     inMemoryLastPolicyGlobaleViolatedWarningOnlyPolicy.datiConsumatiThread=false;
  284.                 }
  285.             }finally {
  286.                 lock.release(slock, "log_"+evento.name());
  287.             }
  288.             break;
  289.         }
  290.            
  291.         case POLICY_API:{
  292.             if(idPolicy==null){
  293.                 throw new CoreException(ID_POLICY_NON_FORNITA);
  294.             }
  295.             SemaphoreLock slock = lock.acquire("log_"+evento.name());
  296.             try {
  297.                 if(!this.inMemoryLastPolicyAPIViolated.containsKey(idPolicy)){
  298.                     this.inMemoryLastPolicyAPIViolated.put(idPolicy, new DatiEventoGenerico());
  299.                 }
  300.                 if(!this.dbLastPolicyAPIViolated.containsKey(idPolicy)){
  301.                     this.dbLastPolicyAPIViolated.put(idPolicy, new DatabaseDatiEventoGenerico());
  302.                 }
  303.                 DatiEventoGenerico inMemoryLastPolicyAPIViolatedPolicy = this.inMemoryLastPolicyAPIViolated.get(idPolicy);
  304.                
  305.                 if(inMemoryLastPolicyAPIViolatedPolicy.datiConsumatiThread){
  306.                     if(date==null)
  307.                         inMemoryLastPolicyAPIViolatedPolicy.data = DateManager.getDate();
  308.                     else
  309.                         inMemoryLastPolicyAPIViolatedPolicy.data = date;
  310.                     inMemoryLastPolicyAPIViolatedPolicy.descrizione = descrizione;
  311.                     inMemoryLastPolicyAPIViolatedPolicy.configurazione = configurazione;
  312.                     /**System.out.println("@@LOG AGGIORNO DATA POLICY VIOLAZIONE ["+
  313.                             newSimpleDateFormat().format(inMemoryLastPolicyAPIViolatedPolicy.data)+"]");*/
  314.                     inMemoryLastPolicyAPIViolatedPolicy.datiConsumatiThread=false;
  315.                 }
  316.             }finally {
  317.                 lock.release(slock, "log_"+evento.name());
  318.             }
  319.             break;
  320.         }
  321.         case POLICY_API_WARNING_ONLY:{
  322.             if(idPolicy==null){
  323.                 throw new CoreException(ID_POLICY_NON_FORNITA);
  324.             }
  325.             SemaphoreLock slock = lock.acquire("log_"+evento.name());
  326.             try {
  327.                 if(!this.inMemoryLastPolicyAPIViolatedWarningOnly.containsKey(idPolicy)){
  328.                     this.inMemoryLastPolicyAPIViolatedWarningOnly.put(idPolicy, new DatiEventoGenerico());
  329.                 }
  330.                 if(!this.dbLastPolicyAPIViolatedWarningOnly.containsKey(idPolicy)){
  331.                     this.dbLastPolicyAPIViolatedWarningOnly.put(idPolicy, new DatabaseDatiEventoGenerico());
  332.                 }
  333.                 DatiEventoGenerico inMemoryLastPolicyAPIViolatedWarningOnlyPolicy = this.inMemoryLastPolicyAPIViolatedWarningOnly.get(idPolicy);
  334.                
  335.                 if(inMemoryLastPolicyAPIViolatedWarningOnlyPolicy.datiConsumatiThread){
  336.                     if(date==null)
  337.                         inMemoryLastPolicyAPIViolatedWarningOnlyPolicy.data = DateManager.getDate();
  338.                     else
  339.                         inMemoryLastPolicyAPIViolatedWarningOnlyPolicy.data = date;
  340.                     inMemoryLastPolicyAPIViolatedWarningOnlyPolicy.descrizione = descrizione;
  341.                     inMemoryLastPolicyAPIViolatedWarningOnlyPolicy.configurazione = configurazione;
  342.                     /**System.out.println("@@LOG AGGIORNO DATA POLICY VIOLAZIONE WARNING_ONLY ["+
  343.                             newSimpleDateFormat().format(inMemoryLastPolicyAPIViolatedWarningOnlyPolicy.data)+"]");*/
  344.                     inMemoryLastPolicyAPIViolatedWarningOnlyPolicy.datiConsumatiThread=false;
  345.                 }
  346.             }finally {
  347.                 lock.release(slock, "log_"+evento.name());
  348.             }
  349.             break;
  350.         }
  351.         case TIMEOUT_CONNESSIONE:{
  352.             if(idPolicy==null){
  353.                 throw new CoreException(ID_POLICY_NON_FORNITA);
  354.             }
  355.             SemaphoreLock slock = lock.acquire("log_"+evento.name());
  356.             try {
  357.                 if(!this.inMemoryLastTimeoutConnessione.containsKey(idPolicy)){
  358.                     this.inMemoryLastTimeoutConnessione.put(idPolicy, new DatiEventoGenerico());
  359.                 }
  360.                 if(!this.dbLastTimeoutConnessione.containsKey(idPolicy)){
  361.                     this.dbLastTimeoutConnessione.put(idPolicy, new DatabaseDatiEventoGenerico());
  362.                 }
  363.                 DatiEventoGenerico inMemoryLastTimeoutConnessioneRead = this.inMemoryLastTimeoutConnessione.get(idPolicy);
  364.                
  365.                 if(inMemoryLastTimeoutConnessioneRead.datiConsumatiThread){
  366.                     if(date==null)
  367.                         inMemoryLastTimeoutConnessioneRead.data = DateManager.getDate();
  368.                     else
  369.                         inMemoryLastTimeoutConnessioneRead.data = date;
  370.                     inMemoryLastTimeoutConnessioneRead.descrizione = descrizione;
  371.                     inMemoryLastTimeoutConnessioneRead.configurazione = configurazione;
  372.                     /**System.out.println("@@LOG AGGIORNO DATA TIMEOUT CONNESSIONE VIOLAZIONE ["+
  373.                             newSimpleDateFormat().format(inMemoryLastTimeoutConnessioneRead.data)+"]");*/
  374.                     inMemoryLastTimeoutConnessioneRead.datiConsumatiThread=false;
  375.                 }
  376.             }finally {
  377.                 lock.release(slock, "log_"+evento.name());
  378.             }
  379.             break;
  380.         }
  381.         case TIMEOUT_RICHIESTA:{
  382.             if(idPolicy==null){
  383.                 throw new CoreException(ID_POLICY_NON_FORNITA);
  384.             }
  385.             SemaphoreLock slock = lock.acquire("log_"+evento.name());
  386.             try {
  387.                 if(!this.inMemoryLastTimeoutRichiesta.containsKey(idPolicy)){
  388.                     this.inMemoryLastTimeoutRichiesta.put(idPolicy, new DatiEventoGenerico());
  389.                 }
  390.                 if(!this.dbLastTimeoutRichiesta.containsKey(idPolicy)){
  391.                     this.dbLastTimeoutRichiesta.put(idPolicy, new DatabaseDatiEventoGenerico());
  392.                 }
  393.                 DatiEventoGenerico inMemoryLastTimeoutRichiestaRead = this.inMemoryLastTimeoutRichiesta.get(idPolicy);
  394.                
  395.                 if(inMemoryLastTimeoutRichiestaRead.datiConsumatiThread){
  396.                     if(date==null)
  397.                         inMemoryLastTimeoutRichiestaRead.data = DateManager.getDate();
  398.                     else
  399.                         inMemoryLastTimeoutRichiestaRead.data = date;
  400.                     inMemoryLastTimeoutRichiestaRead.descrizione = descrizione;
  401.                     inMemoryLastTimeoutRichiestaRead.configurazione = configurazione;
  402.                     /**System.out.println("@@LOG AGGIORNO DATA TIMEOUT RICHIESTA VIOLAZIONE ["+
  403.                             newSimpleDateFormat().format(inMemoryLastTimeoutRichiestaRead.data)+"]");*/
  404.                     inMemoryLastTimeoutRichiestaRead.datiConsumatiThread=false;
  405.                 }
  406.             }finally {
  407.                 lock.release(slock, "log_"+evento.name());
  408.             }
  409.             break;
  410.         }
  411.         case TIMEOUT_RISPOSTA:{
  412.             if(idPolicy==null){
  413.                 throw new CoreException(ID_POLICY_NON_FORNITA);
  414.             }
  415.             SemaphoreLock slock = lock.acquire("log_"+evento.name());
  416.             try {
  417.                 if(!this.inMemoryLastTimeoutRisposta.containsKey(idPolicy)){
  418.                     this.inMemoryLastTimeoutRisposta.put(idPolicy, new DatiEventoGenerico());
  419.                 }
  420.                 if(!this.dbLastTimeoutRisposta.containsKey(idPolicy)){
  421.                     this.dbLastTimeoutRisposta.put(idPolicy, new DatabaseDatiEventoGenerico());
  422.                 }
  423.                 DatiEventoGenerico inMemoryLastTimeoutRispostaRead = this.inMemoryLastTimeoutRisposta.get(idPolicy);
  424.                
  425.                 if(inMemoryLastTimeoutRispostaRead.datiConsumatiThread){
  426.                     if(date==null)
  427.                         inMemoryLastTimeoutRispostaRead.data = DateManager.getDate();
  428.                     else
  429.                         inMemoryLastTimeoutRispostaRead.data = date;
  430.                     inMemoryLastTimeoutRispostaRead.descrizione = descrizione;
  431.                     inMemoryLastTimeoutRispostaRead.configurazione = configurazione;
  432.                     /**System.out.println("@@LOG AGGIORNO DATA TIMEOUT RISPOSTA VIOLAZIONE ["+
  433.                             newSimpleDateFormat().format(inMemoryLastTimeoutRispostaRead.data)+"]");*/
  434.                     inMemoryLastTimeoutRispostaRead.datiConsumatiThread=false;
  435.                 }
  436.             }finally {
  437.                 lock.release(slock, "log_"+evento.name());
  438.             }
  439.             break;
  440.         }


  441.         default:
  442.             throw new CoreException("Tipo di evento ["+evento.name()+"] non gestito con questo metodo");
  443.         }
  444.     }
  445.    
  446.    
  447.     private static void logInfo(Logger log,boolean debug,String msg){
  448.         if(debug){
  449.             log.info(msg);
  450.         }
  451.     }
  452.     private static void logDebug(Logger log,boolean debug,String msg){
  453.         if(debug){
  454.             log.debug(msg);
  455.         }
  456.     }
  457.    
  458.     private static final String SUFFIX_IN_CORSO_PARENTESI_QUADRE = "] ...";
  459.     private static final String SUFFIX_IN_CORSO = ") ...";
  460.     private static final String SUFFIX_WARNING_ONLY_IN_CORSO = ") (warning-only) ...";
  461.     private static final String SUFFIX_TERMINATA = ") terminata";
  462.     private static final String SUFFIX_WARNING_ONLY_TERMINATA = ") (warning-only) terminata";
  463.     private static final String EVERY = " every:";
  464.     private static final String SEPARATOR = "=================================================================================";
  465.    
  466.     private static String getSuffixDate(SimpleDateFormat df, Date newInterval, int secondi) {
  467.         return "[next-interval: "+df.format(newInterval)+
  468.             "] [Prossimo Controllo previsto tra "+secondi+" secondi: "+df.format(new Date(DateManager.getTimeMillis()+(secondi*1000)))+"]";
  469.     }
  470.        
  471.     public Date process(Logger log, int secondi, Date lastInterval, Connection connection, boolean debug) throws UtilsException{
  472.        
  473.         SimpleDateFormat df = DateUtils.getSimpleDateFormatMs();
  474.        
  475.         logInfo(log,debug,SEPARATOR);
  476.        
  477.         logInfo(log,debug,"Analisi memoria per generazione eventi in corso [interval: "+df.format(lastInterval)+SUFFIX_IN_CORSO_PARENTESI_QUADRE);
  478.                
  479.         // Raccolgo informazioni in memoria per rilasciare il lock
  480.        
  481.         // TH_*
  482.         DatiEventoGenerico localInMemoryLastMaxRequests = null;
  483.         DatiEventoGenerico localInMemoryLastMaxRequestsWarningOnly = null;
  484.         DatiEventoGenerico localInMemoryLastPddCongestionata = null;
  485.         Map<String, DatiEventoGenerico> localInMemoryLastPolicyGlobaliViolated = new HashMap<>();
  486.         Map<String, DatiEventoGenerico> localInMemoryLastPolicyGlobaliViolatedWarningOnly = new HashMap<>();    
  487.         Map<String, DatiEventoGenerico> localInMemoryLastPolicyAPIViolated = new HashMap<>();
  488.         Map<String, DatiEventoGenerico> localInMemoryLastPolicyAPIViolatedWarningOnly = new HashMap<>();
  489.         Date newInterval = null;
  490.         SemaphoreLock slock = lock.acquire("process");
  491.         try {
  492.            
  493.             localInMemoryLastMaxRequests = this.inMemoryLastMaxRequests.readAndConsume();
  494.             localInMemoryLastMaxRequestsWarningOnly = this.inMemoryLastMaxRequestsWarningOnly.readAndConsume();
  495.             localInMemoryLastPddCongestionata = this.inMemoryLastPddCongestionata.readAndConsume();
  496.            
  497.             if(this.inMemoryLastPolicyGlobaliViolated!=null && this.inMemoryLastPolicyGlobaliViolated.size()>0){
  498.                 for (Map.Entry<String,DatiEventoGenerico> entry : this.inMemoryLastPolicyGlobaliViolated.entrySet()) {
  499.                     localInMemoryLastPolicyGlobaliViolated.put(entry.getKey(), entry.getValue().readAndConsume());
  500.                 }
  501.             }
  502.             if(this.inMemoryLastPolicyGlobaliViolatedWarningOnly!=null && this.inMemoryLastPolicyGlobaliViolatedWarningOnly.size()>0){
  503.                 for (Map.Entry<String,DatiEventoGenerico> entry : this.inMemoryLastPolicyGlobaliViolatedWarningOnly.entrySet()) {
  504.                     localInMemoryLastPolicyGlobaliViolatedWarningOnly.put(entry.getKey(), entry.getValue().readAndConsume());
  505.                 }
  506.             }
  507.            
  508.             if(this.inMemoryLastPolicyAPIViolated!=null && this.inMemoryLastPolicyAPIViolated.size()>0){
  509.                 for (Map.Entry<String,DatiEventoGenerico> entry : this.inMemoryLastPolicyAPIViolated.entrySet()) {
  510.                     localInMemoryLastPolicyAPIViolated.put(entry.getKey(), entry.getValue().readAndConsume());
  511.                 }
  512.             }
  513.             if(this.inMemoryLastPolicyAPIViolatedWarningOnly!=null && this.inMemoryLastPolicyAPIViolatedWarningOnly.size()>0){
  514.                 for (Map.Entry<String,DatiEventoGenerico> entry : this.inMemoryLastPolicyAPIViolatedWarningOnly.entrySet()) {
  515.                     localInMemoryLastPolicyAPIViolatedWarningOnly.put(entry.getKey(), entry.getValue().readAndConsume());
  516.                 }
  517.             }

  518.             newInterval = DateManager.getDate();
  519.         }finally {
  520.             lock.release(slock, "process");
  521.         }

  522.        
  523.         // Gli eventi db_* sono acceduti solo dal thread non serve un synchronized
  524.        
  525.         // DB_*
  526.         DatabaseDatiEventoGenerico localDbLastMaxRequests = null;
  527.         DatabaseDatiEventoGenerico localDbLastMaxRequestsWarningOnly = null;
  528.         DatabaseDatiEventoGenerico localDbLastPddCongestionata = null;
  529.         Map<String, DatabaseDatiEventoGenerico> localDbLastPolicyGlobaliViolated = null;
  530.         Map<String, DatabaseDatiEventoGenerico> localDbLastPolicyGlobaliViolatedWarningOnly = null;
  531.         Map<String, DatabaseDatiEventoGenerico> localDbLastPolicyAPIViolated = null;
  532.         Map<String, DatabaseDatiEventoGenerico> localDbLastPolicyAPIViolatedWarningOnly = null;
  533.         localDbLastMaxRequests = this.dbLastMaxRequests;
  534.         localDbLastMaxRequestsWarningOnly = this.dbLastMaxRequestsWarningOnly;
  535.         localDbLastPddCongestionata = this.dbLastPddCongestionata;
  536.         localDbLastPolicyGlobaliViolated = this.dbLastPolicyGlobaliViolated;
  537.         localDbLastPolicyGlobaliViolatedWarningOnly = this.dbLastPolicyGlobaliViolatedWarningOnly;
  538.         localDbLastPolicyAPIViolated = this.dbLastPolicyAPIViolated;
  539.         localDbLastPolicyAPIViolatedWarningOnly = this.dbLastPolicyAPIViolatedWarningOnly;

  540.        
  541.         // Procedo ad effettuare l'elaborazione per emettere degli eventi
  542.        
  543.         logDebug(log,debug,"1. Analisi violazioni numero massimo richieste simultanee ...");
  544.         processSingleEvent(log,
  545.                 localInMemoryLastMaxRequests,
  546.                 localDbLastMaxRequests,
  547.                 this.dbLastMaxRequests,
  548.                 TipoEvento.CONTROLLO_TRAFFICO_NUMERO_MASSIMO_RICHIESTE_SIMULTANEE,
  549.                 CodiceEventoControlloTraffico.VIOLAZIONE,
  550.                 CodiceEventoControlloTraffico.VIOLAZIONE_RISOLTA,
  551.                 lastInterval, connection, this.gestoreEventi,
  552.                 null, debug);
  553.         logDebug(log,debug,"1. Analisi violazioni numero massimo richieste simultanee terminata");
  554.        
  555.         logDebug(log,debug,"2. Analisi violazioni numero massimo richieste simultanee (warning-only) ...");
  556.         processSingleEvent(log,
  557.                 localInMemoryLastMaxRequestsWarningOnly,
  558.                 localDbLastMaxRequestsWarningOnly,
  559.                 this.dbLastMaxRequestsWarningOnly,
  560.                 TipoEvento.CONTROLLO_TRAFFICO_NUMERO_MASSIMO_RICHIESTE_SIMULTANEE,
  561.                 CodiceEventoControlloTraffico.VIOLAZIONE_WARNING_ONLY,
  562.                 CodiceEventoControlloTraffico.VIOLAZIONE_RISOLTA_WARNING_ONLY,
  563.                 lastInterval, connection, this.gestoreEventi,
  564.                 null, debug);
  565.         logDebug(log,debug,"2. Analisi violazioni numero massimo richieste simultanee (warning-only) terminata");
  566.        
  567.         logDebug(log,debug,"3. Analisi controllo della congestione ...");
  568.         processSingleEvent(log,
  569.                 localInMemoryLastPddCongestionata,
  570.                 localDbLastPddCongestionata,
  571.                 this.dbLastPddCongestionata,
  572.                 TipoEvento.CONTROLLO_TRAFFICO_SOGLIA_CONGESTIONE,
  573.                 CodiceEventoControlloTraffico.VIOLAZIONE,
  574.                 CodiceEventoControlloTraffico.VIOLAZIONE_RISOLTA,
  575.                 lastInterval, connection, this.gestoreEventi,
  576.                 null, debug);
  577.         logDebug(log,debug,"3. Analisi controllo della congestione terminata");
  578.                
  579.         logDebug(log,debug,"4. Analisi policy globali violate (size:"+localInMemoryLastPolicyGlobaliViolated.size()+SUFFIX_IN_CORSO);
  580.         if(!localInMemoryLastPolicyGlobaliViolated.isEmpty()) {
  581.             for (Map.Entry<String,DatiEventoGenerico> entry : localInMemoryLastPolicyGlobaliViolated.entrySet()) {
  582.                 String idPolicy = entry.getKey();
  583.                 processSingleEvent(log,
  584.                         localInMemoryLastPolicyGlobaliViolated.get(idPolicy),
  585.                         localDbLastPolicyGlobaliViolated.get(idPolicy),
  586.                         this.dbLastPolicyGlobaliViolated.get(idPolicy),
  587.                         TipoEvento.RATE_LIMITING_POLICY_GLOBALE,
  588.                         CodiceEventoControlloTraffico.VIOLAZIONE,
  589.                         CodiceEventoControlloTraffico.VIOLAZIONE_RISOLTA,
  590.                         lastInterval, connection, this.gestoreEventi,
  591.                         idPolicy,debug);
  592.             }
  593.         }
  594.         logDebug(log,debug,"4. Analisi policy globali violate (size:"+localInMemoryLastPolicyGlobaliViolated.size()+SUFFIX_TERMINATA);
  595.        
  596.         logDebug(log,debug,"5. Analisi policy globali violate (size:"+localInMemoryLastPolicyGlobaliViolatedWarningOnly.size()+SUFFIX_WARNING_ONLY_IN_CORSO);
  597.         if(!localInMemoryLastPolicyGlobaliViolatedWarningOnly.isEmpty()) {
  598.             for (Map.Entry<String,DatiEventoGenerico> entry : localInMemoryLastPolicyGlobaliViolatedWarningOnly.entrySet()) {
  599.                 String idPolicy = entry.getKey();
  600.                 processSingleEvent(log,
  601.                         localInMemoryLastPolicyGlobaliViolatedWarningOnly.get(idPolicy),
  602.                         localDbLastPolicyGlobaliViolatedWarningOnly.get(idPolicy),
  603.                         this.dbLastPolicyGlobaliViolatedWarningOnly.get(idPolicy),
  604.                         TipoEvento.RATE_LIMITING_POLICY_GLOBALE,
  605.                         CodiceEventoControlloTraffico.VIOLAZIONE_WARNING_ONLY,
  606.                         CodiceEventoControlloTraffico.VIOLAZIONE_RISOLTA_WARNING_ONLY,
  607.                         lastInterval, connection, this.gestoreEventi,
  608.                         idPolicy,debug);
  609.             }
  610.         }
  611.         logDebug(log,debug,"5. Analisi policy globali violate (size:"+localInMemoryLastPolicyGlobaliViolatedWarningOnly.size()+SUFFIX_WARNING_ONLY_TERMINATA);
  612.        
  613.         logDebug(log,debug,"6. Analisi policy API violate (size:"+localInMemoryLastPolicyAPIViolated.size()+SUFFIX_IN_CORSO);
  614.         if(!localInMemoryLastPolicyAPIViolated.isEmpty()) {
  615.             for (Map.Entry<String,DatiEventoGenerico> entry : localInMemoryLastPolicyAPIViolated.entrySet()) {
  616.                 String idPolicy = entry.getKey();
  617.                 processSingleEvent(log,
  618.                         localInMemoryLastPolicyAPIViolated.get(idPolicy),
  619.                         localDbLastPolicyAPIViolated.get(idPolicy),
  620.                         this.dbLastPolicyAPIViolated.get(idPolicy),
  621.                         TipoEvento.RATE_LIMITING_POLICY_API,
  622.                         CodiceEventoControlloTraffico.VIOLAZIONE,
  623.                         CodiceEventoControlloTraffico.VIOLAZIONE_RISOLTA,
  624.                         lastInterval, connection, this.gestoreEventi,
  625.                         idPolicy,debug);    
  626.             }
  627.         }
  628.         logDebug(log,debug,"6. Analisi policy API violate (size:"+localInMemoryLastPolicyAPIViolated.size()+SUFFIX_TERMINATA);
  629.        
  630.         logDebug(log,debug,"7. Analisi policy API violate (size:"+localInMemoryLastPolicyAPIViolatedWarningOnly.size()+SUFFIX_WARNING_ONLY_IN_CORSO);
  631.         if(!localInMemoryLastPolicyAPIViolatedWarningOnly.isEmpty()) {
  632.             for (Map.Entry<String,DatiEventoGenerico> entry : localInMemoryLastPolicyAPIViolatedWarningOnly.entrySet()) {
  633.                 String idPolicy = entry.getKey();
  634.                 processSingleEvent(log,
  635.                         localInMemoryLastPolicyAPIViolatedWarningOnly.get(idPolicy),
  636.                         localDbLastPolicyAPIViolatedWarningOnly.get(idPolicy),
  637.                         this.dbLastPolicyAPIViolatedWarningOnly.get(idPolicy),
  638.                         TipoEvento.RATE_LIMITING_POLICY_API,
  639.                         CodiceEventoControlloTraffico.VIOLAZIONE_WARNING_ONLY,
  640.                         CodiceEventoControlloTraffico.VIOLAZIONE_RISOLTA_WARNING_ONLY,
  641.                         lastInterval, connection, this.gestoreEventi,
  642.                         idPolicy,debug);    
  643.             }
  644.         }
  645.         logDebug(log,debug,"7. Analisi policy API violate (size:"+localInMemoryLastPolicyAPIViolatedWarningOnly.size()+SUFFIX_WARNING_ONLY_TERMINATA);
  646.        

  647.         logInfo(log,debug,"Analisi memoria per generazione eventi terminata "+getSuffixDate(df, newInterval, secondi));
  648.        
  649.         return newInterval; // da usare per il prossimo intervallo
  650.     }
  651.     public Date processConnectionTimeout(Logger log, int secondi, Date lastInterval, Connection connection, boolean debug) throws UtilsException{
  652.        
  653.         SimpleDateFormat df = DateUtils.getSimpleDateFormatMs();
  654.        
  655.         logInfo(log,debug,SEPARATOR);
  656.        
  657.         logInfo(log,debug,"Analisi memoria per generazione eventi in corso 'ConnectionTimeout' [interval: "+df.format(lastInterval)+SUFFIX_IN_CORSO_PARENTESI_QUADRE);
  658.                
  659.         // Raccolgo informazioni in memoria per rilasciare il lock
  660.        
  661.         // TH_*
  662.         Map<String, DatiEventoGenerico> localInMemoryLastTimeoutConnessione = new HashMap<>();
  663.         Date newInterval = null;
  664.         SemaphoreLock slock = lock.acquire("processConnectionTimeout");
  665.         try {
  666.             if(this.inMemoryLastTimeoutConnessione!=null && this.inMemoryLastTimeoutConnessione.size()>0){
  667.                 for (Map.Entry<String,DatiEventoGenerico> entry : this.inMemoryLastTimeoutConnessione.entrySet()) {
  668.                     localInMemoryLastTimeoutConnessione.put(entry.getKey(), entry.getValue().readAndConsume());
  669.                 }
  670.             }

  671.             newInterval = DateManager.getDate();
  672.         }finally {
  673.             lock.release(slock, "processConnectionTimeout");
  674.         }

  675.        
  676.         // Gli eventi db_* sono acceduti solo dal thread non serve un synchronized
  677.        
  678.         // DB_*
  679.         Map<String, DatabaseDatiEventoGenerico> localDbLastTimeoutConnessione = null;
  680.         localDbLastTimeoutConnessione = this.dbLastTimeoutConnessione;

  681.        
  682.         // Procedo ad effettuare l'elaborazione per emettere degli eventi
  683.        
  684.         logDebug(log,debug,"8. Analisi eventi di Timeout durante la connessione (size:"+localInMemoryLastTimeoutConnessione.size()+SUFFIX_IN_CORSO);
  685.         if(!localInMemoryLastTimeoutConnessione.isEmpty()) {
  686.             for (Map.Entry<String,DatiEventoGenerico> entry : localInMemoryLastTimeoutConnessione.entrySet()) {
  687.                 String idPolicy = entry.getKey();
  688.                 processSingleEvent(log,
  689.                         localInMemoryLastTimeoutConnessione.get(idPolicy),
  690.                         localDbLastTimeoutConnessione.get(idPolicy),
  691.                         this.dbLastTimeoutConnessione.get(idPolicy),
  692.                         TipoEvento.CONTROLLO_TRAFFICO_CONNECTION_TIMEOUT,
  693.                         CodiceEventoControlloTraffico.VIOLAZIONE,
  694.                         CodiceEventoControlloTraffico.VIOLAZIONE_RISOLTA,
  695.                         lastInterval, connection, this.gestoreEventi,
  696.                         idPolicy,debug);    
  697.             }
  698.         }
  699.         logDebug(log,debug,"8. Analisi eventi di Timeout durante la connessione (size:"+localInMemoryLastTimeoutConnessione.size()+SUFFIX_TERMINATA);
  700.        
  701.         logInfo(log,debug,"Analisi memoria per generazione eventi terminata 'ConnectionTimeout' "+getSuffixDate(df, newInterval, secondi));
  702.        
  703.         return newInterval; // da usare per il prossimo intervallo
  704.     }
  705.     public void emitProcessConnectionTimeoutSkip(Logger log, boolean debug, int offsetConnectionTimeoutEveryXTimes, int checkConnectionTimeoutEveryXTimes) {
  706.         logDebug(log,debug,"8. Analisi eventi di Timeout durante la connessione non abilitata in questa iterazione (offset: "+offsetConnectionTimeoutEveryXTimes+EVERY+checkConnectionTimeoutEveryXTimes+")");
  707.     }
  708.     public Date processRequestReadTimeout(Logger log, int secondi, Date lastInterval, Connection connection, boolean debug) throws UtilsException{
  709.        
  710.         SimpleDateFormat df = DateUtils.getSimpleDateFormatMs();
  711.        
  712.         logInfo(log,debug,SEPARATOR);
  713.        
  714.         logInfo(log,debug,"Analisi memoria per generazione eventi in corso 'RequestReadTimeout' [interval: "+df.format(lastInterval)+SUFFIX_IN_CORSO_PARENTESI_QUADRE);
  715.                
  716.         // Raccolgo informazioni in memoria per rilasciare il lock
  717.        
  718.         // TH_*
  719.         Map<String, DatiEventoGenerico> localInMemoryLastTimeoutRichiesta = new HashMap<>();
  720.         Date newInterval = null;
  721.         SemaphoreLock slock = lock.acquire("processRequestReadTimeout");
  722.         try {
  723.             if(this.inMemoryLastTimeoutRichiesta!=null && this.inMemoryLastTimeoutRichiesta.size()>0){
  724.                 for (Map.Entry<String,DatiEventoGenerico> entry : this.inMemoryLastTimeoutRichiesta.entrySet()) {
  725.                     localInMemoryLastTimeoutRichiesta.put(entry.getKey(), entry.getValue().readAndConsume());
  726.                 }
  727.             }

  728.             newInterval = DateManager.getDate();
  729.         }finally {
  730.             lock.release(slock, "processRequestReadTimeout");
  731.         }

  732.        
  733.         // Gli eventi db_* sono acceduti solo dal thread non serve un synchronized
  734.        
  735.         // DB_*
  736.         Map<String, DatabaseDatiEventoGenerico> localDbLastTimeoutRichiesta = null;
  737.         localDbLastTimeoutRichiesta = this.dbLastTimeoutRichiesta;

  738.        
  739.         // Procedo ad effettuare l'elaborazione per emettere degli eventi
  740.                
  741.         logDebug(log,debug,"9. Analisi eventi di Timeout durante la ricezione della richiesta (size:"+localInMemoryLastTimeoutRichiesta.size()+SUFFIX_IN_CORSO);
  742.         if(!localInMemoryLastTimeoutRichiesta.isEmpty()) {
  743.             for (Map.Entry<String,DatiEventoGenerico> entry : localInMemoryLastTimeoutRichiesta.entrySet()) {
  744.                 String idPolicy = entry.getKey();
  745.                 processSingleEvent(log,
  746.                         localInMemoryLastTimeoutRichiesta.get(idPolicy),
  747.                         localDbLastTimeoutRichiesta.get(idPolicy),
  748.                         this.dbLastTimeoutRichiesta.get(idPolicy),
  749.                         TipoEvento.CONTROLLO_TRAFFICO_REQUEST_READ_TIMEOUT,
  750.                         CodiceEventoControlloTraffico.VIOLAZIONE,
  751.                         CodiceEventoControlloTraffico.VIOLAZIONE_RISOLTA,
  752.                         lastInterval, connection, this.gestoreEventi,
  753.                         idPolicy,debug);    
  754.             }
  755.         }
  756.         logDebug(log,debug,"9. Analisi eventi di Timeout durante la ricezione della richiesta (size:"+localInMemoryLastTimeoutRichiesta.size()+SUFFIX_TERMINATA);
  757.        
  758.         logInfo(log,debug,"Analisi memoria per generazione eventi terminata 'RequestReadTimeout' "+getSuffixDate(df, newInterval, secondi));
  759.        
  760.         return newInterval; // da usare per il prossimo intervallo
  761.     }
  762.     public void emitProcessRequestReadTimeoutSkip(Logger log, boolean debug, int offsetRequestReadTimeoutEveryXTimes, int checkRequestReadTimeoutEveryXTimes) {
  763.         logDebug(log,debug,"9. Analisi eventi di Timeout durante la ricezione della richiesta non abilitata in questa iterazione (offset: "+offsetRequestReadTimeoutEveryXTimes+EVERY+checkRequestReadTimeoutEveryXTimes+")");
  764.     }
  765.     public Date processReadTimeout(Logger log, int secondi, Date lastInterval, Connection connection, boolean debug) throws UtilsException{
  766.        
  767.         SimpleDateFormat df = DateUtils.getSimpleDateFormatMs();
  768.        
  769.         logInfo(log,debug,SEPARATOR);
  770.        
  771.         logInfo(log,debug,"Analisi memoria per generazione eventi in corso 'ReadTimeout' [interval: "+df.format(lastInterval)+SUFFIX_IN_CORSO_PARENTESI_QUADRE);
  772.                
  773.         // Raccolgo informazioni in memoria per rilasciare il lock
  774.        
  775.         // TH_*
  776.         Map<String, DatiEventoGenerico> localInMemoryLastTimeoutRisposta = new HashMap<>();
  777.         Date newInterval = null;
  778.         SemaphoreLock slock = lock.acquire("processReadTimeout");
  779.         try {
  780.             if(this.inMemoryLastTimeoutRisposta!=null && this.inMemoryLastTimeoutRisposta.size()>0){
  781.                 for (Map.Entry<String,DatiEventoGenerico> entry : this.inMemoryLastTimeoutRisposta.entrySet()) {
  782.                     localInMemoryLastTimeoutRisposta.put(entry.getKey(), entry.getValue().readAndConsume());
  783.                 }
  784.             }

  785.             newInterval = DateManager.getDate();
  786.         }finally {
  787.             lock.release(slock, "processReadTimeout");
  788.         }

  789.        
  790.         // Gli eventi db_* sono acceduti solo dal thread non serve un synchronized
  791.        
  792.         // DB_*
  793.         Map<String, DatabaseDatiEventoGenerico> localDbLastTimeoutRisposta = null;
  794.         localDbLastTimeoutRisposta = this.dbLastTimeoutRisposta;

  795.        
  796.         // Procedo ad effettuare l'elaborazione per emettere degli eventi
  797.        
  798.         logDebug(log,debug,"10. Analisi eventi di Timeout durante la ricezione della risposta (size:"+localInMemoryLastTimeoutRisposta.size()+SUFFIX_IN_CORSO);
  799.         if(!localInMemoryLastTimeoutRisposta.isEmpty()) {
  800.             for (Map.Entry<String,DatiEventoGenerico> entry : localInMemoryLastTimeoutRisposta.entrySet()) {
  801.                 String idPolicy = entry.getKey();
  802.                 processSingleEvent(log,
  803.                         localInMemoryLastTimeoutRisposta.get(idPolicy),
  804.                         localDbLastTimeoutRisposta.get(idPolicy),
  805.                         this.dbLastTimeoutRisposta.get(idPolicy),
  806.                         TipoEvento.CONTROLLO_TRAFFICO_READ_TIMEOUT,
  807.                         CodiceEventoControlloTraffico.VIOLAZIONE,
  808.                         CodiceEventoControlloTraffico.VIOLAZIONE_RISOLTA,
  809.                         lastInterval, connection, this.gestoreEventi,
  810.                         idPolicy,debug);    
  811.             }
  812.         }
  813.         logDebug(log,debug,"10. Analisi eventi di Timeout durante la ricezione della risposta (size:"+localInMemoryLastTimeoutRisposta.size()+SUFFIX_TERMINATA);

  814.        
  815.         logInfo(log,debug,"Analisi memoria per generazione eventi terminata 'ReadTimeout' "+getSuffixDate(df, newInterval, secondi));
  816.        
  817.         return newInterval; // da usare per il prossimo intervallo
  818.     }
  819.     public void emitProcessReadTimeoutSkip(Logger log, boolean debug, int offsetReadTimeoutEveryXTimes, int checkReadTimeoutEveryXTimes) {
  820.         logDebug(log,debug,"10. Analisi eventi di Timeout durante la ricezione della risposta non abilitata in questa iterazione (offset: "+offsetReadTimeoutEveryXTimes+EVERY+checkReadTimeoutEveryXTimes+")");
  821.     }
  822.    
  823.    
  824.     private static void processSingleEvent(Logger log, DatiEventoGenerico localInMemory,DatabaseDatiEventoGenerico localDb,
  825.             DatabaseDatiEventoGenerico thisDb,
  826.             TipoEvento tipoEvento,
  827.             CodiceEventoControlloTraffico eventoViolazione, CodiceEventoControlloTraffico eventoViolazioneRisolta,
  828.             Date lastInterval, Connection connection, GestoreEventi gestoreEventi,
  829.             String idPolicy, boolean debug) throws UtilsException {
  830.        
  831.         // Gestione violazione maxThreads
  832.        
  833.         boolean esisteTHultimoIntervallo = false;
  834.         if(localInMemory!=null &&
  835.                 localInMemory.data!=null &&
  836.                 localInMemory.data.after(lastInterval)){
  837.             esisteTHultimoIntervallo = true;
  838.         }
  839.        
  840.         if(localDb!=null &&
  841.                 localDb.data!=null){
  842.             logDebug(log,debug,"\tTH_ultimoIntervallo: "+esisteTHultimoIntervallo+
  843.                     " DB_last:"+(localDb!=null)+
  844.                     " DB_last.codiceEvento:"+localDb.codiceEvento.name());
  845.         }
  846.         else{
  847.             logDebug(log,debug,"\tTH_ultimoIntervallo: "+esisteTHultimoIntervallo);
  848.         }
  849.    
  850.         if(esisteTHultimoIntervallo){
  851.             if(localDb!=null &&
  852.                     localDb.data!=null){
  853.                 if(eventoViolazioneRisolta.equals(localDb.codiceEvento)){
  854.                     // sono ritornato dentro una violazione di stato
  855.                     CodiceEventoControlloTraffico codice = eventoViolazione;
  856.                     Evento evento = buildEvento(tipoEvento,codice, idPolicy,
  857.                             localInMemory.descrizione,
  858.                             localInMemory.configurazione,
  859.                             localInMemory.data); // uso come data dell'evento la data in cui e' accaduta la segnalazione
  860.                     logEvento(gestoreEventi, connection, evento, log, debug);
  861.                     /**synchronized (this.semaphore) { // Gli eventi db_* sono acceduti solo dal thread non serve un synchronized*/
  862.                     thisDb.data = evento.getOraRegistrazione();
  863.                     thisDb.codiceEvento = codice;
  864.                     /**}*/
  865.                     if(idPolicy!=null)
  866.                         logDebug(log,debug,getProcessSingleEventMessage(tipoEvento, codice, idPolicy));
  867.                     else
  868.                         logDebug(log,debug,getProcessSingleEventMessage(tipoEvento, codice));
  869.                 }
  870.             }
  871.             else{
  872.                 // prima volta che succede il problema, emetto evento di violazione
  873.                 CodiceEventoControlloTraffico codice = eventoViolazione;
  874.                 Evento evento = buildEvento(tipoEvento,codice, idPolicy,
  875.                         localInMemory.descrizione,
  876.                         localInMemory.configurazione,
  877.                         localInMemory.data); // uso come data dell'evento la data in cui e' accaduta la segnalazione
  878.                 logEvento(gestoreEventi, connection, evento, log, debug);
  879.                 /** synchronized (this.semaphore) { // Gli eventi db_* sono acceduti solo dal thread non serve un synchronized*/
  880.                 thisDb.data = evento.getOraRegistrazione();
  881.                 thisDb.codiceEvento = codice;
  882.                 if(idPolicy!=null)
  883.                     logDebug(log,debug,getProcessSingleEventMessage(tipoEvento, codice, idPolicy));
  884.                 else
  885.                     logDebug(log,debug,getProcessSingleEventMessage(tipoEvento, codice));
  886.                 /**}*/
  887.             }
  888.         }
  889.         else{
  890.             if(localDb!=null &&
  891.                     localDb.data!=null &&
  892.                 eventoViolazione.equals(localDb.codiceEvento)){
  893.                 // emetto evento che non risulta piu' violato.
  894.                 CodiceEventoControlloTraffico codice = eventoViolazioneRisolta;
  895.                 Evento evento = buildEvento(tipoEvento,codice, idPolicy,
  896.                         localDb.descrizione,
  897.                         localInMemory!=null && localInMemory.configurazione!=null ? localInMemory.configurazione : localDb.configurazione, // importante per far arrivare la configurazione usata nella gestione delle policy
  898.                         DateManager.getDate());
  899.                 logEvento(gestoreEventi, connection, evento, log, debug);
  900.                 /** synchronized (this.semaphore) { // Gli eventi db_* sono acceduti solo dal thread non serve un synchronized*/
  901.                 thisDb.data = evento.getOraRegistrazione();
  902.                 thisDb.codiceEvento = codice;
  903.                 if(idPolicy!=null)
  904.                     logDebug(log,debug,getProcessSingleEventMessage(tipoEvento, codice, idPolicy));
  905.                 else
  906.                     logDebug(log,debug,getProcessSingleEventMessage(tipoEvento, codice));
  907.                 /**}*/
  908.             }
  909.         }

  910.     }
  911.    
  912.     private static String getProcessSingleEventMessage(TipoEvento tipoEvento, CodiceEventoControlloTraffico codice, String idPolicy) {
  913.         return "\tEmetto Evento tipo["+tipoEvento+"] codice["+codice+"_"+idPolicy+"]";
  914.     }
  915.     private static String getProcessSingleEventMessage(TipoEvento tipoEvento, CodiceEventoControlloTraffico codice) {
  916.         return "\tEmetto Evento tipo["+tipoEvento+"] codice["+codice+"]";
  917.     }
  918.    
  919.     private static Evento buildEvento(TipoEvento tipoEvento, CodiceEventoControlloTraffico codice, String idPolicy, String descrizione, String configurazione, Date data) throws UtilsException {
  920.         Evento evento = new Evento();
  921.         evento.setTipo(tipoEvento.getValue());
  922.         evento.setCodice(codice.getValue());
  923.         if(idPolicy!=null){
  924.             evento.setIdConfigurazione(idPolicy);
  925.         }      
  926.         evento.setDescrizione(descrizione);
  927.         evento.setConfigurazione(configurazione);
  928.         evento.setOraRegistrazione(data);
  929.        
  930.         switch (tipoEvento) {
  931.         case CONTROLLO_TRAFFICO_NUMERO_MASSIMO_RICHIESTE_SIMULTANEE:
  932.             switch (codice) {
  933.             case VIOLAZIONE:
  934.                 evento.setSeverita(SeveritaConverter.toIntValue(TipoSeverita.ERROR));
  935.                 break;
  936.             case VIOLAZIONE_RISOLTA:
  937.                 evento.setSeverita(SeveritaConverter.toIntValue(TipoSeverita.INFO));
  938.                 break;
  939.             case VIOLAZIONE_WARNING_ONLY:
  940.                 evento.setSeverita(SeveritaConverter.toIntValue(TipoSeverita.WARN));
  941.                 break;
  942.             case VIOLAZIONE_RISOLTA_WARNING_ONLY:
  943.                 evento.setSeverita(SeveritaConverter.toIntValue(TipoSeverita.INFO));
  944.                 break;
  945.             default:
  946.                 // altri casi non previsti per CONTROLLO_TRAFFICO_NUMERO_MASSIMO_RICHIESTE_SIMULTANEE
  947.                 break;
  948.             }
  949.             break;
  950.         case CONTROLLO_TRAFFICO_SOGLIA_CONGESTIONE:
  951.             switch (codice) {
  952.             case VIOLAZIONE:
  953.                 evento.setSeverita(SeveritaConverter.toIntValue(TipoSeverita.WARN));
  954.                 break;
  955.             case VIOLAZIONE_RISOLTA:
  956.                 evento.setSeverita(SeveritaConverter.toIntValue(TipoSeverita.INFO));
  957.                 break;
  958.             default:
  959.                 // altri casi non previsti per CONTROLLO_TRAFFICO_NUMERO_MASSIMO_RICHIESTE_SIMULTANEE
  960.                 break;
  961.             }
  962.             break;
  963.         case RATE_LIMITING_POLICY_GLOBALE:
  964.         case RATE_LIMITING_POLICY_API:
  965.             switch (codice) {
  966.             case VIOLAZIONE:
  967.                 evento.setSeverita(SeveritaConverter.toIntValue(TipoSeverita.ERROR));
  968.                 break;
  969.             case VIOLAZIONE_RISOLTA:
  970.                 evento.setSeverita(SeveritaConverter.toIntValue(TipoSeverita.INFO));
  971.                 break;
  972.             case VIOLAZIONE_WARNING_ONLY:
  973.                 evento.setSeverita(SeveritaConverter.toIntValue(TipoSeverita.WARN));
  974.                 break;
  975.             case VIOLAZIONE_RISOLTA_WARNING_ONLY:
  976.                 evento.setSeverita(SeveritaConverter.toIntValue(TipoSeverita.INFO));
  977.                 break;
  978.             }
  979.             break;
  980.         case CONTROLLO_TRAFFICO_CONNECTION_TIMEOUT:
  981.         case CONTROLLO_TRAFFICO_REQUEST_READ_TIMEOUT:
  982.         case CONTROLLO_TRAFFICO_READ_TIMEOUT:
  983.             switch (codice) {
  984.             case VIOLAZIONE:
  985.                 evento.setSeverita(SeveritaConverter.toIntValue(TipoSeverita.ERROR));
  986.                 break;
  987.             case VIOLAZIONE_RISOLTA:
  988.                 evento.setSeverita(SeveritaConverter.toIntValue(TipoSeverita.INFO));
  989.                 break;
  990.             default:
  991.                 // altri casi non previsti per CONTROLLO_TRAFFICO_READ_TIMEOUT
  992.                 break;
  993.             }
  994.             break;
  995.         default:
  996.             // altri casi non previsti per questo notificatore eventi
  997.             break;
  998.         }
  999.        
  1000.         evento.setClusterId(OpenSPCoop2Properties.getInstance().getClusterId(false));
  1001.         return evento;
  1002.     }
  1003.    
  1004.     private static void logEvento(GestoreEventi gestoreEventi, Connection connection, Evento evento, Logger log, boolean debug) throws UtilsException {
  1005.        
  1006.         // Fix evento per passarlo al notifier
  1007.         String configurazione = evento.getConfigurazione();
  1008.         if(PolicyUtilities.isConfigurazioneEventoPerPolicy(configurazione)) {
  1009.             evento.setConfigurazione(null); // serve solo per passarlo al notifier
  1010.         }
  1011.        
  1012.         try {
  1013.             gestoreEventi.log(evento, connection);
  1014.         }catch(Exception e) {
  1015.             throw new UtilsException(e.getMessage(),e);
  1016.         }
  1017.        
  1018.         OpenSPCoop2Properties properties = OpenSPCoop2Properties.getInstance();
  1019.        
  1020.         if(properties.isControlloTrafficoEnabled()) {
  1021.            
  1022.             ConfigurazioneGatewayControlloTraffico config = properties.getConfigurazioneControlloTraffico();
  1023.             if(config.isNotifierEnabled()) {
  1024.                
  1025.                 INotify notifier = config.getNotifier();
  1026.                 if(notifier.isNotifichePassiveAttive()) {
  1027.                     TipoEvento tipoEvento = TipoEvento.toEnumConstant(evento.getTipo());
  1028.                     CodiceEventoControlloTraffico codiceEvento = CodiceEventoControlloTraffico.toEnumConstant(evento.getCodice());
  1029.                     switch (tipoEvento) {
  1030.                     case CONTROLLO_TRAFFICO_NUMERO_MASSIMO_RICHIESTE_SIMULTANEE:
  1031.                     case CONTROLLO_TRAFFICO_SOGLIA_CONGESTIONE:
  1032.                         notifier.updateStatoRilevamentoCongestione(log, debug, tipoEvento, codiceEvento, evento.getDescrizione());
  1033.                         break;
  1034.                     case RATE_LIMITING_POLICY_GLOBALE:
  1035.                     case RATE_LIMITING_POLICY_API:
  1036.                         notifier.updateStatoRilevamentoViolazionePolicy(log, debug, tipoEvento, codiceEvento, evento.getIdConfigurazione(), configurazione);
  1037.                         break;
  1038.                     case CONTROLLO_TRAFFICO_REQUEST_READ_TIMEOUT:
  1039.                         notifier.updateStatoRilevamentoRequestReadTimeout(log, debug, tipoEvento, codiceEvento, evento.getIdConfigurazione(), configurazione);
  1040.                         break;
  1041.                     case CONTROLLO_TRAFFICO_READ_TIMEOUT:
  1042.                         notifier.updateStatoRilevamentoReadTimeout(log, debug, tipoEvento, codiceEvento, evento.getIdConfigurazione(), configurazione);
  1043.                         break;
  1044.                     default:
  1045.                         // altri casi non previsti per questo notificatore eventi
  1046.                         break;
  1047.                     }
  1048.                 }
  1049.             }
  1050.            
  1051.         }
  1052.        
  1053.     }

  1054.    
  1055.    

  1056.     //---- Per salvare

  1057.     private static final String ZIP_IN_MEMORY = "memory";
  1058.     private static final String ZIP_DB = "db";
  1059.     private static final String ZIP_LAST_MAX_REQUESTS = "lastMaxRequests.xml";
  1060.     private static final String ZIP_LAST_MAX_REQUESTS_WARNING_ONLY = "lastMaxRequests_warningOnly.xml";
  1061.     private static final String ZIP_LAST_PDD_CONGESTIONATA = "lastPddCongestionata.xml";
  1062.     private static final String ZIP_POLICY_GLOBALE = "policyGlobale";
  1063.     private static final String ZIP_POLICY_GLOBALE_WARNING_ONLY = "policyGlobale_warningOnly";
  1064.     private static final String ZIP_POLICY_API = "policyAPI";
  1065.     private static final String ZIP_POLICY_API_WARNING_ONLY = "policyAPI_warningOnly";
  1066.     private static final String ZIP_EVENTO_TIMEOUT_CONNESSIONE = "connectionTimeout";
  1067.     private static final String ZIP_EVENTO_TIMEOUT_RICHIESTA = "requestReadTimeout";
  1068.     private static final String ZIP_EVENTO_TIMEOUT_RISPOSTA = "readTimeout";

  1069.    
  1070.     public void serialize(File file) throws UtilsException{
  1071.         try (FileOutputStream out = new FileOutputStream(file, false);){ // se già esiste lo sovrascrive
  1072.             this.serialize(out);
  1073.             out.flush();
  1074.         }
  1075.         catch(Exception e) {
  1076.             throw new UtilsException(e.getMessage(),e);
  1077.         }
  1078.     }
  1079.    
  1080.     @Deprecated
  1081.     // Il meccanismo non funzionava, non si riusciva a riottenere uno stato corretto
  1082.     public void serialize(OutputStream out) throws UtilsException{
  1083.            
  1084.         try (ZipOutputStream zipOut = new ZipOutputStream(out);){
  1085.            
  1086.             String rootPackageDir = "";
  1087.             // Il codice dopo fissa il problema di inserire una directory nel package.
  1088.             // Commentare la riga sotto per ripristinare il vecchio comportamento.
  1089.             rootPackageDir = Costanti.OPENSPCOOP2_ARCHIVE_ROOT_DIR+File.separatorChar;
  1090.            
  1091.            
  1092.             // String inMemory Path
  1093.             String inMemoryDir = rootPackageDir + ZIP_IN_MEMORY + File.separatorChar;
  1094.            
  1095.             if(this.inMemoryLastMaxRequests!=null) {
  1096.                 zipOut.putNextEntry(new ZipEntry(inMemoryDir+ZIP_LAST_MAX_REQUESTS));
  1097.                 convertToDatiEventoGenericoSerializabled(this.inMemoryLastMaxRequests, "inMemory_lastMaxRequests").
  1098.                     writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1099.             }
  1100.            
  1101.             if(this.inMemoryLastMaxRequestsWarningOnly!=null) {
  1102.                 zipOut.putNextEntry(new ZipEntry(inMemoryDir+ZIP_LAST_MAX_REQUESTS_WARNING_ONLY));
  1103.                 convertToDatiEventoGenericoSerializabled(this.inMemoryLastMaxRequestsWarningOnly, "inMemory_lastMaxRequests_warningOnly").
  1104.                     writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1105.             }
  1106.            
  1107.             if(this.inMemoryLastPddCongestionata!=null) {
  1108.                 zipOut.putNextEntry(new ZipEntry(inMemoryDir+ZIP_LAST_PDD_CONGESTIONATA));
  1109.                 convertToDatiEventoGenericoSerializabled(this.inMemoryLastPddCongestionata, "inMemory_lastPddCongestionata").
  1110.                     writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1111.             }
  1112.            
  1113.             if(this.inMemoryLastPolicyGlobaliViolated!=null && this.inMemoryLastPolicyGlobaliViolated.size()>0) {
  1114.                 int index = 1;
  1115.                 String inMemoryPolicyDir = inMemoryDir + ZIP_POLICY_GLOBALE + File.separatorChar;
  1116.                 for (Map.Entry<String,DatiEventoGenerico> entry : this.inMemoryLastPolicyGlobaliViolated.entrySet()) {
  1117.                     String idPolicy = entry.getKey();
  1118.                     DatiEventoGenerico evento = this.inMemoryLastPolicyGlobaliViolated.get(idPolicy);
  1119.                     zipOut.putNextEntry(new ZipEntry(inMemoryPolicyDir+ZIP_POLICY_GLOBALE+"_"+index+".xml"));
  1120.                     convertToDatiEventoGenericoSerializabled(evento, idPolicy).
  1121.                         writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1122.                     index++;
  1123.                 }
  1124.             }
  1125.            
  1126.             if(this.inMemoryLastPolicyGlobaliViolatedWarningOnly!=null && this.inMemoryLastPolicyGlobaliViolatedWarningOnly.size()>0) {
  1127.                 int index = 1;
  1128.                 String inMemoryPolicyDir = inMemoryDir + ZIP_POLICY_GLOBALE_WARNING_ONLY + File.separatorChar;
  1129.                 for (Map.Entry<String,DatiEventoGenerico> entry : this.inMemoryLastPolicyGlobaliViolatedWarningOnly.entrySet()) {
  1130.                     String idPolicy = entry.getKey();
  1131.                     DatiEventoGenerico evento = this.inMemoryLastPolicyGlobaliViolatedWarningOnly.get(idPolicy);
  1132.                     zipOut.putNextEntry(new ZipEntry(inMemoryPolicyDir+ZIP_POLICY_GLOBALE_WARNING_ONLY+"_"+index+".xml"));
  1133.                     convertToDatiEventoGenericoSerializabled(evento, idPolicy).
  1134.                         writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1135.                     index++;
  1136.                 }
  1137.             }
  1138.            
  1139.             if(this.inMemoryLastPolicyAPIViolated!=null && this.inMemoryLastPolicyAPIViolated.size()>0) {
  1140.                 int index = 1;
  1141.                 String inMemoryPolicyDir = inMemoryDir + ZIP_POLICY_API + File.separatorChar;
  1142.                 for (Map.Entry<String,DatiEventoGenerico> entry : this.inMemoryLastPolicyAPIViolated.entrySet()) {
  1143.                     String idPolicy = entry.getKey();
  1144.                     DatiEventoGenerico evento = this.inMemoryLastPolicyAPIViolated.get(idPolicy);
  1145.                     zipOut.putNextEntry(new ZipEntry(inMemoryPolicyDir+ZIP_POLICY_API+"_"+index+".xml"));
  1146.                     convertToDatiEventoGenericoSerializabled(evento, idPolicy).
  1147.                         writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1148.                     index++;
  1149.                 }
  1150.             }
  1151.            
  1152.             if(this.inMemoryLastPolicyAPIViolatedWarningOnly!=null && this.inMemoryLastPolicyAPIViolatedWarningOnly.size()>0) {
  1153.                 int index = 1;
  1154.                 String inMemoryPolicyDir = inMemoryDir + ZIP_POLICY_API_WARNING_ONLY + File.separatorChar;
  1155.                 for (Map.Entry<String,DatiEventoGenerico> entry : this.inMemoryLastPolicyAPIViolatedWarningOnly.entrySet()) {
  1156.                     String idPolicy = entry.getKey();
  1157.                     DatiEventoGenerico evento = this.inMemoryLastPolicyAPIViolatedWarningOnly.get(idPolicy);
  1158.                     zipOut.putNextEntry(new ZipEntry(inMemoryPolicyDir+ZIP_POLICY_API_WARNING_ONLY+"_"+index+".xml"));
  1159.                     convertToDatiEventoGenericoSerializabled(evento, idPolicy).
  1160.                         writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1161.                     index++;
  1162.                 }
  1163.             }
  1164.            
  1165.             if(this.inMemoryLastTimeoutConnessione!=null && this.inMemoryLastTimeoutConnessione.size()>0) {
  1166.                 int index = 1;
  1167.                 String inMemoryPolicyDir = inMemoryDir + ZIP_EVENTO_TIMEOUT_CONNESSIONE + File.separatorChar;
  1168.                 for (Map.Entry<String,DatiEventoGenerico> entry : this.inMemoryLastTimeoutConnessione.entrySet()) {
  1169.                     String idPolicy = entry.getKey();
  1170.                     DatiEventoGenerico evento = this.inMemoryLastTimeoutConnessione.get(idPolicy);
  1171.                     zipOut.putNextEntry(new ZipEntry(inMemoryPolicyDir+ZIP_EVENTO_TIMEOUT_CONNESSIONE+"_"+index+".xml"));
  1172.                     convertToDatiEventoGenericoSerializabled(evento, idPolicy).
  1173.                         writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1174.                     index++;
  1175.                 }
  1176.             }
  1177.            
  1178.             if(this.inMemoryLastTimeoutRichiesta!=null && this.inMemoryLastTimeoutRichiesta.size()>0) {
  1179.                 int index = 1;
  1180.                 String inMemoryPolicyDir = inMemoryDir + ZIP_EVENTO_TIMEOUT_RICHIESTA + File.separatorChar;
  1181.                 for (Map.Entry<String,DatiEventoGenerico> entry : this.inMemoryLastTimeoutRichiesta.entrySet()) {
  1182.                     String idPolicy = entry.getKey();
  1183.                     DatiEventoGenerico evento = this.inMemoryLastTimeoutRichiesta.get(idPolicy);
  1184.                     zipOut.putNextEntry(new ZipEntry(inMemoryPolicyDir+ZIP_EVENTO_TIMEOUT_RICHIESTA+"_"+index+".xml"));
  1185.                     convertToDatiEventoGenericoSerializabled(evento, idPolicy).
  1186.                         writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1187.                     index++;
  1188.                 }
  1189.             }
  1190.            
  1191.             if(this.inMemoryLastTimeoutRisposta!=null && this.inMemoryLastTimeoutRisposta.size()>0) {
  1192.                 int index = 1;
  1193.                 String inMemoryPolicyDir = inMemoryDir + ZIP_EVENTO_TIMEOUT_RISPOSTA + File.separatorChar;
  1194.                 for (Map.Entry<String,DatiEventoGenerico> entry : this.inMemoryLastTimeoutRisposta.entrySet()) {
  1195.                     String idPolicy = entry.getKey();
  1196.                     DatiEventoGenerico evento = this.inMemoryLastTimeoutRisposta.get(idPolicy);
  1197.                     zipOut.putNextEntry(new ZipEntry(inMemoryPolicyDir+ZIP_EVENTO_TIMEOUT_RISPOSTA+"_"+index+".xml"));
  1198.                     convertToDatiEventoGenericoSerializabled(evento, idPolicy).
  1199.                         writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1200.                     index++;
  1201.                 }
  1202.             }
  1203.            
  1204.            
  1205.             // String db Path
  1206.             String dbDir = rootPackageDir + ZIP_DB + File.separatorChar;
  1207.            
  1208.             if(this.dbLastMaxRequests!=null) {
  1209.                 zipOut.putNextEntry(new ZipEntry(dbDir+ZIP_LAST_MAX_REQUESTS));
  1210.                 convertDBToDatiEventoGenericoSerializabled(this.dbLastMaxRequests, "db_lastMaxRequests").
  1211.                     writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1212.             }
  1213.            
  1214.             if(this.dbLastMaxRequestsWarningOnly!=null) {
  1215.                 zipOut.putNextEntry(new ZipEntry(dbDir+ZIP_LAST_MAX_REQUESTS_WARNING_ONLY));
  1216.                 convertDBToDatiEventoGenericoSerializabled(this.dbLastMaxRequestsWarningOnly, "db_lastMaxRequests_warningOnly").
  1217.                     writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1218.             }
  1219.            
  1220.             if(this.dbLastPddCongestionata!=null) {
  1221.                 zipOut.putNextEntry(new ZipEntry(dbDir+ZIP_LAST_PDD_CONGESTIONATA));
  1222.                 convertDBToDatiEventoGenericoSerializabled(this.dbLastPddCongestionata, "db_lastPddCongestionata").
  1223.                     writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1224.             }
  1225.            
  1226.             if(this.dbLastPolicyGlobaliViolated!=null && this.dbLastPolicyGlobaliViolated.size()>0) {
  1227.                 int index = 1;
  1228.                 String dbPolicyDir = dbDir + ZIP_POLICY_GLOBALE + File.separatorChar;
  1229.                 for (Map.Entry<String,DatabaseDatiEventoGenerico> entry : this.dbLastPolicyGlobaliViolated.entrySet()) {
  1230.                     String idPolicy = entry.getKey();
  1231.                     DatabaseDatiEventoGenerico evento = this.dbLastPolicyGlobaliViolated.get(idPolicy);
  1232.                     zipOut.putNextEntry(new ZipEntry(dbPolicyDir+ZIP_POLICY_GLOBALE+"_"+index+".xml"));
  1233.                     convertDBToDatiEventoGenericoSerializabled(evento, idPolicy).
  1234.                         writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1235.                     index++;
  1236.                 }
  1237.             }
  1238.            
  1239.             if(this.dbLastPolicyGlobaliViolatedWarningOnly!=null && this.dbLastPolicyGlobaliViolatedWarningOnly.size()>0) {
  1240.                 int index = 1;
  1241.                 String dbPolicyDir = dbDir + ZIP_POLICY_GLOBALE_WARNING_ONLY + File.separatorChar;
  1242.                 for (Map.Entry<String,DatabaseDatiEventoGenerico> entry : this.dbLastPolicyGlobaliViolatedWarningOnly.entrySet()) {
  1243.                     String idPolicy = entry.getKey();
  1244.                     DatabaseDatiEventoGenerico evento = this.dbLastPolicyGlobaliViolatedWarningOnly.get(idPolicy);
  1245.                     zipOut.putNextEntry(new ZipEntry(dbPolicyDir+ZIP_POLICY_GLOBALE_WARNING_ONLY+"_"+index+".xml"));
  1246.                     convertDBToDatiEventoGenericoSerializabled(evento, idPolicy).
  1247.                         writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1248.                     index++;
  1249.                 }
  1250.             }
  1251.            
  1252.             if(this.dbLastPolicyAPIViolated!=null && this.dbLastPolicyAPIViolated.size()>0) {
  1253.                 int index = 1;
  1254.                 String dbPolicyDir = dbDir + ZIP_POLICY_API + File.separatorChar;
  1255.                 for (Map.Entry<String,DatabaseDatiEventoGenerico> entry : this.dbLastPolicyAPIViolated.entrySet()) {
  1256.                     String idPolicy = entry.getKey();
  1257.                     DatabaseDatiEventoGenerico evento = this.dbLastPolicyAPIViolated.get(idPolicy);
  1258.                     zipOut.putNextEntry(new ZipEntry(dbPolicyDir+ZIP_POLICY_API+"_"+index+".xml"));
  1259.                     convertDBToDatiEventoGenericoSerializabled(evento, idPolicy).
  1260.                         writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1261.                     index++;
  1262.                 }
  1263.             }
  1264.            
  1265.             if(this.dbLastPolicyAPIViolatedWarningOnly!=null && this.dbLastPolicyAPIViolatedWarningOnly.size()>0) {
  1266.                 int index = 1;
  1267.                 String dbPolicyDir = dbDir + ZIP_POLICY_API_WARNING_ONLY + File.separatorChar;
  1268.                 for (Map.Entry<String,DatabaseDatiEventoGenerico> entry : this.dbLastPolicyAPIViolatedWarningOnly.entrySet()) {
  1269.                     String idPolicy = entry.getKey();
  1270.                     DatabaseDatiEventoGenerico evento = this.dbLastPolicyAPIViolatedWarningOnly.get(idPolicy);
  1271.                     zipOut.putNextEntry(new ZipEntry(dbPolicyDir+ZIP_POLICY_API_WARNING_ONLY+"_"+index+".xml"));
  1272.                     convertDBToDatiEventoGenericoSerializabled(evento, idPolicy).
  1273.                         writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1274.                     index++;
  1275.                 }
  1276.             }
  1277.            
  1278.             if(this.dbLastTimeoutConnessione!=null && this.dbLastTimeoutConnessione.size()>0) {
  1279.                 int index = 1;
  1280.                 String dbPolicyDir = dbDir + ZIP_EVENTO_TIMEOUT_CONNESSIONE + File.separatorChar;
  1281.                 for (Map.Entry<String,DatabaseDatiEventoGenerico> entry : this.dbLastTimeoutConnessione.entrySet()) {
  1282.                     String idPolicy = entry.getKey();
  1283.                     DatabaseDatiEventoGenerico evento = this.dbLastTimeoutConnessione.get(idPolicy);
  1284.                     zipOut.putNextEntry(new ZipEntry(dbPolicyDir+ZIP_EVENTO_TIMEOUT_CONNESSIONE+"_"+index+".xml"));
  1285.                     convertDBToDatiEventoGenericoSerializabled(evento, idPolicy).
  1286.                         writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1287.                     index++;
  1288.                 }
  1289.             }
  1290.            
  1291.             if(this.dbLastTimeoutRichiesta!=null && this.dbLastTimeoutRichiesta.size()>0) {
  1292.                 int index = 1;
  1293.                 String dbPolicyDir = dbDir + ZIP_EVENTO_TIMEOUT_RICHIESTA + File.separatorChar;
  1294.                 for (Map.Entry<String,DatabaseDatiEventoGenerico> entry : this.dbLastTimeoutRichiesta.entrySet()) {
  1295.                     String idPolicy = entry.getKey();
  1296.                     DatabaseDatiEventoGenerico evento = this.dbLastTimeoutRichiesta.get(idPolicy);
  1297.                     zipOut.putNextEntry(new ZipEntry(dbPolicyDir+ZIP_EVENTO_TIMEOUT_RICHIESTA+"_"+index+".xml"));
  1298.                     convertDBToDatiEventoGenericoSerializabled(evento, idPolicy).
  1299.                         writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1300.                     index++;
  1301.                 }
  1302.             }
  1303.            
  1304.             if(this.dbLastTimeoutRisposta!=null && this.dbLastTimeoutRisposta.size()>0) {
  1305.                 int index = 1;
  1306.                 String dbPolicyDir = dbDir + ZIP_EVENTO_TIMEOUT_RISPOSTA + File.separatorChar;
  1307.                 for (Map.Entry<String,DatabaseDatiEventoGenerico> entry : this.dbLastTimeoutRisposta.entrySet()) {
  1308.                     String idPolicy = entry.getKey();
  1309.                     DatabaseDatiEventoGenerico evento = this.dbLastTimeoutRisposta.get(idPolicy);
  1310.                     zipOut.putNextEntry(new ZipEntry(dbPolicyDir+ZIP_EVENTO_TIMEOUT_RISPOSTA+"_"+index+".xml"));
  1311.                     convertDBToDatiEventoGenericoSerializabled(evento, idPolicy).
  1312.                         writeTo(zipOut, WriteToSerializerType.XML_JAXB);
  1313.                     index++;
  1314.                 }
  1315.             }
  1316.        
  1317.            
  1318.             zipOut.flush();

  1319.         }catch(Exception e){
  1320.             throw new UtilsException(e.getMessage(),e);
  1321.         }

  1322.     }
  1323.    
  1324.     private org.openspcoop2.core.eventi.DatiEventoGenerico convertToDatiEventoGenericoSerializabled(DatiEventoGenerico eventoGenerico, String id){
  1325.         org.openspcoop2.core.eventi.DatiEventoGenerico e = new org.openspcoop2.core.eventi.DatiEventoGenerico();
  1326.         e.setIdEvento(id);
  1327.         e.setData(eventoGenerico.data);
  1328.         e.setDatiConsumatiThread(eventoGenerico.datiConsumatiThread);
  1329.         e.setDescrizione(eventoGenerico.descrizione);
  1330.         return e;
  1331.     }
  1332.     private org.openspcoop2.core.eventi.DatiEventoGenerico convertDBToDatiEventoGenericoSerializabled(DatabaseDatiEventoGenerico eventoGenerico, String id){
  1333.         org.openspcoop2.core.eventi.DatiEventoGenerico e = this.convertToDatiEventoGenericoSerializabled(eventoGenerico, id);
  1334.         if(eventoGenerico.codiceEvento!=null) {
  1335.             e.setCodiceEvento(eventoGenerico.codiceEvento.getValue());
  1336.         }
  1337.         return e;
  1338.     }
  1339.    
  1340.    
  1341.     @Deprecated
  1342.     /** Il meccanismo non funzionava, non si riusciva a riottenere uno stato corretto */
  1343.     public void initialize(InputStream in) throws UtilsException{
  1344.            
  1345.         if(in==null){
  1346.             return;
  1347.         }
  1348.        
  1349.         File f = null;
  1350.         String entryName = null;
  1351.         try {
  1352.             // Leggo InputStream
  1353.             byte [] bytesIn = Utilities.getAsByteArray(in);
  1354.             in.close();
  1355.             in = null;
  1356.             if(bytesIn==null || bytesIn.length<=0){
  1357.                 return;
  1358.             }
  1359.             f = FileSystemUtilities.createTempFile("controlloTraffico", ".tmp");
  1360.             FileSystemUtilities.writeFile(f, bytesIn);
  1361.         }catch(Exception e){
  1362.             try {
  1363.                 if(f!=null) {
  1364.                     java.nio.file.Files.delete(f.toPath());
  1365.                 }
  1366.             }catch(Exception eClose) {
  1367.                 // ignore
  1368.             }
  1369.             try{
  1370.                 if(in!=null)
  1371.                     in.close();
  1372.             }catch(Exception eClose){
  1373.                 // close
  1374.             }
  1375.             throw new UtilsException("["+entryName+"] "+e.getMessage(),e);
  1376.         }
  1377.        
  1378.         try (ZipFile zipFile = new ZipFile(f);){
  1379.             // Leggo Struttura ZIP

  1380.             String rootPackageDir = Costanti.OPENSPCOOP2_ARCHIVE_ROOT_DIR+File.separatorChar;
  1381.            
  1382.             String rootDir = null;
  1383.            
  1384.             org.openspcoop2.core.eventi.utils.serializer.JaxbDeserializer deserializer = new org.openspcoop2.core.eventi.utils.serializer.JaxbDeserializer();
  1385.            
  1386.             Iterator<ZipEntry> it = ZipUtilities.entries(zipFile, true);
  1387.             while (it.hasNext()) {
  1388.                 ZipEntry zipEntry = it.next();
  1389.                 entryName = ZipUtilities.operativeSystemConversion(zipEntry.getName());
  1390.                
  1391.                 /**System.out.println("FILE NAME:  "+entryName);
  1392.                 System.out.println("SIZE:  "+zipEntry.getSize());*/

  1393.                 // Il codice dopo fissa il problema di inserire una directory nel package.
  1394.                 // Commentare la riga sotto per ripristinare il vecchio comportamento.
  1395.                 if(rootDir==null){
  1396.                     // Calcolo ROOT DIR
  1397.                     rootDir=ZipUtilities.getRootDir(entryName);
  1398.                 }
  1399.                
  1400.                 if(!zipEntry.isDirectory()) {

  1401.                     FileDataSource fds = new FileDataSource(entryName);
  1402.                     String nome = fds.getName();
  1403.                     String tipo = nome.substring(nome.lastIndexOf(".")+1,nome.length());
  1404.                     tipo = tipo.toUpperCase();
  1405.                     /**System.out.println("VERIFICARE NAME["+nome+"] TIPO["+tipo+"]");*/
  1406.                    
  1407.                     try (InputStream inputStream = zipFile.getInputStream(zipEntry);){
  1408.                    
  1409.                         byte[]content = Utilities.getAsByteArray(inputStream);
  1410.                        
  1411.                         boolean inMemory = false;
  1412.                         String prefix = null;
  1413.                         if(entryName.startsWith((rootPackageDir+ZIP_IN_MEMORY)) ){
  1414.                             inMemory = true;
  1415.                             prefix = rootPackageDir+ZIP_IN_MEMORY+File.separatorChar;
  1416.                         }
  1417.                         else if(entryName.startsWith((rootPackageDir+ZIP_DB)) ){
  1418.                             inMemory = false;
  1419.                             prefix = rootPackageDir+ZIP_DB+File.separatorChar;
  1420.                         }
  1421.                         else {
  1422.                             throw new UtilsException("Entry ["+entryName+"] sconosciuta (Tipologia)");
  1423.                         }
  1424.                        
  1425.                         if(entryName.equals((prefix+ZIP_LAST_MAX_REQUESTS)) ){
  1426.                             org.openspcoop2.core.eventi.DatiEventoGenerico eventoGenericoSerialized =
  1427.                                     deserializer.readDatiEventoGenerico(content);
  1428.                             if(inMemory) {
  1429.                                 this.inMemoryLastMaxRequests = convertToDatiEventoGenerico(eventoGenericoSerialized);
  1430.                             }
  1431.                             else {
  1432.                                 this.dbLastMaxRequests = convertDBToDatiEventoGenerico(eventoGenericoSerialized);
  1433.                             }
  1434.                         }
  1435.                        
  1436.                         else if(entryName.equals((prefix+ZIP_LAST_MAX_REQUESTS_WARNING_ONLY)) ){
  1437.                             org.openspcoop2.core.eventi.DatiEventoGenerico eventoGenericoSerialized =
  1438.                                     deserializer.readDatiEventoGenerico(content);
  1439.                             if(inMemory) {
  1440.                                 this.inMemoryLastMaxRequestsWarningOnly = convertToDatiEventoGenerico(eventoGenericoSerialized);
  1441.                             }
  1442.                             else {
  1443.                                 this.dbLastMaxRequestsWarningOnly = convertDBToDatiEventoGenerico(eventoGenericoSerialized);
  1444.                             }
  1445.                         }
  1446.                        
  1447.                         else if(entryName.equals((prefix+ZIP_LAST_PDD_CONGESTIONATA)) ){
  1448.                             org.openspcoop2.core.eventi.DatiEventoGenerico eventoGenericoSerialized =
  1449.                                     deserializer.readDatiEventoGenerico(content);
  1450.                             if(inMemory) {
  1451.                                 this.inMemoryLastPddCongestionata = convertToDatiEventoGenerico(eventoGenericoSerialized);
  1452.                             }
  1453.                             else {
  1454.                                 this.dbLastPddCongestionata = convertDBToDatiEventoGenerico(eventoGenericoSerialized);
  1455.                             }
  1456.                         }
  1457.                        
  1458.                         else if(entryName.startsWith((prefix+ZIP_POLICY_GLOBALE)) ){
  1459.                             org.openspcoop2.core.eventi.DatiEventoGenerico eventoGenericoSerialized =
  1460.                                     deserializer.readDatiEventoGenerico(content);
  1461.                             if(inMemory) {
  1462.                                 this.inMemoryLastPolicyGlobaliViolated.put(eventoGenericoSerialized.getIdEvento(), convertToDatiEventoGenerico(eventoGenericoSerialized));
  1463.                             }
  1464.                             else {
  1465.                                 this.dbLastPolicyGlobaliViolated.put(eventoGenericoSerialized.getIdEvento(), convertDBToDatiEventoGenerico(eventoGenericoSerialized));
  1466.                             }
  1467.                         }
  1468.                        
  1469.                         else if(entryName.startsWith((prefix+ZIP_POLICY_GLOBALE_WARNING_ONLY)) ){
  1470.                             org.openspcoop2.core.eventi.DatiEventoGenerico eventoGenericoSerialized =
  1471.                                     deserializer.readDatiEventoGenerico(content);
  1472.                             if(inMemory) {
  1473.                                 this.inMemoryLastPolicyGlobaliViolatedWarningOnly.put(eventoGenericoSerialized.getIdEvento(), convertToDatiEventoGenerico(eventoGenericoSerialized));
  1474.                             }
  1475.                             else {
  1476.                                 this.dbLastPolicyGlobaliViolatedWarningOnly.put(eventoGenericoSerialized.getIdEvento(), convertDBToDatiEventoGenerico(eventoGenericoSerialized));
  1477.                             }
  1478.                         }
  1479.                        
  1480.                         else if(entryName.startsWith((prefix+ZIP_POLICY_API)) ){
  1481.                             org.openspcoop2.core.eventi.DatiEventoGenerico eventoGenericoSerialized =
  1482.                                     deserializer.readDatiEventoGenerico(content);
  1483.                             if(inMemory) {
  1484.                                 this.inMemoryLastPolicyAPIViolated.put(eventoGenericoSerialized.getIdEvento(), convertToDatiEventoGenerico(eventoGenericoSerialized));
  1485.                             }
  1486.                             else {
  1487.                                 this.dbLastPolicyAPIViolated.put(eventoGenericoSerialized.getIdEvento(), convertDBToDatiEventoGenerico(eventoGenericoSerialized));
  1488.                             }
  1489.                         }
  1490.                        
  1491.                         else if(entryName.startsWith((prefix+ZIP_POLICY_API_WARNING_ONLY)) ){
  1492.                             org.openspcoop2.core.eventi.DatiEventoGenerico eventoGenericoSerialized =
  1493.                                     deserializer.readDatiEventoGenerico(content);
  1494.                             if(inMemory) {
  1495.                                 this.inMemoryLastPolicyAPIViolatedWarningOnly.put(eventoGenericoSerialized.getIdEvento(), convertToDatiEventoGenerico(eventoGenericoSerialized));
  1496.                             }
  1497.                             else {
  1498.                                 this.dbLastPolicyAPIViolatedWarningOnly.put(eventoGenericoSerialized.getIdEvento(), convertDBToDatiEventoGenerico(eventoGenericoSerialized));
  1499.                             }
  1500.                         }
  1501.                        
  1502.                         else if(entryName.startsWith((prefix+ZIP_EVENTO_TIMEOUT_CONNESSIONE)) ){
  1503.                             org.openspcoop2.core.eventi.DatiEventoGenerico eventoGenericoSerialized =
  1504.                                     deserializer.readDatiEventoGenerico(content);
  1505.                             if(inMemory) {
  1506.                                 this.inMemoryLastTimeoutConnessione.put(eventoGenericoSerialized.getIdEvento(), convertToDatiEventoGenerico(eventoGenericoSerialized));
  1507.                             }
  1508.                             else {
  1509.                                 this.dbLastTimeoutConnessione.put(eventoGenericoSerialized.getIdEvento(), convertDBToDatiEventoGenerico(eventoGenericoSerialized));
  1510.                             }
  1511.                         }
  1512.                        
  1513.                         else if(entryName.startsWith((prefix+ZIP_EVENTO_TIMEOUT_RICHIESTA)) ){
  1514.                             org.openspcoop2.core.eventi.DatiEventoGenerico eventoGenericoSerialized =
  1515.                                     deserializer.readDatiEventoGenerico(content);
  1516.                             if(inMemory) {
  1517.                                 this.inMemoryLastTimeoutRichiesta.put(eventoGenericoSerialized.getIdEvento(), convertToDatiEventoGenerico(eventoGenericoSerialized));
  1518.                             }
  1519.                             else {
  1520.                                 this.dbLastTimeoutRichiesta.put(eventoGenericoSerialized.getIdEvento(), convertDBToDatiEventoGenerico(eventoGenericoSerialized));
  1521.                             }
  1522.                         }
  1523.                        
  1524.                         else if(entryName.startsWith((prefix+ZIP_EVENTO_TIMEOUT_RISPOSTA)) ){
  1525.                             org.openspcoop2.core.eventi.DatiEventoGenerico eventoGenericoSerialized =
  1526.                                     deserializer.readDatiEventoGenerico(content);
  1527.                             if(inMemory) {
  1528.                                 this.inMemoryLastTimeoutRisposta.put(eventoGenericoSerialized.getIdEvento(), convertToDatiEventoGenerico(eventoGenericoSerialized));
  1529.                             }
  1530.                             else {
  1531.                                 this.dbLastTimeoutRisposta.put(eventoGenericoSerialized.getIdEvento(), convertDBToDatiEventoGenerico(eventoGenericoSerialized));
  1532.                             }
  1533.                         }
  1534.                        
  1535.                         else{
  1536.                             throw new UtilsException("Entry ["+entryName+"] sconosciuta");
  1537.                         }
  1538.                        
  1539.                     }
  1540.                 }
  1541.                
  1542.             }
  1543.            
  1544.         }catch(Exception e){
  1545.             throw new UtilsException("["+entryName+"] "+e.getMessage(),e);
  1546.         }
  1547.         finally{
  1548.             try {
  1549.                 if(f!=null) {
  1550.                     java.nio.file.Files.delete(f.toPath());
  1551.                 }
  1552.             }catch(Exception eClose) {
  1553.                 // ignore
  1554.             }
  1555.             try{
  1556.                 if(in!=null)
  1557.                     in.close();
  1558.             }catch(Exception eClose){
  1559.                 // close
  1560.             }
  1561.         }
  1562.        
  1563.     }
  1564.    
  1565.     private DatiEventoGenerico convertToDatiEventoGenerico(org.openspcoop2.core.eventi.DatiEventoGenerico eventoGenericoSerialized){
  1566.         DatiEventoGenerico e = new DatiEventoGenerico();
  1567.         e.data = eventoGenericoSerialized.getData();
  1568.         e.datiConsumatiThread = eventoGenericoSerialized.isDatiConsumatiThread();
  1569.         e.descrizione = eventoGenericoSerialized.getDescrizione();
  1570.         return e;
  1571.     }
  1572.     private DatabaseDatiEventoGenerico convertDBToDatiEventoGenerico(org.openspcoop2.core.eventi.DatiEventoGenerico eventoGenericoSerialized){
  1573.         DatabaseDatiEventoGenerico e = new DatabaseDatiEventoGenerico();
  1574.         e.data = eventoGenericoSerialized.getData();
  1575.         e.datiConsumatiThread = eventoGenericoSerialized.isDatiConsumatiThread();
  1576.         e.descrizione = eventoGenericoSerialized.getDescrizione();
  1577.         if(eventoGenericoSerialized.getCodiceEvento()!=null) {
  1578.             e.codiceEvento = CodiceEventoControlloTraffico.toEnumConstant(eventoGenericoSerialized.getCodiceEvento());
  1579.         }
  1580.         return e;
  1581.     }
  1582. }


  1583. class DatiEventoGenerico{
  1584.    
  1585.     Date data;
  1586.     String descrizione;
  1587.     String configurazione;
  1588.     boolean datiConsumatiThread = true;
  1589.    
  1590.     public DatiEventoGenerico readAndConsume(){
  1591.         DatiEventoGenerico d = new DatiEventoGenerico();
  1592.         if(this.data!=null)
  1593.             d.data = new Date(this.data.getTime());
  1594.         if(this.descrizione!=null)
  1595.             d.descrizione = this.descrizione + "";
  1596.         if(this.configurazione!=null)
  1597.             d.configurazione = this.configurazione + "";
  1598.         d.datiConsumatiThread = this.datiConsumatiThread;
  1599.        
  1600.         this.datiConsumatiThread = true;
  1601.        
  1602.         return d;
  1603.     }
  1604. }

  1605. class DatabaseDatiEventoGenerico extends DatiEventoGenerico{
  1606.    
  1607.     CodiceEventoControlloTraffico codiceEvento;
  1608.    
  1609. }