minor geo-lookup fixes occasional NPE in utrace lookup, deprecate geobytes lookup whose api has changed, better messages in integration test
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/a08c72dd Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/a08c72dd Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/a08c72dd Branch: refs/heads/master Commit: a08c72dd125f388852a5ea125c47135c4c793428 Parents: 44b6b4a Author: Alex Heneveld <[email protected]> Authored: Fri Mar 27 18:43:31 2015 -0500 Committer: Alex Heneveld <[email protected]> Committed: Sat Mar 28 21:27:36 2015 -0500 ---------------------------------------------------------------------- .../brooklyn/location/geo/GeoBytesHostGeoLookup.java | 11 +++++++---- .../brooklyn/location/geo/UtraceHostGeoLookup.java | 15 +++++++++++++-- .../location/geo/HostGeoLookupIntegrationTest.java | 13 ++++++++++--- 3 files changed, 30 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a08c72dd/core/src/main/java/brooklyn/location/geo/GeoBytesHostGeoLookup.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/location/geo/GeoBytesHostGeoLookup.java b/core/src/main/java/brooklyn/location/geo/GeoBytesHostGeoLookup.java index ce01828..1c68000 100644 --- a/core/src/main/java/brooklyn/location/geo/GeoBytesHostGeoLookup.java +++ b/core/src/main/java/brooklyn/location/geo/GeoBytesHostGeoLookup.java @@ -29,6 +29,8 @@ import org.slf4j.LoggerFactory; import brooklyn.util.net.Networking; +/** @deprecated Mar 2015 - the API has changed; GetLocation now discouraged for free access, and valuepairs.txt not supported */ +@Deprecated public class GeoBytesHostGeoLookup implements HostGeoLookup { public static final Logger log = LoggerFactory.getLogger(GeoBytesHostGeoLookup.class); @@ -84,7 +86,12 @@ public class GeoBytesHostGeoLookup implements HostGeoLookup { Properties props = new Properties(); try { props.load( new URL(url).openStream() ); + HostGeoInfo geo = new HostGeoInfo(address.getHostName(), props.getProperty("city")+" ("+props.getProperty("iso2")+")", + Double.parseDouble(props.getProperty("latitude")), Double.parseDouble(props.getProperty("longitude"))); + log.info("Geo info lookup for "+address+" returned: "+geo); + return geo; } catch (Exception e) { + // may be web not available, or gateway giving us funny crap if (log.isDebugEnabled()) log.debug("Geo info lookup for "+address+" failed: "+e); if (!LOGGED_GEO_LOOKUP_UNAVAILABLE) { @@ -93,10 +100,6 @@ public class GeoBytesHostGeoLookup implements HostGeoLookup { } return null; } - HostGeoInfo geo = new HostGeoInfo(address.getHostName(), props.getProperty("city")+" ("+props.getProperty("iso2")+")", - Double.parseDouble(props.getProperty("latitude")), Double.parseDouble(props.getProperty("longitude"))); - log.info("Geo info lookup for "+address+" returned: "+geo); - return geo; } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a08c72dd/core/src/main/java/brooklyn/location/geo/UtraceHostGeoLookup.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/location/geo/UtraceHostGeoLookup.java b/core/src/main/java/brooklyn/location/geo/UtraceHostGeoLookup.java index 2560b55..37ed9f7 100644 --- a/core/src/main/java/brooklyn/location/geo/UtraceHostGeoLookup.java +++ b/core/src/main/java/brooklyn/location/geo/UtraceHostGeoLookup.java @@ -27,6 +27,9 @@ import java.net.InetAddress; import java.net.MalformedURLException; import java.util.concurrent.atomic.AtomicReference; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -189,9 +192,17 @@ Beyond this you get blacklisted and requests may time out, or return none. } } + @Nullable + private static Node getFirstChild(Node xml, String field) { + if (xml==null) return null; + NodeList nl = (NodeList)xml.get(field); + if (nl==null || nl.isEmpty()) return null; + return (Node)nl.get(0); + } + @Nonnull private static String getXmlResultsField(Node xml, String field) { - Node r1 = ((Node)((NodeList)xml.get("result")).get(0)); - Node f1 = ((Node)((NodeList)r1.get(field)).get(0)); + Node f1 = getFirstChild(getFirstChild(xml, "result"), field); + if (f1==null) return ""; return f1.text(); } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a08c72dd/core/src/test/java/brooklyn/location/geo/HostGeoLookupIntegrationTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/brooklyn/location/geo/HostGeoLookupIntegrationTest.java b/core/src/test/java/brooklyn/location/geo/HostGeoLookupIntegrationTest.java index 9b32442..85acb6d 100644 --- a/core/src/test/java/brooklyn/location/geo/HostGeoLookupIntegrationTest.java +++ b/core/src/test/java/brooklyn/location/geo/HostGeoLookupIntegrationTest.java @@ -36,16 +36,21 @@ public class HostGeoLookupIntegrationTest { @Test(groups = "Integration") public void testLocalhostGetsLocation() throws Exception { - SshMachineLocation l = new LocalhostMachineProvisioningLocation().obtain(); + LocalhostMachineProvisioningLocation ll = new LocalhostMachineProvisioningLocation(); + SshMachineLocation l = ll.obtain(); HostGeoInfo geo = HostGeoInfo.fromLocation(l); + Assert.assertNotNull(geo, "host lookup unavailable - is the maxmind database installed? or else network unavailable or too slow?"); log.info("localhost is in "+geo); Assert.assertNotNull(geo, "couldn't load data; must have a valid HostGeoLookup impl (e.g. MaxMind installed, or online and with Utrace credit)"); Assert.assertTrue(-90 <= geo.latitude && geo.latitude <= 90); + ll.close(); } - @Test(groups = "Integration") + @Deprecated // see GeoBytesHostGeoLookup - their API changed + @Test(groups = "Integration", enabled=false) public void testGeobytesLookup() throws Exception { HostGeoInfo geo = new GeoBytesHostGeoLookup().getHostGeoInfo(InetAddress.getByName("geobytes.com")); + Assert.assertNotNull(geo, "host lookup unavailable"); Assert.assertEquals(geo.displayName, "Baltimore (US)"); Assert.assertEquals(geo.latitude, 39.2894, 0.1); Assert.assertEquals(geo.longitude, -76.6384, 0.1); @@ -54,14 +59,16 @@ public class HostGeoLookupIntegrationTest { @Test(groups = "Integration") public void testUtraceLookup() throws Exception { HostGeoInfo geo = new UtraceHostGeoLookup().getHostGeoInfo(InetAddress.getByName("utrace.de")); + Assert.assertNotNull(geo, "host lookup unavailable - maybe network not available "); Assert.assertTrue(geo.displayName.contains("(DE)")); Assert.assertEquals(geo.latitude, 51, 2); Assert.assertEquals(geo.longitude, 9, 5); } - @Test(groups = "Integration") + @Test(groups = "Integration") // only works if maxmind database is installed to ~/.brooklyn/ public void testMaxmindLookup() throws Exception { HostGeoInfo geo = new MaxMind2HostGeoLookup().getHostGeoInfo(InetAddress.getByName("maxmind.com")); + Assert.assertNotNull(geo, "host lookup unavailable - is the maxmind database installed?"); log.info("maxmind.com at "+geo); // used to be Washington; now Dallas - in case this changes again, we will accept either!
