1 package org
.argeo
.api
.uuid
;
3 import java
.security
.DrbgParameters
;
4 import java
.security
.NoSuchAlgorithmException
;
5 import java
.security
.SecureRandom
;
6 import java
.util
.Objects
;
10 * A configurable implementation of an {@link AsyncUuidFactory}, which can be
11 * used as a base class for more optimised implementations.
13 * @see "https://datatracker.ietf.org/doc/html/rfc4122"
15 public class ConcurrentUuidFactory
extends AbstractAsyncUuidFactory
implements TypedUuidFactory
{
16 // private final static Logger logger = System.getLogger(ConcurrentUuidFactory.class.getName());
18 public ConcurrentUuidFactory(long initialClockRange
, byte[] nodeId
) {
19 this(initialClockRange
, nodeId
, 0);
22 /** With a random node id. */
23 public ConcurrentUuidFactory(long initialClockRange
) {
24 this(initialClockRange
, NodeIdSupplier
.randomNodeId());
27 public ConcurrentUuidFactory(long initialClockRange
, byte[] nodeId
, int offset
) {
28 Objects
.requireNonNull(nodeId
);
29 if (offset
+ 6 > nodeId
.length
)
30 throw new IllegalArgumentException("Offset too big: " + offset
);
31 byte[] defaultNodeId
= toNodeIdBytes(nodeId
, offset
);
32 long nodeIdBase
= NodeIdSupplier
.toNodeIdBase(defaultNodeId
);
33 setNodeIdSupplier(() -> nodeIdBase
, initialClockRange
);
37 * Empty constructor for use with component life cycle. A {@link NodeIdSupplier}
38 * must be set externally, otherwise time based UUID won't work.
40 public ConcurrentUuidFactory() {
44 // public ConcurrentUuidFactory() {
45 // byte[] defaultNodeId = getIpBytes();
46 // nodeIdBase = NodeIdSupplier.toNodeIdBase(defaultNodeId);
47 // setNodeIdSupplier(() -> nodeIdBase);
48 // assert newTimeUUID().node() == BitSet.valueOf(defaultNodeId).toLongArray()[0];
55 * The default {@link UUID} to provide. This implementations returns
56 * {@link #timeUUID()} because it is fast and uses few resources.
64 protected SecureRandom
createSecureRandom() {
65 SecureRandom secureRandom
;
67 secureRandom
= SecureRandom
.getInstance("DRBG",
68 DrbgParameters
.instantiation(256, DrbgParameters
.Capability
.PR_AND_RESEED
, "UUID".getBytes()));
69 } catch (NoSuchAlgorithmException e
) {
71 // logger.log(DEBUG, "DRBG secure random not found, using strong");
72 secureRandom
= SecureRandom
.getInstanceStrong();
73 } catch (NoSuchAlgorithmException e1
) {
74 // logger.log(WARNING, "No strong secure random was found, using default");
75 secureRandom
= new SecureRandom();
77 } catch (java
.lang
.NoClassDefFoundError e
) {// Android
78 secureRandom
= new SecureRandom();