View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.net.ssl;
18  
19  import java.security.KeyStoreException;
20  import java.security.NoSuchAlgorithmException;
21  
22  import javax.net.ssl.TrustManagerFactory;
23  
24  import org.apache.logging.log4j.core.config.plugins.Plugin;
25  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
26  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
27  
28  /**
29   * Configuration of the TrustStore
30   */
31  @Plugin(name = "TrustStore", category = "Core", printObject = true)
32  public class TrustStoreConfiguration extends AbstractKeyStoreConfiguration {
33  
34      private final String trustManagerFactoryAlgorithm;
35  
36      public TrustStoreConfiguration(final String location, final String password, final String keyStoreType,
37              final String trustManagerFactoryAlgorithm) throws StoreConfigurationException {
38          super(location, password, keyStoreType);
39          this.trustManagerFactoryAlgorithm = trustManagerFactoryAlgorithm == null ? TrustManagerFactory
40                  .getDefaultAlgorithm() : trustManagerFactoryAlgorithm;
41      }
42  
43      /**
44       * Creates a KeyStoreConfiguration.
45       * 
46       * @param location
47       *        The location of the KeyStore.
48       * @param password
49       *        The password to access the KeyStore.
50       * @param keyStoreType
51       *        The KeyStore type, null defaults to {@code "JKS"}.
52       * @param trustManagerFactoryAlgorithm
53       *        The standard name of the requested trust management algorithm. See the Java Secure Socket Extension Reference Guide for information these names.
54       * @return a new TrustStoreConfiguration
55       * @throws StoreConfigurationException
56       */
57      @PluginFactory
58      public static TrustStoreConfiguration createKeyStoreConfiguration(
59              // @formatter:off
60              @PluginAttribute("location") final String location,
61              @PluginAttribute("password") final String password,
62              @PluginAttribute("type") final String keyStoreType, 
63              @PluginAttribute("trustManagerFactoryAlgorithm") final String trustManagerFactoryAlgorithm) throws StoreConfigurationException {
64              // @formatter:on
65          return new TrustStoreConfiguration(location, password, keyStoreType, trustManagerFactoryAlgorithm);
66      }
67  
68      public TrustManagerFactory initTrustManagerFactory() throws NoSuchAlgorithmException, KeyStoreException {
69          final TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(this.trustManagerFactoryAlgorithm);
70          tmFactory.init(this.getKeyStore());
71          return tmFactory;
72      }
73  }