001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.net.ssl;
015
016import java.security.NoSuchAlgorithmException;
017import java.security.NoSuchProviderException;
018
019import javax.net.ssl.TrustManagerFactory;
020
021/**
022 * A factory bean for a JSSE {@link TrustManagerFactory}.
023 * <p>
024 * This object holds the configurable properties of a trust manager factory and
025 * uses them to create and load a {@link TrustManagerFactory} instance.
026 *
027 * @author Carl Harris
028 */
029public class TrustManagerFactoryFactoryBean {
030
031    private String algorithm;
032    private String provider;
033
034    /**
035     * Creates a {@link TrustManagerFactory} using the receiver's configuration.
036     * 
037     * @return factory object
038     * @throws NoSuchProviderException  if the provider specified by
039     *                                  {@link #setProvider(String)} is not known to
040     *                                  the platform
041     * @throws NoSuchAlgorithmException if the algorithm specified by
042     *                                  {@link #setAlgorithm(String)} is not known
043     *                                  to the specified provider (or to the default
044     *                                  platform provider if no provider is
045     *                                  specified)
046     */
047    public TrustManagerFactory createTrustManagerFactory() throws NoSuchProviderException, NoSuchAlgorithmException {
048
049        return getProvider() != null ? TrustManagerFactory.getInstance(getAlgorithm(), getProvider())
050                : TrustManagerFactory.getInstance(getAlgorithm());
051    }
052
053    /**
054     * Gets the algorithm name for the trust manager factory.
055     * 
056     * @return algorithm name (e.g. {@code PKIX}); the default algorithm (obtained
057     *         from {@link TrustManagerFactory#getDefaultAlgorithm()}) is returned
058     *         if no algorithm has been configured
059     */
060    public String getAlgorithm() {
061        if (algorithm == null) {
062            return TrustManagerFactory.getDefaultAlgorithm();
063        }
064        return algorithm;
065    }
066
067    /**
068     * Sets the algorithm name for the trust manager factory.
069     * 
070     * @param algorithm an algorithm name, which must be recognized by the provider
071     *                  specified by {@link #setProvider(String)} or by the
072     *                  platform's default provider if no provider is specified.
073     */
074    public void setAlgorithm(String algorithm) {
075        this.algorithm = algorithm;
076    }
077
078    /**
079     * Gets the JSSE provider name for the trust manager factory.
080     * 
081     * @return provider name
082     */
083    public String getProvider() {
084        return provider;
085    }
086
087    /**
088     * Sets the JSSE provider name for the trust manager factory.
089     * 
090     * @param provider name of the JSSE provider to utilize in creating the trust
091     *                 manager factory
092     */
093    public void setProvider(String provider) {
094        this.provider = provider;
095    }
096
097}