X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.api.uuid%2Fsrc%2Forg%2Fargeo%2Fapi%2Fuuid%2FConcurrentUuidFactory.java;h=f2bd43689772a24c87ba5ad81e53f1f0fe00ddb4;hb=ee0a9f240a5da3a1437dda5abe7a1c46c5a3a8e9;hp=05b2f05c6b62345e0160363c7129983301d32ade;hpb=e846ef84146b66ae543c29c5f17b2991ff0f5973;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 05b2f05c6..f2bd43689 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,13 +1,10 @@ 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; import java.util.Objects; +import java.util.UUID; /** * A configurable implementation of an {@link AsyncUuidFactory}, which can be @@ -15,50 +12,72 @@ import java.util.Objects; * * @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()); -// private byte[] defaultNodeId; + public ConcurrentUuidFactory(long initialClockRange, byte[] nodeId) { + this(initialClockRange, nodeId, 0); + } - private Long nodeIdBase; + /** 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); - nodeIdBase = NodeIdSupplier.toNodeIdBase(defaultNodeId); - setNodeIdSupplier(() -> nodeIdBase); + long nodeIdBase = NodeIdSupplier.toNodeIdBase(defaultNodeId); + setNodeIdSupplier(() -> nodeIdBase, initialClockRange); } - protected ConcurrentUuidFactory() { + /** + * Empty constructor for use with component life cycle. A {@link NodeIdSupplier} + * must be set externally, otherwise time based UUID won't work. + */ + public ConcurrentUuidFactory() { + super(); + } + +// public ConcurrentUuidFactory() { +// byte[] defaultNodeId = getIpBytes(); +// nodeIdBase = NodeIdSupplier.toNodeIdBase(defaultNodeId); +// setNodeIdSupplier(() -> nodeIdBase); +// assert newTimeUUID().node() == BitSet.valueOf(defaultNodeId).toLongArray()[0]; +// } + /* + * DEFAULT + */ + /** + * The default {@link UUID} to provide. This implementations returns + * {@link #timeUUID()} because it is fast and uses few resources. + */ + @Override + public UUID get() { + return timeUUID(); } @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; } - /* - * TIME-BASED (version 1) - */ -// -// @Override -// public UUID newTimeUUID() { -// return newTimeUUID(timeUuidState.useTimestamp(), timeUuidState.getClockSequence(), defaultNodeId, 0); -// } -} +} \ No newline at end of file