| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.camel.util; |
| 18 |
|
|
| 19 |
|
import java.net.InetAddress; |
| 20 |
|
import java.net.ServerSocket; |
| 21 |
|
import java.util.logging.Level; |
| 22 |
|
import java.util.logging.Logger; |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
public class UuidGenerator { |
| 29 |
|
|
| 30 |
3 |
private static final Logger LOG = Logger.getLogger(UuidGenerator.class.getName()); |
| 31 |
|
private static final String UNIQUE_STUB; |
| 32 |
|
private static int instanceCount; |
| 33 |
|
private static String hostName; |
| 34 |
|
private String seed; |
| 35 |
|
private long sequence; |
| 36 |
|
|
| 37 |
|
static { |
| 38 |
3 |
String stub = ""; |
| 39 |
3 |
boolean canAccessSystemProps = true; |
| 40 |
|
try { |
| 41 |
3 |
SecurityManager sm = System.getSecurityManager(); |
| 42 |
3 |
if (sm != null) { |
| 43 |
0 |
sm.checkPropertiesAccess(); |
| 44 |
|
} |
| 45 |
0 |
} catch (SecurityException se) { |
| 46 |
0 |
canAccessSystemProps = false; |
| 47 |
3 |
} |
| 48 |
|
|
| 49 |
3 |
if (canAccessSystemProps) { |
| 50 |
|
try { |
| 51 |
3 |
hostName = InetAddress.getLocalHost().getHostName(); |
| 52 |
3 |
ServerSocket ss = new ServerSocket(0); |
| 53 |
3 |
stub = "/" + ss.getLocalPort() + "-" + System.currentTimeMillis() + "/"; |
| 54 |
3 |
Thread.sleep(100); |
| 55 |
3 |
ss.close(); |
| 56 |
0 |
} catch (Exception ioe) { |
| 57 |
0 |
LOG.log(Level.WARNING, "could not generate unique stub", ioe); |
| 58 |
3 |
} |
| 59 |
0 |
} else { |
| 60 |
0 |
hostName = "localhost"; |
| 61 |
0 |
stub = "-1-" + System.currentTimeMillis() + "-"; |
| 62 |
|
} |
| 63 |
3 |
UNIQUE_STUB = stub; |
| 64 |
3 |
} |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
6 |
public UuidGenerator(String prefix) { |
| 72 |
6 |
synchronized (UNIQUE_STUB) { |
| 73 |
6 |
this.seed = prefix + UNIQUE_STUB + (instanceCount++) + "-"; |
| 74 |
6 |
} |
| 75 |
6 |
} |
| 76 |
|
|
| 77 |
|
public UuidGenerator() { |
| 78 |
6 |
this("ID-" + hostName); |
| 79 |
6 |
} |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
public static String getHostName() { |
| 89 |
0 |
return hostName; |
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
public synchronized String generateId() { |
| 99 |
2361 |
return this.seed + (this.sequence++); |
| 100 |
|
} |
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
public String generateSanitizedId() { |
| 108 |
0 |
String result = generateId(); |
| 109 |
0 |
result = result.replace(':', '-'); |
| 110 |
0 |
result = result.replace('_', '-'); |
| 111 |
0 |
result = result.replace('.', '-'); |
| 112 |
0 |
return result; |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
} |