From cce4191766291a5427edf36d9d11fd901d74063f Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 9 Mar 2017 22:21:33 +0100 Subject: [PATCH] Drop class LoopbackAddress Replaced by InetAddress#getLoopbackAddress() introduced in Java 1.7 --- .../org/sonar/process/LoopbackAddress.java | 71 ------------------- .../sonar/process/LoopbackAddressTest.java | 57 --------------- .../org/sonar/server/es/EsServerHolder.java | 4 +- 3 files changed, 2 insertions(+), 130 deletions(-) delete mode 100644 server/sonar-process/src/main/java/org/sonar/process/LoopbackAddress.java delete mode 100644 server/sonar-process/src/test/java/org/sonar/process/LoopbackAddressTest.java diff --git a/server/sonar-process/src/main/java/org/sonar/process/LoopbackAddress.java b/server/sonar-process/src/main/java/org/sonar/process/LoopbackAddress.java deleted file mode 100644 index 9f790a44b5d..00000000000 --- a/server/sonar-process/src/main/java/org/sonar/process/LoopbackAddress.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.process; - -import java.net.Inet4Address; -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.net.SocketException; -import java.util.Enumeration; - -public class LoopbackAddress { - - private static volatile InetAddress instance; - - private LoopbackAddress() { - // only static stuff - } - - /** - * Quite similar to {@code InetAddress.getLoopbackAddress()} which was introduced in Java 7. This - * method aims to support Java 6. It returns an IPv4 address, but not IPv6 in order to - * support {@code -Djava.net.preferIPv4Stack=true} which is recommended for Elasticsearch. - */ - public static InetAddress get() { - if (instance == null) { - try { - instance = doGet(NetworkInterface.getNetworkInterfaces()); - } catch (SocketException e) { - throw new IllegalStateException("Fail to browse network interfaces", e); - } - - } - return instance; - } - - static InetAddress doGet(Enumeration ifaces) { - InetAddress result = null; - while (ifaces.hasMoreElements() && result == null) { - NetworkInterface iface = ifaces.nextElement(); - Enumeration addresses = iface.getInetAddresses(); - while (addresses.hasMoreElements()) { - InetAddress addr = addresses.nextElement(); - if (addr.isLoopbackAddress() && addr instanceof Inet4Address) { - result = addr; - break; - } - } - } - if (result == null) { - throw new IllegalStateException("Impossible to get a IPv4 loopback address"); - } - return result; - } -} diff --git a/server/sonar-process/src/test/java/org/sonar/process/LoopbackAddressTest.java b/server/sonar-process/src/test/java/org/sonar/process/LoopbackAddressTest.java deleted file mode 100644 index 4d11dd94708..00000000000 --- a/server/sonar-process/src/test/java/org/sonar/process/LoopbackAddressTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.process; - -import com.google.common.collect.Iterators; -import org.junit.Test; -import org.sonar.test.TestUtils; - -import java.net.NetworkInterface; -import java.util.Collections; -import java.util.Enumeration; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.fail; - -public class LoopbackAddressTest { - - @Test - public void get() { - assertThat(LoopbackAddress.get()).isNotNull(); - assertThat(LoopbackAddress.get().isLoopbackAddress()).isTrue(); - assertThat(LoopbackAddress.get().getHostAddress()).isNotNull(); - } - - @Test - public void fail_to_get_loopback_address() { - Enumeration ifaces = Iterators.asEnumeration(Collections.emptyList().iterator()); - try { - LoopbackAddress.doGet(ifaces); - fail(); - } catch (IllegalStateException e) { - assertThat(e).hasMessage("Impossible to get a IPv4 loopback address"); - } - } - - @Test - public void private_constructor() { - assertThat(TestUtils.hasOnlyPrivateConstructors(LoopbackAddress.class)).isTrue(); - } -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java b/server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java index a30b6c90639..6aacb55cd51 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/EsServerHolder.java @@ -21,13 +21,13 @@ package org.sonar.server.es; import java.io.File; import java.io.IOException; +import java.net.InetAddress; import java.nio.file.Files; import java.util.Properties; import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; -import org.sonar.process.LoopbackAddress; import org.sonar.process.NetworkUtils; import org.sonar.process.ProcessEntryPoint; import org.sonar.process.ProcessProperties; @@ -76,7 +76,7 @@ public class EsServerHolder { .put("network.bind_host", "localhost") .put("cluster.name", clusterName) .build()).build(); - client.addTransportAddress(new InetSocketTransportAddress(LoopbackAddress.get(), port)); + client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getLoopbackAddress(), port)); // wait for node to be ready client.admin().cluster().prepareHealth() -- 2.39.5