public static CallContext unregister() {
    CallContext context = s_currentContext.get();
    if (context == null) {
      return null;
    }
    s_currentContext.remove();
    if (s_logger.isTraceEnabled()) {
      s_logger.trace("Unregistered: " + context);
    }
    String contextId = context.getContextId();
    String sessionIdOnStack = null;
    String sessionIdPushedToNDC = "ctx-" + UuidUtils.first(contextId);
    while ((sessionIdOnStack = NDC.pop()) != null) {
      if (sessionIdPushedToNDC.equals(sessionIdOnStack)) {
        break;
      }
      if (s_logger.isTraceEnabled()) {
        s_logger.trace("Popping from NDC: " + contextId);
      }
    }

    Stack<CallContext> stack = s_currentContextStack.get();
    stack.pop();

    if (!stack.isEmpty()) {
      s_currentContext.set(stack.peek());
    }

    return context;
  }
 public static CallContext registerSystemCallContextOnceOnly() {
   try {
     CallContext context = s_currentContext.get();
     if (context == null) {
       return register(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM);
     }
     assert context.getCallingUserId() == User.UID_SYSTEM
         : "You are calling a very specific method that registers a one time system context.  This method is meant for background threads that does processing.";
     return context;
   } catch (Exception e) {
     s_logger.fatal(
         "Exiting the system because we're unable to register the system call context.", e);
     System.exit(1);
     throw new CloudRuntimeException("Should never hit this");
   }
 }
  /**
   * This method should only be called if you can propagate the context id from another CallContext.
   *
   * @param callingUser calling user
   * @param callingAccount calling account
   * @param contextId context id propagated from another call context
   * @return CallContext
   */
  public static CallContext register(User callingUser, Account callingAccount, String contextId) {
    /*
            Unit tests will have multiple times of setup/tear-down call to this, remove assertions to all unit test to run

            assert s_currentContext.get() == null : "There's a context already so what does this new register context mean? " + s_currentContext.get().toString();
            if (s_currentContext.get() != null) { // FIXME: This should be removed soon.  I added this check only to surface all the places that have this problem.
                throw new CloudRuntimeException("There's a context already so what does this new register context mean? " + s_currentContext.get().toString());
            }
    */
    CallContext callingContext = new CallContext(callingUser, callingAccount, contextId);
    s_currentContext.set(callingContext);
    NDC.push("ctx-" + UuidUtils.first(contextId));
    if (s_logger.isTraceEnabled()) {
      s_logger.trace("Registered: " + callingContext);
    }

    s_currentContextStack.get().push(callingContext);

    return callingContext;
  }
 public static CallContext current() {
   return s_currentContext.get();
 }