// Path: src/main/java/com/homeadvisor/kafdrop/util/JmxUtils.java
// public abstract class JmxUtils
// {
//    public static final String JMX_PORT_PROPERTY = "com.sun.management.jmxremote.port";
// 
//    public static int getJmxPort(final Environment environment)
//    {
//       Optional<Integer> jmxPort = Optional.empty();
// 
//       final Properties managementProperties = Agent.getManagementProperties();
//       if (managementProperties != null)
//       {
//          final String portProperty = managementProperties.getProperty(JMX_PORT_PROPERTY);
//          if (portProperty != null)
//          {
//             final Optional<Integer> port = Optional.ofNullable(Ints.tryParse(portProperty));
//             jmxPort = port;
//          }
//       }
//       return jmxPort.orElse(0);
//    }
// }


import com.google.common.base.Throwables;
import com.homeadvisor.kafdrop.util.JmxUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.apache.curator.x.discovery.*;
import org.apache.curator.x.discovery.details.JsonInstanceSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.info.InfoEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;
import java.beans.Introspector;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
            .build();
      }

      public Map<String, Object> serviceDetails(Integer serverPort)
      {
         Map<String, Object> details = new LinkedHashMap<>();

         Optional.ofNullable(infoEndpoint.info())
            .ifPresent(infoMap -> Optional.ofNullable((Map<String, Object>) infoMap.get("build"))
               .ifPresent(buildInfo -> {
                  details.put("serviceName", buildInfo.get("artifact"));
                  details.put("serviceDescription", buildInfo.get("description"));
                  details.put("serviceVersion", buildInfo.get("version"));
               }));

         final String name = (String) details.getOrDefault("serviceName", "kafdrop");

         String host = null;
         try
         {
            host = InetAddress.getLocalHost().getHostName();
         }
         catch (UnknownHostException e)
         {
            host = "<unknown>";
         }

         details.put("id", Stream.of(name, host, UUID.randomUUID().toString()).collect(Collectors.joining("_")));
         details.put("name", name);
         details.put("host", host);
// Path: src/main/java/com/homeadvisor/kafdrop/util/JmxUtils.java
// public abstract class JmxUtils
// {
//    public static final String JMX_PORT_PROPERTY = "com.sun.management.jmxremote.port";
// 
//    public static int getJmxPort(final Environment environment)
//    {
//       Optional<Integer> jmxPort = Optional.empty();
// 
//       final Properties managementProperties = Agent.getManagementProperties();
//       if (managementProperties != null)
//       {
//          final String portProperty = managementProperties.getProperty(JMX_PORT_PROPERTY);
//          if (portProperty != null)
//          {
//             final Optional<Integer> port = Optional.ofNullable(Ints.tryParse(portProperty));
//             jmxPort = port;
//          }
//       }
//       return jmxPort.orElse(0);
//    }
// }


// Path: src/main/java/com/homeadvisor/kafdrop/config/ServiceDiscoveryConfiguration.java
import com.google.common.base.Throwables;
import com.homeadvisor.kafdrop.util.JmxUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.apache.curator.x.discovery.*;
import org.apache.curator.x.discovery.details.JsonInstanceSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.info.InfoEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;
import java.beans.Introspector;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

            .build();
      }

      public Map<String, Object> serviceDetails(Integer serverPort)
      {
         Map<String, Object> details = new LinkedHashMap<>();

         Optional.ofNullable(infoEndpoint.info())
            .ifPresent(infoMap -> Optional.ofNullable((Map<String, Object>) infoMap.get("build"))
               .ifPresent(buildInfo -> {
                  details.put("serviceName", buildInfo.get("artifact"));
                  details.put("serviceDescription", buildInfo.get("description"));
                  details.put("serviceVersion", buildInfo.get("version"));
               }));

         final String name = (String) details.getOrDefault("serviceName", "kafdrop");

         String host = null;
         try
         {
            host = InetAddress.getLocalHost().getHostName();
         }
         catch (UnknownHostException e)
         {
            host = "<unknown>";
         }

         details.put("id", Stream.of(name, host, UUID.randomUUID().toString()).collect(Collectors.joining("_")));
         details.put("name", name);
         details.put("host", host);
         details.put("jmxPort", JmxUtils.getJmxPort(environment));