]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentUuidFactory.java
Improve build and local deployment
[lgpl/argeo-commons.git] / org.argeo.api.uuid / src / org / argeo / api / uuid / ConcurrentUuidFactory.java
1 package org.argeo.api.uuid;
2
3 import java.security.DrbgParameters;
4 import java.security.NoSuchAlgorithmException;
5 import java.security.SecureRandom;
6 import java.util.Objects;
7 import java.util.UUID;
8
9 /**
10 * A configurable implementation of an {@link AsyncUuidFactory}, which can be
11 * used as a base class for more optimised implementations.
12 *
13 * @see "https://datatracker.ietf.org/doc/html/rfc4122"
14 */
15 public class ConcurrentUuidFactory extends AbstractAsyncUuidFactory implements TypedUuidFactory {
16 // private final static Logger logger = System.getLogger(ConcurrentUuidFactory.class.getName());
17
18 public ConcurrentUuidFactory(long initialClockRange, byte[] nodeId) {
19 this(initialClockRange, nodeId, 0);
20 }
21
22 /** With a random node id. */
23 public ConcurrentUuidFactory(long initialClockRange) {
24 this(initialClockRange, NodeIdSupplier.randomNodeId());
25 }
26
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 = NodeIdSupplier.toNodeIdBytes(nodeId, offset);
32 long nodeIdBase = NodeIdSupplier.toNodeIdBase(defaultNodeId);
33 setNodeIdSupplier(() -> nodeIdBase, initialClockRange);
34 }
35
36 /**
37 * Empty constructor for use with component life cycle. A {@link NodeIdSupplier}
38 * must be set externally, otherwise time based UUID won't work.
39 */
40 public ConcurrentUuidFactory() {
41 super();
42 }
43
44 // public ConcurrentUuidFactory() {
45 // byte[] defaultNodeId = getIpBytes();
46 // nodeIdBase = NodeIdSupplier.toNodeIdBase(defaultNodeId);
47 // setNodeIdSupplier(() -> nodeIdBase);
48 // assert newTimeUUID().node() == BitSet.valueOf(defaultNodeId).toLongArray()[0];
49 // }
50
51 /*
52 * DEFAULT
53 */
54 /**
55 * The default {@link UUID} to provide. This implementations returns
56 * {@link #timeUUID()} because it is fast and uses few resources.
57 */
58 @Override
59 public UUID get() {
60 return timeUUID();
61 }
62
63 @Override
64 protected SecureRandom createSecureRandom() {
65 SecureRandom secureRandom;
66 try {
67 secureRandom = SecureRandom.getInstance("DRBG",
68 DrbgParameters.instantiation(256, DrbgParameters.Capability.PR_AND_RESEED, "UUID".getBytes()));
69 } catch (NoSuchAlgorithmException e) {
70 try {
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();
76 }
77 } catch (java.lang.NoClassDefFoundError e) {// Android
78 secureRandom = new SecureRandom();
79 }
80 return secureRandom;
81 }
82
83 }