Repository: brooklyn-server Updated Branches: refs/heads/master 29583d8c5 -> d85ffa5b4
make BrooklynWebServer return usable address if bound to all NICs and NICs change Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/810ce622 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/810ce622 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/810ce622 Branch: refs/heads/master Commit: 810ce622df974d71ae08127f4cbd1abf60279f33 Parents: 3647d1f Author: Alex Heneveld <[email protected]> Authored: Wed Jul 19 10:37:56 2017 +0100 Committer: Alex Heneveld <[email protected]> Committed: Sat Jul 22 03:10:08 2017 +0100 ---------------------------------------------------------------------- .../util/core/BrooklynNetworkUtils.java | 9 +++++++-- .../brooklyn/launcher/BrooklynWebServer.java | 20 ++++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/810ce622/core/src/main/java/org/apache/brooklyn/util/core/BrooklynNetworkUtils.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/util/core/BrooklynNetworkUtils.java b/core/src/main/java/org/apache/brooklyn/util/core/BrooklynNetworkUtils.java index b1cd49a..190ad75 100644 --- a/core/src/main/java/org/apache/brooklyn/util/core/BrooklynNetworkUtils.java +++ b/core/src/main/java/org/apache/brooklyn/util/core/BrooklynNetworkUtils.java @@ -39,9 +39,14 @@ public class BrooklynNetworkUtils { return LocalhostExternalIpLoader.getLocalhostIpQuicklyOrDefault(); } - /** returns a IP address for localhost paying attention to a system property to prevent lookup in some cases */ + /** returns an IP address for localhost, + * paying attention to system property + * {@link BrooklynServiceAttributes#LOCALHOST_IP_ADDRESS} + * if set to prevent default selection when needed, + * otherwise finding the first bindable/reachable NIC from a system lookup which usually + * prefers non-loopback devices (but use the system property if if needed) */ public static InetAddress getLocalhostInetAddress() { return TypeCoercions.coerce(JavaGroovyEquivalents.elvis(BrooklynServiceAttributes.LOCALHOST_IP_ADDRESS.getValue(), - Networking.getReachableLocalHost()), InetAddress.class); + Networking.getLocalHost(true, false, true, 500)), InetAddress.class); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/810ce622/launcher/src/main/java/org/apache/brooklyn/launcher/BrooklynWebServer.java ---------------------------------------------------------------------- diff --git a/launcher/src/main/java/org/apache/brooklyn/launcher/BrooklynWebServer.java b/launcher/src/main/java/org/apache/brooklyn/launcher/BrooklynWebServer.java index b85e79d..42f1e7f 100644 --- a/launcher/src/main/java/org/apache/brooklyn/launcher/BrooklynWebServer.java +++ b/launcher/src/main/java/org/apache/brooklyn/launcher/BrooklynWebServer.java @@ -23,6 +23,7 @@ import java.io.InputStream; import java.net.BindException; import java.net.InetAddress; import java.net.URI; +import java.net.UnknownHostException; import java.security.KeyPair; import java.security.KeyStore; import java.security.KeyStoreException; @@ -294,10 +295,12 @@ public class BrooklynWebServer { return actualPort; } - /** interface/address where this server is listening; + /** interface/address where this server is or will be listening; * if bound to 0.0.0.0 (all NICs, e.g. because security is set) this will return one NIC where this is bound */ public InetAddress getAddress() { - return actualAddress; + if (actualAddress!=null) return actualAddress; + if (!shouldBindToAll()) return bindAddress; + return BrooklynNetworkUtils.getLocalhostInetAddress(); } /** URL for accessing this web server (root context) */ @@ -421,8 +424,8 @@ public class BrooklynWebServer { connector.setPort(actualPort); server.setConnectors(new Connector[]{connector}); - if (bindAddress == null || bindAddress.equals(InetAddress.getByAddress(new byte[] { 0, 0, 0, 0 }))) { - actualAddress = BrooklynNetworkUtils.getLocalhostInetAddress(); + if (shouldBindToAll()) { + actualAddress = null; } else { actualAddress = bindAddress; } @@ -468,6 +471,15 @@ public class BrooklynWebServer { log.info("Started Brooklyn console at "+getRootUrl()+", running " + rootWar + (allWars!=null && !allWars.isEmpty() ? " and " + wars.values() : "")); } + private boolean shouldBindToAll() { + try { + return bindAddress == null || bindAddress.equals(InetAddress.getByAddress(new byte[] { 0, 0, 0, 0 })); + } catch (UnknownHostException e) { + // shouldn't happen + throw Exceptions.propagate(e); + } + } + private WebAppContext deployRestApi(WebAppContext context) { ImmutableList.Builder<Object> providersListBuilder = ImmutableList.builder(); providersListBuilder.add(
