| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
package org.apache.camel.util; |
| 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 |
1 |
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 |
1 |
String stub = ""; |
| 39 |
1 |
boolean canAccessSystemProps = true; |
| 40 |
|
try{ |
| 41 |
1 |
SecurityManager sm = System.getSecurityManager(); |
| 42 |
1 |
if(sm != null){ |
| 43 |
0 |
sm.checkPropertiesAccess(); |
| 44 |
|
} |
| 45 |
0 |
}catch(SecurityException se){ |
| 46 |
0 |
canAccessSystemProps = false; |
| 47 |
1 |
} |
| 48 |
|
|
| 49 |
1 |
if ( canAccessSystemProps) { |
| 50 |
|
try { |
| 51 |
1 |
hostName = InetAddress.getLocalHost().getHostName(); |
| 52 |
1 |
ServerSocket ss = new ServerSocket(0); |
| 53 |
1 |
stub="/" + ss.getLocalPort() + "-" + System.currentTimeMillis() + "/"; |
| 54 |
1 |
Thread.sleep(100); |
| 55 |
1 |
ss.close(); |
| 56 |
0 |
}catch(Exception ioe){ |
| 57 |
0 |
log.log(Level.WARNING, "could not generate unique stub",ioe); |
| 58 |
1 |
} |
| 59 |
0 |
}else{ |
| 60 |
0 |
hostName="localhost"; |
| 61 |
0 |
stub = "-1-" +System.currentTimeMillis() +"-"; |
| 62 |
|
} |
| 63 |
1 |
UNIQUE_STUB = stub; |
| 64 |
1 |
} |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
public static String getHostName(){ |
| 73 |
0 |
return hostName; |
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
2 |
public UuidGenerator(String prefix){ |
| 82 |
2 |
synchronized(UNIQUE_STUB){ |
| 83 |
2 |
this.seed = prefix + UNIQUE_STUB +(instanceCount++) +"-"; |
| 84 |
2 |
} |
| 85 |
2 |
} |
| 86 |
|
|
| 87 |
|
public UuidGenerator(){ |
| 88 |
2 |
this("ID-" + hostName); |
| 89 |
2 |
} |
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
public synchronized String generateId(){ |
| 97 |
1604 |
return this.seed + (this.sequence++); |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
public String generateSanitizedId(){ |
| 105 |
0 |
String result = generateId(); |
| 106 |
0 |
result = result.replace(':', '-'); |
| 107 |
0 |
result = result.replace('_', '-'); |
| 108 |
0 |
result = result.replace('.', '-'); |
| 109 |
0 |
return result; |
| 110 |
|
} |
| 111 |
|
|
| 112 |
|
} |