import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
+import javax.management.remote.JMXServiceURL;
+
import java.lang.management.ManagementFactory;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.MalformedURLException;
public class JmxUtils {
throw new IllegalStateException("Fail to register JMX MBean named " + name, e);
}
}
+
+ public static JMXServiceURL serviceUrl(InetAddress host, int port) {
+ String address = host.getHostAddress();
+ if (host instanceof Inet6Address) {
+ // See http://docs.oracle.com/javase/7/docs/api/javax/management/remote/JMXServiceURL.html
+ // "The host is a host name, an IPv4 numeric host address, or an IPv6 numeric address enclosed in square brackets."
+ address = String.format("[%s]", address);
+ }
+ try {
+ return new JMXServiceURL("rmi", address, port, String.format("/jndi/rmi://%s:%d/jmxrmi", address, port));
+ } catch (MalformedURLException e) {
+ throw new IllegalStateException("JMX url does not look well formed", e);
+ }
+ }
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.Inet6Address;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.util.Enumeration;
+
+public class LoopbackAddress {
+
+ private static InetAddress instance;
+
+ private LoopbackAddress() {
+ // only static stuff
+ }
+
+ /**
+ * Similar to InetAddress.getLoopbackAddress() which was introduced in Java 7. This
+ * method aims to support Java 6.
+ */
+ 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<NetworkInterface> ifaces) {
+ InetAddress result = null;
+ while (ifaces.hasMoreElements() && result == null) {
+ NetworkInterface iface = ifaces.nextElement();
+ Enumeration<InetAddress> addresses = iface.getInetAddresses();
+ while (addresses.hasMoreElements()) {
+ InetAddress addr = addresses.nextElement();
+ if (addr.isLoopbackAddress()) {
+ result = addr;
+ break;
+ }
+ }
+ }
+ if (result == null) {
+ throw new IllegalStateException("Impossible to get a IP loopback address");
+ }
+ return result;
+ }
+}
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
+
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
-import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
return this;
}
-
@CheckForNull
public Process process() {
return process;
"-Dcom.sun.management.jmxremote.port=" + jmxPort,
"-Dcom.sun.management.jmxremote.authenticate=false",
"-Dcom.sun.management.jmxremote.ssl=false",
- "-Djava.rmi.server.hostname=" + localAddress());
+ "-Djava.rmi.server.hostname=" + LoopbackAddress.get().getHostAddress());
}
private List<String> buildClasspath() {
*/
@CheckForNull
private ProcessMXBean waitForJMX() {
- String loopbackAddress = localAddress();
- String path = "/jndi/rmi://" + loopbackAddress + ":" + jmxPort + "/jmxrmi";
- JMXServiceURL jmxUrl;
- try {
- jmxUrl = new JMXServiceURL("rmi", loopbackAddress, jmxPort, path);
- } catch (MalformedURLException e) {
- throw new IllegalStateException("JMX url does not look well formed", e);
- }
-
+ JMXServiceURL jmxUrl = JmxUtils.serviceUrl(LoopbackAddress.get(), jmxPort);
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000L);
- LOGGER.debug("Try #{} to connect to JMX server for process '{}'", i, processName);
JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxUrl, null);
jmxConnector.addConnectionNotificationListener(new NotificationListener() {
@Override
public void handleNotification(Notification notification, Object handback) {
- LOGGER.info("JMX Connection Notification:{}", notification.getMessage());
+ LOGGER.debug("JMX Connection Notification:{}", notification.getMessage());
}
}, null, null);
MBeanServerConnection mBeanServer = jmxConnector.getMBeanServerConnection();
return JMX.newMBeanProxy(mBeanServer, JmxUtils.objectName(processName), ProcessMXBean.class);
} catch (Exception ignored) {
- LOGGER.trace("Could not connect JMX yet", ignored);
+ LOGGER.info(String.format("Could not connect to JMX (attempt %d). Trying again.", i), ignored);
}
}
// failed to connect
return null;
}
- private String localAddress() {
- // to be replaced by InetAddress.getLoopbackAddress() in Java 7 ?
- return "127.0.0.1";
- }
-
@Override
public void terminate() {
if (processMXBean != null && process != null) {
this.join();
}
} catch (InterruptedException e) {
- //Expected to be interrupted :)
+ // Expected to be interrupted :)
}
}
import javax.management.MBeanServer;
import javax.management.ObjectName;
+import javax.management.remote.JMXServiceURL;
import java.lang.management.ManagementFactory;
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.util.Enumeration;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
@Test
public void fail_jmx_objectName() throws Exception {
try {
- ObjectName objectName = JmxUtils.objectName(":");
+ JmxUtils.objectName(":");
fail();
} catch (Exception e) {
assertThat(e.getMessage()).isEqualTo("Cannot create ObjectName for :");
JmxUtils.registerMBean(mxBean, mxBean.getClass().getSimpleName());
assertThat(mbeanServer.isRegistered(objectName)).isTrue();
}
-}
\ No newline at end of file
+
+ @Test
+ public void serviceUrl_ipv4() throws Exception {
+ JMXServiceURL url = JmxUtils.serviceUrl(ip(Inet4Address.class), 1234);
+ assertThat(url).isNotNull();
+ assertThat(url.getPort()).isEqualTo(1234);
+ }
+
+ @Test
+ public void serviceUrl_ipv6() throws Exception {
+ JMXServiceURL url = JmxUtils.serviceUrl(ip(Inet6Address.class), 1234);
+ assertThat(url).isNotNull();
+ assertThat(url.getPort()).isEqualTo(1234);
+ }
+
+ private static InetAddress ip(Class inetAddressClass) throws SocketException {
+ Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
+ while (ifaces.hasMoreElements()) {
+ NetworkInterface iface = ifaces.nextElement();
+ Enumeration<InetAddress> addresses = iface.getInetAddresses();
+ while (addresses.hasMoreElements()) {
+ InetAddress addr = addresses.nextElement();
+ if (addr.getClass().isAssignableFrom(inetAddressClass)) {
+ return addr;
+ }
+ }
+ }
+ throw new IllegalStateException("no ipv4 address");
+ }
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 java.net.NetworkInterface;
+import java.util.Collections;
+import java.util.Enumeration;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
+
+public class LoopbackAddressTest {
+
+ @Test
+ public void get() throws Exception {
+ assertThat(LoopbackAddress.get()).isNotNull();
+ assertThat(LoopbackAddress.get().isLoopbackAddress()).isTrue();
+ assertThat(LoopbackAddress.get().getHostAddress()).isNotNull();
+ }
+
+ @Test
+ public void fail_to_get_loopback_address() throws Exception {
+ Enumeration<NetworkInterface> ifaces = Iterators.asEnumeration(Collections.<NetworkInterface>emptyList().iterator());
+ try {
+ LoopbackAddress.doGet(ifaces);
+ fail();
+ } catch (IllegalStateException e) {
+ assertThat(e).hasMessage("Impossible to get a IP loopback address");
+ }
+ }
+}