GestioneOperazione.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.ctrlstat.gestori;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.PrintWriter;
  23. import java.sql.Connection;
  24. import java.sql.Timestamp;
  25. import java.util.Date;

  26. import javax.jms.QueueSession;

  27. import org.slf4j.Logger;
  28. import org.openspcoop2.utils.Utilities;
  29. import org.openspcoop2.web.ctrlstat.core.DBManager;
  30. import org.openspcoop2.web.ctrlstat.costanti.OperationsParameter;
  31. import org.openspcoop2.web.lib.queue.ClassQueue;
  32. import org.openspcoop2.web.lib.queue.costanti.OperationStatus;
  33. import org.openspcoop2.web.lib.queue.dao.FilterParameter;
  34. import org.openspcoop2.web.lib.queue.dao.Parameter;

  35. /**
  36.  * Gestisce l'operazione del gestore
  37.  *
  38.  *
  39.  * @author Andrea Poli (apoli@link.it)
  40.  * @author Stefano Corallo (corallo@link.it)
  41.  * @author Sandra Giangrandi (sandra@link.it)
  42.  * @author $Author$
  43.  * @version $Rev$, $Date$
  44.  *
  45.  */
  46. public class GestioneOperazione {

  47.     private DBManager dbManager;
  48.     private Connection connectionDB;
  49.     private QueueSession sessionJMS;
  50.     private org.openspcoop2.web.lib.queue.dao.Operation operation;
  51.     private ClassQueue operationManager;
  52.     private Logger log;
  53.     private String idGestore;
  54.     private String tipoOperazioneCRUD;

  55.     public GestioneOperazione(DBManager db, Connection c, QueueSession s, org.openspcoop2.web.lib.queue.dao.Operation o, ClassQueue operationManager,
  56.             Logger l, String tipoOperazioneCRUD, String idGestore) {
  57.         this.dbManager = db;
  58.         this.connectionDB = c;
  59.         this.sessionJMS = s;
  60.         this.operation = o;
  61.         this.operationManager = operationManager;
  62.         this.log = l;
  63.         this.idGestore = idGestore;
  64.         if (tipoOperazioneCRUD != null)
  65.             this.tipoOperazioneCRUD = "." + tipoOperazioneCRUD;
  66.         else
  67.             this.tipoOperazioneCRUD = "";
  68.     }

  69.     public void error(String msg) throws Exception {
  70.         error(msg, null);
  71.     }

  72.     public void error(String msgParam, Exception e) throws Exception {
  73.        
  74.         String msgErrore = msgParam;
  75.         if(e!=null){
  76.             PrintWriter pw = null;
  77.             ByteArrayOutputStream out = null;
  78.             try{
  79.                 out = new ByteArrayOutputStream();
  80.                 pw = new PrintWriter(out);
  81.                 e.printStackTrace(pw);
  82.                 pw.flush();
  83.                 out.flush();
  84.             }finally{
  85.                 try{
  86.                     if(pw!=null){
  87.                         pw.close();
  88.                     }
  89.                 }catch(Exception eClose){
  90.                     // close
  91.                 }
  92.                 try{
  93.                     if(out!=null){
  94.                         out.close();
  95.                     }
  96.                 }catch(Exception eClose){
  97.                     // close
  98.                 }
  99.             }
  100.             msgErrore = msgErrore +"\n"+out.toString();
  101.         }
  102.        
  103.         if ((e != null) && (e instanceof java.net.ConnectException)) {
  104.             this.log.warn("[" + this.idGestore + "]" + this.tipoOperazioneCRUD + " " + msgParam, e);
  105.         } else {
  106.            
  107.             if(e!=null && Utilities.existsInnerException(e, java.net.ConnectException.class)){
  108.                 this.log.warn("[" + this.idGestore + "]" + this.tipoOperazioneCRUD + " " + msgParam, e);
  109.             }
  110.             else{
  111.                 if (e != null)
  112.                     this.log.error("[" + this.idGestore + "]" + this.tipoOperazioneCRUD + " " + msgParam, e);
  113.                 else
  114.                     this.log.error("[" + this.idGestore + "]" + this.tipoOperazioneCRUD + " " + msgParam);
  115.             }
  116.         }
  117.         this.operation.setStatus(OperationStatus.ERROR);
  118.         this.operation.setDetails(msgErrore);
  119.         this.operation.setTimeExecute(new Timestamp(System.currentTimeMillis()));
  120.         this.operationManager.updateOperation(this.operation);
  121.         this.sessionJMS.rollback();
  122.         this.dbManager.releaseConnection(this.connectionDB);
  123.     }

  124.     public void invalid(String msg) throws Exception {
  125.         this.log.error("[" + this.idGestore + "]" + this.tipoOperazioneCRUD + " " + msg);
  126.         this.operation.setStatus(OperationStatus.INVALID);
  127.         this.operation.setDetails(msg);
  128.         this.operation.setTimeExecute(new Timestamp(System.currentTimeMillis()));
  129.         this.operationManager.updateOperation(this.operation);
  130.         this.sessionJMS.commit();
  131.         this.dbManager.releaseConnection(this.connectionDB);
  132.     }

  133.     /**
  134.      * Una operazione (DI ADD) puo essere in attesa (wait) che si verificano
  135.      * determinate condizioni (commit) affinche essa diventi valida.
  136.      * L'operazione puo rimanere in wait per un numero determinato di volte
  137.      * "waitTime" dopodicche verra settata come invalida
  138.      *
  139.      * In caso di operazioni != da ADD viene subito invalidata
  140.      *
  141.      * @param msg
  142.      * @throws Exception
  143.      */
  144.     public void waitBeforeInvalid(String msg) throws Exception {
  145.         this.log.warn("[" + this.idGestore + "]" + this.tipoOperazioneCRUD + " " + msg);

  146.         /**
  147.          * Se l'operazione non e' di add non attendo ma invalido subito
  148.          */
  149.         if (!"add".equals(this.operation.getOperation())) {
  150.             this.operation.setStatus(OperationStatus.INVALID);
  151.             this.operation.setDetails(msg);
  152.         } else if (this.operation.getWaitTime() > this.operationManager.getDefaultWaitTime()) {
  153.             // se operazione di add ma limite wait superato allora invalido
  154.             this.log.debug("L operazione " + this.operation.toString() + " ha superato il WAIT_TIME(" + this.operationManager.getDefaultWaitTime() + ") setto operazione come INVALID.");
  155.             this.operation.setStatus(OperationStatus.INVALID);
  156.             this.operation.setDetails("Superato WAIT_TIME(" + this.operationManager.getDefaultWaitTime() + ") <<>> Old details:" + msg);
  157.         } else {
  158.             // add e limite wait non superato, attendo incrementando il
  159.             // wait_time di questa operation
  160.             this.operation.setStatus(OperationStatus.WAIT);
  161.             this.operation.setWaitTime(this.operation.getWaitTime() + 1);// incremento
  162.             // waittime
  163.             this.operation.setDetails(msg);
  164.         }

  165.         this.operation.setTimeExecute(new Timestamp(System.currentTimeMillis()));
  166.         this.operationManager.updateOperation(this.operation);

  167.         // se operazione in wait allora rollo, altrimenti committo
  168.         switch (this.operation.getStatus()) {
  169.             case WAIT:
  170.                 this.sessionJMS.rollback();
  171.                 break;
  172.             default:
  173.                 this.sessionJMS.commit();
  174.                 break;
  175.         }

  176.         this.dbManager.releaseConnection(this.connectionDB);

  177.     }

  178.     public void success(String msg) throws Exception {
  179.         this.log.info("[" + this.idGestore + "]" + this.tipoOperazioneCRUD + " " + msg);
  180.         this.operation.setStatus(OperationStatus.SUCCESS);
  181.         this.operation.setDetails(msg);
  182.         this.operation.setTimeExecute(new Timestamp(System.currentTimeMillis()));
  183.         this.operationManager.updateOperation(this.operation);
  184.         this.sessionJMS.commit();
  185.         this.dbManager.releaseConnection(this.connectionDB);
  186.     }

  187.     public void delete() throws Exception {
  188.         this.operation.setStatus(OperationStatus.DELETED);
  189.         this.operation.setDetails("[" + this.idGestore + "]" + this.tipoOperazioneCRUD + " Operazione Marcata come DELETED");
  190.         this.operation.setTimeExecute(new Date(System.currentTimeMillis()));
  191.         this.operationManager.updateOperation(this.operation);
  192.         this.sessionJMS.commit();
  193.         this.dbManager.releaseConnection(this.connectionDB);
  194.     }
  195.    
  196.     public boolean existsOperationNotCompleted(String tipoOperazione,String hostname,FilterParameter filtro) throws Exception{
  197.         return this.operationManager.existsOperationNotCompleted(tipoOperazione, hostname,filtro);
  198.     }

  199.     public FilterParameter getFilterChangeIDSoggetto(String tipoSogg,String nomeSogg,String oldTipoSogg,String oldNomeSogg){
  200.         FilterParameter filtro = new FilterParameter();
  201.         Parameter param = new Parameter();
  202.         param.setName(OperationsParameter.OGGETTO.getNome());
  203.         param.setValue("soggetto");
  204.         filtro.addFilterParameter(param);
  205.         Parameter paramTipoSoggetto = new Parameter();
  206.         paramTipoSoggetto.setName(OperationsParameter.TIPO_SOGGETTO.getNome());
  207.         paramTipoSoggetto.setValue(tipoSogg);
  208.         filtro.addFilterParameter(paramTipoSoggetto);
  209.         Parameter paramNomeSoggetto = new Parameter();
  210.         paramNomeSoggetto.setName(OperationsParameter.NOME_SOGGETTO.getNome());
  211.         paramNomeSoggetto.setValue(nomeSogg);
  212.         filtro.addFilterParameter(paramNomeSoggetto);
  213.         Parameter paramOldTipoSoggetto = new Parameter();
  214.         paramOldTipoSoggetto.setName(OperationsParameter.OLD_TIPO_SOGGETTO.getNome());
  215.         paramOldTipoSoggetto.setValue(oldTipoSogg);
  216.         filtro.addFilterParameter(paramOldTipoSoggetto);
  217.         Parameter paramOldNomeSoggetto = new Parameter();
  218.         paramOldNomeSoggetto.setName(OperationsParameter.OLD_NOME_SOGGETTO.getNome());
  219.         paramOldNomeSoggetto.setValue(oldNomeSogg);
  220.         filtro.addFilterParameter(paramOldNomeSoggetto);
  221.         return filtro;
  222.     }
  223. }