From d327beed4d1541d883c4823485b381d1dff79039 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sat, 17 Nov 2012 08:55:29 +0100 Subject: [PATCH] GenericJMXConfConnection: Automatically determine the host name. Fixes Github issue #182. --- .../java/GenericJMXConfConnection.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/bindings/java/org/collectd/java/GenericJMXConfConnection.java b/bindings/java/org/collectd/java/GenericJMXConfConnection.java index 0c81bc9a..9f062a9c 100644 --- a/bindings/java/org/collectd/java/GenericJMXConfConnection.java +++ b/bindings/java/org/collectd/java/GenericJMXConfConnection.java @@ -1,6 +1,6 @@ /* * collectd/java - org/collectd/java/GenericJMXConfConnection.java - * Copyright (C) 2009,2010 Florian octo Forster + * Copyright (C) 2009-2012 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -16,7 +16,7 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: - * Florian octo Forster + * Florian octo Forster */ package org.collectd.java; @@ -26,6 +26,7 @@ import java.util.Map; import java.util.Iterator; import java.util.ArrayList; import java.util.HashMap; +import java.net.InetAddress; import javax.management.MBeanServerConnection; import javax.management.ObjectName; @@ -77,6 +78,24 @@ class GenericJMXConfConnection return (v.getString ()); } /* }}} String getConfigString */ + private String getHost () /* {{{ */ + { + if (this._host != null) + { + return (this._host); + } + + try + { + InetAddress localHost = InetAddress.getLocalHost(); + return (localHost.getHostName ()); + } + catch (UnknownHostException e) + { + return ("localhost"); + } + } /* }}} String getHost */ + private void connect () /* {{{ */ { JMXServiceURL service_url; @@ -211,7 +230,7 @@ private void connect () /* {{{ */ + ((this._host != null) ? this._host : "(null)")); pd = new PluginData (); - pd.setHost ((this._host != null) ? this._host : "localhost"); + pd.setHost (this.getHost ()); pd.setPlugin ("GenericJMX"); for (int i = 0; i < this._mbeans.size (); i++) -- 2.30.2