GestoreEventi.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.eventi;

  21. import java.sql.Connection;

  22. import org.openspcoop2.core.commons.dao.DAOFactory;
  23. import org.openspcoop2.core.commons.dao.DAOFactoryException;
  24. import org.openspcoop2.core.commons.dao.DAOFactoryProperties;
  25. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  26. import org.openspcoop2.core.eventi.Evento;
  27. import org.openspcoop2.core.eventi.dao.jdbc.JDBCServiceManager;
  28. import org.openspcoop2.generic_project.exception.NotImplementedException;
  29. import org.openspcoop2.generic_project.exception.ServiceException;
  30. import org.openspcoop2.generic_project.utils.ServiceManagerProperties;
  31. import org.openspcoop2.pdd.config.DBTransazioniManager;
  32. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  33. import org.openspcoop2.pdd.config.Resource;
  34. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  35. import org.openspcoop2.utils.date.DateManager;
  36. import org.slf4j.Logger;

  37. /**    
  38.  * GestoreEventi
  39.  *
  40.  * @author Poli Andrea (poli@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  */
  44. public class GestoreEventi {

  45.     private static GestoreEventi staticInstance = null;
  46.     private static synchronized void initialize() throws DriverConfigurazioneException{
  47.         if(staticInstance==null){
  48.             staticInstance = new GestoreEventi();
  49.         }
  50.     }
  51.     public static GestoreEventi getInstance() throws DriverConfigurazioneException{
  52.         if(staticInstance==null){
  53.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  54.             synchronized (GestoreEventi.class) {
  55.                 initialize();
  56.             }
  57.         }
  58.         return staticInstance;
  59.     }
  60.    
  61.    
  62.     private DAOFactory daoFactory = null;
  63.     private ServiceManagerProperties daoFactoryServiceManagerProperties = null;
  64.     private Logger daoFactoryLogger = null;
  65.    
  66.     private OpenSPCoop2Properties properties;
  67.    
  68.     private boolean debug;
  69.    
  70.     private static final String ID_MODULO = "GestoreEventi";
  71.    
  72.     private GestoreEventi() throws DriverConfigurazioneException{
  73.        
  74.         // ** config **
  75.        
  76.         this.properties = OpenSPCoop2Properties.getInstance();
  77.        
  78.         // Inizializzazione DAOFactory
  79.         try{
  80.            
  81.             this.debug = this.properties.isEventiDebug();
  82.            
  83.             DAOFactoryProperties daoFactoryProperties = null;
  84.             this.daoFactoryLogger = OpenSPCoop2Logger.getLoggerOpenSPCoopEventi(this.debug);
  85.             this.daoFactory = DAOFactory.getInstance(this.daoFactoryLogger);
  86.             daoFactoryProperties = DAOFactoryProperties.getInstance(this.daoFactoryLogger);
  87.             this.daoFactoryServiceManagerProperties = daoFactoryProperties.getServiceManagerProperties(org.openspcoop2.core.eventi.utils.ProjectInfo.getInstance());
  88.             this.daoFactoryServiceManagerProperties.setShowSql(this.debug);
  89.             this.daoFactoryServiceManagerProperties.setDatabaseType(DBTransazioniManager.getInstance().getTipoDatabase());
  90.            
  91.         }catch(Exception e){
  92.             throw new DriverConfigurazioneException(e.getMessage(),e);
  93.         }
  94.        
  95.     }
  96.        
  97.     public void log(Evento evento) throws Exception{
  98.         this.log(evento, false);
  99.     }
  100.     public void log(Evento evento, boolean shutdownInCorso) throws Exception{
  101.        
  102.         if(evento.getOraRegistrazione()==null){
  103.             evento.setOraRegistrazione(DateManager.getDate());
  104.         }
  105.        
  106.         boolean logError = true;
  107.         if(shutdownInCorso) {
  108.             logError = false;
  109.         }
  110.        
  111.         DBTransazioniManager dbManager = null;
  112.         Resource r = null;
  113.         String modulo = ID_MODULO+"."+evento.getTipo()+"."+evento.getCodice();
  114.         try{
  115.             dbManager = DBTransazioniManager.getInstance();
  116.             r = dbManager.getResource(this.properties.getIdentitaPortaDefaultWithoutProtocol(), modulo, evento.getIdTransazione(), logError);
  117.             if(r==null){
  118.                 throw new DriverConfigurazioneException("Risorsa al database non disponibile");
  119.             }
  120.             Connection con = (Connection) r.getResource();
  121.             if(con == null)
  122.                 throw new DriverConfigurazioneException("Connessione non disponibile");
  123.             this.log(evento, con);
  124.            
  125.         }finally{
  126.             try{
  127.                 if(r!=null)
  128.                     dbManager.releaseResource(this.properties.getIdentitaPortaDefaultWithoutProtocol(), modulo, r, logError);
  129.             }catch(Exception e){
  130.                 // close
  131.             }
  132.         }
  133.     }
  134.     public void log(Evento evento, Connection connection) throws DriverConfigurazioneException, DAOFactoryException, ServiceException, NotImplementedException{
  135.        
  136.         if(evento.getOraRegistrazione()==null){
  137.             evento.setOraRegistrazione(DateManager.getDate());
  138.         }
  139.        
  140.         if(connection == null) {
  141.             throw new DriverConfigurazioneException("Connessione non fornita");
  142.         }
  143.        
  144.         JDBCServiceManager jdbcServiceManager =
  145.                 (JDBCServiceManager) this.daoFactory.getServiceManager(org.openspcoop2.core.eventi.utils.ProjectInfo.getInstance(), connection,
  146.                         this.daoFactoryServiceManagerProperties, this.daoFactoryLogger);
  147.         jdbcServiceManager.getEventoService().create(evento);  
  148.        
  149.         String msg = "CREATO EVENTO: "+EventiUtilities.toString(evento);
  150.         this.daoFactoryLogger.info(msg);

  151.         /**System.out.println("CREATO EVENTO: "+EventiUtilities.toString(evento));*/
  152.     }

  153. }