Exemplo n.º 1
0
  /** Method to dump debugging information */
  private void dumpAll(Map map, int lvmid) {
    if (DEBUG) {
      Set keys = map.keySet();

      System.err.println("Dump for " + lvmid);
      int j = 0;
      for (Iterator i = keys.iterator(); i.hasNext(); j++) {
        Monitor monitor = (Monitor) map.get(i.next());
        System.err.println(j + "\t" + monitor.getName() + "=" + monitor.getValue());
      }
      System.err.println("nextEntry = " + nextEntry + " pollForEntry = " + pollForEntry);
      System.err.println("Buffer info:");
      System.err.println("buffer = " + buffer);
    }
  }
  /** Fire hostDisconnectEvent events. */
  void fireDisconnectedEvents() {
    ArrayList registered = null;
    HostEvent ev = null;

    synchronized (listeners) {
      registered = (ArrayList) listeners.clone();
    }

    for (Iterator i = registered.iterator(); i.hasNext(); /* empty */ ) {
      HostListener l = (HostListener) i.next();
      if (ev == null) {
        ev = new HostEvent(this);
      }
      l.disconnected(ev);
    }
  }
  /**
   * Fire VmStatusChangeEvent events to HostListener objects
   *
   * @param active Set of Integer objects containing the local Vm Identifiers of the active JVMs
   * @param started Set of Integer objects containing the local Vm Identifiers of new JVMs started
   *     since last interval.
   * @param terminated Set of Integer objects containing the local Vm Identifiers of terminated JVMs
   *     since last interval.
   */
  private void fireVmStatusChangedEvents(Set active, Set started, Set terminated) {
    ArrayList registered = null;
    VmStatusChangeEvent ev = null;

    synchronized (listeners) {
      registered = (ArrayList) listeners.clone();
    }

    for (Iterator i = registered.iterator(); i.hasNext(); /* empty */ ) {
      HostListener l = (HostListener) i.next();
      if (ev == null) {
        ev = new VmStatusChangeEvent(this, active, started, terminated);
      }
      l.vmStatusChanged(ev);
    }
  }
    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);
      }
    }