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

import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.util.JndiCloser;

@Plugin(
   name = "jndi",
   category = "Lookup"
)
public class JndiLookup implements StrLookup {
   static final String CONTAINER_JNDI_RESOURCE_PATH_PREFIX = "java:comp/env/";

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

   public String lookup(LogEvent event, String key) {
      if (key == null) {
         return null;
      } else {
         InitialContext ctx = null;

         Object var5;
         try {
            ctx = new InitialContext();
            String var4 = (String)ctx.lookup(this.convertJndiName(key));
            return var4;
         } catch (NamingException var9) {
            var5 = null;
         } finally {
            JndiCloser.closeSilently(ctx);
         }

         return (String)var5;
      }
   }

   private String convertJndiName(String jndiName) {
      if (!jndiName.startsWith("java:comp/env/") && jndiName.indexOf(58) == -1) {
         jndiName = "java:comp/env/" + jndiName;
      }

      return jndiName;
   }
}
