This project has retired. For details please refer to its Attic page.
KeyGeneratorFactory xref
View Javadoc
1   /*
2    * Copyright 2001-2008 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   */
17  
18  package org.apache.juddi.keygen;
19  
20  import org.apache.commons.configuration.ConfigurationException;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.juddi.ClassUtil;
24  import org.apache.juddi.config.AppConfig;
25  import org.apache.juddi.config.Property;
26  
27  /**
28   * Used to create the org.apache.juddi.keygen.KeyGenerator implementation
29   * as specified by the 'juddi.keygenerator' property. Defaults to
30   * org.apache.juddi.cryptor.DefaultKeyGenerator if an implementation is not
31   * specified.
32   *
33   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
34   */
35  public abstract class KeyGeneratorFactory {
36  	private static Log log = LogFactory.getLog(KeyGeneratorFactory.class);
37  
38  	// Key Generator default implementation
39  	public static final String DEFAULT_IMPL = "org.apache.juddi.keygen.DefaultKeyGenerator";
40  
41  	// the shared Key Generator instance
42  	private static KeyGenerator keyGenerator = null;
43  
44  	/*
45  	 * Returns a new instance of a KeyGenerator.
46  	 * 
47  	 * @return KeyGenerator
48  	 */
49  	public static synchronized KeyGenerator getKeyGenerator() {
50  		if (keyGenerator == null)
51  			keyGenerator = createKeyGenerator();
52  		return keyGenerator;
53  	}
54  	
55  	public static synchronized KeyGenerator forceNewKeyGenerator() {
56  		keyGenerator = null;
57  		keyGenerator = createKeyGenerator();
58  		return keyGenerator;
59  	}
60  
61  	/*
62  	 * Returns a new instance of a Cryptor.
63  	 * 
64  	 * @return Cryptor
65  	 */
66  	private static synchronized KeyGenerator createKeyGenerator() {
67  		if (keyGenerator != null)
68  			return keyGenerator;
69  	
70  		// grab class name of the Cryptor implementation to create
71  		String className = System.getProperty(Property.JUDDI_KEYGENERATOR);
72                  if (className==null){
73                      try {
74                              // grab class name of the Authenticator implementation to create
75                              className = AppConfig.getConfiguration().getString(Property.JUDDI_KEYGENERATOR, DEFAULT_IMPL);
76                      }
77                      catch(ConfigurationException ce) {
78                              log.error("Configuration exception occurred retrieving: " + Property.JUDDI_KEYGENERATOR);
79                      }
80                  }
81                  try {
82                      // write the Cryptor implementation name to the log
83                      log.debug("Configuration Key Generator Implementation = " + AppConfig.getConfiguration().getString(Property.JUDDI_KEYGENERATOR));
84                  } catch (ConfigurationException ex) {
85                  }
86                  log.debug("SysProp Key Generator Implementation = " + System.getProperty(Property.JUDDI_KEYGENERATOR));
87                  log.debug("Using Key Generator Implementation = " + className);
88  	
89  		Class<?> keygenClass = null;
90  		try {
91  			// Use Loader to locate & load the Key Generator implementation
92  			keygenClass = ClassUtil.forName(className,KeyGeneratorFactory.class);
93  			// try to instantiate the Key Generator implementation
94  			keyGenerator = (KeyGenerator)keygenClass.newInstance();
95  		} catch(ClassNotFoundException cnfe) {
96  			throw new RuntimeException("The specified Key Generator class '" + className + "' was not found on classpath.",cnfe);
97  		} catch(InstantiationException ie) {
98  			throw new RuntimeException("The specified Key Generator class '" + className + "' cannot be instantiated.",ie);
99  		} catch(IllegalAccessException iae) {
100 			throw new RuntimeException("The specified Key Generator class '" + className + "' cannot be instantiated due to illegal access.",iae);
101 		} catch(Exception e) {
102 			throw new RuntimeException("Exception while attempting to instantiate the implementation of Key Generator: " + className + "\n" + e.getMessage());
103 		}
104 	
105 		return keyGenerator;
106 	}
107 }