コード例 #1
0
  /**
   * Waits until either the State attribute of this MBean equals the specified <VAR>state</VAR>
   * parameter, or the specified <VAR>timeout</VAR> has elapsed. The method <CODE>waitState</CODE>
   * returns with a boolean value indicating whether the specified <VAR>state</VAR> parameter equals
   * the value of this MBean's State attribute at the time the method terminates.
   *
   * <p>Two special cases for the <VAR>timeout</VAR> parameter value are:
   *
   * <UL>
   *   <LI>if <VAR>timeout</VAR> is negative then <CODE>waitState</CODE> returns immediately (i.e.
   *       does not wait at all),
   *   <LI>if <VAR>timeout</VAR> equals zero then <CODE>waitState</CODE> waits until the value of
   *       this MBean's State attribute is the same as the <VAR>state</VAR> parameter (i.e. will
   *       wait indefinitely if this condition is never met).
   * </UL>
   *
   * @param state The value of this MBean's State attribute to wait for. <VAR>state</VAR> can be one
   *     of: <CODE>DiscoveryResponder.OFFLINE</CODE>, <CODE>DiscoveryResponder.ONLINE</CODE>, <CODE>
   *     DiscoveryResponder.STARTING</CODE>, <CODE>DiscoveryResponder.STOPPING</CODE>.
   * @param timeout The maximum time to wait for, in milliseconds, if positive. Infinite time out if
   *     0, or no waiting at all if negative.
   * @return <code>true</code> if the value of this MBean's State attribute is the same as the
   *     <VAR>state</VAR> parameter; <code>false</code> otherwise.
   */
  public boolean waitState(int state, long timeout) {
    if (logger.finerOn()) {
      logger.finer(
          "waitState",
          state + "(0on,1off,2st) TO=" + timeout + " ; current state = " + getStateString());
    }
    // ------------
    // Test timeout
    // ------------
    if (timeout < 0) {
      return (this.state == state);
    }

    boolean done = (this.state == state);
    Date currentDate;
    long currentTimeOut = -1;

    // -----------------
    // Date start
    // -----------------
    Date endDate = new Date((new Date()).getTime() + timeout);
    while (!done) {
      // -----------------
      // Find out timeout
      // ----------------
      if (timeout != 0) {
        currentTimeOut = endDate.getTime() - (new Date()).getTime();
        if (currentTimeOut <= 0) {
          done = true;
          break;
        }
      }

      try {
        synchronized (this) {
          if (timeout == 0) {
            if (logger.finerOn()) {
              logger.finer("waitState", "Start waiting infinite, current state = " + this.state);
            }
            done = (this.state == state);
            while (!done) {
              done = (this.state == state);
              try {
                wait(1000);
              } catch (Exception e) {
              }
            }
          } else {
            if (logger.finerOn()) {
              logger.finer(
                  "waitState",
                  "Start waiting " + currentTimeOut + " current state = " + this.state);
            }
            wait(currentTimeOut);
          }
        }
        done = (this.state == state);
      } catch (InterruptedException e) {
        done = (this.state == state);
      }
    }
    if (logger.finerOn()) {
      logger.finer("waitState", "End, TO=" + currentTimeOut);
    }
    return (this.state == state);
  }
コード例 #2
0
 @Test
 public void customSimplifier() throws AttributeNotFoundException {
   Date date = new Date();
   Map result = (Map) converter.extractObject(date, new Stack<String>(), true);
   assertEquals(date.getTime(), result.get("millis"));
 }