View Javadoc
1   /**
2    *  Copyright (C) 2016 Gary Gregory. All rights reserved.
3    *
4    *  See the NOTICE.txt file distributed with this work for additional
5    *  information regarding copyright ownership.
6    *  
7    *  Licensed under the Apache License, Version 2.0 (the "License");
8    *  you may not use this file except in compliance with the License.
9    *  You may obtain a copy of the License at
10   *  
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *  
13   *  Unless required by applicable law or agreed to in writing, software
14   *  distributed under the License is distributed on an "AS IS" BASIS,
15   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *  See the License for the specific language governing permissions and
17   *  limitations under the License.
18   */
19  
20  package com.garygregory.jcommander.converters.security;
21  
22  import java.security.KeyPairGenerator;
23  import java.security.NoSuchAlgorithmException;
24  import java.security.NoSuchProviderException;
25  
26  import com.garygregory.jcommander.converters.AbstractBaseConverter;
27  
28  /**
29   * Converts a {@link String} into a {@link KeyPairGenerator}.
30   * <p>
31   * For a description of the format, see {@link KeyPairGenerator#getInstance(String)}.
32   * </p>
33   * 
34   * <p>
35   * Example:
36   * </p>
37   * 
38   * <pre class="prettyprint">
39   * <code class="language-java">&#64;Parameter(names = { "--keyPairGenerator" }, converter = KeyPairGeneratorConverter.class)
40   * private KeyPairGenerator keyPairGenerator;</code>
41   * </pre>
42   * <p>
43   * 
44   * @see KeyPairGenerator
45   * @see KeyPairGenerator#getInstance(String)
46   * 
47   * @since 1.0.0
48   * @author <a href="mailto:ggregory@garygregory.com">Gary Gregory</a>
49   */
50  public class KeyPairGeneratorConverter extends AbstractBaseConverter<KeyPairGenerator> {
51  
52      /**
53       * Constructs a converter.
54       * 
55       * @param optionName
56       *            The option name, may be null.
57       */
58      public KeyPairGeneratorConverter(final String optionName) {
59          super(optionName, KeyPairGenerator.class);
60      }
61  
62      @Override
63      protected KeyPairGenerator convertImpl(final String value) throws NoSuchAlgorithmException, NoSuchProviderException {
64          final String[] split = split(value);
65          final String algorithm = split[0];
66          return isSingle(split) ? KeyPairGenerator.getInstance(value) : KeyPairGenerator.getInstance(algorithm, split[1]);
67      }
68  
69  }