views:

235

answers:

2

I'm using JMX to save some diagnostic information from a remote process. Looking at the interface in jconsole shows that the return type is CompositeData (the data actually comes back as CompositeDataSupport). I want to output all the key/value pairs that are associated with this object.

The problem is that the interface just seems to have a "values()" method with no way of getting the keys. Am I missing something here? Is there some other way to approach this task?

Thanks!

+2  A: 

If I'm not mistaken you could do

Set< String > keys = cData.getCompositeType().keySet();

(given that cData is a CompositeData object)

http://java.sun.com/j2se/1.5.0/docs/api/javax/management/openmbean/CompositeType.html#keySet()

tjlevine
+1  A: 

You can find a more complete example with this small program that prints the attributes of all JVM MBeans

In particular:

StringBuffer writeCompositeData(StringBuffer buffer, 
            String prefix, String name, CompositeData data) {
        if (data == null)
            return writeSimple(buffer,prefix,name,null,true);
        writeSimple(buffer,prefix,name,"CompositeData("+
                data.getCompositeType().getTypeName()+")",true);
        buffer.append(prefix).append("{").append("\n");
        final String fieldprefix = prefix + " ";
        for (String key : data.getCompositeType().keySet()) {
            write(buffer,fieldprefix,name+"."+key,data.get(key));
        }
        buffer.append(prefix).append("}").append("\n");
        return buffer;
    }

The part:

for (String key : data.getCompositeType().keySet()) {
    [...] data.get(key) [...];
}

being what you are after.

VonC