KickJava   Java API By Example, From Geeks To Geeks.

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


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

31 public final class NativeUUIDGen implements UUIDGen
32 {
33   private static final String JavaDoc COMMAND_KEY = "juddi.uuidgenCommand";
34   private static final String JavaDoc DEFAULT_COMMAND = "uuidgen";
35   private String JavaDoc command = null;
36   
37   /**
38    *
39    */

40   public NativeUUIDGen()
41   {
42         this.command = Config.getStringProperty(COMMAND_KEY,DEFAULT_COMMAND);
43   }
44   
45   /**
46    *
47    */

48   public String JavaDoc uuidgen()
49   {
50     try
51     {
52       Runtime JavaDoc r = Runtime.getRuntime();
53       Process JavaDoc p = r.exec(command);
54       BufferedReader JavaDoc x = new BufferedReader JavaDoc(
55                 new InputStreamReader JavaDoc(p.getInputStream()));
56       
57       return x.readLine();
58     }
59     catch (IOException JavaDoc e)
60     {
61         e.printStackTrace();
62     }
63     
64     return null;
65   }
66   
67   /**
68    *
69    */

70   public String JavaDoc[] uuidgen(int nmbr)
71   {
72         String JavaDoc[] uuids = new String JavaDoc[nmbr];
73     for (int i=0; i<uuids.length; i++)
74       uuids[i] = uuidgen();
75   
76     return uuids;
77   }
78         
79                 
80   /***************************************************************************/
81   /***************************** TEST DRIVER *********************************/
82   /***************************************************************************/
83   
84         
85   public static void main(String JavaDoc args[])
86   {
87     UUIDGen generator = new NativeUUIDGen();
88     long start = System.currentTimeMillis();
89
90     for (int i = 1; i <= 100; ++i)
91         generator.uuidgen();
92
93     long end = System.currentTimeMillis();
94
95     System.out.println("\nNativeUUIDGen: Generation of 100 UUID's took " +
96                 (end-start)+" milliseconds.");
97   }
98 }
Popular Tags