]> git.argeo.org Git - lgpl/argeo-commons.git/blob - api/uuid/SimpleUuidFactory.java
Prepare next development cycle
[lgpl/argeo-commons.git] / api / uuid / SimpleUuidFactory.java
1 package org.argeo.api.uuid;
2
3 import static java.lang.System.Logger.Level.DEBUG;
4 import static java.lang.System.Logger.Level.WARNING;
5
6 import java.lang.System.Logger;
7 import java.security.DrbgParameters;
8 import java.security.NoSuchAlgorithmException;
9 import java.security.SecureRandom;
10 import java.time.Clock;
11 import java.util.UUID;
12
13 /**
14 * Simple implementation of an {@link UuidFactory}, which can be used as a base
15 * class for more optimised implementations.
16 *
17 * @see https://datatracker.ietf.org/doc/html/rfc4122
18 */
19 public class SimpleUuidFactory extends AbstractAsyncUuidFactory {
20 private final static Logger logger = System.getLogger(SimpleUuidFactory.class.getName());
21 public final static UuidFactory DEFAULT = new SimpleUuidFactory(null, -1, null);
22
23 // private NodeId macAddressNodeId;
24 // private NodeId defaultNodeId;
25 private byte[] macAddressNodeId;
26 private byte[] defaultNodeId;
27
28 public SimpleUuidFactory(byte[] nodeId, int offset, Clock clock) {
29 byte[] hardwareAddress = getHardwareAddress();
30 // macAddressNodeId = hardwareAddress != null ? new NodeId(hardwareAddress, 0) : null;
31 macAddressNodeId = toNodeId(hardwareAddress, 0);
32
33 // defaultNodeId = nodeId != null ? new NodeId(nodeId, offset) : macAddressNodeId;
34 defaultNodeId = nodeId != null ? toNodeId(nodeId, offset) : toNodeId(macAddressNodeId, 0);
35 if (defaultNodeId == null)
36 throw new IllegalStateException("No default node id specified");
37 }
38
39 @Override
40 protected SecureRandom newSecureRandom() {
41 SecureRandom secureRandom;
42 try {
43 secureRandom = SecureRandom.getInstance("DRBG",
44 DrbgParameters.instantiation(256, DrbgParameters.Capability.PR_AND_RESEED, "UUID".getBytes()));
45 } catch (NoSuchAlgorithmException e) {
46 try {
47 logger.log(DEBUG, "DRBG secure random not found, using strong");
48 secureRandom = SecureRandom.getInstanceStrong();
49 } catch (NoSuchAlgorithmException e1) {
50 logger.log(WARNING, "No strong secure random was found, using default");
51 secureRandom = new SecureRandom();
52 }
53 }
54 return secureRandom;
55 }
56
57 /*
58 * TIME-BASED (version 1)
59 */
60
61 @Override
62 public UUID newTimeUUIDwithMacAddress() {
63 if (macAddressNodeId == null)
64 throw new UnsupportedOperationException("No MAC address is available");
65 return newTimeUUID(timeUuidState.useTimestamp(), timeUuidState.getClockSequence(), macAddressNodeId, 0);
66 }
67
68 @Override
69 public UUID newTimeUUID() {
70 return newTimeUUID(timeUuidState.useTimestamp(), timeUuidState.getClockSequence(), defaultNodeId, 0);
71 }
72
73 /*
74 * RANDOM v4
75 */
76 // @Override
77 // public UUID randomUUID(Random random) {
78 // return newRandomUUID(random);
79 // }
80
81 // @Override
82 // public UUID randomUUID() {
83 // return randomUUID(secureRandom);
84 // }
85 //
86 // static class NodeId extends ThreadLocal<byte[]> {
87 // private byte[] source;
88 // private int offset;
89 //
90 // public NodeId(byte[] source, int offset) {
91 // Objects.requireNonNull(source);
92 // this.source = source;
93 // this.offset = offset;
94 // if (offset < 0 || offset + 6 > source.length)
95 // throw new ArrayIndexOutOfBoundsException(offset);
96 // }
97 //
98 // @Override
99 // protected byte[] initialValue() {
100 // byte[] value = new byte[6];
101 // System.arraycopy(source, offset, value, 0, 6);
102 // return value;
103 // }
104 //
105 // }
106 }