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