Revisions

  • 95178a Mon Sep 15 13:53:55 -0700 2008
gist: 10938 Download_button fork
public
Description:
Generate a GUID composed of username, machine name, and random int
Public Clone URL: git://gist.github.com/10938.git
Embed All Files: show embed
UsernameMachinenameGUIDGenerator.java #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Random;
 
 
public class GetMachineName {
 
/**
* @param args
*/
public static void main(String[] args) {
 
for (int i = 0; i < 10; i++) {
String uid = getMachineAndUserIdentifer();
System.out.println("UID:" + uid);
}
}
 
private static String getMachineAndUserIdentifer() {
String machineName = null;
String userName = null;
Integer microGUID = null;
 
//Username
Map<String,String> env = System.getenv();
userName = env.get("USER");
 
//Machine name
try {
machineName = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
throw new IllegalStateException("Hostname cannot be found, but is required.", e);
}
 
//GUID random number
microGUID = new Random().nextInt();
microGUID = Math.abs(microGUID);
 
return userName + "@" + machineName + "@GUID" + microGUID;
}
 
}