package org.apache.logging.log4j.core.net.ssl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

public class AbstractKeyStoreConfiguration extends StoreConfiguration<KeyStore> {
   private final KeyStore keyStore;
   private final String keyStoreType;

   public AbstractKeyStoreConfiguration(String location, String password, String keyStoreType) throws StoreConfigurationException {
      super(location, password);
      this.keyStoreType = keyStoreType == null ? "JKS" : keyStoreType;
      this.keyStore = this.load();
   }

   protected KeyStore load() throws StoreConfigurationException {
      FileInputStream fin = null;
      LOGGER.debug("Loading keystore from file with params(location={})", new Object[]{this.getLocation()});

      KeyStore var3;
      try {
         if (this.getLocation() == null) {
            throw new IOException("The location is null");
         }

         fin = new FileInputStream(this.getLocation());
         KeyStore ks = KeyStore.getInstance(this.keyStoreType);
         ks.load(fin, this.getPasswordAsCharArray());
         LOGGER.debug("Keystore successfully loaded with params(location={})", new Object[]{this.getLocation()});
         var3 = ks;
      } catch (CertificateException var16) {
         LOGGER.error("No Provider supports a KeyStoreSpi implementation for the specified type {}", new Object[]{this.keyStoreType});
         throw new StoreConfigurationException(var16);
      } catch (NoSuchAlgorithmException var17) {
         LOGGER.error("The algorithm used to check the integrity of the keystore cannot be found");
         throw new StoreConfigurationException(var17);
      } catch (KeyStoreException var18) {
         LOGGER.error(var18);
         throw new StoreConfigurationException(var18);
      } catch (FileNotFoundException var19) {
         LOGGER.error("The keystore file({}) is not found", new Object[]{this.getLocation()});
         throw new StoreConfigurationException(var19);
      } catch (IOException var20) {
         LOGGER.error("Something is wrong with the format of the keystore or the given password");
         throw new StoreConfigurationException(var20);
      } finally {
         try {
            if (fin != null) {
               fin.close();
            }
         } catch (IOException var15) {
            LOGGER.debug(var15);
         }

      }

      return var3;
   }

   public KeyStore getKeyStore() {
      return this.keyStore;
   }
}
