View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.ws.security.util;
21  
22  import java.net.InetAddress;
23  import java.net.UnknownHostException;
24  import java.security.MessageDigest;
25  import java.security.NoSuchAlgorithmException;
26  import java.util.Random;
27  
28  /**
29   * UUID generator (taken from Axis2)
30   */
31  public final class UUIDGenerator {
32  
33      private static final org.apache.commons.logging.Log LOG = 
34          org.apache.commons.logging.LogFactory.getLog(UUIDGenerator.class);
35  
36      private static String baseUUID = null;
37      private static long incrementingValue = 0;
38  
39  
40      private static Random myRand = null;
41      
42      private UUIDGenerator() {
43          // Complete
44      }
45  
46      /**
47       * MD5 a random string with localhost/date etc will return 128 bits
48       * construct a string of 18 characters from those bits.
49       *
50       * @return string
51       */
52      public static synchronized String getUUID() {
53          if (baseUUID == null) {
54              getInitialUUID();
55          }
56          long i = ++incrementingValue;
57          if (i >= Long.MAX_VALUE || i < 0) {
58              incrementingValue = 0;
59              i = 0;
60          }
61          return baseUUID + System.currentTimeMillis() + i;
62      }
63  
64      protected static synchronized void getInitialUUID() {
65          if (baseUUID != null) {
66              return;
67          }
68          if (myRand == null) {
69              myRand = new Random();
70          }
71          long rand = myRand.nextLong();
72          String sid;
73          try {
74              sid = InetAddress.getLocalHost().toString();
75          } catch (UnknownHostException e) {
76              sid = Thread.currentThread().getName();
77          }
78          StringBuilder sb = new StringBuilder();
79          sb.append(sid);
80          sb.append(":");
81          sb.append(Long.toString(rand));
82          MessageDigest md5 = null;
83          try {
84              md5 = MessageDigest.getInstance("MD5");
85          } catch (NoSuchAlgorithmException e) {
86              if (LOG.isDebugEnabled()) {
87                  LOG.debug(e.getMessage(), e);
88              }
89              //todo have to be properly handled
90          }
91          md5.update(sb.toString().getBytes());
92          byte[] array = md5.digest();
93          StringBuilder sb2 = new StringBuilder();
94          for (int j = 0; j < array.length; ++j) {
95              int b = array[j] & 0xFF;
96              sb2.append(Integer.toHexString(b));
97          }
98          int begin = myRand.nextInt();
99          if (begin < 0) {
100             begin = begin * -1;
101         }
102         begin = begin % 8;
103         baseUUID = sb2.toString().substring(begin, begin + 18).toUpperCase();
104     }
105 
106 }