1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ws.addressing.uuid;
18
19 import javax.xml.rpc.JAXRPCException;
20 import java.lang.reflect.Method;
21
22 /***
23 * A {@link UUIdGenerator} implementation that uses JUG, an opensource Java UUID generation
24 * library. Reflection is utilized to avoid a compile-time dependency on jug.jar.
25 *
26 * @author Ian P. Springer <ian_springer@hp.com>
27 */
28 public class JugUUIdGenerator implements UUIdGenerator {
29
30 private static final String JUG_UUIDGENERATOR_CLASSNAME = "org.doomdark.uuid.UUIDGenerator";
31 private static final String JUG_WEBSITE_URL = "http://www.doomdark.org/doomdark/proj/jug/";
32
33 private static Method generateUUIdMethod;
34 private static Method newInstanceMethod;
35
36 static {
37 Class jugUuidGenClass = null;
38 try {
39 jugUuidGenClass = Class.forName(JUG_UUIDGENERATOR_CLASSNAME);
40 } catch (ClassNotFoundException cnfe) {
41 throw new RuntimeException("Java UUID Generator (JUG) " + JUG_UUIDGENERATOR_CLASSNAME + " not found. Please add jug.jar, from " + JUG_WEBSITE_URL + ", to your classpath and restart.");
42 }
43 try {
44 newInstanceMethod = jugUuidGenClass.getMethod("getInstance", new Class[0]);
45 generateUUIdMethod = jugUuidGenClass.getMethod("generateTimeBasedUUID", new Class[0]);
46 } catch (Exception e) {
47 throw new RuntimeException("Fatal error initializing Java UUID Generator (JUG) " + JUG_UUIDGENERATOR_CLASSNAME + ".");
48 }
49 }
50
51 private ThreadLocal JUG_UUIDGENERATOR =
52 new ThreadLocal() {
53 protected synchronized Object initialValue() {
54 try {
55 return newInstanceMethod.invoke(null, new Object[0]);
56 } catch (Exception e) {
57 throw new RuntimeException(e);
58 }
59 }
60 };
61
62 public String generateUUId() {
63 try {
64 return generateUUIdMethod.invoke(JUG_UUIDGENERATOR.get(), new Object[0]).toString();
65 } catch (Exception e) {
66 throw new JAXRPCException(e);
67 }
68 }
69
70 }