TransactionProcessorThread.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.monitor.engine.transaction;

  21. import java.util.Date;
  22. import java.util.List;

  23. import org.openspcoop2.core.commons.dao.DAOFactory;
  24. import org.openspcoop2.core.id.IDServizio;
  25. import org.openspcoop2.core.registry.driver.IDServizioFactory;
  26. import org.openspcoop2.generic_project.exception.NotFoundException;
  27. import org.openspcoop2.monitor.engine.config.BasicServiceLibraryReader;
  28. import org.openspcoop2.monitor.engine.config.TransactionServiceLibrary;
  29. import org.openspcoop2.monitor.engine.config.TransactionServiceLibraryReader;
  30. import org.openspcoop2.monitor.sdk.plugins.ITransactionProcessing;
  31. import org.openspcoop2.monitor.sdk.transaction.Transaction;
  32. import org.slf4j.Logger;

  33. /**
  34.  * TransactionProcessorThread
  35.  *
  36.  * @author Poli Andrea (apoli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public class TransactionProcessorThread implements Runnable {

  41.     private List<String> idTransazioni;
  42.     private Logger log;
  43.    
  44.     private BasicServiceLibraryReader basicServiceLibraryReader;
  45.     private TransactionServiceLibraryReader transactionServiceLibraryReader;
  46.     private DAOFactory daoFactory;
  47.    
  48.     private int threadNumber;
  49.     private boolean finished = false;

  50.     private boolean debug;

  51.     public TransactionProcessorThread(List<String> idTransazioni,Logger log,
  52.             BasicServiceLibraryReader basicServiceLibraryReader,
  53.             TransactionServiceLibraryReader transactionServiceLibraryReader,
  54.             DAOFactory daoFactory,
  55.             int threadNumber,
  56.             boolean debug){
  57.         this.idTransazioni = idTransazioni;
  58.         this.log = log;
  59.         this.basicServiceLibraryReader = basicServiceLibraryReader;
  60.         this.transactionServiceLibraryReader = transactionServiceLibraryReader;
  61.         this.daoFactory = daoFactory;
  62.         this.threadNumber = threadNumber;
  63.         this.debug = debug;
  64.     }
  65.    
  66.     private Date lastDateTransaction;
  67.     private String error;
  68.    

  69.     @Override
  70.     public void run() {
  71.        
  72.         String identificativo = null;
  73.         try{
  74.        
  75.             for (int i = 0; i < this.idTransazioni.size(); i++) {
  76.                
  77.                 String idTransazione  = this.idTransazioni.get(i);
  78.                 Transaction transaction = TransactionManager.getTransaction(this.daoFactory,this.log,idTransazione, this.debug);
  79.                
  80.                 identificativo = idTransazione+"-(#T-"+this.threadNumber+" #M-"+(i+1)+")";
  81.                
  82.                 if(this.debug)
  83.                     this.log.debug("["+identificativo+"] Check Transazione ["+i+"]=["+idTransazione+"] ...");
  84.                
  85.                 IDServizio idServizio = null;
  86.                 if(transaction.getTransaction().getTipoSoggettoErogatore()!=null && transaction.getTransaction().getNomeSoggettoErogatore()!=null &&
  87.                         transaction.getTransaction().getTipoServizio()!=null && transaction.getTransaction().getNomeServizio()!=null){
  88.                     idServizio =  IDServizioFactory.getInstance().getIDServizioFromValues(transaction.getTransaction().getTipoServizio(),
  89.                             transaction.getTransaction().getNomeServizio(),
  90.                             transaction.getTransaction().getTipoSoggettoErogatore(),
  91.                             transaction.getTransaction().getNomeSoggettoErogatore(),
  92.                             transaction.getTransaction().getVersioneServizio());
  93.                     idServizio.setAzione(transaction.getTransaction().getAzione());
  94.                 }
  95.                
  96.                 if(idServizio!=null){
  97.                
  98.                     if(this.debug)
  99.                         this.log.debug("["+identificativo+"] Search and Apply plugins SDK for idService["+idServizio.toString()+"] ...");
  100.                    
  101.                     TransactionServiceLibrary transactionServiceLibrary = null;
  102.                     try{
  103.                         transactionServiceLibrary = TransactionLibrary.getTransactionServiceLibrary(idServizio, this.basicServiceLibraryReader,
  104.                                 this.transactionServiceLibraryReader, this.log);
  105.                     }catch(NotFoundException notFound){
  106.                         if(this.debug)
  107.                             this.log.debug("["+identificativo+"] Library not found: "+notFound.getMessage(),notFound);
  108.                     }
  109.                     if(transactionServiceLibrary!=null){
  110.                    
  111.                         List<ITransactionProcessing> listPlugins = transactionServiceLibrary.mergeServiceActionTransactionLibrary_sdkPlugins(this.log);
  112.                         if(listPlugins!=null && listPlugins.size()>0){
  113.                             for (ITransactionProcessing iTransactionProcessing : listPlugins) {
  114.                                 if(this.debug)
  115.                                     this.log.debug("["+identificativo+"] Applico plugin ["+iTransactionProcessing.getClass().getName()+"] per la transazione " + transaction.getIdTransazione()+" ...");
  116.                                 iTransactionProcessing.postProcessTransaction(transaction);
  117.                                 if(this.debug)
  118.                                     this.log.info("["+identificativo+"] Applicato plugin ["+iTransactionProcessing.getClass().getName()+"] per la transazione " + transaction.getIdTransazione());
  119.                             }
  120.                         }
  121.                        
  122.                         // Aggiorno stato su db della transazione
  123.                         TransactionManager.updateContentResources(transaction);
  124.                        
  125.                     }
  126.                    
  127.                 }
  128.                
  129.                 this.lastDateTransaction = transaction.getTransaction().getDataIngressoRichiesta();
  130.                
  131.                 if(this.debug)
  132.                     this.log.debug("["+identificativo+"] Check Transazione ["+i+"]=["+idTransazione+"] ...");
  133.                
  134.             }
  135.            
  136.            
  137.             this.log.info("Thread #"+this.threadNumber+" finished");
  138.            
  139.         }catch(Exception e){
  140.             String msg = "Thread #"+this.threadNumber+" finished with error (id in corso di gestione: "+identificativo+"): "+e.getMessage();
  141.             this.log.error(msg,e);
  142.             this.error = msg;
  143.         }
  144.         finally{
  145.             this.finished = true;
  146.         }
  147.        
  148.     }

  149.     public boolean isFinished() {
  150.         return this.finished;
  151.     }
  152.     public Date getLastDateTransaction() {
  153.         return this.lastDateTransaction;
  154.     }
  155.     public String getError() {
  156.         return this.error;
  157.     }
  158. }