package org.jboss.mx.remote;
import javax.management.ObjectName;
import javax.management.MBeanException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
/**
* JMXRemotingObjectName is a helper utility for creating a JMXRemoting conformant
* ObjectName
*
* @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
*/
public class JMXRemotingObjectName
{
/**
* strip a remote ObjectName to its local version
*
* @param obj
* @return local object name
* @throws MBeanException
*/
public static final ObjectName toLocal (ObjectName obj)
throws MBeanException
{
if (obj.getCanonicalName().indexOf("*")!=-1)
{
// leave wildcards alone
return obj;
}
// remove the remote keys
Map map = new HashMap(obj.getKeyPropertyList());
map.remove("MBeanServerId");
map.remove("MBeanServerIpAddress");
map.remove("MBeanServerHostname");
map.remove("MBeanServerInstanceId");
//System.err.println("++++ toLocal = "+obj+", map="+map);
String domain = obj.getDomain();
StringBuffer newName = new StringBuffer(domain + ":");
Iterator iter = map.keySet().iterator();
while (iter.hasNext())
{
String key = (String) iter.next();
String val = (String) map.get(key);
newName.append(key).append("=").append(val);
if (iter.hasNext())
{
newName.append(",");
}
}
try
{
ObjectName newObjectName = new ObjectName(newName.toString());
return newObjectName;
}
catch (Exception ex)
{
throw new MBeanException(ex,"Error creating local version of ObjectName: "+newName);
}
}
/**
* create a new objectname that is suitable for JMX remoting
*
* @param serverId
* @param ip
* @param name
* @param serviceName
* @return remote object name
* @throws Exception
*/
public static final ObjectName create(String serverId, InetAddress ip, ObjectName name, String serviceName, String instanceid)
throws Exception
{
Hashtable keys = name.getKeyPropertyList();
Map map = new HashMap(keys.size() + 4);
Iterator iter = keys.keySet().iterator();
while (iter.hasNext())
{
String key = (String) iter.next();
map.put(key, name.getKeyProperty(key));
}
map.put("MBeanServerId", serverId);
map.put("MBeanServerIpAddress", ip.getHostAddress());
map.put("MBeanServerHostname", ip.getHostName());
map.put("MBeanServerInstanceId", instanceid);
String domain = name.getDomain();
StringBuffer newName = new StringBuffer(domain + ":");
iter = map.keySet().iterator();
while (iter.hasNext())
{
String key = (String) iter.next();
String val = (String) map.get(key);
newName.append(key).append("=").append(val);
if (iter.hasNext())
{
newName.append(",");
}
}
ObjectName newObjectName = new ObjectName(newName.toString());
return newObjectName;
}
}