SPCoopStaticCounter.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.protocol.spcoop.builder;

  21. import java.util.Date;
  22. import java.util.HashMap;
  23. import java.util.Map;

  24. import org.openspcoop2.protocol.sdk.ProtocolException;

  25. /**
  26.  * Classe che contiene l'informazione sul contatore
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class SPCoopStaticCounter {

  33.     private static Map<String, SPCoopStaticCounter> mapCounter = new HashMap<String, SPCoopStaticCounter>();
  34.     private static synchronized void initCounter(Date now, String formatLastDate, String key) {
  35.         if(!mapCounter.containsKey(key)) {
  36.             mapCounter.put(key, new SPCoopStaticCounter(now, formatLastDate, key));
  37.         }
  38.     }
  39.     public static int getNextSerialCounter(Date now, String formatLastDate, String codAmm, String idPdd) throws ProtocolException {
  40.         String key = codAmm+"_"+idPdd;
  41.         if(!mapCounter.containsKey(key)) {
  42.             initCounter(now, formatLastDate, key);
  43.         }
  44.         return mapCounter.get(key).getNextSerialCounter(now, formatLastDate);
  45.     }
  46.    
  47.     // Usato nel tipo di identificativo static
  48.     /** Contatore seriale */
  49.     @SuppressWarnings("unused")
  50.     private String identificativo;
  51.     private int serialCounter;
  52.     @SuppressWarnings("unused")
  53.     private Date lastDate; // serve per azzerare il contatore ogni minuto
  54.     private String formatLastDate;
  55.    
  56.     public SPCoopStaticCounter(Date now, String formatLastDate, String identificativo) {
  57.         this.identificativo = identificativo;
  58.         this.serialCounter = 0;
  59.         this.lastDate = now;
  60.         this.formatLastDate = formatLastDate;
  61.     }
  62.    
  63.     public synchronized int getNextSerialCounter(Date now, String formatNow) throws ProtocolException{

  64.         //System.out.println("["+this.identificativo+"] getNextSerialCounter ...");
  65.        
  66.         try {
  67.            
  68.             /*
  69.             long millisecondiTrascorsi = now.getTime() - this.lastDate.getTime();
  70.             long secondiTrascorsi = 0;
  71.             if(millisecondiTrascorsi>999) {
  72.                 secondiTrascorsi = millisecondiTrascorsi/1000;
  73.             }
  74.             if(secondiTrascorsi>60) {
  75.                 //System.out.println("AZZERO PER 60 SECONDI PASSATI! ["+this.identificativo+"]");
  76.             */
  77.             // FIX: il controllo precedente creava identificativi uguali
  78.             if(!this.formatLastDate.equals(formatNow)) {
  79.                 //System.out.println("AZZERO PERCHE SIAMO NEL PROSSIMO MINUTO ["+this.identificativo+"]");
  80.                 this.serialCounter = 0;
  81.                 this.lastDate = now;
  82.                 this.formatLastDate = formatNow;
  83.             }
  84.                    
  85.             if((this.serialCounter+1) > SPCoopImbustamento.maxSeriale){
  86.                 throw new ProtocolException("Numero massimo del seriale ("+SPCoopImbustamento.maxSeriale+") associabile all'identificato nel minuto raggiunto");
  87.             }
  88.            
  89.             this.serialCounter=this.serialCounter+1;
  90.             return this.serialCounter;
  91.         }finally {
  92.             //System.out.println("["+this.identificativo+"] getNextSerialCounter: "+this.serialCounter);
  93.         }
  94.     }
  95.    
  96. }