public T executeRequestInBackground()

in src/org/jetbrains/tfsIntegration/webservice/TfsRequestManager.java [84:177]


  public <T> T executeRequestInBackground(final Object projectOrComponent, final boolean force, final Request<T> request)
    throws TfsException {
    LOG.assertTrue(!ApplicationManager.getApplication().isDispatchThread());
    LOG.assertTrue(myServerUri != null);

    boolean showDialog = shouldShowDialog(force);
    final Ref<String> message = new Ref<>();
    final Ref<Credentials> credentials = new Ref<>(TFSConfigurationManager.getInstance().getCredentials(myServerUri));

    while (true) {
      if (showDialog || !message.isNull()) {
        try {
          ourShowDialogLock.lock();
          ProgressManager.checkCanceled();
          showDialog = shouldShowDialog(force); // check again since another thread could already enter right credentials
          // TODO we probably have to compare original password and current one
          if (!message.isNull() || showDialog) {
            ProgressIndicator pi = ProgressManager.getInstance().getProgressIndicator();
            if (pi != null) {
              WaitForProgressToShow.execute(pi);
            }

            Ref<Boolean> ok = new Ref<>();
            ApplicationManager.getApplication().invokeAndWait(() -> {
              if (message.isNull()) {
                try {
                  if (!shouldShowDialog(force)) {
                    ok.set(true);
                    return; // check one more time since UI thread call could already enter right credentials
                  }
                }
                catch (UserCancelledException e) {
                  ok.set(false);
                  return;
                }
              }
              TfsLoginDialog d;
              if (projectOrComponent instanceof JComponent) {
                d = new TfsLoginDialog((JComponent)projectOrComponent, myServerUri, credentials.get(), false, null);
              }
              else {
                d = new TfsLoginDialog((Project)projectOrComponent, myServerUri, credentials.get(), false, null);
              }
              d.setMessage(message.get());
              if (d.showAndGet()) {
                credentials.set(d.getCredentials());
                ok.set(true);
              }
              else {
                ok.set(false);
              }
            });

            if (!ok.get()) {
              if (!force) {
                TFSConfigurationManager.getInstance().setAuthCanceled(myServerUri, projectOrComponent);
              }
              throw new AuthCancelledException(myServerUri);
            }
          }
          else {
            credentials.set(TFSConfigurationManager.getInstance().getCredentials(myServerUri));
          }
        }
        finally {
          ourShowDialogLock.unlock();
        }
      }
      LOG.assertTrue(!credentials.isNull());
      try {
        myRequestLock.lock();
        ProgressManager.checkCanceled();
        ProgressIndicator pi = ProgressManager.getInstance().getProgressIndicator();
        T result = executeRequestImpl(myServerUri, credentials, request, pi);
        TFSConfigurationManager.getInstance().storeCredentials(myServerUri, credentials.get());
        return result;
      }
      catch (Exception e) {
        final TfsException tfsException = TfsExceptionManager.processException(e);
        LOG.warn(tfsException);
        if (tfsException instanceof UnauthorizedException) {
          message.set(getMessage(tfsException, credentials.get().getType()));
          continue;
        }
        else if (!(tfsException instanceof ConnectionFailedException)) {
          TFSConfigurationManager.getInstance().storeCredentials(myServerUri, credentials.get());
        }
        throw tfsException;
      }
      finally {
        myRequestLock.unlock();
      }
    }
  }