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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 *16 */1718package org.apache.juddi.keygen;
1920import org.apache.commons.configuration.ConfigurationException;
21import org.apache.commons.logging.Log;
22import org.apache.commons.logging.LogFactory;
23import org.apache.juddi.ClassUtil;
24import org.apache.juddi.config.AppConfig;
25import org.apache.juddi.config.Property;
2627/**28 * Used to create the org.apache.juddi.keygen.KeyGenerator implementation29 * as specified by the 'juddi.keygenerator' property. Defaults to30 * org.apache.juddi.cryptor.DefaultKeyGenerator if an implementation is not31 * specified.32 *33 * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>34 */35publicabstractclassKeyGeneratorFactory {
36privatestatic Log log = LogFactory.getLog(KeyGeneratorFactory.class);
3738// Key Generator default implementation39publicstaticfinal String DEFAULT_IMPL = "org.apache.juddi.keygen.DefaultKeyGenerator";
4041// the shared Key Generator instance42privatestaticKeyGenerator keyGenerator = null;
4344/*45 * Returns a new instance of a KeyGenerator.46 * 47 * @return KeyGenerator48 */49publicstaticsynchronizedKeyGenerator getKeyGenerator() {
50if (keyGenerator == null)
51 keyGenerator = createKeyGenerator();
52return keyGenerator;
53 }
5455publicstaticsynchronizedKeyGenerator forceNewKeyGenerator() {
56 keyGenerator = null;
57 keyGenerator = createKeyGenerator();
58return keyGenerator;
59 }
6061/*62 * Returns a new instance of a Cryptor.63 * 64 * @return Cryptor65 */66privatestaticsynchronizedKeyGenerator createKeyGenerator() {
67if (keyGenerator != null)
68return keyGenerator;
6970// grab class name of the Cryptor implementation to create71 String className = System.getProperty(Property.JUDDI_KEYGENERATOR);
72if (className==null){
73try {
74// grab class name of the Authenticator implementation to create75 className = AppConfig.getConfiguration().getString(Property.JUDDI_KEYGENERATOR, DEFAULT_IMPL);
76 }
77catch(ConfigurationException ce) {
78 log.error("Configuration exception occurred retrieving: " + Property.JUDDI_KEYGENERATOR);
79 }
80 }
81try {
82// write the Cryptor implementation name to the log83 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);
8889 Class<?> keygenClass = null;
90try {
91// Use Loader to locate & load the Key Generator implementation92 keygenClass = ClassUtil.forName(className,KeyGeneratorFactory.class);
93// try to instantiate the Key Generator implementation94 keyGenerator = (KeyGenerator)keygenClass.newInstance();
95 } catch(ClassNotFoundException cnfe) {
96thrownew RuntimeException("The specified Key Generator class '" + className + "' was not found on classpath.",cnfe);
97 } catch(InstantiationException ie) {
98thrownew RuntimeException("The specified Key Generator class '" + className + "' cannot be instantiated.",ie);
99 } catch(IllegalAccessException iae) {
100thrownew RuntimeException("The specified Key Generator class '" + className + "' cannot be instantiated due to illegal access.",iae);
101 } catch(Exception e) {
102thrownew RuntimeException("Exception while attempting to instantiate the implementation of Key Generator: " + className + "\n" + e.getMessage());
103 }
104105return keyGenerator;
106 }
107 }