1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.soap.security.support;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.security.GeneralSecurityException;
22 import java.security.KeyStore;
23
24 import org.springframework.beans.factory.FactoryBean;
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.core.io.Resource;
27 import org.springframework.util.StringUtils;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32
33
34
35
36
37
38
39
40
41
42
43 public class KeyStoreFactoryBean implements FactoryBean<KeyStore>, InitializingBean {
44
45 private static final Log logger = LogFactory.getLog(KeyStoreFactoryBean.class);
46
47 private KeyStore keyStore;
48
49 private String type;
50
51 private String provider;
52
53 private Resource location;
54
55 private char[] password;
56
57
58
59
60
61
62 public void setLocation(Resource location) {
63 this.location = location;
64 }
65
66
67
68
69
70 public void setPassword(String password) {
71 if (password != null) {
72 this.password = password.toCharArray();
73 }
74 }
75
76
77 public void setProvider(String provider) {
78 this.provider = provider;
79 }
80
81
82
83
84
85
86 public void setType(String type) {
87 this.type = type;
88 }
89
90 public KeyStore getObject() {
91 return keyStore;
92 }
93
94 public Class<KeyStore> getObjectType() {
95 return KeyStore.class;
96 }
97
98 public boolean isSingleton() {
99 return true;
100 }
101
102 public final void afterPropertiesSet() throws GeneralSecurityException, IOException {
103 if (StringUtils.hasLength(provider) && StringUtils.hasLength(type)) {
104 keyStore = KeyStore.getInstance(type, provider);
105 }
106 else if (StringUtils.hasLength(type)) {
107 keyStore = KeyStore.getInstance(type);
108 }
109 else {
110 keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
111 }
112 InputStream is = null;
113 try {
114 if (location != null && location.exists()) {
115 is = location.getInputStream();
116 if (logger.isInfoEnabled()) {
117 logger.info("Loading key store from " + location);
118 }
119 }
120 else if (logger.isWarnEnabled()) {
121 logger.warn("Creating empty key store");
122 }
123 keyStore.load(is, password);
124 }
125 finally {
126 if (is != null) {
127 is.close();
128 }
129 }
130 }
131 }