mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
extracted FileDownloader api
This commit is contained in:
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package com.intellij.facet.impl.ui.libraries;
|
||||
|
||||
import com.intellij.util.download.DownloadableFileDescription;
|
||||
import com.intellij.framework.library.DownloadableLibraryType;
|
||||
import com.intellij.framework.library.FrameworkLibraryVersion;
|
||||
import com.intellij.framework.library.LibraryVersionProperties;
|
||||
@@ -23,6 +22,8 @@ import com.intellij.openapi.roots.OrderRootType;
|
||||
import com.intellij.openapi.roots.ui.configuration.libraryEditor.NewLibraryEditor;
|
||||
import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.util.download.DownloadableFileDescription;
|
||||
import com.intellij.util.download.DownloadableFileService;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -108,9 +109,10 @@ public class LibraryDownloadSettings {
|
||||
|
||||
@Nullable
|
||||
public NewLibraryEditor download(JComponent parent) {
|
||||
LibraryDownloader downloader = new LibraryDownloader(mySelectedDownloads, null, parent, myDirectoryForDownloadedLibrariesPath, myLibraryName);
|
||||
VirtualFile[] files = downloader.download();
|
||||
if (files.length != mySelectedDownloads.size()) {
|
||||
VirtualFile[] files = DownloadableFileService.getInstance().createDownloader(mySelectedDownloads, null, parent, myLibraryName + " Library")
|
||||
.toDirectory(myDirectoryForDownloadedLibrariesPath)
|
||||
.download();
|
||||
if (files == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,10 +16,14 @@
|
||||
package com.intellij.util.download;
|
||||
|
||||
import com.intellij.openapi.components.ServiceManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.util.download.impl.FileDownloader;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author nik
|
||||
@@ -33,7 +37,14 @@ public abstract class DownloadableFileService {
|
||||
public abstract DownloadableFileDescription createFileDescription(@NotNull String downloadUrl, @NotNull String fileName);
|
||||
|
||||
@NotNull
|
||||
public abstract DownloadableFileSetVersions<DownloadableFileSetDescription> createFileSetVersions(@NotNull String groupId, @NotNull URL... localUrls);
|
||||
public abstract DownloadableFileSetVersions<DownloadableFileSetDescription> createFileSetVersions(@NotNull String groupId,
|
||||
@NotNull URL... localUrls);
|
||||
|
||||
public abstract void loadVersionsToCombobox(@NotNull DownloadableFileSetVersions<?> versions, @NotNull JComboBox comboBox);
|
||||
@NotNull
|
||||
public abstract FileDownloader createDownloader(@NotNull DownloadableFileSetDescription description, @Nullable Project project,
|
||||
JComponent parent);
|
||||
|
||||
@NotNull
|
||||
public abstract FileDownloader createDownloader(List<? extends DownloadableFileDescription> fileDescriptions, @Nullable Project project,
|
||||
JComponent parent, @NotNull String presentableDownloadName);
|
||||
}
|
||||
|
||||
+14
-1
@@ -16,12 +16,14 @@
|
||||
package com.intellij.util.download.impl;
|
||||
|
||||
import com.intellij.facet.frameworks.beans.Artifact;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.util.download.DownloadableFileDescription;
|
||||
import com.intellij.util.download.DownloadableFileService;
|
||||
import com.intellij.util.download.DownloadableFileSetDescription;
|
||||
import com.intellij.util.download.DownloadableFileSetVersions;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.net.URL;
|
||||
@@ -49,7 +51,18 @@ public class DownloadableFileServiceImpl extends DownloadableFileService {
|
||||
};
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public void loadVersionsToCombobox(@NotNull DownloadableFileSetVersions<?> versions, @NotNull JComboBox comboBox) {
|
||||
public FileDownloader createDownloader(@NotNull DownloadableFileSetDescription description,
|
||||
@Nullable Project project,
|
||||
JComponent parent) {
|
||||
return createDownloader(description.getFiles(), project, parent, description.getName());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public FileDownloader createDownloader(final List<? extends DownloadableFileDescription> fileDescriptions,
|
||||
final @Nullable Project project,
|
||||
JComponent parent, @NotNull String presentableDownloadName) {
|
||||
return new FileDownloaderImpl(fileDescriptions, project, parent, presentableDownloadName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2000-2011 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.util.download.impl;
|
||||
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author nik
|
||||
*/
|
||||
public interface FileDownloader {
|
||||
@NotNull
|
||||
FileDownloader toDirectory(@NotNull String directoryForDownloadedFilesPath);
|
||||
|
||||
@Nullable
|
||||
VirtualFile[] download();
|
||||
}
|
||||
+54
-45
@@ -14,9 +14,8 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.intellij.facet.impl.ui.libraries;
|
||||
package com.intellij.util.download.impl;
|
||||
|
||||
import com.intellij.util.download.DownloadableFileDescription;
|
||||
import com.intellij.ide.IdeBundle;
|
||||
import com.intellij.openapi.application.PathManager;
|
||||
import com.intellij.openapi.application.Result;
|
||||
@@ -38,11 +37,13 @@ import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VfsUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VirtualFileManager;
|
||||
import com.intellij.util.download.DownloadableFileDescription;
|
||||
import com.intellij.util.io.UrlConnectionUtil;
|
||||
import com.intellij.util.net.HttpConfigurable;
|
||||
import com.intellij.util.net.IOExceptionDialog;
|
||||
import com.intellij.util.net.NetUtils;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -55,7 +56,7 @@ import java.util.List;
|
||||
/**
|
||||
* @author nik
|
||||
*/
|
||||
public class LibraryDownloader {
|
||||
public class FileDownloaderImpl implements FileDownloader {
|
||||
private static final int CONNECTION_TIMEOUT = 60*1000;
|
||||
private static final int READ_TIMEOUT = 60*1000;
|
||||
@NonNls private static final String LIB_SCHEMA = "lib://";
|
||||
@@ -66,18 +67,24 @@ public class LibraryDownloader {
|
||||
private String myDirectoryForDownloadedFilesPath;
|
||||
private String myDialogTitle;
|
||||
|
||||
public LibraryDownloader(final List<? extends DownloadableFileDescription> fileDescriptions, final @Nullable Project project, JComponent parent,
|
||||
@Nullable String directoryForDownloadedFilePath, @Nullable String libraryPresentableName) {
|
||||
public FileDownloaderImpl(final List<? extends DownloadableFileDescription> fileDescriptions,
|
||||
final @Nullable Project project,
|
||||
JComponent parent,
|
||||
@NotNull String presentableDownloadName) {
|
||||
myProject = project;
|
||||
myFileDescriptions = fileDescriptions;
|
||||
myParent = parent;
|
||||
myDirectoryForDownloadedFilesPath = directoryForDownloadedFilePath;
|
||||
myDialogTitle = IdeBundle.message("progress.download.libraries.title");
|
||||
if (libraryPresentableName != null) {
|
||||
myDialogTitle = IdeBundle.message("progress.download.0.libraries.title", StringUtil.capitalize(libraryPresentableName));
|
||||
}
|
||||
myDialogTitle = IdeBundle.message("progress.download.0.title", StringUtil.capitalize(presentableDownloadName));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public FileDownloader toDirectory(@NotNull String directoryForDownloadedFilesPath) {
|
||||
myDirectoryForDownloadedFilesPath = directoryForDownloadedFilesPath;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VirtualFile[] download() {
|
||||
VirtualFile dir = null;
|
||||
if (myDirectoryForDownloadedFilesPath != null) {
|
||||
@@ -87,21 +94,23 @@ public class LibraryDownloader {
|
||||
}
|
||||
|
||||
if (dir == null) {
|
||||
dir = chooseDirectoryForLibraries();
|
||||
dir = chooseDirectoryForFiles();
|
||||
}
|
||||
|
||||
if (dir != null) {
|
||||
return doDownload(dir);
|
||||
}
|
||||
return VirtualFile.EMPTY_ARRAY;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private VirtualFile[] doDownload(final VirtualFile dir) {
|
||||
HttpConfigurable.getInstance().setAuthenticator();
|
||||
final List<Pair<DownloadableFileDescription, File>> downloadedFiles = new ArrayList<Pair<DownloadableFileDescription, File>>();
|
||||
final List<VirtualFile> existingFiles = new ArrayList<VirtualFile>();
|
||||
final List<File> existingFiles = new ArrayList<File>();
|
||||
final Ref<Exception> exceptionRef = Ref.create(null);
|
||||
final Ref<DownloadableFileDescription> currentFile = new Ref<DownloadableFileDescription>();
|
||||
final File ioDir = VfsUtil.virtualToIoFile(dir);
|
||||
|
||||
ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
|
||||
public void run() {
|
||||
@@ -112,11 +121,11 @@ public class LibraryDownloader {
|
||||
currentFile.set(description);
|
||||
if (indicator != null) {
|
||||
indicator.checkCanceled();
|
||||
indicator.setText(IdeBundle.message("progress.0.of.1.file.downloaded.text", i, myFileDescriptions.size()));
|
||||
indicator.setText(IdeBundle.message("progress.downloading.0.of.1.file.text", i+1, myFileDescriptions.size()));
|
||||
}
|
||||
|
||||
final VirtualFile existing = dir.findChild(description.getDefaultFileName());
|
||||
long size = existing != null ? existing.getLength() : -1;
|
||||
final File existing = new File(ioDir, description.getDefaultFileName());
|
||||
long size = existing.exists() ? existing.length() : -1;
|
||||
|
||||
if (!download(description, size, downloadedFiles)) {
|
||||
existingFiles.add(existing);
|
||||
@@ -133,39 +142,39 @@ public class LibraryDownloader {
|
||||
}, myDialogTitle, true, myProject, myParent);
|
||||
|
||||
Exception exception = exceptionRef.get();
|
||||
if (exception == null) {
|
||||
try {
|
||||
return moveToDir(existingFiles, downloadedFiles, dir);
|
||||
}
|
||||
catch (IOException e) {
|
||||
if (myProject != null) {
|
||||
Messages.showErrorDialog(myProject, myDialogTitle, e.getMessage());
|
||||
if (exception != null) {
|
||||
deleteFiles(downloadedFiles);
|
||||
if (exception instanceof IOException) {
|
||||
String message = IdeBundle.message("error.file.download.failed", exception.getMessage());
|
||||
if (currentFile.get() != null) {
|
||||
message += ": " + currentFile.get().getDownloadUrl();
|
||||
}
|
||||
else {
|
||||
Messages.showErrorDialog(myParent, myDialogTitle, e.getMessage());
|
||||
final boolean tryAgain = IOExceptionDialog.showErrorDialog(myDialogTitle, message);
|
||||
if (tryAgain) {
|
||||
return doDownload(dir);
|
||||
}
|
||||
return VirtualFile.EMPTY_ARRAY;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
deleteFiles(downloadedFiles);
|
||||
if (exception instanceof IOException) {
|
||||
String message = IdeBundle.message("error.library.download.failed", exception.getMessage());
|
||||
if (currentFile.get() != null) {
|
||||
message += ": " + currentFile.get().getDownloadUrl();
|
||||
try {
|
||||
return moveToDir(existingFiles, downloadedFiles, dir);
|
||||
}
|
||||
catch (IOException e) {
|
||||
if (myProject != null) {
|
||||
Messages.showErrorDialog(myProject, myDialogTitle, e.getMessage());
|
||||
}
|
||||
final boolean tryAgain = IOExceptionDialog.showErrorDialog(myDialogTitle, message);
|
||||
if (tryAgain) {
|
||||
return doDownload(dir);
|
||||
else {
|
||||
Messages.showErrorDialog(myParent, myDialogTitle, e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return VirtualFile.EMPTY_ARRAY;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private VirtualFile chooseDirectoryForLibraries() {
|
||||
private VirtualFile chooseDirectoryForFiles() {
|
||||
final FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
|
||||
descriptor.setTitle(IdeBundle.message("dialog.directory.for.libraries.title"));
|
||||
descriptor.setTitle(IdeBundle.message("dialog.directory.for.downloaded.files.title"));
|
||||
|
||||
final VirtualFile[] files;
|
||||
if (myProject != null) {
|
||||
@@ -178,7 +187,8 @@ public class LibraryDownloader {
|
||||
return files.length > 0 ? files[0] : null;
|
||||
}
|
||||
|
||||
private static VirtualFile[] moveToDir(final List<VirtualFile> existingFiles, final List<Pair<DownloadableFileDescription, File>> downloadedFiles, final VirtualFile dir) throws IOException {
|
||||
@NotNull
|
||||
private static VirtualFile[] moveToDir(final List<File> existingFiles, final List<Pair<DownloadableFileDescription, File>> downloadedFiles, final VirtualFile dir) throws IOException {
|
||||
List<VirtualFile> files = new ArrayList<VirtualFile>();
|
||||
|
||||
final File ioDir = VfsUtil.virtualToIoFile(dir);
|
||||
@@ -201,10 +211,10 @@ public class LibraryDownloader {
|
||||
}
|
||||
}
|
||||
|
||||
for (final VirtualFile file : existingFiles) {
|
||||
for (final File file : existingFiles) {
|
||||
VirtualFile libraryRootFile = new WriteAction<VirtualFile>() {
|
||||
protected void run(final Result<VirtualFile> result) {
|
||||
final String url = VfsUtil.getUrlForLibraryRoot(VfsUtil.virtualToIoFile(file));
|
||||
final String url = VfsUtil.getUrlForLibraryRoot(file);
|
||||
result.setResult(VirtualFileManager.getInstance().refreshAndFindFileByUrl(url));
|
||||
}
|
||||
|
||||
@@ -213,7 +223,6 @@ public class LibraryDownloader {
|
||||
files.add(libraryRootFile);
|
||||
}
|
||||
}
|
||||
|
||||
return VfsUtil.toVirtualFileArray(files);
|
||||
}
|
||||
|
||||
@@ -241,7 +250,7 @@ public class LibraryDownloader {
|
||||
final String presentableUrl = fileDescription.getPresentableDownloadUrl();
|
||||
final String url = fileDescription.getDownloadUrl();
|
||||
if (url.startsWith(LIB_SCHEMA)) {
|
||||
indicator.setText2(IdeBundle.message("progress.locate.jar.text", fileDescription.getPresentableFileName()));
|
||||
indicator.setText2(IdeBundle.message("progress.locate.file.text", fileDescription.getPresentableFileName()));
|
||||
final String path = FileUtil.toSystemDependentName(StringUtil.trimStart(url, LIB_SCHEMA));
|
||||
final File file = PathManager.findFileInLibDirectory(path);
|
||||
downloadedFiles.add(Pair.create(fileDescription, file));
|
||||
@@ -254,7 +263,7 @@ public class LibraryDownloader {
|
||||
}
|
||||
}
|
||||
else {
|
||||
indicator.setText2(IdeBundle.message("progress.connecting.to.dowload.jar.text", presentableUrl));
|
||||
indicator.setText2(IdeBundle.message("progress.connecting.to.download.file.text", presentableUrl));
|
||||
indicator.setIndeterminate(true);
|
||||
HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
|
||||
connection.setConnectTimeout(CONNECTION_TIMEOUT);
|
||||
@@ -276,10 +285,10 @@ public class LibraryDownloader {
|
||||
return false;
|
||||
}
|
||||
|
||||
tempFile = FileUtil.createTempFile("downloaded", "jar");
|
||||
tempFile = FileUtil.createTempFile("downloaded", "file");
|
||||
input = UrlConnectionUtil.getConnectionInputStreamWithException(connection, indicator);
|
||||
output = new BufferedOutputStream(new FileOutputStream(tempFile));
|
||||
indicator.setText2(IdeBundle.message("progress.download.jar.text", fileDescription.getPresentableFileName(), presentableUrl));
|
||||
indicator.setText2(IdeBundle.message("progress.download.file.text", fileDescription.getPresentableFileName(), presentableUrl));
|
||||
indicator.setIndeterminate(size == -1);
|
||||
|
||||
NetUtils.copyStreamContent(indicator, input, output, size);
|
||||
@@ -958,15 +958,15 @@ message.text.creating.deployment.descriptor=Creating Deployment Descriptor
|
||||
|
||||
button.facet.quickfix.text=&Fix
|
||||
|
||||
progress.download.0.title=Downloading {0}
|
||||
progress.download.file.text=Downloading ''{0}'' from ''{1}''...
|
||||
progress.connecting.to.download.file.text=Connecting to ''{0}''...
|
||||
progress.locate.file.text=Locating ''{0}''...
|
||||
progress.downloading.0.of.1.file.text=Downloading {0} of {1} {1, choice, 1#file|2#files}...
|
||||
dialog.directory.for.downloaded.files.title=Downloaded files will be copied to selected directory
|
||||
error.file.download.failed=Downloading failed: {0}
|
||||
|
||||
maven.repository.presentable.name=Maven repository
|
||||
progress.download.libraries.title=Downloading Libraries
|
||||
progress.download.0.libraries.title=Downloading {0} Libraries
|
||||
progress.download.jar.text=Downloading ''{0}'' from ''{1}''...
|
||||
progress.connecting.to.dowload.jar.text=Connecting to ''{0}''...
|
||||
progress.locate.jar.text=Locating ''{0}''...
|
||||
progress.0.of.1.file.downloaded.text={0} of {1} files downloaded
|
||||
dialog.directory.for.libraries.title=Downloaded libraries will be copied to selected directory
|
||||
error.library.download.failed=Library downloading failed: {0}
|
||||
label.missed.libraries.prefix=The following libraries are missing:
|
||||
label.missed.libraries.text={0}.<br>Class ''{1}'' not found
|
||||
missing.libraries.fix.button=Fix...
|
||||
|
||||
Reference in New Issue
Block a user