1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.net.ssl;
15
16 import java.io.FileNotFoundException;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.URL;
20 import java.security.KeyStore;
21 import java.security.KeyStoreException;
22 import java.security.NoSuchAlgorithmException;
23 import java.security.NoSuchProviderException;
24
25 import ch.qos.logback.core.util.LocationUtil;
26
27
28
29
30
31
32
33
34
35 public class KeyStoreFactoryBean {
36
37 private String location;
38 private String provider;
39 private String type;
40 private String password;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public KeyStore createKeyStore() throws NoSuchProviderException, NoSuchAlgorithmException, KeyStoreException {
59
60 if (getLocation() == null) {
61 throw new IllegalArgumentException("location is required");
62 }
63
64 InputStream inputStream = null;
65 try {
66 URL url = LocationUtil.urlForResource(getLocation());
67 inputStream = url.openStream();
68 KeyStore keyStore = newKeyStore();
69 keyStore.load(inputStream, getPassword().toCharArray());
70 return keyStore;
71 } catch (NoSuchProviderException ex) {
72 throw new NoSuchProviderException("no such keystore provider: " + getProvider());
73 } catch (NoSuchAlgorithmException ex) {
74 throw new NoSuchAlgorithmException("no such keystore type: " + getType());
75 } catch (FileNotFoundException ex) {
76 throw new KeyStoreException(getLocation() + ": file not found");
77 } catch (Exception ex) {
78 throw new KeyStoreException(getLocation() + ": " + ex.getMessage(), ex);
79 } finally {
80 try {
81 if (inputStream != null) {
82 inputStream.close();
83 }
84 } catch (IOException ex) {
85 ex.printStackTrace(System.err);
86 }
87 }
88 }
89
90
91
92
93
94 private KeyStore newKeyStore() throws NoSuchAlgorithmException, NoSuchProviderException, KeyStoreException {
95
96 return getProvider() != null ? KeyStore.getInstance(getType(), getProvider()) : KeyStore.getInstance(getType());
97 }
98
99
100
101
102
103
104 public String getLocation() {
105 return location;
106 }
107
108
109
110
111
112
113
114
115 public void setLocation(String location) {
116 this.location = location;
117 }
118
119
120
121
122
123
124
125
126 public String getType() {
127 if (type == null) {
128 return SSL.DEFAULT_KEYSTORE_TYPE;
129 }
130 return type;
131 }
132
133
134
135
136
137
138
139
140
141 public void setType(String type) {
142 this.type = type;
143 }
144
145
146
147
148
149
150 public String getProvider() {
151 return provider;
152 }
153
154
155
156
157
158
159 public void setProvider(String provider) {
160 this.provider = provider;
161 }
162
163
164
165
166
167
168
169 public String getPassword() {
170 if (password == null) {
171 return SSL.DEFAULT_KEYSTORE_PASSWORD;
172 }
173 return password;
174 }
175
176
177
178
179
180
181 public void setPassword(String password) {
182 this.password = password;
183 }
184
185 }