Skip to content

Commit

Permalink
Aggiunta possibilita' di caricare una configurazione di log4j custom …
Browse files Browse the repository at this point in the history
…a runtime.

Aggiunto salvataggio degli eventi relativi all'acquisizione di pagamenti senza rpt.
  • Loading branch information
pintorig committed Mar 2, 2022
1 parent aafecac commit db86b18
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 5 deletions.
61 changes: 56 additions & 5 deletions jars/core/src/main/java/it/govpay/core/business/Incassi.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ public RichiestaIncassoDTOResponse elaboraRiconciliazione(String codDomino, Stri
String iuv = null;
String idf = null;
log.debug("Elaborazione riconciliazione [Dominio:"+codDomino+", Id: "+idRiconciliazione+"] ...");
List<Evento> listaEventi = new ArrayList<>();

try {
if(gpContext.getEventoCtx().getDatiPagoPA() == null) {
Expand Down Expand Up @@ -347,7 +348,7 @@ public RichiestaIncassoDTOResponse elaboraRiconciliazione(String codDomino, Stri
idf = incasso.getIdFlussoRendicontazione();
}

return eseguiAcquisizioneRiconciliazione(richiestaIncassoResponse, codDomino, idRiconciliazione, ctx, configWrapper, incassiBD, incasso, causale, iuv, idf, false, true);
return eseguiAcquisizioneRiconciliazione(richiestaIncassoResponse, codDomino, idRiconciliazione, ctx, configWrapper, incassiBD, incasso, causale, iuv, idf, false, true, listaEventi);
} catch (ServiceException e) {
throw new GovPayException(e);
} catch (UtilsException e) {
Expand Down Expand Up @@ -385,12 +386,32 @@ public RichiestaIncassoDTOResponse elaboraRiconciliazione(String codDomino, Stri

if(incassiBD != null)
incassiBD.closeConnection();

if(listaEventi.size() >0) {
EventiBD eventiBD = null;

try {
eventiBD = new EventiBD(configWrapper);
eventiBD.setupConnection(configWrapper.getTransactionID());
eventiBD.setAtomica(false);

for (Evento evento : listaEventi) {
eventiBD.insertEvento(evento);
}
} catch (ServiceException e) {
throw new GovPayException(e);
} finally {
if(eventiBD != null) {
eventiBD.closeConnection();
}
}
}
}
}


private RichiestaIncassoDTOResponse eseguiAcquisizioneRiconciliazione(RichiestaIncassoDTOResponse richiestaIncassoResponse, String codDomino, String idRiconciliazione, IContext ctx, BDConfigWrapper configWrapper,
IncassiBD incassiBD, Incasso incasso, String causale, String iuv, String idf, boolean ricercaIdFlussoCaseInsensitive, boolean salvaConUpdate)
IncassiBD incassiBD, Incasso incasso, String causale, String iuv, String idf, boolean ricercaIdFlussoCaseInsensitive, boolean salvaConUpdate, List<Evento> listaEventi)
throws ServiceException, IncassiException, UtilsException, GovPayException {
GpContext gpContext = (GpContext) ctx.getApplicationContext();

Expand Down Expand Up @@ -453,7 +474,7 @@ private RichiestaIncassoDTOResponse eseguiAcquisizioneRiconciliazione(RichiestaI
}

Versamento versamentoBusiness = new Versamento();
EventiBD eventiBD = new EventiBD(configWrapper);


for(Rendicontazione rendicontazione : fr.getRendicontazioni(incassiBD)) {

Expand Down Expand Up @@ -519,6 +540,7 @@ private RichiestaIncassoDTOResponse eseguiAcquisizioneRiconciliazione(RichiestaI

versamentiBD.updateVersamento(versamento);

// salvo l'evento in una lista, effettuo l'inserimento di tutti gli eventi insieme al termine della procedura perche' quando veniva impostato l'id_fr il DB andava in deadlock perche' il flusso e' in lock dalla select precedente.
Evento eventoNota = new Evento();
eventoNota.setCategoriaEvento(CategoriaEvento.INTERNO);
eventoNota.setRuoloEvento(RuoloEvento.CLIENT);
Expand All @@ -527,7 +549,10 @@ private RichiestaIncassoDTOResponse eseguiAcquisizioneRiconciliazione(RichiestaI
eventoNota.setEsitoEvento(EsitoEvento.OK);
eventoNota.setDettaglioEsito("Riconciliato flusso " + fr.getCodFlusso() + " con Pagamento senza RPT [IUV: " + rendicontazione.getIuv() + " IUR:" + rendicontazione.getIur() + "].");
eventoNota.setTipoEvento("Pagamento eseguito senza RPT");
eventiBD.insertEvento(eventoNota);
eventoNota.setIuv(rendicontazione.getIuv());
eventoNota.setCodDominio(fr.getCodDominio());
listaEventi.add(eventoNota);

} catch (MultipleResultException e) {
ctx.getApplicationLogger().log("incasso.frAnomala", idf);
throw new IncassiException(FaultType.FR_ANOMALA, "La rendicontazione [Dominio:"+fr.getCodDominio()+" Iuv:" + rendicontazione.getIuv()+ " Iur:" + rendicontazione.getIur() + " Indice:" + rendicontazione.getIndiceDati() + "] non identifica univocamente un pagamento");
Expand Down Expand Up @@ -584,6 +609,11 @@ private RichiestaIncassoDTOResponse eseguiAcquisizioneRiconciliazione(RichiestaI
incassiBD.commit();
gpContext.getEventoCtx().setIdIncasso(incasso.getId());

for (Evento evento : listaEventi) {
evento.setIdIncasso(incasso.getId());
evento.setIdFr(fr.getId());
}

log.debug("Riconciliazione [Dominio:"+codDomino+", Id: "+idRiconciliazione+"] Iuv ["+iuv+"], IdFlusso ["+idf+"], Causale ["+causale+"] completata con esito OK.");
} catch(Exception e) {
incassiBD.rollback();
Expand All @@ -604,6 +634,7 @@ public RichiestaIncassoDTOResponse richiestaIncasso(RichiestaIncassoDTO richiest
boolean ricercaIdFlussoCaseInsensitive = richiestaIncasso.isRicercaIdFlussoCaseInsensitive();
BDConfigWrapper configWrapper = new BDConfigWrapper(ContextThreadLocal.get().getTransactionId(), true);
IncassiBD incassiBD = null;
List<Evento> listaEventi = new ArrayList<>();
try {
IContext ctx = ContextThreadLocal.get();
ctx.getApplicationLogger().log("incasso.richiesta");
Expand Down Expand Up @@ -786,7 +817,7 @@ public RichiestaIncassoDTOResponse richiestaIncasso(RichiestaIncassoDTO richiest

incassiBD.setAutoCommit(false);

return eseguiAcquisizioneRiconciliazione(richiestaIncassoResponse, incasso.getCodDominio(), incasso.getIdRiconciliazione(), ctx, configWrapper, incassiBD, incasso, causale, iuv, idf, ricercaIdFlussoCaseInsensitive, false);
return eseguiAcquisizioneRiconciliazione(richiestaIncassoResponse, incasso.getCodDominio(), incasso.getIdRiconciliazione(), ctx, configWrapper, incassiBD, incasso, causale, iuv, idf, ricercaIdFlussoCaseInsensitive, false, listaEventi);
} catch (ServiceException e) {
throw new GovPayException(e);
} catch (UtilsException e) {
Expand All @@ -799,6 +830,26 @@ public RichiestaIncassoDTOResponse richiestaIncasso(RichiestaIncassoDTO richiest

if(incassiBD != null)
incassiBD.closeConnection();

if(listaEventi.size() >0) {
EventiBD eventiBD = null;

try {
eventiBD = new EventiBD(configWrapper);
eventiBD.setupConnection(configWrapper.getTransactionID());
eventiBD.setAtomica(false);

for (Evento evento : listaEventi) {
eventiBD.insertEvento(evento);
}
} catch (ServiceException e) {
throw new GovPayException(e);
} finally {
if(eventiBD != null) {
eventiBD.closeConnection();
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import it.govpay.core.utils.GovpayConfig;
import it.govpay.core.utils.client.BasicClient;
import it.govpay.core.utils.client.BasicClientCORE;
import it.govpay.core.utils.logger.Log4JUtils;
import it.govpay.core.utils.thread.InviaNotificaAppIoThread;
import it.govpay.core.utils.thread.InviaNotificaThread;
import it.govpay.core.utils.thread.SpedizioneTracciatoNotificaPagamentiThread;
Expand Down Expand Up @@ -470,6 +471,10 @@ public static String resetCacheAnagrafica(IContext ctx){
BasicClient.cleanCache();
BasicClientCORE.cleanCache();
log.info("Aggiornamento della data di reset della cache anagrafica del sistema completato con successo.");

Log4JUtils.reloadLog4j();
log.info("Reload Log4J completato.");

return "Aggiornamento della data di reset della cache anagrafica del sistema completato con successo.";
} catch (Exception e) {
log.error("Aggiornamento della data di reset cache anagrafica del sistema fallita", e);
Expand Down Expand Up @@ -500,6 +505,10 @@ public static String resetCacheAnagraficaCheck(IContext ctx){
BasicClient.cleanCache();
BasicClientCORE.cleanCache();
log.info("Nodo ["+clusterId+"]: Reset della cache anagrafica locale completato.");

log.info("Nodo ["+clusterId+"]: Reload Log4J in corso...");
Log4JUtils.reloadLog4j();
log.info("Nodo ["+clusterId+"]: Reload Log4J completato.");
}

log.debug("Check reset della cache anagrafica locale completato con successo.");
Expand Down
23 changes: 23 additions & 0 deletions jars/core/src/main/java/it/govpay/core/utils/GovpayConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,29 @@ public void readProperties() throws Exception {
}
}

public synchronized void leggiFileEsternoLog4j2() {
// Recupero la configurazione della working dir Se e' configurata, la uso come prioritaria
try {
if(this.resourceDir != null) {
File resourceDirFile = new File(escape(this.resourceDir));
if(!resourceDirFile.isDirectory())
throw new Exception("Il path indicato nella property \"it.govpay.resource.path\" (" + this.resourceDir + ") non esiste o non e' un folder.");

File log4j2ConfigFile = new File(this.resourceDir + File.separatorChar + LOG4J2_XML_FILE_NAME);

if(log4j2ConfigFile.exists()) {
this.log4j2Config = log4j2ConfigFile.toURI();
LoggerWrapperFactory.getLogger(GovpayConfig.class).info("Individuata configurazione log4j: " + this.log4j2Config);
} else {
this.log4j2Config = null;
LoggerWrapperFactory.getLogger(GovpayConfig.class).info("Individuata configurazione log4j interna.");
}
}
} catch (Exception e) {
LoggerWrapperFactory.getLogger(GovpayConfig.class).warn("Errore di inizializzazione: " + e.getMessage() + ". Property ignorata.");
}
}

private static Map<String,String> getProperties(String baseName, Properties[] props, boolean required, Logger log) throws Exception {
Map<String, String> valori = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.slf4j.MDC;

import it.govpay.bd.anagrafica.AnagraficaManager;
import it.govpay.core.utils.logger.Log4JUtils;
import it.govpay.core.utils.service.context.GpContextFactory;
import it.govpay.core.utils.thread.ThreadExecutorManager;
import it.govpay.stampe.utils.GovpayStampe;
Expand Down Expand Up @@ -96,6 +97,9 @@ public static synchronized IContext startup(Logger log, String warName, String g
throw new RuntimeException("Inizializzazione di "+getGovpayVersion(warName, govpayVersion, buildVersion)+" fallita: " + e, e);
}

// inizializzo utilities di logging
Log4JUtils.initialize(log4j2XmlFile);

// Configurazione del logger Diagnostici/Tracce/Dump
try {
DiagnosticConfig diagnosticConfig = new DiagnosticConfig();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* GovPay - Porta di Accesso al Nodo dei Pagamenti SPC
* http://www.gov4j.it/govpay
*
* Copyright (c) 2014-2022 Link.it srl (http://www.link.it).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package it.govpay.core.utils.logger;

import java.io.File;
import java.net.URI;
import java.net.URL;

import org.openspcoop2.utils.LoggerWrapperFactory;
import org.openspcoop2.utils.UtilsException;
import org.slf4j.Logger;

import it.govpay.core.utils.GovpayConfig;

/***
*
* @author pintori
*
*/
public class Log4JUtils {

private static Logger log = LoggerWrapperFactory.getLogger(Log4JUtils.class);
private static URL log4j2XmlFile;

public static synchronized void initialize(URL log4j2XmlFile) {
Log4JUtils.log4j2XmlFile = log4j2XmlFile;
}

public static synchronized void reloadLog4j() {

GovpayConfig gpConfig = GovpayConfig.getInstance();

try {
// controllo che sia stato impostato un nuovo file esterno di log4j2
gpConfig.leggiFileEsternoLog4j2();

// leggo l'eventuale path al file
URI log4j2Config = gpConfig.getLog4j2Config();

if(log4j2Config != null) {
LoggerWrapperFactory.setLogConfiguration(new File(log4j2Config));
log.info("Caricata configurazione di log4j: " + log4j2Config.toString());
} else {
LoggerWrapperFactory.setLogConfiguration(log4j2XmlFile);
log.info("Caricata configurazione di log4j: " + log4j2XmlFile.toString());
}
} catch (Throwable e) {
log.error("Errore durante il reload del log4j: " + e.getMessage(), e);

try {
LoggerWrapperFactory.setLogConfiguration(log4j2XmlFile);
log.info("La configurazione esterna indicata non e' valida, caricata configurazione default log4j: " + log4j2XmlFile.toString());
} catch (UtilsException e1) {
log.error("Errore durante il reload del log4j: " + e1.getMessage(), e1);
}
}
}
}

0 comments on commit db86b18

Please sign in to comment.