View Javadoc

1   /*
2    * Copyright 2006 the original author or authors.
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.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.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  import org.springframework.beans.factory.FactoryBean;
28  import org.springframework.beans.factory.InitializingBean;
29  import org.springframework.core.io.Resource;
30  import org.springframework.util.StringUtils;
31  
32  /**
33   * Spring factory bean for a {@link KeyStore}.
34   * <p/>
35   * To load an existing key store, you must set the <code>location</code> property. If this property is not set, a new,
36   * empty key store is created, which is most likely not what you want.
37   *
38   * @author Arjen Poutsma
39   * @see #setLocation(org.springframework.core.io.Resource)
40   * @see KeyStore
41   * @since 1.0.0
42   */
43  public class KeyStoreFactoryBean implements FactoryBean, 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       * Sets the location of the key store to use. If this is not set, a new, empty key store will be used.
59       *
60       * @see KeyStore#load(java.io.InputStream,char[])
61       */
62      public void setLocation(Resource location) {
63          this.location = location;
64      }
65  
66      /**
67       * Sets the password to use for integrity checking. If this property is not set, then integrity checking is not
68       * performed.
69       */
70      public void setPassword(String password) {
71          if (password != null) {
72              this.password = password.toCharArray();
73          }
74      }
75  
76      /** Sets the provider of the key store to use. If this is not set, the default is used. */
77      public void setProvider(String provider) {
78          this.provider = provider;
79      }
80  
81      /**
82       * Sets the type of the <code>KeyStore</code> to use. If this is not set, the default is used.
83       *
84       * @see KeyStore#getDefaultType()
85       */
86      public void setType(String type) {
87          this.type = type;
88      }
89  
90      public Object getObject() {
91          return keyStore;
92      }
93  
94      public Class 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 }