View Javadoc

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;
19  import java.io.BufferedReader;
20  import java.io.InputStreamReader;
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 uuidgen()
34    {
35      String[] uuids = this.uuidgen(1);
36      return uuids[0];
37    }
38  
39    /***
40     *
41     */
42    public String[] uuidgen(int nmbr)
43    {
44      String[] uuids = new String[nmbr];
45  
46      try
47      {
48        Runtime r = Runtime.getRuntime();
49        Process p = r.exec("uuidgen -n" + nmbr);
50        BufferedReader x = new BufferedReader(new InputStreamReader(p.getInputStream()));
51  
52        for (int i = 0; i < nmbr; ++i)
53          uuids[i] = x.readLine();
54      }
55      catch (IOException ex)
56      {
57        ex.printStackTrace();
58        throw new RuntimeException(ex.getMessage());
59      }
60  
61      return uuids;
62    }
63  
64  
65    /****************************************************************************/
66    /****************************** TEST DRIVER *********************************/
67    /****************************************************************************/
68  
69  
70    public static void main(String 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[] 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  }