This project has retired. For details please refer to its
Attic page.
CryptorFactory xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
31
32
33
34
35
36
37
38 public abstract class CryptorFactory {
39
40 private static final Log log = LogFactory.getLog(CryptorFactory.class);
41
42 private static Cryptor cryptor = null;
43 private static Map<String, Cryptor> cache = new HashMap<String, Cryptor>();
44
45
46
47
48
49
50
51
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
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
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
82
83
84
85 public static synchronized Cryptor getCryptor() {
86 if (cryptor == null) {
87 cryptor = createCryptor();
88 }
89 return cryptor;
90 }
91
92
93
94
95
96
97 private static synchronized Cryptor createCryptor() {
98 if (cryptor != null) {
99 return cryptor;
100 }
101
102
103 String className = Property.DEFAULT_CRYPTOR;
104
105
106 log.debug("Cryptor Implementation = " + className);
107
108 Class<?> cryptorClass = null;
109 try {
110
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
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 }