/** Wait for the server to die. */
  private void waitForDeath() throws CommandException {
    if (!programOpts.isTerse()) {
      // use stdout because logger always appends a newline
      System.out.print(Strings.get("StopInstance.waitForDeath") + " ");
    }
    long startWait = System.currentTimeMillis();
    boolean alive = true;
    int count = 0;

    while (!timedOut(startWait)) {
      if (!isRunning()) {
        alive = false;
        break;
      }
      try {
        Thread.sleep(100);
        if (!programOpts.isTerse() && count++ % 10 == 0) System.out.print(".");
      } catch (InterruptedException ex) {
        // don't care
      }
    }

    if (!programOpts.isTerse()) System.out.println();

    if (alive) {
      throw new CommandException(
          Strings.get("StopInstance.instanceNotDead", (CLIConstants.DEATH_TIMEOUT_MS / 1000)));
    }
  }
  /** Print message and return exit code when we detect that the DAS is not running. */
  protected int instanceNotRunning() throws CommandException {
    if (kill) return kill();

    // by definition this is not an error
    // https://glassfish.dev.java.net/issues/show_bug.cgi?id=8387

    logger.warning(Strings.get("StopInstance.instanceNotRunning"));
    return 0;
  }
  private int kill() throws CommandException {
    File prevPid = null;
    String pids = null;

    try {
      prevPid = new File(getServerDirs().getPidFile().getPath() + ".prev");

      if (!prevPid.canRead())
        throw new CommandException(Strings.get("StopInstance.nopidprev", prevPid));

      pids = FileUtils.readSmallFile(prevPid).trim();
      String s = ProcessUtils.kill(Integer.parseInt(pids));

      if (s != null) logger.finer(s);
    } catch (CommandException ce) {
      throw ce;
    } catch (Exception ex) {
      throw new CommandException(
          Strings.get("StopInstance.pidprevreaderror", prevPid, ex.getMessage()));
    }
    return 0;
  }
 /** Print message and return exit code when we detect that there is no such instance */
 private int noSuchInstance() {
   // by definition this is not an error
   // https://glassfish.dev.java.net/issues/show_bug.cgi?id=8387
   logger.warning(Strings.get("Instance.noSuchInstance"));
   return 0;
 }