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