001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.synapse.util;
021:
022: import java.net.InetAddress;
023: import java.net.UnknownHostException;
024: import java.security.MessageDigest;
025: import java.security.NoSuchAlgorithmException;
026: import java.util.Random;
027:
028: /**
029: * This is a thread-safe version of the Axiom UUIDGenerator
030: * to be used until it is fixed in the next Axiom release
031: */
032: public class UUIDGenerator {
033: /** This class will give UUIDs for axis2. */
034:
035: private static String baseUUID = null;
036: private static long incrementingValue = 0;
037:
038: private static Random myRand = null;
039: private static boolean useNano = false;
040:
041: /**
042: * MD5 a random string with localhost/date etc will return 128 bits construct a string of 18
043: * characters from those bits.
044: *
045: * @return string
046: */
047: public static String getUUID() {
048: if (baseUUID == null) {
049: baseUUID = getInitialUUID();
050: baseUUID = "urn:uuid:" + baseUUID;
051: }
052: if (++incrementingValue >= Long.MAX_VALUE) {
053: incrementingValue = 0;
054: }
055:
056: if (useNano) {
057: return baseUUID + (System.nanoTime() + incrementingValue)
058: + Integer.toString(myRand.nextInt());
059: } else {
060:
061: return baseUUID
062: + (System.currentTimeMillis() + incrementingValue + Integer
063: .toString(myRand.nextInt()));
064: }
065:
066: }
067:
068: protected static String getInitialUUID() {
069:
070: try {
071: if (System.class.getMethod("nanoTime", new Class[0]) != null) {
072: useNano = true;
073: }
074: } catch (NoSuchMethodException ignore) {
075: }
076:
077: if (myRand == null) {
078: myRand = new Random();
079: }
080: long rand = myRand.nextLong();
081: String sid;
082: try {
083: sid = InetAddress.getLocalHost().toString();
084: } catch (UnknownHostException e) {
085: sid = Thread.currentThread().getName();
086: }
087: StringBuffer sb = new StringBuffer();
088: sb.append(sid);
089: sb.append(":");
090: sb.append(Long.toString(rand));
091: MessageDigest md5 = null;
092: try {
093: md5 = MessageDigest.getInstance("MD5");
094: } catch (NoSuchAlgorithmException e) {
095: //System.out.println("Error: " + e);
096: //todo heve to be properly handle
097: }
098: md5.update(sb.toString().getBytes());
099: byte[] array = md5.digest();
100: StringBuffer sb2 = new StringBuffer();
101: for (int j = 0; j < array.length; ++j) {
102: int b = array[j] & 0xFF;
103: sb2.append(Integer.toHexString(b));
104: }
105: int begin = myRand.nextInt();
106: if (begin < 0)
107: begin = begin * -1;
108: begin = begin % 8;
109: return sb2.toString().substring(begin, begin + 18)
110: .toUpperCase();
111: }
112: }
|