Search in sources :

Example 1 with FutureResult

use of com.intellij.util.concurrency.FutureResult in project intellij-community by JetBrains.

the class ExecutionHandler method runBuildImpl.

/**
   * @param antBuildListener should not be null. Use {@link com.intellij.lang.ant.config.AntBuildListener#NULL}
   */
@Nullable
private static FutureResult<ProcessHandler> runBuildImpl(final AntBuildFileBase buildFile, String[] targets, @Nullable final AntBuildMessageView buildMessageViewToReuse, final DataContext dataContext, List<BuildFileProperty> additionalProperties, @NotNull final AntBuildListener antBuildListener, final boolean waitFor) {
    final AntBuildMessageView messageView;
    final GeneralCommandLine commandLine;
    final Project project = buildFile.getProject();
    try {
        FileDocumentManager.getInstance().saveAllDocuments();
        final AntCommandLineBuilder builder = new AntCommandLineBuilder();
        builder.setBuildFile(buildFile.getAllOptions(), VfsUtilCore.virtualToIoFile(buildFile.getVirtualFile()));
        builder.calculateProperties(dataContext, buildFile.getProject(), additionalProperties);
        builder.addTargets(targets);
        builder.getCommandLine().setCharset(EncodingProjectManager.getInstance(buildFile.getProject()).getDefaultCharset());
        messageView = prepareMessageView(buildMessageViewToReuse, buildFile, targets, additionalProperties);
        commandLine = builder.getCommandLine().toCommandLine();
        messageView.setBuildCommandLine(commandLine.getCommandLineString());
    } catch (RunCanceledException e) {
        e.showMessage(project, AntBundle.message("run.ant.error.dialog.title"));
        antBuildListener.buildFinished(AntBuildListener.FAILED_TO_RUN, 0);
        return null;
    } catch (CantRunException e) {
        ExecutionErrorDialog.show(e, AntBundle.message("cant.run.ant.error.dialog.title"), project);
        antBuildListener.buildFinished(AntBuildListener.FAILED_TO_RUN, 0);
        return null;
    } catch (Macro.ExecutionCancelledException e) {
        antBuildListener.buildFinished(AntBuildListener.ABORTED, 0);
        return null;
    } catch (Throwable e) {
        antBuildListener.buildFinished(AntBuildListener.FAILED_TO_RUN, 0);
        LOG.error(e);
        return null;
    }
    final FutureResult<ProcessHandler> future = new FutureResult<>();
    new Task.Backgroundable(buildFile.getProject(), AntBundle.message("ant.build.progress.dialog.title"), true) {

        public boolean shouldStartInBackground() {
            return true;
        }

        public void onCancel() {
            antBuildListener.buildFinished(AntBuildListener.ABORTED, 0);
        }

        public void run(@NotNull final ProgressIndicator indicator) {
            try {
                ProcessHandler handler = runBuild(indicator, messageView, buildFile, antBuildListener, commandLine);
                future.set(handler);
                if (waitFor && handler != null) {
                    handler.waitFor();
                }
            } catch (Throwable e) {
                LOG.error(e);
                antBuildListener.buildFinished(AntBuildListener.FAILED_TO_RUN, 0);
            }
        }
    }.queue();
    return future;
}
Also used : Task(com.intellij.openapi.progress.Task) Macro(com.intellij.ide.macro.Macro) Project(com.intellij.openapi.project.Project) CantRunException(com.intellij.execution.CantRunException) FutureResult(com.intellij.util.concurrency.FutureResult) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with FutureResult

use of com.intellij.util.concurrency.FutureResult in project android by JetBrains.

the class DistributionServiceTest method testFallbackToPrevious.

/**
   * Test that we pick up previously-downloaded distributions if a new one can't be loaded.
   */
