Skip to content

Commit c076d64

Browse files
committed
Fix #55
1 parent b923c2c commit c076d64

File tree

6 files changed

+106
-20
lines changed

6 files changed

+106
-20
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ JUG is licensed under [Apache License 2.0](http://www.apache.org/licenses/LICENS
2626
JUG can be used as a command-line tool (via class `com.fasterxml.uuid.Jug`),
2727
or as a pluggable component.
2828

29-
### Via Maven
29+
### Maven Dependency
3030

3131
Maven coordinates are:
3232

@@ -38,7 +38,7 @@ Maven coordinates are:
3838
</dependency>
3939
```
4040

41-
#### Dependencies
41+
#### Third-party Dependencies by JUG
4242

4343
The only dependency for JUG is the logging library:
4444

@@ -53,7 +53,7 @@ Since version `3.2.0`, JUG defines JDK9+ compatible `module-info.class`, with mo
5353

5454
For direct downloads, check out [Project Wiki](../../wiki).
5555

56-
### Using JUG
56+
### Using JUG as Library
5757

5858
#### Generating UUIDs
5959

@@ -126,6 +126,24 @@ UUID uuidFromStr = UUID.fromString("ebb8e8fe-b1b1-11d7-8adb-00b0d078fa18");
126126
it is rather slower than JUG version: for more information, read
127127
[Measuring performance of Java UUID.fromString()](https://cowtowncoder.medium.com/measuring-performance-of-java-uuid-fromstring-or-lack-thereof-d16a910fa32a).
128128

129+
### Using JUG as CLI
130+
131+
JUG jar built under `target/` like:
132+
133+
```
134+
target/java-uuid-generator-4.1.1-SNAPSHOT.jar
135+
```
136+
137+
can be invoked directly. To see usage you can do something like:
138+
139+
java -jar target/java-uuid-generator-4.1.1-SNAPSHOT.jar
140+
141+
and get full instructions, but to generate 5 Random-based UUIDs, you would use:
142+
143+
java -jar target/java-uuid-generator-4.1.1-SNAPSHOT.jar -c 5 r
144+
145+
(where `-c` (or `--count`) means number of UUIDs to generate, and `r` means Random-based variant)
146+
129147
## Compatibility
130148

131149
JUG versions 3.1 and later require JDK 1.6 to work, mostly to be able to access local Ethernet MAC address.

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ https://stackoverflow.com/questions/37958104/maven-javadoc-no-source-files-for-p
148148
org.slf4j;version="[${slf4j.version},2)"
149149
</Import-Package>
150150
<Private-Package />
151+
<Main-Class>com.fasterxml.uuid.Jug</Main-Class>
151152
</instructions>
152153
</configuration>
153154
</plugin>

release-notes/VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Releases
1313
#52: Add `Generators.egressTimeBasedGenerator()` method that constructs `TimedBasedGenerator`
1414
with a sensible choice of interface
1515
(contributed by Paul G)
16+
#55: Add `Main-Class` manifest to make jar invoke `Jug` class
1617
- Fix a minor issue with argument validation for `Jug` tool class
1718
- Update junit dependency (via oss-parent:41)
1819
- Update slf4j-api to 1.7.36

src/main/java/com/fasterxml/uuid/UUIDTimer.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
import java.io.*;
1919
import java.util.*;
2020

21+
import com.fasterxml.uuid.impl.LoggerFacade;
2122
import com.fasterxml.uuid.impl.UUIDUtil;
2223

23-
import org.slf4j.Logger;
24-
import org.slf4j.LoggerFactory;
25-
2624
/**
2725
* UUIDTimer produces the time stamps required for time-based UUIDs.
2826
* It works as outlined in the UUID specification, with following
@@ -75,9 +73,8 @@
7573
*/
7674
public class UUIDTimer
7775
{
76+
private final LoggerFacade _logger = LoggerFacade.getLogger(getClass());
7877

79-
private static final Logger logger = LoggerFactory.getLogger(UUIDTimer.class);
80-
8178
// // // Constants
8279

8380
/**
@@ -247,7 +244,8 @@ public synchronized long getTimestamp()
247244
* independent of whether we can use it:
248245
*/
249246
if (systime < _lastSystemTimestamp) {
250-
logger.warn("System time going backwards! (got value {}, last {})", systime, _lastSystemTimestamp);
247+
_logger.warn("System time going backwards! (got value %d, last %d)",
248+
systime, _lastSystemTimestamp);
251249
// Let's write it down, still
252250
_lastSystemTimestamp = systime;
253251
}
@@ -267,7 +265,7 @@ public synchronized long getTimestamp()
267265
long origTime = systime;
268266
systime = _lastUsedTimestamp + 1L;
269267

270-
logger.warn("Timestamp over-run: need to reinitialize random sequence");
268+
_logger.warn("Timestamp over-run: need to reinitialize random sequence");
271269

272270
/* Clock counter is now at exactly the multiplier; no use
273271
* just anding its value. So, we better get some random
@@ -371,7 +369,7 @@ protected final void getAndSetTimestamp(byte[] uuidBytes)
371369
* @param actDiff Number of milliseconds to wait for from current
372370
* time point, to catch up
373371
*/
374-
protected static void slowDown(long startTime, long actDiff)
372+
protected void slowDown(long startTime, long actDiff)
375373
{
376374
/* First, let's determine how long we'd like to wait.
377375
* This is based on how far ahead are we as of now.
@@ -388,7 +386,7 @@ protected static void slowDown(long startTime, long actDiff)
388386
} else {
389387
delay = 5L;
390388
}
391-
logger.warn("Need to wait for {} milliseconds; virtual clock advanced too far in the future", delay);
389+
_logger.warn("Need to wait for %d milliseconds; virtual clock advanced too far in the future", delay);
392390
long waitUntil = startTime + delay;
393391
int counter = 0;
394392
do {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.fasterxml.uuid.impl;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
/**
7+
* Wrapper we (only) need to support CLI usage (see {@link com.fasterxml.uuid.Jug}
8+
* wherein we do not actually have logger package included; in which case we
9+
* will print warning(s) out to {@code System.err}.
10+
* For normal embedded usage no benefits, except if someone forgot their SLF4j API
11+
* package. :)
12+
*
13+
* @since 4.1
14+
*/
15+
public class LoggerFacade {
16+
private final Class<?> _forClass;
17+
18+
private WrappedLogger _logger;
19+
20+
private LoggerFacade(Class<?> forClass) {
21+
_forClass = forClass;
22+
}
23+
24+
public static LoggerFacade getLogger(Class<?> forClass) {
25+
return new LoggerFacade(forClass);
26+
}
27+
28+
public void warn(String msg) {
29+
_warn(msg);
30+
}
31+
32+
public void warn(String msg, Object arg) {
33+
_warn(String.format(msg, arg));
34+
}
35+
36+
public void warn(String msg, Object arg, Object arg2) {
37+
_warn(String.format(msg, arg, arg2));
38+
}
39+
40+
private synchronized void _warn(String message) {
41+
if (_logger == null) {
42+
_logger = WrappedLogger.logger(_forClass);
43+
}
44+
_logger.warn(message);
45+
}
46+
47+
private static class WrappedLogger {
48+
private final Logger _logger;
49+
50+
private WrappedLogger(Logger l) {
51+
_logger = l;
52+
}
53+
54+
public static WrappedLogger logger(Class<?> forClass) {
55+
// Why all these contortions? To support case where Slf4j API missing
56+
// (or, if it ever fails for not having impl) to just print to STDERR
57+
try {
58+
return new WrappedLogger(LoggerFactory.getLogger(forClass));
59+
} catch (Throwable t) {
60+
return new WrappedLogger(null);
61+
}
62+
}
63+
64+
public void warn(String message) {
65+
if (_logger != null) {
66+
_logger.warn(message);
67+
} else {
68+
System.err.println("WARN: "+message);
69+
}
70+
}
71+
}
72+
}

src/main/java/com/fasterxml/uuid/impl/NameBasedGenerator.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
import com.fasterxml.uuid.StringArgGenerator;
88
import com.fasterxml.uuid.UUIDType;
99

10-
import org.slf4j.Logger;
11-
import org.slf4j.LoggerFactory;
12-
1310
/**
1411
* Implementation of UUID generator that uses one of name-based generation methods
1512
* (variants 3 (MD5) and 5 (SHA1)).
@@ -21,14 +18,13 @@
2118
*/
2219
public class NameBasedGenerator extends StringArgGenerator
2320
{
24-
25-
private static final Logger logger = LoggerFactory.getLogger(NameBasedGenerator.class);
26-
2721
public final static Charset _utf8;
2822
static {
2923
_utf8 = Charset.forName("UTF-8");
3024
}
31-
25+
26+
private final LoggerFacade _logger = LoggerFacade.getLogger(getClass());
27+
3228
/**
3329
* Namespace used when name is a DNS name.
3430
*/
@@ -95,7 +91,7 @@ public NameBasedGenerator(UUID namespace, MessageDigest digester, UUIDType type)
9591
} else {
9692
// Hmmh... error out? Let's default to SHA-1, but log a warning
9793
type = UUIDType.NAME_BASED_SHA1;
98-
logger.warn("Could not determine type of Digester from '{}'; assuming 'SHA-1' type", typeStr);
94+
_logger.warn("Could not determine type of Digester from '%s'; assuming 'SHA-1' type", typeStr);
9995
}
10096
}
10197
_digester = digester;

0 commit comments

Comments
 (0)