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;
}
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);
}
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();
}
});
}
}
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);
}
Aggregations