GestoreGeneral.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.util.ArrayList;
  22. import java.util.List;

  23. import javax.jms.ObjectMessage;
  24. import javax.jms.Queue;
  25. import javax.jms.QueueReceiver;
  26. import javax.jms.QueueSession;

  27. import org.slf4j.Logger;
  28. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  29. import org.openspcoop2.web.lib.queue.ClassQueue;
  30. import org.openspcoop2.web.lib.queue.dao.Operation;

  31. /**
  32.  * GestoreGeneral
  33.  *
  34.  * @author Andrea Poli (apoli@link.it)
  35.  * @author Stefano Corallo (corallo@link.it)
  36.  * @author Sandra Giangrandi (sandra@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  *
  40.  */
  41. public abstract class GestoreGeneral implements IGestore, Runnable {

  42.     /**
  43.      * Filtra le operation con stesso identificativo, settando lo stato a
  44.      * SUCCESS
  45.      *
  46.      * @param idOperation
  47.      *            l'identificativo delle operation da filtrare
  48.      * @param operationManager
  49.      *            la libreria per la scrittura nel db operation
  50.      * @param originalOperation
  51.      *            l'operazione orginale che funge da filtro per le altre
  52.      * @throws Exception
  53.      */
  54.     public List<Operation> filterOperations(String idOperazione, ClassQueue operationManager, Operation originalOperation, QueueSession qs, Queue queue, Logger log) throws Exception {

  55.         List<Operation> filteredOperations = new ArrayList<Operation>();

  56.         // bug fix: il filtro puo contenere caratteri ' ma devono essere
  57.         // preceduti da un '
  58.         // ad esempio: "literal's" deve essere trasformato in "literal''s
  59.         // escaping filter
  60.         String escapedFilter = idOperazione.replaceAll("'", "''");
  61.         String strMessageSelector = "ID = '" + escapedFilter + "'";
  62.        
  63.         QueueReceiver filtro = null;
  64.         try {
  65.             filtro = qs.createReceiver(queue, strMessageSelector);
  66.             ObjectMessage operazioneFiltrata = (ObjectMessage) filtro.receive(CostantiControlStation.JMS_FILTER);

  67.             while (operazioneFiltrata != null) {
  68.                
  69.                 // Ricezione Operazione
  70.                 Object objFilter;
  71.                 try {
  72.                     objFilter = operazioneFiltrata.getObject();
  73.                 } catch (Exception e) {
  74.                     log.error(getName() + ": Impossibile recuperare il messaggio :" + e.toString(), e);
  75.                     continue;
  76.                 }

  77.                 int idOpFiltrata = Integer.parseInt(objFilter.toString());
  78.                 if (idOpFiltrata == 0) {
  79.                     log.error(getName() + ": Impossibile recuperare l'id del messaggio da filtrare.");
  80.                     continue;
  81.                 }

  82.                 Operation operazioneDaFiltrare = operationManager.getOperation(idOpFiltrata);

  83.                 operationManager.filterOperation(operazioneDaFiltrare, originalOperation);

  84.                 filteredOperations.add(operazioneDaFiltrare);
  85.                
  86.                 // ricevo altra operazione duplicata se presente
  87.                 operazioneFiltrata = (ObjectMessage) filtro.receive(CostantiControlStation.JMS_FILTER);
  88.             }
  89.            
  90.             return filteredOperations;
  91.         } catch (Exception e) {
  92.             throw e;
  93.         } finally {
  94.             try {
  95.                 if (filtro != null)
  96.                     filtro.close();
  97.             } catch (Exception e) {
  98.             }
  99.         }
  100.     }
  101.    
  102.     /**
  103.      * Nome del Gestore
  104.      *
  105.      * @return Il nome del Gestore
  106.      */
  107.     protected abstract String getName();

  108. }