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.KeyManagerFactory;
020
021/**
022 * A factory bean for a JSSE {@link KeyManagerFactory}.
023 * <p>
024 * This object holds the configurable properties of a key manager factory and
025 * uses them to create and load a {@link KeyManagerFactory} instance.
026 *
027 * @author Carl Harris
028 */
029public class KeyManagerFactoryFactoryBean {
030
031    private String algorithm;
032    private String provider;
033
034    /**
035     * Creates a {@link KeyManagerFactory} 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 KeyManagerFactory createKeyManagerFactory() throws NoSuchProviderException, NoSuchAlgorithmException {
048
049        return getProvider() != null ? KeyManagerFactory.getInstance(getAlgorithm(), getProvider())
050                : KeyManagerFactory.getInstance(getAlgorithm());
051    }
052
053    /**
054     * Gets the algorithm name for the key manager factory.
055     * 
056     * @return algorithm name (e.g. {@code SunX509}); the default algorithm
057     *         (obtained from {@link KeyManagerFactory#getDefaultAlgorithm()}) is
058     *         returned if no algorithm has been configured
059     */
060    public String getAlgorithm() {
061        if (algorithm == null) {
062            return KeyManagerFactory.getDefaultAlgorithm();
063        }
064        return algorithm;
065    }
066
067    /**
068     * Sets the algorithm name for the key 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 key 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 key manager factory.
089     * 
090     * @param provider name of the JSSE provider to utilize in creating the key
091     *                 manager factory
092     */
093    public void setProvider(String provider) {
094        this.provider = provider;
095    }
096
097}