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

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
import org.apache.logging.log4j.core.config.plugins.util.PluginType;
import org.apache.logging.log4j.core.util.Loader;
import org.apache.logging.log4j.status.StatusLogger;

public class Interpolator implements StrLookup {
   private static final Logger LOGGER = StatusLogger.getLogger();
   private static final char PREFIX_SEPARATOR = ':';
   private final Map<String, StrLookup> lookups;
   private final StrLookup defaultLookup;

   public Interpolator(StrLookup defaultLookup) {
      this.lookups = new HashMap();
      this.defaultLookup = (StrLookup)(defaultLookup == null ? new MapLookup(new HashMap()) : defaultLookup);
      PluginManager manager = new PluginManager("Lookup");
      manager.collectPlugins();
      Map<String, PluginType<?>> plugins = manager.getPlugins();
      Iterator i$ = plugins.entrySet().iterator();

      while(i$.hasNext()) {
         Entry<String, PluginType<?>> entry = (Entry)i$.next();
         Class clazz = ((PluginType)entry.getValue()).getPluginClass();

         try {
            this.lookups.put(entry.getKey(), clazz.getConstructor().newInstance());
         } catch (Exception var8) {
            LOGGER.error("Unable to create Lookup for {}", entry.getKey(), var8);
         }
      }

   }

   public Interpolator() {
      this((Map)null);
   }

   public Interpolator(Map<String, String> properties) {
      this.lookups = new HashMap();
      this.defaultLookup = new MapLookup((Map)(properties == null ? new HashMap() : properties));
      this.lookups.put("sys", new SystemPropertiesLookup());
      this.lookups.put("env", new EnvironmentLookup());

      try {
         this.lookups.put("jndi", (StrLookup)Class.forName("org.apache.logging.log4j.core.lookup.JndiLookup").newInstance());
      } catch (Throwable var4) {
         LOGGER.warn("JNDI lookup class is not available because this JRE does not support JNDI. JNDI string lookups will not be available, continuing configuration.", var4);
      }

      this.lookups.put("date", new DateLookup());
      this.lookups.put("ctx", new ContextMapLookup());
      if (Loader.isClassAvailable("javax.servlet.ServletContext")) {
         try {
            this.lookups.put("web", Loader.newCheckedInstanceOf("org.apache.logging.log4j.web.WebLookup", StrLookup.class));
         } catch (Exception var3) {
            LOGGER.info("Log4j appears to be running in a Servlet environment, but there's no log4j-web module available. If you want better web container support, please add the log4j-web JAR to your web archive or server lib directory.");
         }
      } else {
         LOGGER.debug("Not in a ServletContext environment, thus not loading WebLookup plugin.");
      }

   }

   public String lookup(String var) {
      return this.lookup((LogEvent)null, var);
   }

   public String lookup(LogEvent event, String var) {
      if (var == null) {
         return null;
      } else {
         int prefixPos = var.indexOf(58);
         if (prefixPos >= 0) {
            String prefix = var.substring(0, prefixPos);
            String name = var.substring(prefixPos + 1);
            StrLookup lookup = (StrLookup)this.lookups.get(prefix);
            String value = null;
            if (lookup != null) {
               value = event == null ? lookup.lookup(name) : lookup.lookup(event, name);
            }

            if (value != null) {
               return value;
            }

            var = var.substring(prefixPos + 1);
         }

         if (this.defaultLookup != null) {
            return event == null ? this.defaultLookup.lookup(var) : this.defaultLookup.lookup(event, var);
         } else {
            return null;
         }
      }
   }

   public String toString() {
      StringBuilder sb = new StringBuilder();

      String name;
      for(Iterator i$ = this.lookups.keySet().iterator(); i$.hasNext(); sb.append(name)) {
         name = (String)i$.next();
         if (sb.length() == 0) {
            sb.append('{');
         } else {
            sb.append(", ");
         }
      }

      if (sb.length() > 0) {
         sb.append('}');
      }

      return sb.toString();
   }
}
