UniversallyUniqueIdentifierV1Generator.java

/*
 * GovWay - A customizable API Gateway 
 * https://govway.org
 * 
 * Copyright (c) 2005-2024 Link.it srl (https://link.it). 
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


package org.openspcoop2.utils.id;

import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.UUID;

import com.fasterxml.uuid.EthernetAddress;
import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.impl.TimeBasedGenerator;

/**
 * Implementazione tramite com.fasterxml.uuid.impl.TimeBasedGenerator;
 *
 * @author Poli Andrea (apoli@link.it)
 * @author $Author$
 * @version $Rev$, $Date$
 */

public class UniversallyUniqueIdentifierV1Generator extends AbstractUniversallyUniqueIdentifierGenerator{

// 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.
//	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, 
//	plus some random-ish stuff to make a number that will never be generated by that algorithm again. 
//	This is appropriate for a transaction ID (even if everyone is doing millions of transactions/s).

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

	@Override
	public void init(Object... o) throws UniqueIdentifierException {
		if(o!=null && o.length>0 && o[0]!=null) {
			EthernetAddress ethAddr = null;
			if(o[0] instanceof EthernetAddress) {
				ethAddr = (EthernetAddress) o[0];
			}
			else if(o[0] instanceof String) {
				String macAddress = (String) o[0];
				ethAddr = new EthernetAddress(macAddress);
			}
			else {
				throw new UniqueIdentifierException("Unknown mac address type ("+o[0].getClass().getName()+")");
			}
			//System.out.println("PARAM '"+ethAddr+"'");
			this.uuidv1 = Generators.timeBasedGenerator(ethAddr);
		}
		else {
			// Voglio selezionare una scheda di rete sulla mia macchina
			// Altrimenti il metodo che genera senza passargli nulla, genera un mac address casuale:
			/*Factory method that can be used to construct a random multicast
		     * address; to be used in cases where there is no "real" ethernet
		     * address to use. Address to generate should be a multicase address
		     * to avoid accidental collision with real manufacturer-assigned
		     * MAC addresses. */
			
			try {
				EthernetAddress ethAddrFounded = null;
				Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
				while (enumNetworkInterfaces.hasMoreElements()) {
					NetworkInterface networkInterface = (NetworkInterface) enumNetworkInterfaces.nextElement();
					//System.out.println("\n============");
					//System.out.println("Network: "+networkInterface.getDisplayName());
					if(networkInterface.getHardwareAddress()!=null) {
						EthernetAddress ethAddr = new EthernetAddress(networkInterface.getHardwareAddress());
						if(ethAddrFounded==null) {
							ethAddrFounded = ethAddr;
						}
						//System.out.println("ethAddr: "+ethAddr.toString());
					}
//					Enumeration<InetAddress> enumInetAddresses = networkInterface.getInetAddresses();
//					while (enumInetAddresses.hasMoreElements()) {
//						InetAddress inetAddress = (InetAddress) enumInetAddresses.nextElement();
//						System.out.println("getInetAddresses: "+inetAddress);		
//					}
				}
				if(ethAddrFounded!=null) {
					//System.out.println("DYNAMIC '"+ethAddrFounded+"'");
					this.uuidv1 = Generators.timeBasedGenerator(ethAddrFounded);
				}
				else {
					throw new Exception("NetworkInterface with mac address not found");
				}
			}catch(Throwable e) {
				throw new UniqueIdentifierException("MacAddress identification failed: "+e.getMessage(),e);
			}
			//initUUIDProducer( this.uuidv1 );
			//this.uuidKey = this.uuidv1.getClass().getName();
		}
	}

	@Override
	protected UUID generateUUID() {
		return this.uuidv1.generate();
	}

}