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.Core;
25  import org.apache.logging.log4j.core.config.plugins.Plugin;
26  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
27  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
28  
29  /**
30   * Configuration of the TrustStore
31   */
32  @Plugin(name = "TrustStore", category = Core.CATEGORY_NAME, printObject = true)
33  public class TrustStoreConfiguration extends AbstractKeyStoreConfiguration {
34  
35      private final String trustManagerFactoryAlgorithm;
36  
37      public TrustStoreConfiguration(final String location, final String password, final String keyStoreType,
38              final String trustManagerFactoryAlgorithm) throws StoreConfigurationException {
39          super(location, password, keyStoreType);
40          this.trustManagerFactoryAlgorithm = trustManagerFactoryAlgorithm == null ? TrustManagerFactory
41                  .getDefaultAlgorithm() : trustManagerFactoryAlgorithm;
42      }
43  
44      /**
45       * Creates a KeyStoreConfiguration.
46       * 
47       * @param location
48       *        The location of the KeyStore.
49       * @param password
50       *        The password to access the KeyStore.
51       * @param keyStoreType
52       *        The KeyStore type, null defaults to {@code "JKS"}.
53       * @param trustManagerFactoryAlgorithm
54       *        The standard name of the requested trust management algorithm. See the Java Secure Socket Extension Reference Guide for information these names.
55       * @return a new TrustStoreConfiguration
56       * @throws StoreConfigurationException Thrown if this instance cannot load the KeyStore.
57       */
58      @PluginFactory
59      public static TrustStoreConfiguration createKeyStoreConfiguration(
60              // @formatter:off
61              @PluginAttribute("location") final String location,
62              @PluginAttribute(value = "password", sensitive = true) final String password,
63              @PluginAttribute("type") final String keyStoreType, 
64              @PluginAttribute("trustManagerFactoryAlgorithm") final String trustManagerFactoryAlgorithm) throws StoreConfigurationException {
65              // @formatter:on
66          return new TrustStoreConfiguration(location, password, keyStoreType, trustManagerFactoryAlgorithm);
67      }
68  
69      public TrustManagerFactory initTrustManagerFactory() throws NoSuchAlgorithmException, KeyStoreException {
70          final TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(this.trustManagerFactoryAlgorithm);
71          tmFactory.init(this.getKeyStore());
72          return tmFactory;
73      }
74  
75      @Override
76      public int hashCode() {
77          final int prime = 31;
78          int result = super.hashCode();
79          result = prime * result
80                  + ((trustManagerFactoryAlgorithm == null) ? 0 : trustManagerFactoryAlgorithm.hashCode());
81          return result;
82      }
83  
84      @Override
85      public boolean equals(final Object obj) {
86          if (this == obj) {
87              return true;
88          }
89          if (!super.equals(obj)) {
90              return false;
91          }
92          if (getClass() != obj.getClass()) {
93              return false;
94          }
95          final TrustStoreConfiguration other = (TrustStoreConfiguration) obj;
96          if (trustManagerFactoryAlgorithm == null) {
97              if (other.trustManagerFactoryAlgorithm != null) {
98                  return false;
99              }
100         } else if (!trustManagerFactoryAlgorithm.equals(other.trustManagerFactoryAlgorithm)) {
101             return false;
102         }
103         return true;
104     }
105 }