X-Git-Url: https://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=blobdiff_plain;f=org.argeo.api.uuid%2Fsrc%2Forg%2Fargeo%2Fapi%2Fuuid%2FMacAddressUuidFactory.java;fp=org.argeo.api.uuid%2Fsrc%2Forg%2Fargeo%2Fapi%2Fuuid%2FMacAddressUuidFactory.java;h=6cbe98178123bb59b7a4342a87efdd7a260869f9;hp=31fe3783112661e23f28074d31669870c0db0d42;hb=b95462873703848193e56fcbe997693630db6121;hpb=55d88fba80cec198a0f11ba7545e19878c51fc5e 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 31fe37831..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 @@ -35,24 +35,26 @@ public class MacAddressUuidFactory extends ConcurrentUuidFactory { try { localHost = InetAddress.getLocalHost(); NetworkInterface nic = NetworkInterface.getByInetAddress(localHost); - if (nic != null) + if (nic != null && nic.getHardwareAddress() != null) return hardwareAddressToNodeId(nic); Enumeration netInterfaces = null; - try { - netInterfaces = NetworkInterface.getNetworkInterfaces(); - } catch (SocketException e) { - throw new IllegalStateException(e); - } + netInterfaces = NetworkInterface.getNetworkInterfaces(); if (netInterfaces == null || !netInterfaces.hasMoreElements()) throw new IllegalStateException("No interfaces"); - return hardwareAddressToNodeId(netInterfaces.nextElement()); + 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) { + public static byte[] hardwareAddressToNodeId(NetworkInterface nic) throws IllegalStateException { try { byte[] hardwareAddress = nic.getHardwareAddress(); final int length = 6;