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 org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.apache.juddi.util.Config;
21  import org.apache.juddi.util.Loader;
22  
23  /***
24   * Used to create the org.apache.juddi.uuidgen.UUIDGen implementation
25   * as specified by the 'juddi.uuidgen.impl' property. Defaults to
26   * org.apache.juddi.uuidgen.SecureUUIDGen if an implementation is not
27   * specified.
28   *
29   * @author Steve Viens (sviens@apache.org)
30   */
31  public abstract class UUIDGenFactory
32  {
33    // private reference to the jUDDI logger
34    private static Log log = LogFactory.getLog(UUIDGenFactory.class);
35  
36    // UUIDGen property key & default implementation
37    private static final String IMPL_KEY = "juddi.uuidgen";
38    private static final String DEFAULT_IMPL = "org.apache.juddi.uuidgen.DefaultUUIDGen";
39  
40    // the shared UUIDGen instance
41    private static UUIDGen uuidgen = null;
42  
43    /***
44     * Returns a new instance of a UUIDGenFactory.
45     *
46     * @return UUIDGen
47     */
48    public static UUIDGen getUUIDGen()
49    {
50      if (uuidgen == null)
51        uuidgen = createUUIDGen();
52      return uuidgen;
53    }
54  
55    /***
56     * Returns a new instance of a UUIDGen.
57     *
58     * @return UUIDGen
59     */
60    private static synchronized UUIDGen createUUIDGen()
61    {
62      if (uuidgen != null)
63        return uuidgen;
64  
65      // grab class name of the UUIDGen implementation to create
66      String className = Config.getStringProperty(IMPL_KEY,DEFAULT_IMPL);
67  
68      // write the UUIDGen implementation name to the log
69      log.debug("UUIDGen Implementation = " + className);
70  
71      Class uuidgenClass = null;
72      try
73      {
74        // Use Loader to locate & load the UUIDGen implementation
75        uuidgenClass = Loader.getClassForName(className);
76      }
77      catch(ClassNotFoundException e)
78      {
79        log.error("The specified UUIDGen class '" + className +
80          "' was not found in classpath.");
81        log.error(e);
82      }
83  
84      try
85      {
86        // try to instantiate the UUIDGen implementation
87        uuidgen = (UUIDGen)uuidgenClass.newInstance();
88      }
89      catch(Exception e)
90      {
91        log.error("Exception while attempting to instantiate the " +
92          "implementation of UUIDGen: " + uuidgenClass.getName() +
93          "\n" + e.getMessage());
94        log.error(e);
95      }
96  
97      return uuidgen;
98    }
99  
100 
101   /****************************************************************************/
102   /****************************** TEST DRIVER *********************************/
103   /****************************************************************************/
104 
105 
106   public static void main(String[] args)
107   {
108     // number of UUID's to generate
109     final int max = 100;
110 
111     try
112     {
113       UUIDGen uuidgen = UUIDGenFactory.getUUIDGen();
114 
115       for (int i=0; i<max; ++i)
116         System.out.println( i + ":  " + uuidgen.uuidgen());
117     }
118     catch (Exception ex) { ex.printStackTrace(); }
119   }
120 }