X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=org.argeo.api.uuid%2Fsrc%2Forg%2Fargeo%2Fapi%2Fuuid%2FConcurrentUuidFactory.java;h=d78be0c5d548a470c0d16b06e67095fafaf194cc;hb=52964c9b7edf437ca3eec0aa7a0baa32bc6dafd6;hp=6debd83b803426453c9e56b1594b6793bcaf6dcb;hpb=5f820a14abf7371714514ed5f20580ceb5edaeec;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentUuidFactory.java b/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentUuidFactory.java index 6debd83b8..d78be0c5d 100644 --- a/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentUuidFactory.java +++ b/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentUuidFactory.java @@ -1,9 +1,5 @@ package org.argeo.api.uuid; -import static java.lang.System.Logger.Level.DEBUG; -import static java.lang.System.Logger.Level.WARNING; - -import java.lang.System.Logger; import java.security.DrbgParameters; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; @@ -14,22 +10,27 @@ import java.util.UUID; * A configurable implementation of an {@link AsyncUuidFactory}, which can be * used as a base class for more optimised implementations. * - * @see https://datatracker.ietf.org/doc/html/rfc4122 + * @see "https://datatracker.ietf.org/doc/html/rfc4122" */ -public class ConcurrentUuidFactory extends AbstractAsyncUuidFactory { - private final static Logger logger = System.getLogger(ConcurrentUuidFactory.class.getName()); +public class ConcurrentUuidFactory extends AbstractAsyncUuidFactory implements TypedUuidFactory { +// private final static Logger logger = System.getLogger(ConcurrentUuidFactory.class.getName()); + + public ConcurrentUuidFactory(long initialClockRange, byte[] nodeId) { + this(initialClockRange, nodeId, 0); + } - public ConcurrentUuidFactory(byte[] nodeId) { - this(nodeId, 0); + /** With a random node id. */ + public ConcurrentUuidFactory(long initialClockRange) { + this(initialClockRange, NodeIdSupplier.randomNodeId()); } - public ConcurrentUuidFactory(byte[] nodeId, int offset) { + public ConcurrentUuidFactory(long initialClockRange, byte[] nodeId, int offset) { Objects.requireNonNull(nodeId); if (offset + 6 > nodeId.length) throw new IllegalArgumentException("Offset too big: " + offset); - byte[] defaultNodeId = toNodeIdBytes(nodeId, offset); + byte[] defaultNodeId = NodeIdSupplier.toNodeIdBytes(nodeId, offset); long nodeIdBase = NodeIdSupplier.toNodeIdBase(defaultNodeId); - setNodeIdSupplier(() -> nodeIdBase); + setNodeIdSupplier(() -> nodeIdBase, initialClockRange); } /** @@ -60,19 +61,21 @@ public class ConcurrentUuidFactory extends AbstractAsyncUuidFactory { } @Override - protected SecureRandom newSecureRandom() { + protected SecureRandom createSecureRandom() { SecureRandom secureRandom; try { secureRandom = SecureRandom.getInstance("DRBG", DrbgParameters.instantiation(256, DrbgParameters.Capability.PR_AND_RESEED, "UUID".getBytes())); } catch (NoSuchAlgorithmException e) { try { - logger.log(DEBUG, "DRBG secure random not found, using strong"); +// logger.log(DEBUG, "DRBG secure random not found, using strong"); secureRandom = SecureRandom.getInstanceStrong(); } catch (NoSuchAlgorithmException e1) { - logger.log(WARNING, "No strong secure random was found, using default"); +// logger.log(WARNING, "No strong secure random was found, using default"); secureRandom = new SecureRandom(); } + } catch (java.lang.NoClassDefFoundError e) {// Android + secureRandom = new SecureRandom(); } return secureRandom; }