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

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Hashtable;
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.config.plugins.Plugin;
import org.apache.logging.log4j.core.util.Integers;
import org.apache.logging.log4j.core.util.Loader;
import org.apache.logging.log4j.status.StatusLogger;

@Plugin(
   name = "multicastdns",
   category = "Core",
   elementType = "advertiser",
   printObject = false
)
public class MulticastDnsAdvertiser implements Advertiser {
   protected static final Logger LOGGER = StatusLogger.getLogger();
   private static Object jmDNS = initializeJmDns();
   private static Class<?> jmDNSClass;
   private static Class<?> serviceInfoClass;

   public Object advertise(Map<String, String> properties) {
      Map<String, String> truncatedProperties = new HashMap();
      Iterator i$ = properties.entrySet().iterator();

      while(i$.hasNext()) {
         Entry<String, String> entry = (Entry)i$.next();
         if (((String)entry.getKey()).length() <= 255 && ((String)entry.getValue()).length() <= 255) {
            truncatedProperties.put(entry.getKey(), entry.getValue());
         }
      }

      String protocol = (String)truncatedProperties.get("protocol");
      String zone = "._log4j._" + (protocol != null ? protocol : "tcp") + ".local.";
      String portString = (String)truncatedProperties.get("port");
      int port = Integers.parseInt(portString, 4555);
      String name = (String)truncatedProperties.get("name");
      if (jmDNS != null) {
         boolean isVersion3 = false;

         try {
            jmDNSClass.getMethod("create");
            isVersion3 = true;
         } catch (NoSuchMethodException var14) {
         }

         Object serviceInfo;
         if (isVersion3) {
            serviceInfo = buildServiceInfoVersion3(zone, port, name, truncatedProperties);
         } else {
            serviceInfo = buildServiceInfoVersion1(zone, port, name, truncatedProperties);
         }

         try {
            Method method = jmDNSClass.getMethod("registerService", serviceInfoClass);
            method.invoke(jmDNS, serviceInfo);
         } catch (IllegalAccessException var11) {
            LOGGER.warn((String)"Unable to invoke registerService method", (Throwable)var11);
         } catch (NoSuchMethodException var12) {
            LOGGER.warn((String)"No registerService method", (Throwable)var12);
         } catch (InvocationTargetException var13) {
            LOGGER.warn((String)"Unable to invoke registerService method", (Throwable)var13);
         }

         return serviceInfo;
      } else {
         LOGGER.warn("JMDNS not available - will not advertise ZeroConf support");
         return null;
      }
   }

   public void unadvertise(Object serviceInfo) {
      if (jmDNS != null) {
         try {
            Method method = jmDNSClass.getMethod("unregisterService", serviceInfoClass);
            method.invoke(jmDNS, serviceInfo);
         } catch (IllegalAccessException var3) {
            LOGGER.warn((String)"Unable to invoke unregisterService method", (Throwable)var3);
         } catch (NoSuchMethodException var4) {
            LOGGER.warn((String)"No unregisterService method", (Throwable)var4);
         } catch (InvocationTargetException var5) {
            LOGGER.warn((String)"Unable to invoke unregisterService method", (Throwable)var5);
         }
      }

   }

   private static Object createJmDnsVersion1() {
      try {
         return jmDNSClass.getConstructor().newInstance();
      } catch (InstantiationException var1) {
         LOGGER.warn((String)"Unable to instantiate JMDNS", (Throwable)var1);
      } catch (IllegalAccessException var2) {
         LOGGER.warn((String)"Unable to instantiate JMDNS", (Throwable)var2);
      } catch (NoSuchMethodException var3) {
         LOGGER.warn((String)"Unable to instantiate JMDNS", (Throwable)var3);
      } catch (InvocationTargetException var4) {
         LOGGER.warn((String)"Unable to instantiate JMDNS", (Throwable)var4);
      }

      return null;
   }

   private static Object createJmDnsVersion3() {
      try {
         Method jmDNSCreateMethod = jmDNSClass.getMethod("create");
         return jmDNSCreateMethod.invoke((Object)null, (Object[])null);
      } catch (IllegalAccessException var1) {
         LOGGER.warn((String)"Unable to invoke create method", (Throwable)var1);
      } catch (NoSuchMethodException var2) {
         LOGGER.warn((String)"Unable to get create method", (Throwable)var2);
      } catch (InvocationTargetException var3) {
         LOGGER.warn((String)"Unable to invoke create method", (Throwable)var3);
      }

      return null;
   }

   private static Object buildServiceInfoVersion1(String zone, int port, String name, Map<String, String> properties) {
      Hashtable hashtableProperties = new Hashtable(properties);

      try {
         return serviceInfoClass.getConstructor(String.class, String.class, Integer.TYPE, Integer.TYPE, Integer.TYPE, Hashtable.class).newInstance(zone, name, port, 0, 0, hashtableProperties);
      } catch (IllegalAccessException var6) {
         LOGGER.warn((String)"Unable to construct ServiceInfo instance", (Throwable)var6);
      } catch (NoSuchMethodException var7) {
         LOGGER.warn((String)"Unable to get ServiceInfo constructor", (Throwable)var7);
      } catch (InstantiationException var8) {
         LOGGER.warn((String)"Unable to construct ServiceInfo instance", (Throwable)var8);
      } catch (InvocationTargetException var9) {
         LOGGER.warn((String)"Unable to construct ServiceInfo instance", (Throwable)var9);
      }

      return null;
   }

   private static Object buildServiceInfoVersion3(String zone, int port, String name, Map<String, String> properties) {
      try {
         return serviceInfoClass.getMethod("create", String.class, String.class, Integer.TYPE, Integer.TYPE, Integer.TYPE, Map.class).invoke((Object)null, zone, name, port, 0, 0, properties);
      } catch (IllegalAccessException var5) {
         LOGGER.warn((String)"Unable to invoke create method", (Throwable)var5);
      } catch (NoSuchMethodException var6) {
         LOGGER.warn((String)"Unable to find create method", (Throwable)var6);
      } catch (InvocationTargetException var7) {
         LOGGER.warn((String)"Unable to invoke create method", (Throwable)var7);
      }

      return null;
   }

   private static Object initializeJmDns() {
      try {
         jmDNSClass = Loader.loadClass("javax.jmdns.JmDNS");
         serviceInfoClass = Loader.loadClass("javax.jmdns.ServiceInfo");
         boolean isVersion3 = false;

         try {
            jmDNSClass.getMethod("create");
            isVersion3 = true;
         } catch (NoSuchMethodException var2) {
         }

         if (isVersion3) {
            return createJmDnsVersion3();
         }

         return createJmDnsVersion1();
      } catch (ClassNotFoundException var3) {
         LOGGER.warn((String)"JmDNS or serviceInfo class not found", (Throwable)var3);
      } catch (ExceptionInInitializerError var4) {
         LOGGER.warn((String)"JmDNS or serviceInfo class not found", (Throwable)var4);
      }

      return null;
   }
}
