public void run() {
      super.run();

      // save the last set of active JVMs
      Set lastActiveVms = activeVms;

      try {
        // get the current set of active JVMs
        activeVms = (HashSet) vmManager.activeVms();

      } catch (MonitorException e) {
        // XXX: use logging api
        System.err.println("MonitoredHostProvider: polling task " + "caught MonitorException:");
        e.printStackTrace();

        // mark the HostManager as errored and notify listeners
        setLastException(e);
        fireDisconnectedEvents();
      }

      if (activeVms.isEmpty()) {
        return;
      }

      Set startedVms = new HashSet();
      Set terminatedVms = new HashSet();

      for (Iterator i = activeVms.iterator(); i.hasNext(); /* empty */ ) {
        Integer vmid = (Integer) i.next();
        if (!lastActiveVms.contains(vmid)) {
          // a new file has been detected, add to set
          startedVms.add(vmid);
        }
      }

      for (Iterator i = lastActiveVms.iterator(); i.hasNext();
      /* empty */ ) {
        Object o = i.next();
        if (!activeVms.contains(o)) {
          // JVM has terminated, remove it from the active list
          terminatedVms.add(o);
        }
      }

      if (!startedVms.isEmpty() || !terminatedVms.isEmpty()) {
        fireVmStatusChangedEvents(activeVms, startedVms, terminatedVms);
      }
    }
  static int findVmid(String agentName) {
    MonitoredHost host;
    if (activeVms == null) {
      try {
        host = MonitoredHost.getMonitoredHost(new HostIdentifier((String) null));
        activeVms = host.activeVms();
      } catch (java.net.URISyntaxException e) {
        throw new InternalError(e.getMessage());
      } catch (sun.jvmstat.monitor.MonitorException e) {
        throw new InternalError(e.getMessage());
      }
    }

    for (Object vm : activeVms) {
      try {
        String address = ConnectorAddressLink.importFrom((Integer) vm);
        if (address == null) continue;
        JMXConnector jmxc;
        MBeanServerConnection server;
        JMXServiceURL jmxUrl = new JMXServiceURL(address);
        jmxc = JMXConnectorFactory.connect(jmxUrl);
        server = jmxc.getMBeanServerConnection();
        Object result;
        result = server.getAttribute(new ObjectName("net.multiverse:type=Engine"), "AgentName");
        jmxc.close();
        if (result != null && result.toString().equals(agentName)) return (Integer) vm;
      } catch (IOException e) {
        System.err.println("Unable to attach to " + (Integer) vm + ": " + e.getMessage());
      } catch (javax.management.InstanceNotFoundException e) {
        // ignore
      } catch (JMException e) {
        System.err.println("Unable to attach to " + (Integer) vm + ": " + e);
      }
    }

    return -1;
  }