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
025 * and 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     * @return factory object
037     * @throws NoSuchProviderException if the provider specified by 
038     *    {@link #setProvider(String)} is not known to the platform
039     * @throws NoSuchAlgorithmException if the algorithm specified by
040     *    {@link #setAlgorithm(String)} is not known to the specified provider
041     *    (or to the default platform provider if no provider is specified)
042     */
043    public TrustManagerFactory createTrustManagerFactory() throws NoSuchProviderException, NoSuchAlgorithmException {
044
045        return getProvider() != null ? TrustManagerFactory.getInstance(getAlgorithm(), getProvider()) : TrustManagerFactory.getInstance(getAlgorithm());
046    }
047
048    /**
049     * Gets the algorithm name for the trust manager factory.
050     * @return algorithm name (e.g. {@code PKIX}); the default algorithm
051     *    (obtained from {@link TrustManagerFactory#getDefaultAlgorithm()})
052     *    is returned if no algorithm has been configured
053     */
054    public String getAlgorithm() {
055        if (algorithm == null) {
056            return TrustManagerFactory.getDefaultAlgorithm();
057        }
058        return algorithm;
059    }
060
061    /**
062     * Sets the algorithm name for the trust manager factory.
063     * @param algorithm an algorithm name, which must be recognized by the
064     *    provider specified by {@link #setProvider(String)} or by the 
065     *    platform's default provider if no provider is specified. 
066     */
067    public void setAlgorithm(String algorithm) {
068        this.algorithm = algorithm;
069    }
070
071    /**
072     * Gets the JSSE provider name for the trust manager factory.
073     * @return provider name
074     */
075    public String getProvider() {
076        return provider;
077    }
078
079    /**
080     * Sets the JSSE provider name for the trust manager factory.
081     * @param provider name of the JSSE provider to utilize in creating the
082     *    trust manager factory
083     */
084    public void setProvider(String provider) {
085        this.provider = provider;
086    }
087
088}