/**
* Helios, OpenSource Monitoring
* Brought to you by the Helios Development Group
*
* Copyright 2007, Helios Development Group and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This 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 2.1 of
* the License, or (at your option) any later version.
*
* This software 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*
*/
package org.helios.gmx.util;
import java.util.Hashtable;
import javax.management.ObjectName;
import javax.management.remote.JMXServiceURL;
/**
* <p>Title: JMXHelper</p>
* <p>Description: JMX Helper Utilities.</p>
* <p>Company: Helios Development Group LLC</p>
* @author Whitehead (nwhitehead AT heliosdev DOT org)
* <p><code>org.helios.gmx.JMXHelper</code></p>
*/
public class JMXHelper {
/**
* Creates a new JMX object name.
* @param on A string type representing the ObjectName string.
* @return an ObjectName the created ObjectName
*/
public static ObjectName objectName(CharSequence on) {
try {
return new ObjectName(on.toString());
} catch (Exception e) {
throw new RuntimeException("Failed to create Object Name", e);
}
}
/**
* Creates a new JMX object name by appending properties on the end of an existing name
* @param on An existing ObjectName
* @param props Appended properties in the for {@code key=value}
* @return an ObjectName the created ObjectName
*/
public static ObjectName objectName(ObjectName on, CharSequence...props) {
StringBuilder b = new StringBuilder(on.toString());
try {
if(props!=null) {
for(CharSequence prop: props) {
b.append(",").append(prop);
}
}
return new ObjectName(b.toString());
} catch (Exception e) {
throw new RuntimeException("Failed to create Object Name from [" + b + "]", e);
}
}
/**
* Creates a new JMX object name.
* @param domain A string type representing the ObjectName domain
* @param properties A hash table of the Object name's properties
* @return an ObjectName the created ObjectName
*/
public static ObjectName objectName(CharSequence domain, Hashtable<String, String> properties) {
try {
return new ObjectName(domain.toString(), properties);
} catch (Exception e) {
throw new RuntimeException("Failed to create Object Name", e);
}
}
/**
* Generates a new ObjectName for the passed class in the format:</br>
* <pre><package-name>:name=<simple-class-name></pre>
* @param clazz The class to generate the ObjectName for
* @return an ObjectName
*/
public static ObjectName getName(Class<?> clazz) {
return objectName(new StringBuilder(clazz.getPackage().getName()).append(":name=").append(clazz.getSimpleName()));
}
/**
* Generates a (more or less) new ObjectName for the passed object in the format:</br>
* <pre><package-name>:name=<simple-class-name>,id=<object-hash-code></pre>
* @param obj The object to generate the ObjectName for
* @return an ObjectName
*/
public static ObjectName getName(Object obj) {
Class<?> clazz = obj.getClass();
return objectName(new StringBuilder(clazz.getPackage().getName())
.append(":name=").append(clazz.getSimpleName())
.append(",id=").append(System.identityHashCode(obj)));
}
/**
* Creates a new JMXServiceURL
* @param serviceUrl The string form of the URL
* @return a JMXServiceURL
*/
public static JMXServiceURL serviceURL(CharSequence serviceUrl) {
if(serviceUrl==null) throw new IllegalArgumentException("The passed serviceURL was null", new Throwable());
try {
return new JMXServiceURL(serviceUrl.toString());
} catch (Exception e) {
throw new RuntimeException("Failed to create JMXServiceURL from string [" + serviceUrl + "]", e);
}
}
}