Search in sources :

Example 1 with DownloadableFileDescription

use of com.intellij.util.download.DownloadableFileDescription in project intellij-community by JetBrains.

the class FindJarFix method downloadJar.

private void downloadJar(String jarUrl, String jarName) {
    final Project project = myModule.getProject();
    final String dirPath = PropertiesComponent.getInstance(project).getValue("findjar.last.used.dir");
    VirtualFile toSelect = dirPath == null ? null : LocalFileSystem.getInstance().findFileByIoFile(new File(dirPath));
    final VirtualFile file = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFolderDescriptor(), project, toSelect);
    if (file != null) {
        PropertiesComponent.getInstance(project).setValue("findjar.last.used.dir", file.getPath());
        final DownloadableFileService downloader = DownloadableFileService.getInstance();
        final DownloadableFileDescription description = downloader.createFileDescription(jarUrl, jarName);
        final List<VirtualFile> jars = downloader.createDownloader(Arrays.asList(description), jarName).downloadFilesWithProgress(file.getPath(), project, myEditorComponent);
        if (jars != null && jars.size() == 1) {
            WriteAction.run(() -> OrderEntryFix.addJarToRoots(jars.get(0).getPresentableUrl(), myModule, myRef));
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) DownloadableFileDescription(com.intellij.util.download.DownloadableFileDescription) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) DownloadableFileService(com.intellij.util.download.DownloadableFileService)

Example 2 with DownloadableFileDescription

use of com.intellij.util.download.DownloadableFileDescription in project intellij-community by JetBrains.

the class FileDownloaderImpl method moveToDir.

private static List<Pair<File, DownloadableFileDescription>> moveToDir(List<Pair<File, DownloadableFileDescription>> downloadedFiles, final File targetDir) throws IOException {
    FileUtil.createDirectory(targetDir);
    List<Pair<File, DownloadableFileDescription>> result = new ArrayList<>();
    for (Pair<File, DownloadableFileDescription> pair : downloadedFiles) {
        final DownloadableFileDescription description = pair.getSecond();
        final String fileName = description.generateFileName(s -> !new File(targetDir, s).exists());
        final File toFile = new File(targetDir, fileName);
        FileUtil.rename(pair.getFirst(), toFile);
        result.add(Pair.create(toFile, description));
    }
    return result;
}
Also used : DownloadableFileDescription(com.intellij.util.download.DownloadableFileDescription) ArrayList(java.util.ArrayList) File(java.io.File) Pair(com.intellij.openapi.util.Pair)

Example 3 with DownloadableFileDescription

use of com.intellij.util.download.DownloadableFileDescription in project intellij-community by JetBrains.

the class FileDownloaderImpl method download.

@NotNull
@Override
public List<Pair<File, DownloadableFileDescription>> download(@NotNull final File targetDir) throws IOException {
    List<Pair<File, DownloadableFileDescription>> downloadedFiles = Collections.synchronizedList(new ArrayList<>());
    List<Pair<File, DownloadableFileDescription>> existingFiles = Collections.synchronizedList(new ArrayList<>());
    ProgressIndicator parentIndicator = ProgressManager.getInstance().getProgressIndicator();
    if (parentIndicator == null) {
        parentIndicator = new EmptyProgressIndicator();
    }
    try {
        final ConcurrentTasksProgressManager progressManager = new ConcurrentTasksProgressManager(parentIndicator, myFileDescriptions.size());
        parentIndicator.setText(IdeBundle.message("progress.downloading.0.files.text", myFileDescriptions.size()));
        int maxParallelDownloads = Runtime.getRuntime().availableProcessors();
        LOG.debug("Downloading " + myFileDescriptions.size() + " files using " + maxParallelDownloads + " threads");
        long start = System.currentTimeMillis();
        ExecutorService executor = AppExecutorUtil.createBoundedApplicationPoolExecutor("FileDownloaderImpl pool", maxParallelDownloads);
        List<Future<Void>> results = new ArrayList<>();
        final AtomicLong totalSize = new AtomicLong();
        for (final DownloadableFileDescription description : myFileDescriptions) {
            results.add(executor.submit(() -> {
                SubTaskProgressIndicator indicator = progressManager.createSubTaskIndicator();
                indicator.checkCanceled();
                final File existing = new File(targetDir, description.getDefaultFileName());
                final String url = description.getDownloadUrl();
                if (url.startsWith(LIB_SCHEMA)) {
                    final String path = FileUtil.toSystemDependentName(StringUtil.trimStart(url, LIB_SCHEMA));
                    final File file = PathManager.findFileInLibDirectory(path);
                    existingFiles.add(Pair.create(file, description));
                } else if (url.startsWith(LocalFileSystem.PROTOCOL_PREFIX)) {
                    String path = FileUtil.toSystemDependentName(StringUtil.trimStart(url, LocalFileSystem.PROTOCOL_PREFIX));
                    File file = new File(path);
                    if (file.exists()) {
                        existingFiles.add(Pair.create(file, description));
                    }
                } else {
                    File downloaded;
                    try {
                        downloaded = downloadFile(description, existing, indicator);
                    } catch (IOException e) {
                        throw new IOException(IdeBundle.message("error.file.download.failed", description.getDownloadUrl(), e.getMessage()), e);
                    }
                    if (FileUtil.filesEqual(downloaded, existing)) {
                        existingFiles.add(Pair.create(existing, description));
                    } else {
                        totalSize.addAndGet(downloaded.length());
                        downloadedFiles.add(Pair.create(downloaded, description));
                    }
                }
                indicator.finished();
                return null;
            }));
        }
        for (Future<Void> result : results) {
            try {
                result.get();
            } catch (InterruptedException e) {
                throw new ProcessCanceledException();
            } catch (ExecutionException e) {
                Throwables.propagateIfInstanceOf(e.getCause(), IOException.class);
                Throwables.propagateIfInstanceOf(e.getCause(), ProcessCanceledException.class);
                LOG.error(e);
            }
        }
        long duration = System.currentTimeMillis() - start;
        LOG.debug("Downloaded " + StringUtil.formatFileSize(totalSize.get()) + " in " + StringUtil.formatDuration(duration) + "(" + duration + "ms)");
        List<Pair<File, DownloadableFileDescription>> localFiles = new ArrayList<>();
        localFiles.addAll(moveToDir(downloadedFiles, targetDir));
        localFiles.addAll(existingFiles);
        return localFiles;
    } catch (ProcessCanceledException | IOException e) {
        deleteFiles(downloadedFiles);
        throw e;
    }
}
Also used : EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) DownloadableFileDescription(com.intellij.util.download.DownloadableFileDescription) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File) Pair(com.intellij.openapi.util.Pair) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with DownloadableFileDescription

