protected HostGeoInfo inferHostGeoInfo()

in software/webapp/src/main/java/org/apache/brooklyn/entity/dns/AbstractGeoDnsServiceImpl.java [374:408]


    protected HostGeoInfo inferHostGeoInfo(String hostname, String ip) throws UnknownHostException {
        HostGeoInfo geoH = null;
        if (hostname != null) {
            try {
                // For some entities, the hostname can actually be an IP! Therefore use Networking.getInetAddressWithFixedName
                InetAddress addr = Networking.getInetAddressWithFixedName(hostname);
                geoH = HostGeoInfo.fromIpAddress(addr);
            } catch (RuntimeException e) {
                // Most likely caused by (a wrapped) UnknownHostException
                Exceptions.propagateIfFatal(e);
                if (ip == null) {
                    if (log.isTraceEnabled()) log.trace("inferHostGeoInfo failing ("+Exceptions.getFirstInteresting(e)+"): hostname="+hostname+"; ip="+ip);
                    throw e;
                } else {
                    if (log.isTraceEnabled()) log.trace("GeoDns failed to infer GeoInfo from hostname {}; will try with IP {} ({})", new Object[] {hostname, ip, e});
                }
            }
        }

        // Try IP address (prior to Mar 2014 we did not do this if USE_HOSTNAME was set but don't think that is desirable due to #1216)
        if (ip != null) {
            if (geoH == null) {
                InetAddress addr = Networking.getInetAddressWithFixedName(ip);
                geoH = HostGeoInfo.fromIpAddress(addr);
                if (log.isTraceEnabled()) log.trace("GeoDns inferred GeoInfo {} from ip {} (could not infer from hostname {})", new Object[] {geoH, ip, hostname});
            } else {
                geoH = HostGeoInfo.create(ip, geoH.displayName, geoH.latitude, geoH.longitude);
                if (log.isTraceEnabled()) log.trace("GeoDns inferred GeoInfo {} from hostname {}; switching it to ip {}", new Object[] {geoH, hostname, ip});
            }
        } else {
            if (log.isTraceEnabled()) log.trace("GeoDns inferred GeoInfo {} from hostname {}", geoH, hostname);
        }

        return geoH;
    }