/** {@inheritDoc} */
  public void endMaintenance() {
    log.info("Resuming transport listeners, senders and tasks from maintenance mode...");

    // resume transport listeners and senders
    Axis2TransportHelper transportHelper = new Axis2TransportHelper(configurationContext);
    transportHelper.resumeListeners();
    transportHelper.resumeSenders();

    // resume tasks
    SynapseTaskManager synapseTaskManager = synapseEnvironment.getTaskManager();
    if (synapseTaskManager.isInitialized()) {
      synapseTaskManager.resumeAll();
    }

    log.info("Resumed normal operation from maintenance mode");
  }
  /** {@inheritDoc} */
  public void startMaintenance() {
    log.info("Putting transport listeners, senders and tasks into maintenance mode..");

    // pause transport listeners and senders
    Axis2TransportHelper transportHelper = new Axis2TransportHelper(configurationContext);
    transportHelper.pauseListeners();
    transportHelper.pauseSenders();

    // put tasks on hold
    SynapseTaskManager synapseTaskManager = synapseEnvironment.getTaskManager();
    if (synapseTaskManager.isInitialized()) {
      synapseTaskManager.pauseAll();
    }

    log.info("Entered maintenance mode");
  }
  /**
   * Waits until it is safe to stop or the the specified end time has been reached. A delay of
   * <code>waitIntervalMillis</code> milliseconds is used between each subsequent check. If the
   * state "safeToStop" is reached before the specified <code>endTime</code>, the return value is
   * true.
   *
   * @param waitIntervalMillis the pause time (delay) in milliseconds between subsequent checks
   * @param endTime the time until which the checks need to finish successfully
   * @return true, if a safe state is reached before the specified <code>endTime</code>, otherwise
   *     false (forceful stop required)
   */
  public boolean waitUntilSafeToStop(long waitIntervalMillis, long endTime) {

    boolean safeToStop = false;
    boolean forcefulStop = false;
    Axis2TransportHelper transportHelper = new Axis2TransportHelper(configurationContext);

    // wait until it is safe to shutdown (listeners and tasks are idle, no callbacks)
    while (!safeToStop && !forcefulStop) {

      int pendingListenerThreads = transportHelper.getPendingListenerThreadCount();
      if (pendingListenerThreads > 0) {
        log.info(
            new StringBuilder("Waiting for: ")
                .append(pendingListenerThreads)
                .append(" listener threads to complete")
                .toString());
      }
      int pendingSenderThreads = transportHelper.getPendingSenderThreadCount();
      if (pendingSenderThreads > 0) {
        log.info(
            new StringBuilder("Waiting for: ")
                .append(pendingSenderThreads)
                .append(" listener threads to complete")
                .toString());
      }
      int activeConnections = transportHelper.getActiveConnectionsCount();
      if (activeConnections > 0) {
        log.info("Waiting for: " + activeConnections + " active connections to be closed..");
      }
      int pendingTransportThreads = pendingListenerThreads + pendingSenderThreads;

      int pendingCallbacks = serverContextInformation.getCallbackCount();
      if (pendingCallbacks > 0) {
        log.info("Waiting for: " + pendingCallbacks + " callbacks/replies..");
      }

      int runningTasks = 0;
      SynapseTaskManager synapseTaskManager = synapseEnvironment.getTaskManager();
      if (synapseTaskManager.isInitialized()) {
        runningTasks = synapseTaskManager.getTaskScheduler().getRunningTaskCount();
        if (runningTasks > 0) {
          log.info("Waiting for : " + runningTasks + " tasks to complete..");
        }
      }

      // it is safe to stop if all used listener threads, callbacks and tasks are zero
      safeToStop = ((pendingTransportThreads + pendingCallbacks + runningTasks) == 0);

      if (safeToStop) {
        log.info("All transport threads and tasks are idle and no pending callbacks..");
      } else {
        if (System.currentTimeMillis() < endTime) {
          log.info(
              new StringBuilder("Waiting for a maximum of another ")
                  .append((endTime - System.currentTimeMillis()) / 1000)
                  .append(" seconds until transport threads and tasks become idle, ")
                  .append("active connections to get closed,")
                  .append(" and callbacks to be completed..")
                  .toString());
          try {
            Thread.sleep(waitIntervalMillis);
          } catch (InterruptedException ignore) {
            // nothing to do here
          }
        } else {
          // maximum time to wait is over, do a forceful stop
          forcefulStop = true;
        }
      }
    }

    return !forcefulStop;
  }