|
| 1 | +/* |
| 2 | + * Copyright 2005-2012 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.security.KeyStore; |
| 20 | +import javax.net.ssl.KeyManager; |
| 21 | +import javax.net.ssl.KeyManagerFactory; |
| 22 | + |
| 23 | +import org.springframework.beans.factory.FactoryBean; |
| 24 | +import org.springframework.beans.factory.InitializingBean; |
| 25 | +import org.springframework.util.StringUtils; |
| 26 | + |
| 27 | +/** |
| 28 | + * Spring factory bean for an array of {@link KeyManager}s. |
| 29 | + * <p/> |
| 30 | + * Uses the {@link KeyManagerFactory} to create the {@code KeyManager}s. |
| 31 | + * |
| 32 | + * @author Stephen More |
| 33 | + * @author Arjen Poutsma |
| 34 | + * @see KeyManager |
| 35 | + * @see KeyManagerFactory |
| 36 | + * @since 2.1.2 |
| 37 | + */ |
| 38 | +public class KeyManagersFactoryBean implements FactoryBean<KeyManager[]>, InitializingBean { |
| 39 | + |
| 40 | + private KeyManagerFactory keyManagerFactory; |
| 41 | + |
| 42 | + private KeyStore keyStore; |
| 43 | + |
| 44 | + private String algorithm; |
| 45 | + |
| 46 | + private String provider; |
| 47 | + |
| 48 | + private char[] password; |
| 49 | + |
| 50 | + /** |
| 51 | + * Sets the password to use for integrity checking. If this property is not set, then integrity checking is not |
| 52 | + * performed. |
| 53 | + */ |
| 54 | + public void setPassword(String password) { |
| 55 | + if (password != null) { |
| 56 | + this.password = password.toCharArray(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Sets the provider of the key store to use. If this is not set, the default is used. |
| 62 | + */ |
| 63 | + public void setProvider(String provider) { |
| 64 | + this.provider = provider; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Sets the algorithm of the <code>KeyManager</code> to use. If this is not set, the default is used. |
| 69 | + * |
| 70 | + * @see KeyManagerFactory#getDefaultAlgorithm() |
| 71 | + */ |
| 72 | + public void setAlgorithm(String algorithm) { |
| 73 | + this.algorithm = algorithm; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Sets the source of key material. |
| 78 | + * |
| 79 | + * @see KeyManagerFactory#init(KeyStore, char[]) |
| 80 | + */ |
| 81 | + public void setKeyStore(KeyStore keyStore) { |
| 82 | + this.keyStore = keyStore; |
| 83 | + } |
| 84 | + |
| 85 | + public KeyManager[] getObject() throws Exception { |
| 86 | + return keyManagerFactory.getKeyManagers(); |
| 87 | + } |
| 88 | + |
| 89 | + public Class<?> getObjectType() { |
| 90 | + return KeyManager[].class; |
| 91 | + } |
| 92 | + |
| 93 | + public boolean isSingleton() { |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + public void afterPropertiesSet() throws Exception { |
| 98 | + String algorithm = |
| 99 | + StringUtils.hasLength(this.algorithm) ? this.algorithm : KeyManagerFactory.getDefaultAlgorithm(); |
| 100 | + |
| 101 | + keyManagerFactory = |
| 102 | + StringUtils.hasLength(this.provider) ? KeyManagerFactory.getInstance(algorithm, this.provider) : |
| 103 | + KeyManagerFactory.getInstance(algorithm); |
| 104 | + |
| 105 | + keyManagerFactory.init(keyStore, password); |
| 106 | + } |
| 107 | +} |
0 commit comments