1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.applications.manager.idgen;
12
13 import org.astrogrid.component.descriptor.ComponentDescriptor;
14
15 import java.net.InetAddress;
16 import java.util.Random;
17
18 import junit.framework.Test;
19
20 /*** Implementation of idgen that should produce globally-unique urn-style identifiers.
21 * @author Noel Winstanley nw@jb.man.ac.uk 16-Jun-2004
22 *
23 */
24 public class GloballyUniqueIdGen implements IdGen, ComponentDescriptor {
25 /*** Construct a new GloballyUniqueIdGen
26 *
27 */
28 public GloballyUniqueIdGen() {
29 super();
30 }
31
32
33 /*** stuff for generating a unique job urn */
34 protected static String hostname;
35 static {
36 hostname = null;
37 try {
38 hostname = InetAddress.getLocalHost().toString();
39 } catch (Exception e) {
40 hostname="unavailable";
41 }
42 }
43 private static Random rand = new Random();
44
45 /***
46 * @see org.astrogrid.applications.manager.idgen.IdGen#getNewID()
47 */
48 public synchronized String getNewID() {
49
50 StringBuffer
51 buffer = new StringBuffer(128);
52
53 buffer
54 .append("cea:")
55 .append(hostname)
56
57
58
59
60
61
62 .append( '/' )
63 .append( System.currentTimeMillis())
64 .append( ':' )
65 .append(Math.abs(rand.nextInt()));
66 return buffer.toString();
67
68 }
69
70
71 /***
72 * @see org.astrogrid.component.descriptor.ComponentDescriptor#getName()
73 */
74 public String getName() {
75 return "Globally-Unique ID generator";
76 }
77
78
79 /***
80 * @see org.astrogrid.component.descriptor.ComponentDescriptor#getDescription()
81 */
82 public String getDescription() {
83 return "generates identifiers such as " + getNewID();
84 }
85
86
87 /***
88 * @see org.astrogrid.component.descriptor.ComponentDescriptor#getInstallationTest()
89 */
90 public Test getInstallationTest() {
91 return null;
92 }
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108