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.AlgorithmParameterGenerator;
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 AlgorithmParameterGenerator}.
30   * <p>
31   * For a description of the format, see {@link AlgorithmParameterGenerator#getInstance(String)}.
32   * </p>
33   * 
34   * <p>
35   * Example:
36   * </p>
37   * 
38   * <pre class="prettyprint">
39   * <code class=
40   * "language-java">&#64;Parameter(names = { "--algorithmParameterGenerator" }, converter = AlgorithmParameterGeneratorConverter.class)
41   * private AlgorithmParameterGenerator algorithmParameterGenerator;</code>
42   * </pre>
43   * <p>
44   * 
45   * @see AlgorithmParameterGenerator
46   * @see AlgorithmParameterGenerator#getInstance(String)
47   * 
48   * @since 1.0.0
49   * @author <a href="mailto:ggregory@garygregory.com">Gary Gregory</a>
50   */
51  public class AlgorithmParameterGeneratorConverter extends AbstractBaseConverter<AlgorithmParameterGenerator> {
52  
53      /**
54       * Constructs a converter.
55       * 
56       * @param optionName
57       *            The option name, may be null.
58       */
59      public AlgorithmParameterGeneratorConverter(final String optionName) {
60          super(optionName, AlgorithmParameterGenerator.class);
61      }
62  
63      @Override
64      protected AlgorithmParameterGenerator convertImpl(final String value) throws NoSuchAlgorithmException, NoSuchProviderException {
65          final String[] split = split(value);
66          final String algorithm = split[0];
67          return isSingle(split) ? AlgorithmParameterGenerator.getInstance(value)
68                  : AlgorithmParameterGenerator.getInstance(algorithm, split[1]);
69      }
70  
71  }