X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.api.uuid%2Fsrc%2Forg%2Fargeo%2Fapi%2Fuuid%2FMacAddressUuidFactory.java;h=6cbe98178123bb59b7a4342a87efdd7a260869f9;hb=b95462873703848193e56fcbe997693630db6121;hp=fa68df1dbe2777328a87b008e932039b23e56a5b;hpb=0f8bddc982e2ea4f260e1eb639777b74e7893ea3;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.api.uuid/src/org/argeo/api/uuid/MacAddressUuidFactory.java b/org.argeo.api.uuid/src/org/argeo/api/uuid/MacAddressUuidFactory.java index fa68df1db..6cbe98178 100644 --- a/org.argeo.api.uuid/src/org/argeo/api/uuid/MacAddressUuidFactory.java +++ b/org.argeo.api.uuid/src/org/argeo/api/uuid/MacAddressUuidFactory.java @@ -4,45 +4,68 @@ import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; +import java.util.Enumeration; import java.util.UUID; /** * An {@link UUID} factory whose node id (for time based UUIDs) is the hardware * MAC address as specified in RFC4122. * - * @see https://datatracker.ietf.org/doc/html/rfc4122.html#section-4.1.6 + * @see "https://datatracker.ietf.org/doc/html/rfc4122.html#section-4.1.6" */ public class MacAddressUuidFactory extends ConcurrentUuidFactory { - public final static UuidFactory DEFAULT = new MacAddressUuidFactory(); - public MacAddressUuidFactory() { - this(0); + this(0, localHardwareAddressAsNodeId()); } public MacAddressUuidFactory(long initialClockRange) { - super(initialClockRange, localHardwareAddressAsNodeId()); + this(initialClockRange, localHardwareAddressAsNodeId()); + } + + public MacAddressUuidFactory(byte[] hardwareAddress) { + this(0, hardwareAddress); } - public static byte[] localHardwareAddressAsNodeId() { + public MacAddressUuidFactory(long initialClockRange, byte[] hardwareAddress) { + super(initialClockRange, hardwareAddress); + } + + private static byte[] localHardwareAddressAsNodeId() { InetAddress localHost; try { localHost = InetAddress.getLocalHost(); NetworkInterface nic = NetworkInterface.getByInetAddress(localHost); - return hardwareAddressToNodeId(nic); + if (nic != null && nic.getHardwareAddress() != null) + return hardwareAddressToNodeId(nic); + Enumeration netInterfaces = null; + netInterfaces = NetworkInterface.getNetworkInterfaces(); + if (netInterfaces == null || !netInterfaces.hasMoreElements()) + throw new IllegalStateException("No interfaces"); + while (netInterfaces.hasMoreElements()) { + // TODO find out public/physical interfaces + nic = netInterfaces.nextElement(); + if (nic.getHardwareAddress() != null) + return hardwareAddressToNodeId(nic); + } + throw new IllegalStateException("No interfaces with a MAC address"); } catch (UnknownHostException | SocketException e) { throw new IllegalStateException(e); } } - public static byte[] hardwareAddressToNodeId(NetworkInterface nic) throws SocketException { - byte[] hardwareAddress = nic.getHardwareAddress(); - final int length = 6; - byte[] arr = new byte[length]; - for (int i = 0; i < length; i++) { - arr[i] = hardwareAddress[length - 1 - i]; + public static byte[] hardwareAddressToNodeId(NetworkInterface nic) throws IllegalStateException { + try { + byte[] hardwareAddress = nic.getHardwareAddress(); + final int length = 6; + byte[] arr = new byte[length]; + for (int i = 0; i < length; i++) { + arr[i] = hardwareAddress[length - 1 - i]; + } + return arr; + } catch (SocketException e) { + throw new IllegalStateException("Cannot retrieve hardware address from NIC", e); } - return arr; } }