This project has retired. For details please refer to its Attic page.
CryptorFactory 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  package org.apache.juddi.v3.client.cryptor;
18  
19  import java.io.BufferedReader;
20  import java.io.File;
21  import java.io.FileReader;
22  import java.util.HashMap;
23  import java.util.Map;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.juddi.v3.client.ClassUtil;
27  import org.apache.juddi.v3.client.config.Property;
28  
29  /**
30   * Used to create the org.apache.juddi.cryptor.Cryptor implementation as
31   * specified by the 'juddi.cryptor' property. Defaults to
32   * org.apache.juddi.cryptor.DefaultCryptor if an implementation is not
33   * specified.
34   *
35   * @author Steve Viens (sviens@apache.org)
36   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
37   */
38  public abstract class CryptorFactory {
39  
40          private static final Log log = LogFactory.getLog(CryptorFactory.class);
41          // the shared Cryptor instance
42          private static Cryptor cryptor = null;
43          private static Map<String, Cryptor> cache = new HashMap<String, Cryptor>();
44  
45          /**
46           * Returns a crypto provider with the matching classname, throws if not
47           * possible
48           *
49           * @param className
50           * @return a Cryptor instance
51           * @throws Exception
52           */
53          public static Cryptor getCryptor(String className) throws Exception {
54                  if (cache.containsKey(className)) {
55                          return cache.get(className);
56                  }
57                  Class<?> cryptorClass = null;
58                  try {
59                          // Use Loader to locate & load the Cryptor implementation
60                          cryptorClass = ClassUtil.forName(className, CryptorFactory.class);
61                  } catch (ClassNotFoundException e) {
62                          log.error("The specified Cryptor class '" + className + "' was not found in classpath.");
63                          log.error(e);
64                          throw e;
65                  }
66  
67                  try {
68                          // try to instantiate the Cryptor implementation
69                          cryptor = (Cryptor) cryptorClass.newInstance();
70                          cache.put(className, cryptor);
71                  } catch (Exception e) {
72                          log.error("Exception while attempting to instantiate the implementation of Cryptor: " + cryptorClass.getName() + "\n" + e.getMessage());
73                          log.error(e);
74                          throw e;
75                  }
76  
77                  return cryptor;
78          }
79  
80          /**
81           * Returns a new instance of a CryptorFactory.
82           *
83           * @return Cryptor
84           */
85          public static synchronized Cryptor getCryptor() {
86                  if (cryptor == null) {
87                          cryptor = createCryptor();
88                  }
89                  return cryptor;
90          }
91  
92          /*
93           * Returns a new instance of a Cryptor.
94           * 
95           * @return Cryptor
96           */
97          private static synchronized Cryptor createCryptor() {
98                  if (cryptor != null) {
99                          return cryptor;
100                 }
101 
102                 // grab class name of the Cryptor implementation to create
103                 String className = Property.DEFAULT_CRYPTOR;
104 
105                 // write the Cryptor implementation name to the log
106                 log.debug("Cryptor Implementation = " + className);
107 
108                 Class<?> cryptorClass = null;
109                 try {
110                         // Use Loader to locate & load the Cryptor implementation
111                         cryptorClass = ClassUtil.forName(className, CryptorFactory.class);
112                 } catch (ClassNotFoundException e) {
113                         log.error("The specified Cryptor class '" + className + "' was not found in classpath.");
114                         log.error(e);
115                 }
116 
117                 if (cryptorClass != null) {
118                         try {
119                                 // try to instantiate the Cryptor implementation
120                                 cryptor = (Cryptor) cryptorClass.newInstance();
121                         } catch (Exception e) {
122                                 log.error("Exception while attempting to instantiate the implementation of Cryptor: " + cryptorClass.getName() + "\n" + e.getMessage());
123                                 log.error(e);
124                         }
125                 }
126 
127                 return cryptor;
128         }
129 
130         protected static String loadKeyFromFile(String provider) {
131                 String fs = System.getProperty("juddi.encryptionKeyFile." + provider);
132                 if (fs == null || fs.length() == 0) {
133                         return null;
134                 }
135                 File cwd = new File(".");
136                 log.debug("CWD="+cwd.getAbsolutePath());
137                 File f = new File(fs);
138                 if (f.exists() && f.isFile()) {
139                         BufferedReader reader = null;
140                         try {
141                                 reader = new BufferedReader(new FileReader(f));
142                                 String line = null;
143                                 StringBuilder stringBuilder = new StringBuilder();
144                                 String ls = System.getProperty("line.separator");
145 
146                                 while ((line = reader.readLine()) != null) {
147                                         stringBuilder.append(line);
148                                         stringBuilder.append(ls);
149                                 }
150                                 reader.close();
151 
152                                 return stringBuilder.toString().trim();
153                         } catch (Exception ex) {
154                                 log.warn("the system property juddi.encryptionKeyFile."  + provider+" is defined, however there was an error reading the file!", ex);
155                         } finally {
156                                 if (reader != null) {
157                                         try {
158                                                 reader.close();
159                                         } catch (Exception ex) {
160 
161                                         }
162                                 }
163                         }
164                 } else {
165                         log.warn("the system property juddi.encryptionKeyFile."  + provider+" is defined, however that file either couldn't be found or isn't a file");
166                 }
167                 return null;
168         }
169 }