use of com.intellij.util.download.DownloadableFileDescription in project intellij-community by JetBrains.

the class FileDownloaderImpl method downloadWithProcess.

@Nullable
private List<Pair<VirtualFile, DownloadableFileDescription>> downloadWithProcess(final File targetDir, Project project, JComponent parentComponent) {
    final Ref<List<Pair<File, DownloadableFileDescription>>> localFiles = Ref.create(null);
    final Ref<IOException> exceptionRef = Ref.create(null);
    boolean completed = ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
        try {
            localFiles.set(download(targetDir));
        } catch (IOException e) {
            exceptionRef.set(e);
        }
    }, myDialogTitle, true, project, parentComponent);
    if (!completed) {
        return null;
    }
    @SuppressWarnings("ThrowableResultOfMethodCallIgnored") Exception exception = exceptionRef.get();
    if (exception != null) {
        final boolean tryAgain = IOExceptionDialog.showErrorDialog(myDialogTitle, exception.getMessage());
        if (tryAgain) {
            return downloadWithProcess(targetDir, project, parentComponent);
        }
        return null;
    }
    return findVirtualFiles(localFiles.get());
}
Also used : DownloadableFileDescription(com.intellij.util.download.DownloadableFileDescription) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) File(java.io.File) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with DownloadableFileDescription

use of com.intellij.util.download.DownloadableFileDescription in project android by JetBrains.

the class DistributionService method getInstance.

public static DistributionService getInstance() {
    if (ourInstance == null) {
        DownloadableFileDescription description = DownloadableFileService.getInstance().createFileDescription(STATS_URL, DOWNLOAD_FILENAME);
        FileDownloader downloader = DownloadableFileService.getInstance().createDownloader(ImmutableList.of(description), "Distribution Stats");
        ourInstance = new DistributionService(downloader, CACHE_PATH, FALLBACK_URL);
    }
    return ourInstance;
}
Also used : DownloadableFileDescription(com.intellij.util.download.DownloadableFileDescription) FileDownloader(com.intellij.util.download.FileDownloader)

Aggregations

DownloadableFileDescription (com.intellij.util.download.DownloadableFileDescription)10 File (java.io.File)8 Pair (com.intellij.openapi.util.Pair)5 ArrayList (java.util.ArrayList)5 FileDownloader (com.intellij.util.download.FileDownloader)4 IOException (java.io.IOException)4 List (java.util.List)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 ImmutableList (com.google.common.collect.ImmutableList)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 Semaphore (com.intellij.util.concurrency.Semaphore)2 DownloadableFileService (com.intellij.util.download.DownloadableFileService)2 ExecutionException (java.util.concurrent.ExecutionException)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 ExecutionException (com.intellij.execution.ExecutionException)1 DownloadableLibraryFileDescription (com.intellij.framework.library.DownloadableLibraryFileDescription)1 LibraryVersionProperties (com.intellij.framework.library.LibraryVersionProperties)1 AnAction (com.intellij.openapi.actionSystem.AnAction)1