UniversallyUniqueIdentifierV1Generator.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.net.NetworkInterface;
  22. import java.util.Enumeration;
  23. import java.util.UUID;

  24. import com.fasterxml.uuid.EthernetAddress;
  25. import com.fasterxml.uuid.Generators;
  26. import com.fasterxml.uuid.impl.TimeBasedGenerator;

  27. /**
  28.  * Implementazione tramite com.fasterxml.uuid.impl.TimeBasedGenerator;
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */

  34. public class UniversallyUniqueIdentifierV1Generator extends AbstractUniversallyUniqueIdentifierGenerator{

  35. // Version 1: This generates a unique ID based on a network card MAC address and a timer.
  36. // These IDs are easy to predict (given one, I might be able to guess another one) and can be traced back to your network card. It's not recommended to create these.
  37. //  UUIDv1 is NOT secure. It isn't meant to be. It is meant to be UNIQUE, not un-guessable. UUIDv1 uses the current timestamp, plus a machine identifier,
  38. //  plus some random-ish stuff to make a number that will never be generated by that algorithm again.
  39. //  This is appropriate for a transaction ID (even if everyone is doing millions of transactions/s).

  40.     // Generators are fully thread-safe, so a single instance may be shared among multiple threads.
  41.     private TimeBasedGenerator uuidv1 = Generators.timeBasedGenerator();

  42.     @Override
  43.     public void init(Object... o) throws UniqueIdentifierException {
  44.         if(o!=null && o.length>0 && o[0]!=null) {
  45.             EthernetAddress ethAddr = null;
  46.             if(o[0] instanceof EthernetAddress) {
  47.                 ethAddr = (EthernetAddress) o[0];
  48.             }
  49.             else if(o[0] instanceof String) {
  50.                 String macAddress = (String) o[0];
  51.                 ethAddr = new EthernetAddress(macAddress);
  52.             }
  53.             else {
  54.                 throw new UniqueIdentifierException("Unknown mac address type ("+o[0].getClass().getName()+")");
  55.             }
  56.             //System.out.println("PARAM '"+ethAddr+"'");
  57.             this.uuidv1 = Generators.timeBasedGenerator(ethAddr);
  58.         }
  59.         else {
  60.             // Voglio selezionare una scheda di rete sulla mia macchina
  61.             // Altrimenti il metodo che genera senza passargli nulla, genera un mac address casuale:
  62.             /*Factory method that can be used to construct a random multicast
  63.              * address; to be used in cases where there is no "real" ethernet
  64.              * address to use. Address to generate should be a multicase address
  65.              * to avoid accidental collision with real manufacturer-assigned
  66.              * MAC addresses. */
  67.            
  68.             try {
  69.                 EthernetAddress ethAddrFounded = null;
  70.                 Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
  71.                 while (enumNetworkInterfaces.hasMoreElements()) {
  72.                     NetworkInterface networkInterface = (NetworkInterface) enumNetworkInterfaces.nextElement();
  73.                     //System.out.println("\n============");
  74.                     //System.out.println("Network: "+networkInterface.getDisplayName());
  75.                     if(networkInterface.getHardwareAddress()!=null) {
  76.                         EthernetAddress ethAddr = new EthernetAddress(networkInterface.getHardwareAddress());
  77.                         if(ethAddrFounded==null) {
  78.                             ethAddrFounded = ethAddr;
  79.                         }
  80.                         //System.out.println("ethAddr: "+ethAddr.toString());
  81.                     }
  82. //                  Enumeration<InetAddress> enumInetAddresses = networkInterface.getInetAddresses();
  83. //                  while (enumInetAddresses.hasMoreElements()) {
  84. //                      InetAddress inetAddress = (InetAddress) enumInetAddresses.nextElement();
  85. //                      System.out.println("getInetAddresses: "+inetAddress);      
  86. //                  }
  87.                 }
  88.                 if(ethAddrFounded!=null) {
  89.                     //System.out.println("DYNAMIC '"+ethAddrFounded+"'");
  90.                     this.uuidv1 = Generators.timeBasedGenerator(ethAddrFounded);
  91.                 }
  92.                 else {
  93.                     throw new Exception("NetworkInterface with mac address not found");
  94.                 }
  95.             }catch(Throwable e) {
  96.                 throw new UniqueIdentifierException("MacAddress identification failed: "+e.getMessage(),e);
  97.             }
  98.             //initUUIDProducer( this.uuidv1 );
  99.             //this.uuidKey = this.uuidv1.getClass().getName();
  100.         }
  101.     }

  102.     @Override
  103.     protected UUID generateUUID() {
  104.         return this.uuidv1.generate();
  105.     }

  106. }