]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsUuidFactory.java
357a0102f432f639058ce4deff06c546e5be3625
[lgpl/argeo-commons.git] / CmsUuidFactory.java
1 package org.argeo.cms.acr;
2
3 import java.net.Inet4Address;
4 import java.net.Inet6Address;
5 import java.net.InetAddress;
6 import java.net.InterfaceAddress;
7 import java.net.NetworkInterface;
8 import java.net.SocketException;
9 import java.util.BitSet;
10 import java.util.Enumeration;
11
12 import org.argeo.api.cms.CmsLog;
13 import org.argeo.api.uuid.ConcurrentUuidFactory;
14 import org.argeo.api.uuid.NodeIdSupplier;
15 import org.argeo.api.uuid.UuidBinaryUtils;
16
17 public class CmsUuidFactory extends ConcurrentUuidFactory {
18 private final static CmsLog log = CmsLog.getLog(CmsUuidFactory.class);
19
20 public CmsUuidFactory(byte[] nodeId) {
21 super(0, nodeId);
22 assert createTimeUUID().node() == BitSet.valueOf(toNodeIdBytes(nodeId, 0)).toLongArray()[0];
23 }
24
25 public CmsUuidFactory() {
26 this(getIpBytes());
27 }
28
29 /** Returns an SHA1 digest of one of the IP addresses. */
30 protected static byte[] getIpBytes() {
31 Enumeration<NetworkInterface> netInterfaces = null;
32 try {
33 netInterfaces = NetworkInterface.getNetworkInterfaces();
34 } catch (SocketException e) {
35 throw new IllegalStateException(e);
36 }
37 if (netInterfaces == null)
38 throw new IllegalStateException("No interfaces");
39
40 InetAddress selectedIpv6 = null;
41 InetAddress selectedIpv4 = null;
42 netInterfaces: while (netInterfaces.hasMoreElements()) {
43 NetworkInterface netInterface = netInterfaces.nextElement();
44 byte[] hardwareAddress = null;
45 try {
46 hardwareAddress = netInterface.getHardwareAddress();
47 if (hardwareAddress != null) {
48 // first IPv6
49 addr: for (InterfaceAddress addr : netInterface.getInterfaceAddresses()) {
50 InetAddress ip = addr.getAddress();
51 if (ip instanceof Inet6Address) {
52 Inet6Address ipv6 = (Inet6Address) ip;
53 if (ipv6.isAnyLocalAddress() || ipv6.isLinkLocalAddress() || ipv6.isLoopbackAddress())
54 continue addr;
55 selectedIpv6 = ipv6;
56 break netInterfaces;
57 }
58
59 }
60 // then IPv4
61 addr: for (InterfaceAddress addr : netInterface.getInterfaceAddresses()) {
62 InetAddress ip = addr.getAddress();
63 if (ip instanceof Inet4Address) {
64 Inet4Address ipv4 = (Inet4Address) ip;
65 if (ipv4.isAnyLocalAddress() || ipv4.isLinkLocalAddress() || ipv4.isLoopbackAddress())
66 continue addr;
67 selectedIpv4 = ipv4;
68 }
69
70 }
71 }
72 } catch (SocketException e) {
73 throw new IllegalStateException(e);
74 }
75 }
76 InetAddress selectedIp = selectedIpv6 != null ? selectedIpv6 : selectedIpv4;
77 if (selectedIp == null) {
78 log.warn("No IP address found, using a random node id for UUID generation");
79 return NodeIdSupplier.randomNodeId();
80 }
81 byte[] digest = sha1(selectedIp.getAddress());
82 log.info("Use IP " + selectedIp + " hashed as " + UuidBinaryUtils.toHexString(digest) + " as node id");
83 byte[] nodeId = toNodeIdBytes(digest, 0);
84 // marks that this is not based on MAC address
85 forceToNoMacAddress(nodeId, 0);
86 return nodeId;
87 }
88
89 }