From: Mathieu Baudier Date: Tue, 7 Jun 2022 10:49:53 +0000 (+0200) Subject: Remove dependencies to System.getLogger, for Android compatibility. X-Git-Tag: v2.3.10~201 X-Git-Url: https://git.argeo.org/?a=commitdiff_plain;h=ee0a9f240a5da3a1437dda5abe7a1c46c5a3a8e9;p=lgpl%2Fargeo-commons.git Remove dependencies to System.getLogger, for Android compatibility. --- diff --git a/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentTimeUuidState.java b/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentTimeUuidState.java index 5bab35980..8131df8ee 100644 --- a/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentTimeUuidState.java +++ b/org.argeo.api.uuid/src/org/argeo/api/uuid/ConcurrentTimeUuidState.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.SecureRandom; import java.time.Clock; import java.time.Duration; @@ -29,7 +25,7 @@ import org.argeo.api.uuid.UuidFactory.TimeUuidState; * and that such reallocation won't have to happen too often. */ public class ConcurrentTimeUuidState implements UuidFactory.TimeUuidState { - private final static Logger logger = System.getLogger(ConcurrentTimeUuidState.class.getName()); +// private final static Logger logger = System.getLogger(ConcurrentTimeUuidState.class.getName()); /** The maximum possible value of the clocksequence. */ private final static int MAX_CLOCKSEQUENCE = 0x3F00; @@ -275,11 +271,11 @@ public class ConcurrentTimeUuidState implements UuidFactory.TimeUuidState { } assert holderToRemove != null; - long oldClockSequence = holderToRemove.clockSequence; +// long oldClockSequence = holderToRemove.clockSequence; holderToRemove.clockSequence = -1; activeHolders.remove(holderToRemove); - if (logger.isLoggable(WARNING)) - logger.log(WARNING, "Removed " + holderToRemove + ", oldClockSequence=" + oldClockSequence); +// if (logger.isLoggable(WARNING)) +// logger.log(WARNING, "Removed " + holderToRemove + ", oldClockSequence=" + oldClockSequence); } long newClockSequence = -1; @@ -300,13 +296,13 @@ public class ConcurrentTimeUuidState implements UuidFactory.TimeUuidState { // TODO use an iterator to check the values holder.setClockSequence(newClockSequence); activeHolders.put(holder, newClockSequence); - if (logger.isLoggable(DEBUG)) { - String clockDesc = range >= 0 ? Long.toHexString(newClockSequence & 0x00FF) - : Long.toHexString(newClockSequence | 0x8000); - String rangeDesc = Long.toHexString(min | 0x8000) + "-" + Long.toHexString(max | 0x8000); - logger.log(DEBUG, "New clocksequence " + clockDesc + " for thread " + Thread.currentThread().getId() - + " (in range " + rangeDesc + ")"); - } +// if (logger.isLoggable(DEBUG)) { +// String clockDesc = range >= 0 ? Long.toHexString(newClockSequence & 0x00FF) +// : Long.toHexString(newClockSequence | 0x8000); +// String rangeDesc = Long.toHexString(min | 0x8000) + "-" + Long.toHexString(max | 0x8000); +// logger.log(DEBUG, "New clocksequence " + clockDesc + " for thread " + Thread.currentThread().getId() +// + " (in range " + rangeDesc + ")"); +// } } private synchronized int getRangeSize() { 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 264e04706..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,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; @@ -17,12 +13,17 @@ import java.util.UUID; * @see https://datatracker.ietf.org/doc/html/rfc4122 */ public class ConcurrentUuidFactory extends AbstractAsyncUuidFactory implements TypedUuidFactory { - private final static Logger logger = System.getLogger(ConcurrentUuidFactory.class.getName()); +// private final static Logger logger = System.getLogger(ConcurrentUuidFactory.class.getName()); public ConcurrentUuidFactory(long initialClockRange, byte[] nodeId) { this(initialClockRange, nodeId, 0); } + /** With a random node id. */ + public ConcurrentUuidFactory(long initialClockRange) { + this(initialClockRange, NodeIdSupplier.randomNodeId()); + } + public ConcurrentUuidFactory(long initialClockRange, byte[] nodeId, int offset) { Objects.requireNonNull(nodeId); if (offset + 6 > nodeId.length) @@ -67,12 +68,14 @@ public class ConcurrentUuidFactory extends AbstractAsyncUuidFactory implements T 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; } diff --git a/org.argeo.api.uuid/src/org/argeo/api/uuid/NodeIdSupplier.java b/org.argeo.api.uuid/src/org/argeo/api/uuid/NodeIdSupplier.java index 94ec50da4..81d368d2c 100644 --- a/org.argeo.api.uuid/src/org/argeo/api/uuid/NodeIdSupplier.java +++ b/org.argeo.api.uuid/src/org/argeo/api/uuid/NodeIdSupplier.java @@ -1,13 +1,13 @@ package org.argeo.api.uuid; +import java.security.SecureRandom; import java.util.function.Supplier; /** A factory for node id base */ public interface NodeIdSupplier extends Supplier { static long toNodeIdBase(byte[] node) { assert node.length == 6; - return UuidFactory.LEAST_SIG_RFC4122_VARIANT - | (node[0] & 0xFFL) // + return UuidFactory.LEAST_SIG_RFC4122_VARIANT | (node[0] & 0xFFL) // | ((node[1] & 0xFFL) << 8) // | ((node[2] & 0xFFL) << 16) // | ((node[3] & 0xFFL) << 24) // @@ -19,4 +19,10 @@ public interface NodeIdSupplier extends Supplier { return (nodeId[0] & 1) != 0; } + static byte[] randomNodeId() { + SecureRandom random = new SecureRandom(); + byte[] nodeId = new byte[6]; + random.nextBytes(nodeId); + return nodeId; + } }