View Javadoc

1   /*
2    * Copyright  2004 The Apache Software Foundation.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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  }