UniversallyUniqueIdentifier.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.util.UUID;


  22. /**
  23.  * UniversallyUniqueIdentifier
  24.  *
  25.  * @author Poli Andrea (apoli@link.it)
  26.  * @author $Author$
  27.  * @version $Rev$, $Date$
  28.  */
  29. public class UniversallyUniqueIdentifier implements IUniqueIdentifier {
  30.    
  31. //  There are two different ways of generating a UUID.
  32. //
  33. //  If you just need a unique ID, you want a version 1 or version 4.
  34. //
  35. //      Version 1: This generates a unique ID based on a network card MAC address and a timer. 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.
  36. //
  37. //      Version 4: These are generated from random (or pseudo-random) numbers. If you just need to generate a UUID, this is probably what you want.
  38. //
  39. //  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,
  40. //  plus some random-ish stuff to make a number that will never be generated by that algorithm again.
  41. //  This is appropriate for a transaction ID (even if everyone is doing millions of transactions/s).
  42. //  UUIDv4 from reading RFC4122, it looks like that version does NOT eliminate possibility of collisions.
  43. //  It is just a random number generator. If that is true, than you have a very GOOD chance of two machines in the world eventually creating the same "UUID"v4
  44. //  (quotes because there isn't a mechanism for guaranteeing U.niversal U.niqueness).
  45. //  In that situation, I don't think that algorithm belongs in a RFC describing methods for generating unique values. It would belong in a RFC about generating randomness.
  46. //  For a set of random numbers:
  47. //
  48. //  Riassunto:
  49. //
  50. //  Version 1 is based on MAC address and current time ("space and time"). Much less likely to have collisions than Version 4.
  51. //
  52. //  Version 4 is based on entirely being generated from random numbers using a cryptographically strong random generator.
  53. //
  54. //  The Oracle JVM does not provide a Version 1 generator, apparently because of security and privacy concerns.
  55. //  The JVM does not provide access to the MAC address of host machine.

  56.    
  57.     /**
  58.      *
  59.      */
  60.     private static final long serialVersionUID = 1L;
  61.    
  62.     private UUID uuid;
  63.    
  64.     public UUID getUuid() {
  65.         return this.uuid;
  66.     }

  67.     public void setUuid(UUID uuid) {
  68.         this.uuid = uuid;
  69.     }

  70.     @Override
  71.     public String getAsString() {
  72.         return this.uuid.toString();
  73.     }
  74.        
  75.     @Override
  76.     public String toString(){
  77.         return this.getAsString();
  78.     }
  79. }