IDUtilities.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.utils.id;

  21. import java.security.SecureRandom;
  22. import java.util.Map;
  23. import java.util.concurrent.ConcurrentHashMap;

  24. import org.openspcoop2.utils.SemaphoreLock;
  25. import org.openspcoop2.utils.Utilities;
  26. import org.openspcoop2.utils.date.DateEngineType;
  27. import org.openspcoop2.utils.date.DateManager;
  28. import org.openspcoop2.utils.date.DateUtils;

  29. /**
  30.  * Identity
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class IDUtilities {

  37.     private static long uniqueSerialNumber = 0;
  38.     private static org.openspcoop2.utils.Semaphore semaphore_getUniqueSerialNumber = new org.openspcoop2.utils.Semaphore("IDUtilities.getUniqueSerialNumber");
  39.     public static long getUniqueSerialNumber(){
  40.         return getUniqueSerialNumber("anonymous", null);
  41.     }
  42.     public static long getUniqueSerialNumber(String methodName){
  43.         return getUniqueSerialNumber(methodName, null);
  44.     }
  45.     public static long getUniqueSerialNumber(String methodName, String idTransazione){
  46.         SemaphoreLock lock = semaphore_getUniqueSerialNumber.acquireThrowRuntime(methodName, idTransazione);
  47.         try {
  48.            
  49.             if((IDUtilities.uniqueSerialNumber+1) > Long.MAX_VALUE){
  50.                 IDUtilities.uniqueSerialNumber = 0;
  51.             }
  52.             IDUtilities.uniqueSerialNumber++;
  53.             return IDUtilities.uniqueSerialNumber;
  54.            
  55.         }finally {
  56.             semaphore_getUniqueSerialNumber.release(lock, methodName, idTransazione);
  57.         }
  58.     }
  59.    
  60.     private static final char[] symbols;
  61.     static {
  62.         StringBuilder tmp = new StringBuilder();
  63.         for (char ch = '0'; ch <= '9'; ++ch)
  64.           tmp.append(ch);
  65.         for (char ch = 'a'; ch <= 'z'; ++ch)
  66.           tmp.append(ch);
  67.         symbols = tmp.toString().toCharArray();
  68.     }  
  69.     private static final SecureRandom random = new SecureRandom();
  70.     private static final Map<String, char[]> mapRandom = new ConcurrentHashMap<String, char[]>();
  71.     private static synchronized char[] getBufferForRandom(int length){
  72.         String key = length+"";
  73.         if(IDUtilities.mapRandom.containsKey(key)){
  74.             return IDUtilities.mapRandom.get(key);
  75.         }
  76.         else{
  77.             char[] buf = new char[length];
  78.             IDUtilities.mapRandom.put(key, buf);
  79.             return buf;
  80.         }
  81.     }
  82.     public static String generateAlphaNumericRandomString(int length) {
  83.         char[] buf = IDUtilities.getBufferForRandom(length);
  84.         for (int idx = 0; idx < buf.length; ++idx)
  85.             buf[idx] = IDUtilities.symbols[IDUtilities.random.nextInt(IDUtilities.symbols.length)];
  86.         return new String(buf);
  87.     }
  88.    
  89.     private static org.openspcoop2.utils.Semaphore semaphore_generateDateTime = new org.openspcoop2.utils.Semaphore("IDUtilities.generateDateTime");
  90.     public static String generateDateTime(String format, int syncMs) {
  91.         return IDUtilities.generateDateTime(DateUtils.getDEFAULT_DATA_ENGINE_TYPE(), format, syncMs);
  92.     }
  93.     public static String generateDateTime(DateEngineType type, String format, int syncMs) {
  94.         SemaphoreLock lock = semaphore_generateDateTime.acquireThrowRuntime("generateDateTime");
  95.         try {
  96.             Utilities.sleep(syncMs);
  97.             return DateUtils.getTimeFormatter(type, format).format(DateManager.getDate());
  98.         }finally {
  99.             semaphore_generateDateTime.release(lock, "generateDateTime");
  100.         }
  101.     }
  102.     public static String generateDateTime_ISO_8601_TZ(String format, int syncMs) {
  103.         return IDUtilities.generateDateTime_ISO_8601_TZ(DateUtils.getDEFAULT_DATA_ENGINE_TYPE(), format, syncMs);
  104.     }
  105.     public static String generateDateTime_ISO_8601_TZ(DateEngineType type, String format, int syncMs) {
  106.         SemaphoreLock lock = semaphore_generateDateTime.acquireThrowRuntime("generateDateTime_ISO_8601_TZ");
  107.         try {
  108.             Utilities.sleep(syncMs);
  109.             return DateUtils.getDateTimeFormatter_ISO_8601_TZ(type, format).format(DateManager.getDate());
  110.         }finally {
  111.             semaphore_generateDateTime.release(lock, "generateDateTime_ISO_8601_TZ");
  112.         }
  113.     }
  114. }