package org.apache.logging.log4j.core.config;

import java.net.URI;
import java.net.URISyntaxException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.impl.Log4jContextFactory;
import org.apache.logging.log4j.core.util.FileUtils;
import org.apache.logging.log4j.spi.LoggerContextFactory;
import org.apache.logging.log4j.status.StatusLogger;

public final class Configurator {
   private static final Logger LOGGER = StatusLogger.getLogger();
   private static final String FQCN = Configurator.class.getName();

   private Configurator() {
   }

   public static LoggerContext initialize(String name, ClassLoader loader, String configLocation) {
      return initialize(name, loader, (String)configLocation, (Object)null);
   }

   public static LoggerContext initialize(String name, ClassLoader loader, String configLocation, Object externalContext) {
      try {
         URI uri = configLocation == null ? null : FileUtils.getCorrectedFilePathUri(configLocation);
         return initialize(name, loader, uri, externalContext);
      } catch (URISyntaxException var5) {
         LOGGER.error("There was a problem parsing the configuration location [{}].", configLocation, var5);
         return null;
      }
   }

   public static LoggerContext initialize(String name, String configLocation) {
      return initialize((String)name, (ClassLoader)null, (String)configLocation);
   }

   public static LoggerContext initialize(String name, ClassLoader loader, URI configLocation) {
      return initialize(name, loader, (URI)configLocation, (Object)null);
   }

   public static LoggerContext initialize(String name, ClassLoader loader, URI configLocation, Object externalContext) {
      try {
         Log4jContextFactory factory = getFactory();
         return factory == null ? null : factory.getContext(FQCN, loader, externalContext, false, configLocation, name);
      } catch (Exception var5) {
         LOGGER.error("There was a problem initializing the LoggerContext [{}] using configuration at [{}].", name, configLocation, var5);
         return null;
      }
   }

   public static LoggerContext initialize(ClassLoader loader, ConfigurationSource source) {
      return initialize((ClassLoader)loader, (ConfigurationSource)source, (Object)null);
   }

   public static LoggerContext initialize(ClassLoader loader, ConfigurationSource source, Object externalContext) {
      try {
         Log4jContextFactory factory = getFactory();
         return factory == null ? null : factory.getContext(FQCN, loader, externalContext, false, source);
      } catch (Exception var4) {
         LOGGER.error("There was a problem obtaining a LoggerContext using the configuration source [{}]", source, var4);
         return null;
      }
   }

   private static Log4jContextFactory getFactory() {
      LoggerContextFactory factory = LogManager.getFactory();
      if (factory instanceof Log4jContextFactory) {
         return (Log4jContextFactory)factory;
      } else if (factory != null) {
         LOGGER.error("LogManager returned an instance of {} which does not implement {}. Unable to initialize Log4j.", factory.getClass().getName(), Log4jContextFactory.class.getName());
         return null;
      } else {
         LOGGER.error("LogManager did not return a LoggerContextFactory. This indicates something has gone terribly wrong!");
         return null;
      }
   }

   public static void shutdown(LoggerContext ctx) {
      if (ctx != null) {
         ctx.stop();
      }

   }
}
