package org.apache.logging.log4j.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.status.StatusLogger;

public final class PropertiesUtil {
   private static final PropertiesUtil LOG4J_PROPERTIES = new PropertiesUtil("log4j2.component.properties");
   private static final Logger LOGGER = StatusLogger.getLogger();
   private final Properties props;

   public PropertiesUtil(Properties props) {
      this.props = props;
   }

   static Properties loadClose(InputStream in, Object source) {
      Properties props = new Properties();
      if (null != in) {
         try {
            props.load(in);
         } catch (IOException var12) {
            LOGGER.error("Unable to read {}", source, var12);
         } finally {
            try {
               in.close();
            } catch (IOException var11) {
               LOGGER.error("Unable to close {}", source, var11);
            }

         }
      }

      return props;
   }

   public PropertiesUtil(String propsLocn) {
      ClassLoader loader = LoaderUtil.getThreadContextClassLoader();
      Properties properties = new Properties();

      try {
         Enumeration enumeration = loader.getResources(propsLocn);

         while(enumeration.hasMoreElements()) {
            URL url = (URL)enumeration.nextElement();
            InputStream in = url.openStream();

            try {
               properties.load(in);
            } catch (IOException var17) {
               LOGGER.error("Unable to read {}", url.toString());
            } finally {
               try {
                  in.close();
               } catch (IOException var16) {
                  LOGGER.error("Unable to close {}", url.toString(), var16);
               }

            }
         }
      } catch (IOException var19) {
         LOGGER.error("Unable to access {}", propsLocn, var19);
      }

      this.props = properties;
   }

   public static PropertiesUtil getProperties() {
      return LOG4J_PROPERTIES;
   }

   public String getStringProperty(String name) {
      String prop = null;

      try {
         prop = System.getProperty(name);
      } catch (SecurityException var4) {
      }

      return prop == null ? this.props.getProperty(name) : prop;
   }

   public int getIntegerProperty(String name, int defaultValue) {
      String prop = null;

      try {
         prop = System.getProperty(name);
      } catch (SecurityException var6) {
      }

      if (prop == null) {
         prop = this.props.getProperty(name);
      }

      if (prop != null) {
         try {
            return Integer.parseInt(prop);
         } catch (Exception var5) {
            return defaultValue;
         }
      } else {
         return defaultValue;
      }
   }

   public long getLongProperty(String name, long defaultValue) {
      String prop = null;

      try {
         prop = System.getProperty(name);
      } catch (SecurityException var7) {
      }

      if (prop == null) {
         prop = this.props.getProperty(name);
      }

      if (prop != null) {
         try {
            return Long.parseLong(prop);
         } catch (Exception var6) {
            return defaultValue;
         }
      } else {
         return defaultValue;
      }
   }

   public String getStringProperty(String name, String defaultValue) {
      String prop = this.getStringProperty(name);
      return prop == null ? defaultValue : prop;
   }

   public boolean getBooleanProperty(String name) {
      return this.getBooleanProperty(name, false);
   }

   public boolean getBooleanProperty(String name, boolean defaultValue) {
      String prop = this.getStringProperty(name);
      return prop == null ? defaultValue : "true".equalsIgnoreCase(prop);
   }

   public static Properties getSystemProperties() {
      try {
         return new Properties(System.getProperties());
      } catch (SecurityException var1) {
         LOGGER.error((String)"Unable to access system properties.", (Throwable)var1);
         return new Properties();
      }
   }
}
