- setNodeIdSupplier(() -> {
- byte[] hardwareAddress = getHardwareAddress();
- byte[] macAddressNodeId = toNodeIdBytes(hardwareAddress, 0);
- long nodeIdBase = NodeIdSupplier.toNodeIdBase(macAddressNodeId);
- return nodeIdBase;
- });
+ this(0, localHardwareAddressAsNodeId());
+ }
+
+ public MacAddressUuidFactory(long initialClockRange) {
+ this(initialClockRange, localHardwareAddressAsNodeId());
+ }
+
+ public MacAddressUuidFactory(byte[] hardwareAddress) {
+ this(0, hardwareAddress);
+ }
+
+ public MacAddressUuidFactory(long initialClockRange, byte[] hardwareAddress) {
+ super(initialClockRange, hardwareAddress);
+ }
+
+ private static byte[] localHardwareAddressAsNodeId() {
+ InetAddress localHost;
+ try {
+ localHost = InetAddress.getLocalHost();
+ NetworkInterface nic = NetworkInterface.getByInetAddress(localHost);
+ if (nic != null)
+ return hardwareAddressToNodeId(nic);
+ Enumeration<NetworkInterface> netInterfaces = null;
+ try {
+ netInterfaces = NetworkInterface.getNetworkInterfaces();
+ } catch (SocketException e) {
+ throw new IllegalStateException(e);
+ }
+ if (netInterfaces == null || !netInterfaces.hasMoreElements())
+ throw new IllegalStateException("No interfaces");
+ return hardwareAddressToNodeId(netInterfaces.nextElement());
+ } catch (UnknownHostException | SocketException e) {
+ throw new IllegalStateException(e);
+ }
+
+ }
+
+ public static byte[] hardwareAddressToNodeId(NetworkInterface nic) {
+ 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);
+ }