ProgressListener is now a method arg (was an instance arg)

This commit is contained in:
Federico Fissore 2015-08-04 11:01:00 +02:00
parent b3baa27435
commit dc93bb93d0
13 changed files with 86 additions and 107 deletions

View File

@ -24,6 +24,7 @@ public class ContributionsSelfCheck extends TimerTask {
private final ContributionInstaller contributionInstaller;
private final LibrariesIndexer librariesIndexer;
private final LibraryInstaller libraryInstaller;
private final ProgressListener progressListener;
public ContributionsSelfCheck(Base base, HyperlinkListener hyperlinkListener, ContributionsIndexer contributionsIndexer, ContributionInstaller contributionInstaller, LibrariesIndexer librariesIndexer, LibraryInstaller libraryInstaller) {
this.base = base;
@ -32,6 +33,7 @@ public class ContributionsSelfCheck extends TimerTask {
this.contributionInstaller = contributionInstaller;
this.librariesIndexer = librariesIndexer;
this.libraryInstaller = libraryInstaller;
this.progressListener = new NoopProgressListener();
}
@Override
@ -67,7 +69,7 @@ public class ContributionsSelfCheck extends TimerTask {
private void updateLibrariesIndex() {
try {
libraryInstaller.updateIndex();
libraryInstaller.updateIndex(progressListener);
} catch (Exception e) {
// ignore
}
@ -75,7 +77,7 @@ public class ContributionsSelfCheck extends TimerTask {
private void updateContributionIndex() {
try {
contributionInstaller.updateIndex();
contributionInstaller.updateIndex(progressListener);
} catch (Exception e) {
// ignore
}

View File

@ -183,12 +183,7 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
filterField.setEnabled(contribModel.getRowCount() > 0);
// Create LibrariesInstaller tied with the provided index
installer = new LibraryInstaller(indexer, platform) {
@Override
public void onProgress(Progress progress) {
setProgress(progress);
}
};
installer = new LibraryInstaller(indexer, platform);
}
public void selectDropdownItemByClassName(String dropdownItem) {
@ -216,7 +211,7 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
installerThread = new Thread(() -> {
try {
setProgressVisible(true, "");
installer.updateIndex();
installer.updateIndex(this::setProgress);
onIndexesUpdated();
} catch (Exception e) {
throw new RuntimeException(e);
@ -233,7 +228,7 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
installerThread = new Thread(() -> {
try {
setProgressVisible(true, tr("Installing..."));
installer.install(lib, replaced);
installer.install(lib, replaced, this::setProgress);
onIndexesUpdated(); // TODO: Do a better job in refreshing only the needed element
//getContribModel().updateLibrary(lib);
} catch (Exception e) {
@ -260,7 +255,7 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
installerThread = new Thread(() -> {
try {
setProgressVisible(true, tr("Removing..."));
installer.remove(lib);
installer.remove(lib, this::setProgress);
onIndexesUpdated(); // TODO: Do a better job in refreshing only the needed element
//getContribModel().updateLibrary(lib);
} catch (Exception e) {

View File

@ -118,7 +118,7 @@ public class ContributionManagerUI extends InstallerJDialog {
}
// Create ConstributionInstaller tied with the provided index
installer = new ContributionInstaller(indexer, platform, new GPGDetachedSignatureVerifier(), this::setProgress);
installer = new ContributionInstaller(indexer, platform, new GPGDetachedSignatureVerifier());
}
public void setProgress(Progress progress) {
@ -146,7 +146,7 @@ public class ContributionManagerUI extends InstallerJDialog {
installerThread = new Thread(() -> {
try {
setProgressVisible(true, "");
List<String> downloadedPackageIndexFiles = installer.updateIndex();
List<String> downloadedPackageIndexFiles = installer.updateIndex(this::setProgress);
installer.deleteUnknownFiles(downloadedPackageIndexFiles);
onIndexesUpdated();
} catch (Exception e) {
@ -165,7 +165,7 @@ public class ContributionManagerUI extends InstallerJDialog {
List<String> errors = new LinkedList<>();
try {
setProgressVisible(true, tr("Installing..."));
errors.addAll(installer.install(platformToInstall));
errors.addAll(installer.install(platformToInstall, this::setProgress));
if (platformToRemove != null && !platformToRemove.isReadOnly()) {
errors.addAll(installer.remove(platformToRemove));
}

View File

@ -33,7 +33,6 @@ import cc.arduino.contributions.packages.ContributionsIndexer;
import cc.arduino.contributions.packages.ui.ContributionManagerUI;
import cc.arduino.files.DeleteFilesOnShutdown;
import cc.arduino.packages.DiscoveryManager;
import cc.arduino.utils.Progress;
import cc.arduino.view.Event;
import cc.arduino.view.JMenuUtils;
import cc.arduino.view.SplashScreenHelper;
@ -341,19 +340,10 @@ public class Base {
if (parser.isInstallBoard()) {
ContributionsIndexer indexer = new ContributionsIndexer(BaseNoGui.getSettingsFolder(), BaseNoGui.getPlatform(), new GPGDetachedSignatureVerifier());
ContributionInstaller installer = new ContributionInstaller(indexer, BaseNoGui.getPlatform(), new GPGDetachedSignatureVerifier(), new ProgressListener() {
private String lastStatus = "";
ProgressListener progressListener = new ConsoleProgressListener();
ContributionInstaller installer = new ContributionInstaller(indexer, BaseNoGui.getPlatform(), new GPGDetachedSignatureVerifier());
@Override
public void onProgress(Progress progress) {
if (!lastStatus.equals(progress.getStatus())) {
System.out.println(progress.getStatus());
}
lastStatus = progress.getStatus();
}
});
List<String> downloadedPackageIndexFiles = installer.updateIndex();
List<String> downloadedPackageIndexFiles = installer.updateIndex(progressListener);
installer.deleteUnknownFiles(downloadedPackageIndexFiles);
indexer.parseIndex();
indexer.syncWithFilesystem(BaseNoGui.getHardwareFolder());
@ -378,7 +368,7 @@ public class Base {
ContributedPlatform installed = indexer.getInstalled(boardToInstallParts[0], boardToInstallParts[1]);
if (!selected.isReadOnly()) {
installer.install(selected);
installer.install(selected, progressListener);
}
if (installed != null && !installed.isReadOnly()) {
@ -389,22 +379,13 @@ public class Base {
} else if (parser.isInstallLibrary()) {
LibrariesIndexer indexer = new LibrariesIndexer(BaseNoGui.getSettingsFolder(), new ContributionsIndexer(BaseNoGui.getSettingsFolder(), BaseNoGui.getPlatform(), new GPGDetachedSignatureVerifier()));
LibraryInstaller installer = new LibraryInstaller(indexer, BaseNoGui.getPlatform()) {
private String lastStatus = "";
@Override
protected void onProgress(Progress progress) {
if (!lastStatus.equals(progress.getStatus())) {
System.out.println(progress.getStatus());
}
lastStatus = progress.getStatus();
}
};
ProgressListener progressListener = new ConsoleProgressListener();
LibraryInstaller installer = new LibraryInstaller(indexer, BaseNoGui.getPlatform());
indexer.parseIndex();
BaseNoGui.onBoardOrPortChange();
indexer.setSketchbookLibrariesFolder(BaseNoGui.getSketchbookLibrariesFolder());
indexer.setLibrariesFolders(BaseNoGui.getLibrariesPath());
installer.updateIndex();
installer.updateIndex(progressListener);
for (String library : parser.getLibraryToInstall().split(",")) {
String[] libraryToInstallParts = library.split(":");
@ -426,9 +407,9 @@ public class Base {
ContributedLibrary installed = indexer.getIndex().getInstalled(libraryToInstallParts[0]);
if (selected.isReadOnly()) {
installer.remove(installed);
installer.remove(installed, progressListener);
} else {
installer.install(selected, installed);
installer.install(selected, installed, progressListener);
}
}

View File

@ -38,7 +38,7 @@ public class GzippedJsonDownloaderTest {
@Test
public void testJsonDownload() throws Exception {
new GZippedJsonDownloader(downloader, new URL("http://downloads.arduino.cc/libraries/library_index.json"), new URL("http://downloads.arduino.cc/libraries/library_index.json.gz")).download(tempFile, new MultiStepProgress(1), "");
new GZippedJsonDownloader(downloader, new URL("http://downloads.arduino.cc/libraries/library_index.json"), new URL("http://downloads.arduino.cc/libraries/library_index.json.gz")).download(tempFile, new MultiStepProgress(1), "", new NoopProgressListener());
InputStream indexIn = new FileInputStream(tempFile);
ObjectMapper mapper = new ObjectMapper();

View File

@ -38,7 +38,7 @@ public class JsonDownloaderTest {
@Test
public void testJsonDownload() throws Exception {
new JsonDownloader(downloader, new URL("http://downloads.arduino.cc/libraries/library_index.json")).download(tempFile, new MultiStepProgress(1), "");
new JsonDownloader(downloader, new URL("http://downloads.arduino.cc/libraries/library_index.json")).download(tempFile, new MultiStepProgress(1), "", new NoopProgressListener());
InputStream indexIn = new FileInputStream(tempFile);
ObjectMapper mapper = new ObjectMapper();

View File

@ -0,0 +1,15 @@
package cc.arduino.contributions;
import cc.arduino.utils.Progress;
public class ConsoleProgressListener implements ProgressListener {
private String lastStatus = "";
@Override
public void onProgress(Progress progress) {
if (!lastStatus.equals(progress.getStatus())) {
System.out.println(progress.getStatus());
}
lastStatus = progress.getStatus();
}
}

View File

@ -51,7 +51,7 @@ public class DownloadableContributionsDownloader {
stagingFolder = _stagingFolder;
}
public File download(DownloadableContribution contribution, Progress progress, final String statusText) throws Exception {
public File download(DownloadableContribution contribution, Progress progress, final String statusText, ProgressListener progressListener) throws Exception {
URL url = new URL(contribution.getUrl());
Path outputFile = Paths.get(stagingFolder.getAbsolutePath(), contribution.getArchiveFileName());
@ -64,12 +64,12 @@ public class DownloadableContributionsDownloader {
// Need to download or resume downloading?
if (!Files.isRegularFile(outputFile, LinkOption.NOFOLLOW_LINKS) || (Files.size(outputFile) < contribution.getSize())) {
download(url, outputFile.toFile(), progress, statusText);
download(url, outputFile.toFile(), progress, statusText, progressListener);
}
// Test checksum
progress.setStatus(tr("Verifying archive integrity..."));
onProgress(progress);
progressListener.onProgress(progress);
String checksum = contribution.getChecksum();
if (hasChecksum(contribution)) {
String algo = checksum.split(":")[0];
@ -94,7 +94,7 @@ public class DownloadableContributionsDownloader {
return algo != null && !algo.isEmpty();
}
public void download(URL url, File tmpFile, Progress progress, String statusText) throws Exception {
public void download(URL url, File tmpFile, Progress progress, String statusText, ProgressListener progressListener) throws Exception {
FileDownloader downloader = new FileDownloader(url, tmpFile);
downloader.addObserver((o, arg) -> {
FileDownloader me = (FileDownloader) o;
@ -106,7 +106,7 @@ public class DownloadableContributionsDownloader {
}
progress.setStatus(statusText + " " + msg);
progress.setProgress(me.getProgress());
onProgress(progress);
progressListener.onProgress(progress);
});
downloader.download();
if (!downloader.isCompleted()) {
@ -114,8 +114,4 @@ public class DownloadableContributionsDownloader {
}
}
protected void onProgress(Progress progress) {
// Empty
}
}

View File

@ -49,14 +49,14 @@ public class GZippedJsonDownloader {
this.gzippedUrl = gzippedUrl;
}
public void download(File tmpFile, Progress progress, String statusText) throws Exception {
public void download(File tmpFile, Progress progress, String statusText, ProgressListener progressListener) throws Exception {
try {
new JsonDownloader(downloader, gzippedUrl).download(tmpFile, progress, statusText);
new JsonDownloader(downloader, gzippedUrl).download(tmpFile, progress, statusText, progressListener);
File gzipTmpFile = new File(tmpFile.getParentFile(), GzipUtils.getCompressedFilename(tmpFile.getName()));
tmpFile.renameTo(gzipTmpFile);
decompress(gzipTmpFile, tmpFile);
} catch (Exception e) {
new JsonDownloader(downloader, url).download(tmpFile, progress, statusText);
new JsonDownloader(downloader, url).download(tmpFile, progress, statusText, progressListener);
}
}

View File

@ -44,9 +44,9 @@ public class JsonDownloader {
this.url = url;
}
public void download(File tmpFile, Progress progress, String statusText) throws Exception {
public void download(File tmpFile, Progress progress, String statusText, ProgressListener progressListener) throws Exception {
try {
downloader.download(url, tmpFile, progress, statusText);
downloader.download(url, tmpFile, progress, statusText, progressListener);
} catch (InterruptedException e) {
// Download interrupted... just exit
}

View File

@ -0,0 +1,12 @@
package cc.arduino.contributions;
import cc.arduino.utils.Progress;
public class NoopProgressListener implements ProgressListener {
@Override
public void onProgress(Progress progress) {
}
}

View File

@ -31,9 +31,9 @@ package cc.arduino.contributions.libraries;
import cc.arduino.contributions.DownloadableContributionsDownloader;
import cc.arduino.contributions.GZippedJsonDownloader;
import cc.arduino.contributions.ProgressListener;
import cc.arduino.utils.ArchiveExtractor;
import cc.arduino.utils.MultiStepProgress;
import cc.arduino.utils.Progress;
import processing.app.I18n;
import processing.app.Platform;
import processing.app.helpers.FileUtils;
@ -67,15 +67,10 @@ public class LibraryInstaller {
this.indexer = indexer;
this.platform = platform;
File stagingFolder = indexer.getStagingFolder();
downloader = new DownloadableContributionsDownloader(stagingFolder) {
@Override
protected void onProgress(Progress progress) {
LibraryInstaller.this.onProgress(progress);
}
};
downloader = new DownloadableContributionsDownloader(stagingFolder);
}
public void updateIndex() throws Exception {
public void updateIndex(ProgressListener progressListener) throws Exception {
final MultiStepProgress progress = new MultiStepProgress(2);
// Step 1: Download index
@ -83,7 +78,7 @@ public class LibraryInstaller {
File tmpFile = new File(outputFile.getAbsolutePath() + ".tmp");
try {
GZippedJsonDownloader gZippedJsonDownloader = new GZippedJsonDownloader(downloader, new URL(LIBRARY_INDEX_URL), new URL(LIBRARY_INDEX_URL_GZ));
gZippedJsonDownloader.download(tmpFile, progress, tr("Downloading libraries index..."));
gZippedJsonDownloader.download(tmpFile, progress, tr("Downloading libraries index..."), progressListener);
} catch (InterruptedException e) {
// Download interrupted... just exit
return;
@ -99,10 +94,10 @@ public class LibraryInstaller {
throw new Exception(tr("An error occurred while updating libraries index!"));
// Step 2: Rescan index
rescanLibraryIndex(progress);
rescanLibraryIndex(progress, progressListener);
}
public void install(ContributedLibrary lib, ContributedLibrary replacedLib) throws Exception {
public void install(ContributedLibrary lib, ContributedLibrary replacedLib, ProgressListener progressListener) throws Exception {
if (lib.isInstalled()) {
System.out.println(I18n.format(tr("Library is already installed: {0} version {1}"), lib.getName(), lib.getParsedVersion()));
return;
@ -112,7 +107,7 @@ public class LibraryInstaller {
// Step 1: Download library
try {
downloader.download(lib, progress, I18n.format(tr("Downloading library: {0}"), lib.getName()));
downloader.download(lib, progress, I18n.format(tr("Downloading library: {0}"), lib.getName()), progressListener);
} catch (InterruptedException e) {
// Download interrupted... just exit
return;
@ -124,7 +119,7 @@ public class LibraryInstaller {
// Step 2: Unpack library on the correct location
progress.setStatus(I18n.format(tr("Installing library: {0}"), lib.getName()));
onProgress(progress);
progressListener.onProgress(progress);
File libsFolder = indexer.getSketchbookLibrariesFolder();
File tmpFolder = FileUtils.createTempFolderIn(libsFolder);
try {
@ -137,16 +132,16 @@ public class LibraryInstaller {
// Step 3: Remove replaced library and move installed one to the correct location
// TODO: Fix progress bar...
remove(replacedLib);
remove(replacedLib, progressListener);
File destFolder = new File(libsFolder, lib.getName().replaceAll(" ", "_"));
tmpFolder.renameTo(destFolder);
progress.stepDone();
// Step 4: Rescan index
rescanLibraryIndex(progress);
rescanLibraryIndex(progress, progressListener);
}
public void remove(ContributedLibrary lib) throws IOException {
public void remove(ContributedLibrary lib, ProgressListener progressListener) throws IOException {
if (lib == null || lib.isReadOnly()) {
return;
}
@ -155,22 +150,18 @@ public class LibraryInstaller {
// Step 1: Remove library
progress.setStatus(I18n.format(tr("Removing library: {0}"), lib.getName()));
onProgress(progress);
progressListener.onProgress(progress);
FileUtils.recursiveDelete(lib.getInstalledFolder());
progress.stepDone();
// Step 2: Rescan index
rescanLibraryIndex(progress);
rescanLibraryIndex(progress, progressListener);
}
private void rescanLibraryIndex(MultiStepProgress progress) {
private void rescanLibraryIndex(MultiStepProgress progress, ProgressListener progressListener) {
progress.setStatus(tr("Updating list of installed libraries"));
onProgress(progress);
progressListener.onProgress(progress);
indexer.rescanLibraries();
progress.stepDone();
}
protected void onProgress(Progress progress) {
// Empty
}
}

View File

@ -37,7 +37,6 @@ import cc.arduino.contributions.SignatureVerifier;
import cc.arduino.filters.FileExecutablePredicate;
import cc.arduino.utils.ArchiveExtractor;
import cc.arduino.utils.MultiStepProgress;
import cc.arduino.utils.Progress;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.Executor;
@ -65,28 +64,16 @@ public class ContributionInstaller {
private final DownloadableContributionsDownloader downloader;
private final Platform platform;
private final SignatureVerifier signatureVerifier;
private final ProgressListener progressListener;
public ContributionInstaller(ContributionsIndexer contributionsIndexer, Platform platform, SignatureVerifier signatureVerifier) {
this(contributionsIndexer, platform, signatureVerifier, progress -> {
});
}
public ContributionInstaller(ContributionsIndexer contributionsIndexer, Platform platform, SignatureVerifier signatureVerifier, ProgressListener progressListener) {
this.platform = platform;
this.signatureVerifier = signatureVerifier;
this.progressListener = progressListener;
File stagingFolder = contributionsIndexer.getStagingFolder();
indexer = contributionsIndexer;
downloader = new DownloadableContributionsDownloader(stagingFolder) {
@Override
protected void onProgress(Progress progress) {
progressListener.onProgress(progress);
}
};
downloader = new DownloadableContributionsDownloader(stagingFolder);
}
public List<String> install(ContributedPlatform contributedPlatform) throws Exception {
public List<String> install(ContributedPlatform contributedPlatform, ProgressListener progressListener) throws Exception {
List<String> errors = new LinkedList<>();
if (contributedPlatform.isInstalled()) {
throw new Exception("Platform is already installed!");
@ -112,7 +99,7 @@ public class ContributionInstaller {
// Download all
try {
// Download platform
downloader.download(contributedPlatform, progress, tr("Downloading boards definitions."));
downloader.download(contributedPlatform, progress, tr("Downloading boards definitions."), progressListener);
progress.stepDone();
// Download tools
@ -120,7 +107,7 @@ public class ContributionInstaller {
for (ContributedTool tool : tools) {
String msg = format(tr("Downloading tools ({0}/{1})."), i, tools.size());
i++;
downloader.download(tool.getDownloadableContribution(platform), progress, msg);
downloader.download(tool.getDownloadableContribution(platform), progress, msg, progressListener);
progress.stepDone();
}
} catch (InterruptedException e) {
@ -282,11 +269,11 @@ public class ContributionInstaller {
return errors;
}
public List<String> updateIndex() throws Exception {
public List<String> updateIndex(ProgressListener progressListener) throws Exception {
MultiStepProgress progress = new MultiStepProgress(1);
List<String> downloadedPackageIndexFilesAccumulator = new LinkedList<>();
downloadIndexAndSignature(progress, downloadedPackageIndexFilesAccumulator, Constants.PACKAGE_INDEX_URL);
downloadIndexAndSignature(progress, downloadedPackageIndexFilesAccumulator, Constants.PACKAGE_INDEX_URL, progressListener);
Set<String> packageIndexURLs = new HashSet<>();
String additionalURLs = PreferencesData.get(Constants.PREF_BOARDS_MANAGER_ADDITIONAL_URLS, "");
@ -295,7 +282,7 @@ public class ContributionInstaller {
}
for (String packageIndexURL : packageIndexURLs) {
downloadIndexAndSignature(progress, downloadedPackageIndexFilesAccumulator, packageIndexURL);
downloadIndexAndSignature(progress, downloadedPackageIndexFilesAccumulator, packageIndexURL, progressListener);
}
progress.stepDone();
@ -303,11 +290,11 @@ public class ContributionInstaller {
return downloadedPackageIndexFilesAccumulator;
}
private void downloadIndexAndSignature(MultiStepProgress progress, List<String> downloadedPackagedIndexFilesAccumulator, String packageIndexUrl) throws Exception {
File packageIndex = download(progress, packageIndexUrl);
private void downloadIndexAndSignature(MultiStepProgress progress, List<String> downloadedPackagedIndexFilesAccumulator, String packageIndexUrl, ProgressListener progressListener) throws Exception {
File packageIndex = download(progress, packageIndexUrl, progressListener);
downloadedPackagedIndexFilesAccumulator.add(packageIndex.getName());
try {
File packageIndexSignature = download(progress, packageIndexUrl + ".sig");
File packageIndexSignature = download(progress, packageIndexUrl + ".sig", progressListener);
boolean signatureVerified = signatureVerifier.isSigned(packageIndex);
if (signatureVerified) {
downloadedPackagedIndexFilesAccumulator.add(packageIndexSignature.getName());
@ -322,13 +309,13 @@ public class ContributionInstaller {
}
}
private File download(MultiStepProgress progress, String packageIndexUrl) throws Exception {
private File download(MultiStepProgress progress, String packageIndexUrl, ProgressListener progressListener) throws Exception {
String statusText = tr("Downloading platforms index...");
URL url = new URL(packageIndexUrl);
String[] urlPathParts = url.getFile().split("/");
File outputFile = indexer.getIndexFile(urlPathParts[urlPathParts.length - 1]);
File tmpFile = new File(outputFile.getAbsolutePath() + ".tmp");
downloader.download(url, tmpFile, progress, statusText);
downloader.download(url, tmpFile, progress, statusText, progressListener);
Files.deleteIfExists(outputFile.toPath());
Files.move(tmpFile.toPath(), outputFile.toPath());