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.net.ssl;
21
22 import java.security.NoSuchAlgorithmException;
23 import java.security.NoSuchProviderException;
24 import java.security.Provider;
25
26 import javax.net.ssl.KeyManagerFactory;
27
28 import com.garygregory.jcommander.converters.AbstractBaseConverter;
29
30 /**
31 * Converts a {@link String} into a {@link KeyManagerFactory}.
32 * <p>
33 * For a description of the format, see {@link KeyManagerFactory#getInstance(String)}.
34 * </p>
35 * <p>
36 * To get a KeyManagerFactory from a specific {@link Provider}, use the syntax {@code algorithm:provider} as described by
37 * {@link KeyManagerFactory#getInstance(String, String)}.
38 * </p>
39 *
40 * <p>
41 * Example:
42 * </p>
43 *
44 * <pre class="prettyprint">
45 * <code class="language-java">@Parameter(names = { "--keyManagerFactory" }, converter = KeyManagerFactoryConverter.class)
46 * private KeyManagerFactory keyManagerFactory;</code>
47 * </pre>
48 * <p>
49 *
50 * @see KeyManagerFactory
51 * @see KeyManagerFactory#getInstance(String)
52 * @see KeyManagerFactory#getInstance(String, String)
53 *
54 * @since 1.0.0
55 * @author <a href="mailto:ggregory@garygregory.com">Gary Gregory</a>
56 */
57 public class KeyManagerFactoryConverter extends AbstractBaseConverter<KeyManagerFactory> {
58
59 /**
60 * Constructs a converter.
61 *
62 * @param optionName
63 * The option name, may be null.
64 */
65 public KeyManagerFactoryConverter(final String optionName) {
66 super(optionName, KeyManagerFactory.class);
67 }
68
69 @Override
70 protected KeyManagerFactory convertImpl(final String value) throws NoSuchAlgorithmException, NoSuchProviderException {
71 final String[] split = split(value);
72 final String algorithm = split[0];
73 return isSingle(split) ? KeyManagerFactory.getInstance(value) : KeyManagerFactory.getInstance(algorithm, split[1]);
74 }
75
76 }