changeset 361:77ac1de4a238

Regression: binary link should also work on JDK 6.
author shade
date Wed, 22 Jan 2014 04:40:42 +0400
parents 1846fe42b40b
children 8f81e562fbf5
files jmh-core/src/main/java/org/openjdk/jmh/link/BinaryLinkServer.java
diffstat 1 files changed, 23 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/link/BinaryLinkServer.java	Wed Jan 22 04:21:15 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/link/BinaryLinkServer.java	Wed Jan 22 04:40:42 2014 +0400
@@ -144,12 +144,33 @@
         this.plan.set(actionPlan);
     }
 
+    private InetAddress getLoopback() {
+        // Try to use JDK 7+ method first, it is more reliable.
+        try {
+            Method m = InetAddress.class.getMethod("getLoopbackAddress");
+            return (InetAddress) m.invoke(null);
+        } catch (InvocationTargetException e) {
+            // shun
+        } catch (NoSuchMethodException e) {
+            // shun
+        } catch (IllegalAccessException e) {
+            // shun
+        }
+
+        // Otherwise fallback to well-known
+        try {
+            return InetAddress.getLocalHost();
+        } catch (UnknownHostException e) {
+            throw new IllegalStateException("Unable to resolve local host", e);
+        }
+    }
+
     private final class Acceptor extends Thread {
 
         private final ServerSocket server;
 
         public Acceptor() throws IOException {
-            server = new ServerSocket(0, 50, InetAddress.getLoopbackAddress());
+            server = new ServerSocket(0, 50, getLoopback());
         }
 
         @Override
@@ -173,7 +194,7 @@
         }
 
         public String getHost() {
-            return InetAddress.getLoopbackAddress().getHostAddress();
+            return getLoopback().getHostAddress();
         }
 
         public int getPort() {