org.jboss.seam.jmx.AgentID Maven / Gradle / Ivy
Show all versions of jboss-seam Show documentation
package org.jboss.seam.jmx;
import java.net.InetAddress;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.SecureRandom;
import java.util.concurrent.atomic.AtomicLong;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.jboss.mx.server.ServerConstants;
/**
* Utility class for creating JMX agent identifiers. Also contains the
* helper method for retrieving the AgentID of an existing MBean server
* instance.
*
* @see javax.management.MBeanServerDelegateMBean
*
* @author Juha Lindfors.
* @version $Revision: 81019 $
*
*/
public class AgentID implements ServerConstants {
// Static ----------------------------------------------------
private static AtomicLong id = new AtomicLong(0L);
private static final SecureRandom rand = new SecureRandom();
/**
* Creates a new agent ID string. The identifier is of the form
* <ip.address>/<creation time in ms>/<VMID+(random int 0-100)>/<sequence #>.
*
* This AgentID string is globally unique.
*
* @return Agent ID string
*/
public static String create() {
String ipAddress = getIP();
// use the VMID to create a more unique ID that can be used to guarantee that this
// MBeanServerID is unique across multiple JVMs, even on the same host
String vmid = new java.rmi.dgc.VMID().toString().replace(':', 'x').replace('-', 'X') + rand.nextInt(100);
return ipAddress + "/" + System.currentTimeMillis() + "/" + vmid + "/" + id.incrementAndGet();
}
private static String getIP() {
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction() {
@Override
public String run() throws Exception {
return InetAddress.getLocalHost().getHostAddress();
}
});
} catch (PrivilegedActionException e) {
return "127.0.0.1";
}
}
/**
* Returns the agent identifier string of a given MBean server instance.
*
* @return MBeanServerId attribute of the MBean server delegate.
*/
public static String get(MBeanServer server) {
try {
ObjectName name = new ObjectName(MBEAN_SERVER_DELEGATE);
return (String) server.getAttribute(name, "MBeanServerId");
} catch (Exception t) {
throw new Error("Cannot find the MBean server delegate: ", t);
}
}
}