public void testFallbackToPrevious() throws Exception {
    File newFile = new File(CACHE_PATH, "distributions_2.json");
    FileUtil.copy(new File(new File(myFixture.getTestDataPath(), DISTRIBUTION_PATH), "testPreviousDistributions.json"), newFile);
    if (!newFile.setLastModified(20000)) {
        fail();
    }
    File oldFile = new File(CACHE_PATH, "distributions.json");
    FileUtil.copy(new File(new File(myFixture.getTestDataPath(), DISTRIBUTION_PATH), "testPreviousDistributions2.json"), oldFile);
    if (!oldFile.setLastModified(10000)) {
        fail();
    }
    FileDownloader downloader = Mockito.mock(FileDownloader.class);
    Mockito.when(downloader.download(Matchers.any(File.class))).thenThrow(new RuntimeException("expected exception"));
    DistributionService service = new DistributionService(downloader, CACHE_PATH, myDistributionFileUrl);
    final FutureResult<Boolean> result = new FutureResult<>();
    service.refresh(() -> result.set(true), () -> {
        assert false;
    });
    assertTrue(result.get(5, TimeUnit.SECONDS));
    assertEquals(.3, service.getSupportedDistributionForApiLevel(16), 0.0001);
}
Also used : FutureResult(com.intellij.util.concurrency.FutureResult) FileDownloader(com.intellij.util.download.FileDownloader) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) File(java.io.File)

Example 3 with FutureResult

use of com.intellij.util.concurrency.FutureResult in project intellij-community by JetBrains.

the class SearchResults method updateThreadSafe.

public void updateThreadSafe(final FindModel findModel, final boolean toChangeSelection, @Nullable final TextRange next, final int stamp) {
    if (myDisposed)
        return;
    final Editor editor = getEditor();
    final ArrayList<FindResult> results = new ArrayList<>();
    if (findModel != null) {
        updatePreviousFindModel(findModel);
        final FutureResult<int[]> startsRef = new FutureResult<>();
        final FutureResult<int[]> endsRef = new FutureResult<>();
        getSelection(editor, startsRef, endsRef);
        ApplicationManager.getApplication().runReadAction(() -> {
            Project project = getProject();
            if (myDisposed || project != null && project.isDisposed())
                return;
            int[] starts = new int[0];
            int[] ends = new int[0];
            try {
                starts = startsRef.get();
                ends = endsRef.get();
            } catch (InterruptedException | ExecutionException ignore) {
            }
            if (starts.length == 0 || findModel.isGlobal()) {
                findInRange(new TextRange(0, Integer.MAX_VALUE), editor, findModel, results);
            } else {
                for (int i = 0; i < starts.length; ++i) {
                    findInRange(new TextRange(starts[i], ends[i]), editor, findModel, results);
                }
            }
            final Runnable searchCompletedRunnable = () -> searchCompleted(results, editor, findModel, toChangeSelection, next, stamp);
            if (!ApplicationManager.getApplication().isUnitTestMode()) {
                UIUtil.invokeLaterIfNeeded(searchCompletedRunnable);
            } else {
                searchCompletedRunnable.run();
            }
        });
    }
}
Also used : ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) Project(com.intellij.openapi.project.Project) FutureResult(com.intellij.util.concurrency.FutureResult) Editor(com.intellij.openapi.editor.Editor) ExecutionException(java.util.concurrent.ExecutionException) FindResult(com.intellij.find.FindResult)

Example 4 with FutureResult

use of com.intellij.util.concurrency.FutureResult in project android by JetBrains.

the class DistributionServiceTest method testFailure.

/**
   * Test that the failure callback will be called if the download fails, and then the fallback data will be used.
   */
public void testFailure() throws Exception {
    FileDownloader downloader = Mockito.mock(FileDownloader.class);
    Mockito.when(downloader.download(Matchers.any(File.class))).thenThrow(new RuntimeException("expected exception"));
    DistributionService service = new DistributionService(downloader, CACHE_PATH, myDistributionFileUrl);
    final FutureResult<Boolean> result = new FutureResult<>();
    service.refresh(() -> {
        assert false;
    }, () -> result.set(true));
    assertTrue(result.get(5, TimeUnit.SECONDS));
    assertEquals(0.4, service.getSupportedDistributionForApiLevel(17), 0.001);
}
Also used : FutureResult(com.intellij.util.concurrency.FutureResult) FileDownloader(com.intellij.util.download.FileDownloader) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) File(java.io.File)

Aggregations

FutureResult (com.intellij.util.concurrency.FutureResult)4 Project (com.intellij.openapi.project.Project)2 FileDownloader (com.intellij.util.download.FileDownloader)2 File (java.io.File)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CantRunException (com.intellij.execution.CantRunException)1 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)1 FindResult (com.intellij.find.FindResult)1 Macro (com.intellij.ide.macro.Macro)1 Editor (com.intellij.openapi.editor.Editor)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 TextRange (com.intellij.openapi.util.TextRange)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 Nullable (org.jetbrains.annotations.Nullable)1