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.KeyManagerFactory; 027 028import com.garygregory.jcommander.converters.AbstractBaseConverter; 029 030/** 031 * Converts a {@link String} into a {@link KeyManagerFactory}. 032 * <p> 033 * For a description of the format, see {@link KeyManagerFactory#getInstance(String)}. 034 * </p> 035 * <p> 036 * To get a KeyManagerFactory from a specific {@link Provider}, use the syntax {@code algorithm:provider} as described by 037 * {@link KeyManagerFactory#getInstance(String, String)}. 038 * </p> 039 * 040 * <p> 041 * Example: 042 * </p> 043 * 044 * <pre class="prettyprint"> 045 * <code class="language-java">@Parameter(names = { "--keyManagerFactory" }, converter = KeyManagerFactoryConverter.class) 046 * private KeyManagerFactory keyManagerFactory;</code> 047 * </pre> 048 * <p> 049 * 050 * @see KeyManagerFactory 051 * @see KeyManagerFactory#getInstance(String) 052 * @see KeyManagerFactory#getInstance(String, String) 053 * 054 * @since 1.0.0 055 * @author <a href="mailto:ggregory@garygregory.com">Gary Gregory</a> 056 */ 057public class KeyManagerFactoryConverter extends AbstractBaseConverter<KeyManagerFactory> { 058 059 /** 060 * Constructs a converter. 061 * 062 * @param optionName 063 * The option name, may be null. 064 */ 065 public KeyManagerFactoryConverter(final String optionName) { 066 super(optionName, KeyManagerFactory.class); 067 } 068 069 @Override 070 protected KeyManagerFactory convertImpl(final String value) throws NoSuchAlgorithmException, NoSuchProviderException { 071 final String[] split = split(value); 072 final String algorithm = split[0]; 073 return isSingle(split) ? KeyManagerFactory.getInstance(value) : KeyManagerFactory.getInstance(algorithm, split[1]); 074 } 075 076}