/** Constructs a PrintGCStat object to monitor a remote JVM. */
  public PrintGCStat(MBeanServerConnection server) throws IOException {
    // Create the platform mxbean proxies
    this.rmbean = newPlatformMXBeanProxy(server, RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
    this.mmbean = newPlatformMXBeanProxy(server, MEMORY_MXBEAN_NAME, MemoryMXBean.class);
    ObjectName poolName = null;
    ObjectName gcName = null;
    try {
      poolName = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*");
      gcName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*");
    } catch (MalformedObjectNameException e) {
      // should not reach here
      assert (false);
    }

    Set<ObjectName> mbeans = server.queryNames(poolName, null);
    if (mbeans != null) {
      pools = new ArrayList<MemoryPoolMXBean>();
      for (ObjectName objName : mbeans) {
        MemoryPoolMXBean p =
            newPlatformMXBeanProxy(server, objName.getCanonicalName(), MemoryPoolMXBean.class);
        pools.add(p);
      }
    }

    mbeans = server.queryNames(gcName, null);
    if (mbeans != null) {
      gcmbeans = new ArrayList<GarbageCollectorMXBean>();
      for (ObjectName objName : mbeans) {
        GarbageCollectorMXBean gc =
            newPlatformMXBeanProxy(
                server, objName.getCanonicalName(), GarbageCollectorMXBean.class);
        gcmbeans.add(gc);
      }
    }
  }
 private Object getAttribute(ObjectName objName, String attrName)
     throws MBeanException, InstanceNotFoundException, AttributeNotFoundException,
         ReflectionException, IOException {
   final NameValueMap values = getCachedAttributes(objName, Collections.singleton(attrName));
   Object value = values.get(attrName);
   if (value != null || values.containsKey(attrName)) {
     return value;
   }
   // Not in cache, presumably because it was omitted from the
   // getAttributes result because of an exception.  Following
   // call will probably provoke the same exception.
   return conn.getAttribute(objName, attrName);
 }
 private synchronized NameValueMap getCachedAttributes(ObjectName objName, Set<String> attrNames)
     throws InstanceNotFoundException, ReflectionException, IOException {
   NameValueMap values = cachedValues.get(objName);
   if (values != null && values.keySet().containsAll(attrNames)) {
     return values;
   }
   attrNames = new TreeSet<String>(attrNames);
   Set<String> oldNames = cachedNames.get(objName);
   if (oldNames != null) {
     attrNames.addAll(oldNames);
   }
   values = new NameValueMap();
   final AttributeList attrs =
       conn.getAttributes(objName, attrNames.toArray(new String[attrNames.size()]));
   for (Attribute attr : attrs.asList()) {
     values.put(attr.getName(), attr.getValue());
   }
   cachedValues.put(objName, values);
   cachedNames.put(objName, attrNames);
   return values;
 }