001/**
002 *  Copyright (C) 2016 Gary Gregory. All rights reserved.
003 *
004 *  See the NOTICE.txt file distributed with this work for additional
005 *  information regarding copyright ownership.
006 *  
007 *  Licensed under the Apache License, Version 2.0 (the "License");
008 *  you may not use this file except in compliance with the License.
009 *  You may obtain a copy of the License at
010 *  
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *  
013 *  Unless required by applicable law or agreed to in writing, software
014 *  distributed under the License is distributed on an "AS IS" BASIS,
015 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 *  See the License for the specific language governing permissions and
017 *  limitations under the License.
018 */
019
020package com.garygregory.jcommander.converters.net.ssl;
021
022import java.security.NoSuchAlgorithmException;
023import java.security.NoSuchProviderException;
024import java.security.Provider;
025
026import javax.net.ssl.TrustManagerFactory;
027
028import com.garygregory.jcommander.converters.AbstractBaseConverter;
029
030/**
031 * Converts a {@link String} into a {@link TrustManagerFactory}.
032 * <p>
033 * For a description of the format, see {@link TrustManagerFactory#getInstance(String)}.
034 * </p>
035 * <p>
036 * To get a TrustManagerFactory from a specific {@link Provider}, use the syntax {@code algorithm:provider} as described by
037 * {@link TrustManagerFactory#getInstance(String, String)}.
038 * </p>
039 * 
040 * <p>
041 * Example:
042 * </p>
043 * 
044 * <pre class="prettyprint">
045 * <code class="language-java">&#64;Parameter(names = { "--trustManagerFactory" }, converter = TrustManagerFactoryConverter.class)
046 * private TrustManagerFactory trustManagerFactory;</code>
047 * </pre>
048 * <p>
049 * 
050 * @see TrustManagerFactory
051 * @see TrustManagerFactory#getInstance(String)
052 * @see TrustManagerFactory#getInstance(String, String)
053 * 
054 * @since 1.0.0
055 * @author <a href="mailto:ggregory@garygregory.com">Gary Gregory</a>
056 */
057public class TrustManagerFactoryConverter extends AbstractBaseConverter<TrustManagerFactory> {
058
059    /**
060     * Constructs a converter.
061     * 
062     * @param optionName
063     *            The option name, may be null.
064     */
065    public TrustManagerFactoryConverter(final String optionName) {
066        super(optionName, TrustManagerFactory.class);
067    }
068
069    @Override
070    protected TrustManagerFactory convertImpl(final String value) throws NoSuchAlgorithmException, NoSuchProviderException {
071        final String[] split = split(value);
072        final String algorithm = split[0];
073        return isSingle(split) ? TrustManagerFactory.getInstance(value) : TrustManagerFactory.getInstance(algorithm, split[1]);
074    }
075
076}