From: Florian Forster Date: Sat, 17 Nov 2012 07:55:29 +0000 (+0100) Subject: GenericJMXConfConnection: Automatically determine the host name. X-Git-Tag: collectd-5.2.0~16 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=d327beed4d1541d883c4823485b381d1dff79039;p=collectd.git GenericJMXConfConnection: Automatically determine the host name. Fixes Github issue #182. --- 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++)