KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > uuidgen > Win32UUIDGen


1 /*
2  * Copyright 2001-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 package org.apache.juddi.uuidgen;
17
18 import java.io.IOException JavaDoc;
19 import java.io.BufferedReader JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21
22 /**
23  * Used to create new universally unique identifiers or UUID's (sometimes called
24  * GUID's). UDDI UUID's are allways formmated according to DCE UUID conventions.
25  *
26  * @author Steve Viens (sviens@apache.org)
27  */

28 public final class Win32UUIDGen implements UUIDGen
29 {
30   /**
31    *
32    */

33   public String JavaDoc uuidgen()
34   {
35     String JavaDoc[] uuids = this.uuidgen(1);
36     return uuids[0];
37   }
38
39   /**
40    *
41    */

42   public String JavaDoc[] uuidgen(int nmbr)
43   {
44     String JavaDoc[] uuids = new String JavaDoc[nmbr];
45
46     try
47     {
48       Runtime JavaDoc r = Runtime.getRuntime();
49       Process JavaDoc p = r.exec("uuidgen -n" + nmbr);
50       BufferedReader JavaDoc x = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
51
52       for (int i = 0; i < nmbr; ++i)
53         uuids[i] = x.readLine();
54     }
55     catch (IOException JavaDoc ex)
56     {
57       ex.printStackTrace();
58       throw new RuntimeException JavaDoc(ex.getMessage());
59     }
60
61     return uuids;
62   }
63
64
65   /***************************************************************************/
66   /***************************** TEST DRIVER *********************************/
67   /***************************************************************************/
68
69
70   public static void main(String JavaDoc args[])
71   {
72     UUIDGen uuidgen = new Win32UUIDGen();
73
74     long start = System.currentTimeMillis();
75
76     // fast (3705 milliseconds)
77
for (int i = 1; i <= 250; ++i)
78       System.out.println( i + ": " + uuidgen.uuidgen());
79
80     long end = System.currentTimeMillis();
81
82     System.out.println("Generation (and display) of 250 UUID's took "+(end-start)+" milliseconds.");
83
84
85     start = System.currentTimeMillis();
86
87     // much faster (90 milliseconds)
88
String JavaDoc[] ids = uuidgen.uuidgen(250);
89     for (int i = 1; i < 250; ++i)
90       System.out.println( i + ": " + ids[i]);
91
92     end = System.currentTimeMillis();
93
94     System.out.println("Generation (and display) of 250 UUID's took "+(end-start)+" milliseconds.");
95   }
96 }
97
Popular Tags