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  package org.apache.juddi.cryptor;
18  
19  import org.apache.commons.configuration.ConfigurationException;
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.juddi.ClassUtil;
23  import org.apache.juddi.config.AppConfig;
24  import org.apache.juddi.config.Property;
25  import org.apache.juddi.v3.client.cryptor.Cryptor;
26  
27  /**
28   * Used to create the org.apache.juddi.cryptor.Cryptor implementation as
29   * specified by the 'juddi.cryptor' property. Defaults to
30   * org.apache.juddi.cryptor.DefaultCryptor if an implementation is not
31   * specified.<Br>
32   * <br>
33   * This class is a special instance that loads from the juddi server side config file.<br>
34   *
35   * Use org.apache.juddi.v3.client.cryptor.CryptorFactor for all client side actions
36   * @see org.apache.juddi.v3.client.cryptor.CryptorFactory
37   * @author Steve Viens (sviens@apache.org)
38   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
39   */
40  
41  public abstract class CryptorFactory {
42  
43      private static final Log log = LogFactory.getLog(CryptorFactory.class);
44      // the shared Cryptor instance
45      private static Cryptor cryptor = null;
46  
47      /*
48       * Returns a new instance of a CryptorFactory.
49       * 
50       * @return Cryptor
51       */
52      public static synchronized Cryptor getCryptor() {
53          if (cryptor == null) {
54              cryptor = createCryptor();
55          }
56          return cryptor;
57      }
58  
59      public static synchronized Cryptor getCryptor(String className) throws Exception {
60          return org.apache.juddi.v3.client.cryptor.CryptorFactory.getCryptor(className);
61          
62      }
63  
64      /*
65       * Returns a new instance of a Cryptor.
66       * 
67       * @return Cryptor
68       */
69      private static synchronized Cryptor createCryptor() {
70          if (cryptor != null) {
71              return cryptor;
72          }
73  
74          // grab class name of the Cryptor implementation to create
75          String className = Property.DEFAULT_CRYPTOR;
76          try {
77              // grab class name of the Authenticator implementation to create
78              className = AppConfig.getConfiguration().getString(Property.JUDDI_CRYPTOR, Property.DEFAULT_CRYPTOR);
79          } catch (ConfigurationException ce) {
80              log.error("Configuration exception occurred retrieving: " + Property.JUDDI_CRYPTOR, ce);
81          }
82  
83          // write the Cryptor implementation name to the log
84          log.debug("Cryptor Implementation = " + className);
85  
86          Class<?> cryptorClass = null;
87          try {
88              // Use Loader to locate & load the Cryptor implementation
89              cryptorClass = ClassUtil.forName(className, CryptorFactory.class);
90          } catch (ClassNotFoundException e) {
91              log.error("The specified Cryptor class '" + className + "' was not found in classpath.");
92              log.error(e);
93          }
94  
95          if (cryptorClass != null) {
96              try {
97                  // try to instantiate the Cryptor implementation
98                  cryptor = (Cryptor) cryptorClass.newInstance();
99              } catch (Exception e) {
100                 log.error("Exception while attempting to instantiate the implementation of Cryptor: " + cryptorClass.getName() + "\n" + e.getMessage());
101                 log.error(e);
102             }
103         }
104 
105         return cryptor;
106     }
107 }