View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.net.ssl;
15  
16  import java.security.NoSuchAlgorithmException;
17  import java.security.NoSuchProviderException;
18  
19  import javax.net.ssl.KeyManagerFactory;
20  
21  /**
22   * A factory bean for a JSSE {@link KeyManagerFactory}.
23   * <p>
24   * This object holds the configurable properties of a key manager factory
25   * and uses them to create and load a {@link KeyManagerFactory} instance.
26   *
27   * @author Carl Harris
28   */
29  public class KeyManagerFactoryFactoryBean {
30  
31      private String algorithm;
32      private String provider;
33  
34      /**
35       * Creates a {@link KeyManagerFactory} using the receiver's configuration.
36       * @return factory object
37       * @throws NoSuchProviderException if the provider specified by 
38       *    {@link #setProvider(String)} is not known to the platform
39       * @throws NoSuchAlgorithmException if the algorithm specified by
40       *    {@link #setAlgorithm(String)} is not known to the specified provider
41       *    (or to the default platform provider if no provider is specified)
42       */
43      public KeyManagerFactory createKeyManagerFactory() throws NoSuchProviderException, NoSuchAlgorithmException {
44  
45          return getProvider() != null ? KeyManagerFactory.getInstance(getAlgorithm(), getProvider()) : KeyManagerFactory.getInstance(getAlgorithm());
46      }
47  
48      /**
49       * Gets the algorithm name for the key manager factory.
50       * @return algorithm name (e.g. {@code SunX509}); the default algorithm
51       *    (obtained from {@link KeyManagerFactory#getDefaultAlgorithm()})
52       *    is returned if no algorithm has been configured
53       */
54      public String getAlgorithm() {
55          if (algorithm == null) {
56              return KeyManagerFactory.getDefaultAlgorithm();
57          }
58          return algorithm;
59      }
60  
61      /**
62       * Sets the algorithm name for the key manager factory.
63       * @param algorithm an algorithm name, which must be recognized by the
64       *    provider specified by {@link #setProvider(String)} or by the 
65       *    platform's default provider if no provider is specified. 
66       */
67      public void setAlgorithm(String algorithm) {
68          this.algorithm = algorithm;
69      }
70  
71      /**
72       * Gets the JSSE provider name for the key manager factory.
73       * @return provider name
74       */
75      public String getProvider() {
76          return provider;
77      }
78  
79      /**
80       * Sets the JSSE provider name for the key manager factory.
81       * @param provider name of the JSSE provider to utilize in creating the
82       *    key manager factory
83       */
84      public void setProvider(String provider) {
85          this.provider = provider;
86      }
87  
88  }