X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.api.acr%2Fsrc%2Forg%2Fargeo%2Fapi%2Facr%2Fuuid%2FSimpleUuidFactory.java;h=cffd446bd29adbf853eacbb1d2e4caa9e2e6402b;hb=8b201d419e0de2f0df848e47dd4b48a2a10c711b;hp=2d522fa98d42db819ed09ee3ab095b7366d4c51d;hpb=840822c55293563f4a09256ca29185fe57f64a37;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.api.acr/src/org/argeo/api/acr/uuid/SimpleUuidFactory.java b/org.argeo.api.acr/src/org/argeo/api/acr/uuid/SimpleUuidFactory.java index 2d522fa98..cffd446bd 100644 --- a/org.argeo.api.acr/src/org/argeo/api/acr/uuid/SimpleUuidFactory.java +++ b/org.argeo.api.acr/src/org/argeo/api/acr/uuid/SimpleUuidFactory.java @@ -8,7 +8,6 @@ import java.security.DrbgParameters; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.time.Clock; -import java.util.Random; import java.util.UUID; /** @@ -17,19 +16,29 @@ import java.util.UUID; * * @see https://datatracker.ietf.org/doc/html/rfc4122 */ -public class SimpleUuidFactory extends AbstractUuidFactory { +public class SimpleUuidFactory extends AbstractAsyncUuidFactory { private final static Logger logger = System.getLogger(SimpleUuidFactory.class.getName()); public final static UuidFactory DEFAULT = new SimpleUuidFactory(null, -1, null); - private SecureRandom secureRandom; - private final TimeUuidState timeUuidState; - // private NodeId macAddressNodeId; // private NodeId defaultNodeId; private byte[] macAddressNodeId; private byte[] defaultNodeId; public SimpleUuidFactory(byte[] nodeId, int offset, Clock clock) { + byte[] hardwareAddress = getHardwareAddress(); +// macAddressNodeId = hardwareAddress != null ? new NodeId(hardwareAddress, 0) : null; + macAddressNodeId = toNodeId(hardwareAddress, 0); + +// defaultNodeId = nodeId != null ? new NodeId(nodeId, offset) : macAddressNodeId; + defaultNodeId = nodeId != null ? toNodeId(nodeId, offset) : toNodeId(macAddressNodeId, 0); + if (defaultNodeId == null) + throw new IllegalStateException("No default node id specified"); + } + + @Override + protected SecureRandom newSecureRandom() { + SecureRandom secureRandom; try { secureRandom = SecureRandom.getInstance("DRBG", DrbgParameters.instantiation(256, DrbgParameters.Capability.PR_AND_RESEED, "UUID".getBytes())); @@ -42,17 +51,7 @@ public class SimpleUuidFactory extends AbstractUuidFactory { secureRandom = new SecureRandom(); } } - - byte[] hardwareAddress = getHardwareAddress(); -// macAddressNodeId = hardwareAddress != null ? new NodeId(hardwareAddress, 0) : null; - macAddressNodeId = toNodeId(hardwareAddress, 0); - -// defaultNodeId = nodeId != null ? new NodeId(nodeId, offset) : macAddressNodeId; - defaultNodeId = nodeId != null ? toNodeId(nodeId, offset) : toNodeId(macAddressNodeId, 0); - if (defaultNodeId == null) - throw new IllegalStateException("No default node id specified"); - - timeUuidState = new ConcurrentTimeUuidState(secureRandom, clock); + return secureRandom; } /* @@ -60,43 +59,30 @@ public class SimpleUuidFactory extends AbstractUuidFactory { */ @Override - public UUID timeUUIDwithMacAddress() { + public UUID newTimeUUIDwithMacAddress() { if (macAddressNodeId == null) throw new UnsupportedOperationException("No MAC address is available"); return newTimeUUID(timeUuidState.useTimestamp(), timeUuidState.getClockSequence(), macAddressNodeId, 0); } @Override - public UUID timeUUID() { + public UUID newTimeUUID() { return newTimeUUID(timeUuidState.useTimestamp(), timeUuidState.getClockSequence(), defaultNodeId, 0); } - /* - * NAME BASED (version 3 and 5) - */ - @Override - public UUID nameUUIDv5(UUID namespace, byte[] name) { - return newNameUUIDv5(namespace, name); - } - - @Override - public UUID nameUUIDv3(UUID namespace, byte[] name) { - return newNameUUIDv3(namespace, name); - } - /* * RANDOM v4 */ - @Override - public UUID randomUUID(Random random) { - return newRandomUUID(random); - } - - @Override - public UUID randomUUID() { - return randomUUID(secureRandom); - } +// @Override +// public UUID randomUUID(Random random) { +// return newRandomUUID(random); +// } +// @Override +// public UUID randomUUID() { +// return randomUUID(secureRandom); +// } +// // static class NodeId extends ThreadLocal { // private byte[] source; // private int offset;