1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.soap.security.wss4j.support;
18
19 import java.io.IOException;
20 import java.util.Properties;
21
22 import org.springframework.beans.factory.BeanClassLoaderAware;
23 import org.springframework.beans.factory.FactoryBean;
24 import org.springframework.beans.factory.InitializingBean;
25 import org.springframework.core.io.ClassPathResource;
26 import org.springframework.core.io.Resource;
27 import org.springframework.util.Assert;
28
29 import org.apache.ws.security.components.crypto.Crypto;
30 import org.apache.ws.security.components.crypto.CryptoFactory;
31 import org.apache.ws.security.components.crypto.Merlin;
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class CryptoFactoryBean implements FactoryBean<Crypto>, BeanClassLoaderAware, InitializingBean {
46
47 private Properties configuration = new Properties();
48
49 private ClassLoader classLoader;
50
51 private Crypto crypto;
52
53 private static final String CRYPTO_PROVIDER_PROPERTY = "org.apache.ws.security.crypto.provider";
54
55
56
57
58
59
60
61 public void setConfiguration(Properties properties) {
62 Assert.notNull(properties, "'properties' must not be null");
63 this.configuration.putAll(properties);
64 }
65
66
67
68
69
70
71
72
73
74 public void setCryptoProvider(Class<? extends Crypto> cryptoProviderClass) {
75 this.configuration.setProperty(CRYPTO_PROVIDER_PROPERTY, cryptoProviderClass.getName());
76 }
77
78
79
80
81
82
83
84
85
86
87 public void setKeyStoreLocation(Resource location) throws IOException {
88 String resourcePath = getResourcePath(location);
89 this.configuration.setProperty("org.apache.ws.security.crypto.merlin.file", resourcePath);
90 }
91
92 private String getResourcePath(Resource resource) throws IOException {
93 try {
94 return resource.getFile().getAbsolutePath();
95 }
96 catch (IOException ex) {
97 if (resource instanceof ClassPathResource) {
98 ClassPathResource classPathResource = (ClassPathResource) resource;
99 return classPathResource.getPath();
100 }
101 else {
102 throw ex;
103 }
104 }
105 }
106
107
108
109
110
111
112
113
114 public void setKeyStoreProvider(String provider) {
115 this.configuration.setProperty("org.apache.ws.security.crypto.merlin.keystore.provider", provider);
116 }
117
118
119
120
121
122
123
124
125 public void setKeyStorePassword(String password) {
126 this.configuration.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", password);
127 }
128
129
130
131
132
133
134
135
136 public void setKeyStoreType(String type) {
137 this.configuration.setProperty("org.apache.ws.security.crypto.merlin.keystore.type", type);
138 }
139
140
141
142
143
144
145
146
147
148
149
150 public void setTrustStorePassword(String password) {
151 this.configuration.setProperty("org.apache.ws.security.crypto.merlin.cacerts.password", password);
152 }
153
154
155
156
157
158
159
160
161
162
163 public void setDefaultX509Alias(String defaultX509Alias) {
164 this.configuration.setProperty("org.apache.ws.security.crypto.merlin.keystore.alias", defaultX509Alias);
165 }
166
167 public void setBeanClassLoader(ClassLoader classLoader) {
168 this.classLoader = classLoader;
169 }
170
171 public void afterPropertiesSet() throws Exception {
172 if (!configuration.containsKey(CRYPTO_PROVIDER_PROPERTY)) {
173 configuration.setProperty(CRYPTO_PROVIDER_PROPERTY, Merlin.class.getName());
174 }
175 this.crypto = CryptoFactory.getInstance(configuration, classLoader);
176 }
177
178 public Class<Crypto> getObjectType() {
179 return Crypto.class;
180 }
181
182 public boolean isSingleton() {
183 return true;
184 }
185
186 public Crypto getObject() throws Exception {
187 return crypto;
188 }
189
190 }