GeneratoreCasualeDate.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.pdd.core.handlers;

  21. import java.security.SecureRandom;
  22. import java.util.Date;
  23. import java.util.HashMap;
  24. import java.util.Map;

  25. import org.slf4j.Logger;

  26. /**
  27.  * GeneratoreCasualeDate
  28.  *
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class GeneratoreCasualeDate {

  35.     private static GeneratoreCasualeDate generatore = null;
  36.     private static long sogliaMsMin = 1;
  37.     private static long sogliaMsMax = 20;

  38.     public static synchronized void init(Date intervalloInizio,Date intervalloFine,Logger log){
  39.         if(GeneratoreCasualeDate.generatore==null){
  40.             GeneratoreCasualeDate.generatore = new GeneratoreCasualeDate(intervalloInizio,intervalloFine,log);
  41.         }
  42.     }

  43.     public static GeneratoreCasualeDate getGeneratoreCasualeDate(){
  44.         // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  45.         if (GeneratoreCasualeDate.generatore == null) {
  46.             synchronized (GeneratoreCasualeDate.class) {
  47.                 if (GeneratoreCasualeDate.generatore == null) {
  48.                     return null;
  49.                 }
  50.             }
  51.         }
  52.         return GeneratoreCasualeDate.generatore;
  53.     }
  54.    
  55.     private static java.util.Random randomEngine = null;
  56.     private static synchronized void initRandom() {
  57.         if(randomEngine==null) {
  58.             randomEngine = new SecureRandom();
  59.         }
  60.     }
  61.     public static java.util.Random getRandom() {
  62.         if(randomEngine==null) {
  63.             initRandom();
  64.         }
  65.         return randomEngine;
  66.     }
  67.    
  68.     private static long getRandom(long inizio, long fine){
  69.         return inizio +
  70.             Math.round( getRandom().nextDouble() * (fine - inizio) );
  71.     }
  72.    

  73.     /* ------- Istanza ---------- */
  74.    
  75.     private Date intervalloInizio = null;
  76.     private Date intervalloFine = null;
  77.     private GeneratoreCasualeDate(Date intervalloInizio,Date intervalloFine,Logger log){
  78.         this.intervalloInizio = intervalloInizio;
  79.         this.intervalloFine = intervalloFine;
  80.         String msg = "Generatore di date causauli dei log attivato con intervallo ["+this.intervalloInizio+"] - ["+this.intervalloFine+"]";
  81.         log.info(msg);
  82.     }
  83.    
  84.     private Map<String, Date>  dateGenerate = new HashMap<>();
  85.    
  86.     private Date getNextDate(String idCluster){
  87.         Date nextDate = null;
  88.         if(this.dateGenerate.containsKey(idCluster)){
  89.             Date data = this.dateGenerate.remove(idCluster);
  90.             nextDate = new Date(data.getTime()+GeneratoreCasualeDate.getRandom(GeneratoreCasualeDate.sogliaMsMin, GeneratoreCasualeDate.sogliaMsMax));
  91.         }
  92.         else{
  93.             nextDate = new Date(GeneratoreCasualeDate.getRandom(this.intervalloInizio.getTime(),this.intervalloFine.getTime()));    
  94.         }      
  95.         this.dateGenerate.put(idCluster, nextDate);
  96.         return nextDate;
  97.     }
  98.    
  99.     public Date getProssimaData(String idCluster){
  100.         return getNextDate(idCluster);  
  101.     }
  102.    
  103.     public void releaseResource(String idCluster){
  104.         this.dateGenerate.remove(idCluster);
  105.     }
  106.    
  107. }