KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > util > UniqueGenerator


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.internal.util;
21
22 import java.net.InetAddress JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24
25 import junit.framework.TestCase;
26
27 /**
28  * Generates a quasi-unique id for a test case.
29  *
30  * @version $Id: UniqueGenerator.java,v 1.1 2004/05/22 11:34:48 vmassol Exp $
31  */

32 public class UniqueGenerator
33 {
34     /**
35      * Counter with synchronized access to prevent possibly
36      * identical ids from two threads requesting an id in the
37      * same millisecond.
38      */

39     private static byte count = 0;
40     
41     /**
42      * Lock for count.
43      */

44     private static Object JavaDoc lock = new Object JavaDoc();
45
46     /**
47      * The local IP address in hexadecimal format.
48      */

49     private static String JavaDoc ipAddress;
50     static
51     {
52         try
53         {
54             byte ip[] = InetAddress.getLocalHost().getAddress();
55             ipAddress = toHex(((ip[0] & 0xff) << 24)
56                 | ((ip[1] & 0xff) << 16) | ((ip[2] & 0xff) << 8)
57                 | (ip[3] & 0xff));
58         }
59         catch (UnknownHostException JavaDoc e)
60         {
61             ipAddress = "";
62         }
63     }
64
65     /**
66      * Generates a unique identifier for a Cactus test.
67      *
68      * @param theTestCase The Test to generate a unique ID for
69      * @return The generated ID
70      */

71     public static String JavaDoc generate(TestCase theTestCase)
72     {
73         long time = System.currentTimeMillis();
74         synchronized (lock)
75         {
76             time += count++;
77         }
78         return generate(theTestCase, time);
79     }
80
81     /**
82      * Generates a unique identifier for a Cactus test.
83      *
84      * @param theTestCase The Test to generate a unique ID for
85      * @param theTime The time component to include in the generated ID
86      * @return The generated ID
87      */

88     public static String JavaDoc generate(TestCase theTestCase,
89         long theTime)
90     {
91         String JavaDoc id = ipAddress;
92         id += "-" + toHex(theTime);
93         id += "-" + toHex(System.identityHashCode(theTestCase));
94         id += toHex(theTestCase.getName().hashCode());
95         return id;
96     }
97
98     /**
99      * Returns the hexadecimal representation of an integer as string.
100      *
101      * @param theValue The integer value
102      * @return The integer value as string of hexadecimal digits
103      */

104     private static String JavaDoc toHex(long theValue)
105     {
106         return Long.toString(theValue, 16).toUpperCase();
107     }
108
109 }
110
Popular Tags