AuditDBAppender.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.web.lib.audit.appender;

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

  23. import javax.sql.DataSource;

  24. import org.openspcoop2.utils.Utilities;
  25. import org.openspcoop2.utils.date.DateManager;
  26. import org.openspcoop2.utils.resources.GestoreJNDI;
  27. import org.openspcoop2.web.lib.audit.AuditException;
  28. import org.openspcoop2.web.lib.audit.DriverAuditDBAppender;
  29. import org.openspcoop2.web.lib.audit.log.Operation;
  30. import org.openspcoop2.web.lib.audit.log.constants.Stato;

  31. /**
  32.  * Appender per registrare operazione di audit
  33.  *
  34.  *
  35.  * @author Andrea Poli (apoli@link.it)
  36.  * @author Stefano Corallo (corallo@link.it)
  37.  * @author Sandra Giangrandi (sandra@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40.  *
  41.  */
  42. public class AuditDBAppender implements IAuditAppender {

  43.     /** DataSource dove attingere connessioni */
  44.     private DataSource ds = null;
  45.     private String dsName = null;
  46.     private String tipoDatabase = null; //tipoDatabase
  47.    
  48.     private String nomeAppender = null;
  49.    
  50.     @Override
  51.     public void initAppender(String nomeAppender,Properties properties) throws AuditException{
  52.        
  53.         try{
  54.             this.nomeAppender = nomeAppender;
  55.        
  56.             // Datasource
  57.             this.dsName = properties.getProperty("datasource");
  58.             if(this.dsName==null){
  59.                 throw new AuditException("Appender["+this.nomeAppender+"] Proprieta' 'datasource' non definita");
  60.             }else{
  61.                 this.dsName = this.dsName.trim();
  62.             }
  63.            
  64.             // TipoDatabase
  65.             this.tipoDatabase = properties.getProperty("tipoDatabase");
  66.             if(this.tipoDatabase==null){
  67.                 throw new AuditException("Appender["+this.nomeAppender+"] Proprieta' 'tipoDatabase' non definita");
  68.             }else{
  69.                 this.tipoDatabase = this.tipoDatabase.trim();
  70.             }
  71.            
  72.             // Contesto
  73.             java.util.Properties ctx = Utilities.readProperties("context-", properties);
  74.            
  75.             // lookup ds
  76.             GestoreJNDI jndi = new GestoreJNDI(ctx);
  77.             this.ds = (DataSource) jndi.lookup(this.dsName);
  78.            
  79.         }catch(AuditException e){
  80.             throw e;
  81.         }catch(Exception e){
  82.             throw new AuditException("Inizializzazione appender["+this.nomeAppender+"] non riuscita: "+e.getMessage(),e);
  83.         }
  84.        
  85.     }
  86.    
  87.     @Override
  88.     public Object registraOperazioneInFaseDiElaborazione(Operation operation) throws AuditException{
  89.        
  90.         Connection con = null;
  91.         try{
  92.        
  93.             con = this.ds.getConnection();
  94.             checkConnection(con);
  95.            
  96.             DriverAuditDBAppender driverDBAppender = new DriverAuditDBAppender(con,this.tipoDatabase);
  97.             driverDBAppender.createOperation(operation);
  98.             return operation.getId();
  99.            
  100.         }catch(Exception e){
  101.             throw new AuditException("Appender["+this.nomeAppender+"] Errore durante la registrazione dell'operazione: "+e.getMessage(),e);
  102.         }finally{
  103.             try{
  104.                 if(con!=null) {
  105.                     con.close();
  106.                 }
  107.             }catch(Exception e){
  108.                 // close
  109.             }
  110.         }
  111.     }
  112.    
  113.     private void checkConnection(Connection con) throws Exception {
  114.         if(con == null)
  115.             throw new Exception("Connessione non fornita dal datasource ["+this.dsName+"]");
  116.     }
  117.    
  118.     @Override
  119.     public void registraOperazioneCompletataConSuccesso(Object idOperation) throws AuditException{
  120.        
  121.         Connection con = null;
  122.         try{
  123.        
  124.             con = this.ds.getConnection();
  125.             checkConnection(con);
  126.            
  127.             DriverAuditDBAppender driverDBAppender = new DriverAuditDBAppender(con,this.tipoDatabase);
  128.             Operation operation = driverDBAppender.getOperation((Long)idOperation);
  129.            
  130.             // Aggiorno stato
  131.             operation.setStato(Stato.COMPLETED);
  132.            
  133.             // Aggiorno tempo di esecuzione
  134.             operation.setTimeExecute(DateManager.getDate());
  135.            
  136.             driverDBAppender.updateOperation(operation, false);
  137.                        
  138.         }catch(Exception e){
  139.             throw new AuditException("Appender["+this.nomeAppender+"] Errore durante la registrazione dell'operazione: "+e.getMessage(),e);
  140.         }finally{
  141.             try{
  142.                 if(con!=null) {
  143.                     con.close();
  144.                 }
  145.             }catch(Exception e){
  146.                 // close
  147.             }
  148.         }
  149.     }
  150.    
  151.     @Override
  152.     public void registraOperazioneTerminataConErrore(Object idOperation,String motivoErrore) throws AuditException{
  153.        
  154.         Connection con = null;
  155.         try{
  156.        
  157.             con = this.ds.getConnection();
  158.             checkConnection(con);
  159.            
  160.             DriverAuditDBAppender driverDBAppender = new DriverAuditDBAppender(con,this.tipoDatabase);
  161.             Operation operation = driverDBAppender.getOperation((Long)idOperation);
  162.            
  163.             // Aggiorno stato
  164.             operation.setStato(Stato.ERROR);
  165.             operation.setError(motivoErrore);
  166.            
  167.             // Aggiorno tempo di esecuzione
  168.             operation.setTimeExecute(DateManager.getDate());
  169.            
  170.             driverDBAppender.updateOperation(operation, false);
  171.                        
  172.         }catch(Exception e){
  173.             throw new AuditException("Appender["+this.nomeAppender+"] Errore durante la registrazione dell'operazione: "+e.getMessage(),e);
  174.         }finally{
  175.             try{
  176.                 if(con!=null) {
  177.                     con.close();
  178.                 }
  179.             }catch(Exception e){
  180.                 // close
  181.             }
  182.         }
  183.     }
  184.    
  185. }