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.TrustManagerFactory;
20  
21  /**
22   * A factory bean for a JSSE {@link TrustManagerFactory}.
23   * <p>
24   * This object holds the configurable properties of a trust manager factory and
25   * uses them to create and load a {@link TrustManagerFactory} instance.
26   *
27   * @author Carl Harris
28   */
29  public class TrustManagerFactoryFactoryBean {
30  
31      private String algorithm;
32      private String provider;
33  
34      /**
35       * Creates a {@link TrustManagerFactory} using the receiver's configuration.
36       * 
37       * @return factory object
38       * @throws NoSuchProviderException  if the provider specified by
39       *                                  {@link #setProvider(String)} is not known to
40       *                                  the platform
41       * @throws NoSuchAlgorithmException if the algorithm specified by
42       *                                  {@link #setAlgorithm(String)} is not known
43       *                                  to the specified provider (or to the default
44       *                                  platform provider if no provider is
45       *                                  specified)
46       */
47      public TrustManagerFactory createTrustManagerFactory() throws NoSuchProviderException, NoSuchAlgorithmException {
48  
49          return getProvider() != null ? TrustManagerFactory.getInstance(getAlgorithm(), getProvider())
50                  : TrustManagerFactory.getInstance(getAlgorithm());
51      }
52  
53      /**
54       * Gets the algorithm name for the trust manager factory.
55       * 
56       * @return algorithm name (e.g. {@code PKIX}); the default algorithm (obtained
57       *         from {@link TrustManagerFactory#getDefaultAlgorithm()}) is returned
58       *         if no algorithm has been configured
59       */
60      public String getAlgorithm() {
61          if (algorithm == null) {
62              return TrustManagerFactory.getDefaultAlgorithm();
63          }
64          return algorithm;
65      }
66  
67      /**
68       * Sets the algorithm name for the trust manager factory.
69       * 
70       * @param algorithm an algorithm name, which must be recognized by the provider
71       *                  specified by {@link #setProvider(String)} or by the
72       *                  platform's default provider if no provider is specified.
73       */
74      public void setAlgorithm(String algorithm) {
75          this.algorithm = algorithm;
76      }
77  
78      /**
79       * Gets the JSSE provider name for the trust manager factory.
80       * 
81       * @return provider name
82       */
83      public String getProvider() {
84          return provider;
85      }
86  
87      /**
88       * Sets the JSSE provider name for the trust manager factory.
89       * 
90       * @param provider name of the JSSE provider to utilize in creating the trust
91       *                 manager factory
92       */
93      public void setProvider(String provider) {
94          this.provider = provider;
95      }
96  
97  }