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