Skip to content

Commit 2daf330

Browse files
author
Federico Fissore
committed
LibraryInstaller and ContributionInstaller are now singletons: members of Base, they get passed to dependents, thus allowing a synchronized method execution, needed to avoid race conditions when accessing files
1 parent dc93bb9 commit 2daf330

File tree

6 files changed

+50
-74
lines changed

6 files changed

+50
-74
lines changed

app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import cc.arduino.contributions.libraries.LibraryTypeComparator;
3737
import cc.arduino.contributions.ui.*;
3838
import cc.arduino.utils.Progress;
39-
import processing.app.Platform;
4039

4140
import javax.swing.*;
4241
import java.awt.*;
@@ -53,8 +52,8 @@
5352
public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
5453

5554
private final JComboBox typeChooser;
56-
private final Platform platform;
57-
private LibrariesIndexer indexer;
55+
private final LibrariesIndexer indexer;
56+
private final LibraryInstaller installer;
5857
private Predicate<ContributedLibrary> typeFilter;
5958

6059
@Override
@@ -90,9 +89,10 @@ protected void onRemove(ContributedLibrary library) {
9089
};
9190
}
9291

93-
public LibraryManagerUI(Frame parent, Platform platform) {
92+
public LibraryManagerUI(Frame parent, LibrariesIndexer indexer, LibraryInstaller installer) {
9493
super(parent, "Library Manager", Dialog.ModalityType.APPLICATION_MODAL, tr("Unable to reach Arduino.cc due to possible network issues."));
95-
this.platform = platform;
94+
this.indexer = indexer;
95+
this.installer = installer;
9696

9797
filtersContainer.add(new JLabel(tr("Topic")), 1);
9898
filtersContainer.remove(2);
@@ -130,9 +130,7 @@ public void updateIndexFilter(String[] filters, Predicate<ContributedLibrary>...
130130
super.updateIndexFilter(filters, additionalFilters);
131131
}
132132

133-
public void setIndexer(LibrariesIndexer indexer) {
134-
this.indexer = indexer;
135-
133+
public void updateUI() {
136134
DropdownItem<DownloadableContribution> previouslySelectedCategory = (DropdownItem<DownloadableContribution>) categoryChooser.getSelectedItem();
137135
DropdownItem<DownloadableContribution> previouslySelectedType = (DropdownItem<DownloadableContribution>) typeChooser.getSelectedItem();
138136

@@ -181,9 +179,6 @@ public void setIndexer(LibrariesIndexer indexer) {
181179
}
182180

183181
filterField.setEnabled(contribModel.getRowCount() > 0);
184-
185-
// Create LibrariesInstaller tied with the provided index
186-
installer = new LibraryInstaller(indexer, platform);
187182
}
188183

189184
public void selectDropdownItemByClassName(String dropdownItem) {
@@ -194,7 +189,6 @@ public void setProgress(Progress progress) {
194189
progressBar.setValue(progress);
195190
}
196191

197-
private LibraryInstaller installer;
198192
private Thread installerThread = null;
199193

200194
@Override

app/src/cc/arduino/contributions/packages/ui/ContributionManagerUI.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@
3030
package cc.arduino.contributions.packages.ui;
3131

3232
import cc.arduino.contributions.DownloadableContribution;
33-
import cc.arduino.contributions.GPGDetachedSignatureVerifier;
3433
import cc.arduino.contributions.packages.ContributedPlatform;
3534
import cc.arduino.contributions.packages.ContributionInstaller;
3635
import cc.arduino.contributions.packages.ContributionsIndexer;
3736
import cc.arduino.contributions.ui.*;
3837
import cc.arduino.utils.Progress;
3938
import processing.app.I18n;
40-
import processing.app.Platform;
4139

4240
import javax.swing.*;
4341
import java.awt.*;
@@ -50,7 +48,8 @@
5048
@SuppressWarnings("serial")
5149
public class ContributionManagerUI extends InstallerJDialog {
5250

53-
private final Platform platform;
51+
private final ContributionsIndexer indexer;
52+
private final ContributionInstaller installer;
5453

5554
@Override
5655
protected FilteredAbstractTableModel createContribModel() {
@@ -85,12 +84,13 @@ protected void onRemove(ContributedPlatform installedPlatform) {
8584
};
8685
}
8786

88-
public ContributionManagerUI(Frame parent, Platform platform) {
87+
public ContributionManagerUI(Frame parent, ContributionsIndexer indexer, ContributionInstaller installer) {
8988
super(parent, tr("Boards Manager"), Dialog.ModalityType.APPLICATION_MODAL, tr("Unable to reach Arduino.cc due to possible network issues."));
90-
this.platform = platform;
89+
this.indexer = indexer;
90+
this.installer = installer;
9191
}
9292

93-
public void setIndexer(ContributionsIndexer indexer) {
93+
public void updateUI() {
9494
DropdownItem<DownloadableContribution> previouslySelectedCategory = (DropdownItem<DownloadableContribution>) categoryChooser.getSelectedItem();
9595

9696
categoryChooser.removeActionListener(categoryChooserActionListener);
@@ -116,9 +116,6 @@ public void setIndexer(ContributionsIndexer indexer) {
116116
} else {
117117
categoryChooser.setSelectedIndex(0);
118118
}
119-
120-
// Create ConstributionInstaller tied with the provided index
121-
installer = new ContributionInstaller(indexer, platform, new GPGDetachedSignatureVerifier());
122119
}
123120

124121
public void setProgress(Progress progress) {
@@ -129,7 +126,6 @@ public void setProgress(Progress progress) {
129126
* Installer methods follows
130127
*/
131128

132-
private ContributionInstaller installer;
133129
private Thread installerThread = null;
134130

135131
@Override

app/src/processing/app/Base.java

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public boolean test(UserLibrary library) {
9292

9393
public static SplashScreenHelper splashScreenHelper = new SplashScreenHelper(SplashScreen.getSplashScreen());
9494
public static Map<String, Object> FIND_DIALOG_STATE = new HashMap<String, Object>();
95+
private final ContributionInstaller contributionInstaller;
96+
private final LibraryInstaller libraryInstaller;
9597
private Timer selfCheckTimer;
9698

9799
// set to true after the first time the menu is built.
@@ -302,6 +304,9 @@ public Base(String[] args) throws Exception {
302304
this.pdeKeywords = new PdeKeywords();
303305
this.pdeKeywords.reload();
304306

307+
contributionInstaller = new ContributionInstaller(BaseNoGui.indexer, BaseNoGui.getPlatform(), new GPGDetachedSignatureVerifier());
308+
libraryInstaller = new LibraryInstaller(BaseNoGui.librariesIndexer, BaseNoGui.getPlatform());
309+
305310
parser.parseArgumentsPhase2();
306311

307312
for (String path : parser.getFilenames()) {
@@ -341,10 +346,9 @@ public Base(String[] args) throws Exception {
341346
if (parser.isInstallBoard()) {
342347
ContributionsIndexer indexer = new ContributionsIndexer(BaseNoGui.getSettingsFolder(), BaseNoGui.getPlatform(), new GPGDetachedSignatureVerifier());
343348
ProgressListener progressListener = new ConsoleProgressListener();
344-
ContributionInstaller installer = new ContributionInstaller(indexer, BaseNoGui.getPlatform(), new GPGDetachedSignatureVerifier());
345349

346-
List<String> downloadedPackageIndexFiles = installer.updateIndex(progressListener);
347-
installer.deleteUnknownFiles(downloadedPackageIndexFiles);
350+
List<String> downloadedPackageIndexFiles = contributionInstaller.updateIndex(progressListener);
351+
contributionInstaller.deleteUnknownFiles(downloadedPackageIndexFiles);
348352
indexer.parseIndex();
349353
indexer.syncWithFilesystem(BaseNoGui.getHardwareFolder());
350354

@@ -368,24 +372,23 @@ public Base(String[] args) throws Exception {
368372
ContributedPlatform installed = indexer.getInstalled(boardToInstallParts[0], boardToInstallParts[1]);
369373

370374
if (!selected.isReadOnly()) {
371-
installer.install(selected, progressListener);
375+
contributionInstaller.install(selected, progressListener);
372376
}
373377

374378
if (installed != null && !installed.isReadOnly()) {
375-
installer.remove(installed);
379+
contributionInstaller.remove(installed);
376380
}
377381

378382
System.exit(0);
379383

380384
} else if (parser.isInstallLibrary()) {
381385
LibrariesIndexer indexer = new LibrariesIndexer(BaseNoGui.getSettingsFolder(), new ContributionsIndexer(BaseNoGui.getSettingsFolder(), BaseNoGui.getPlatform(), new GPGDetachedSignatureVerifier()));
382386
ProgressListener progressListener = new ConsoleProgressListener();
383-
LibraryInstaller installer = new LibraryInstaller(indexer, BaseNoGui.getPlatform());
384387
indexer.parseIndex();
385388
BaseNoGui.onBoardOrPortChange();
386389
indexer.setSketchbookLibrariesFolder(BaseNoGui.getSketchbookLibrariesFolder());
387390
indexer.setLibrariesFolders(BaseNoGui.getLibrariesPath());
388-
installer.updateIndex(progressListener);
391+
libraryInstaller.updateIndex(progressListener);
389392

390393
for (String library : parser.getLibraryToInstall().split(",")) {
391394
String[] libraryToInstallParts = library.split(":");
@@ -407,9 +410,9 @@ public Base(String[] args) throws Exception {
407410

408411
ContributedLibrary installed = indexer.getIndex().getInstalled(libraryToInstallParts[0]);
409412
if (selected.isReadOnly()) {
410-
installer.remove(installed, progressListener);
413+
libraryInstaller.remove(installed, progressListener);
411414
} else {
412-
installer.install(selected, installed, progressListener);
415+
libraryInstaller.install(selected, installed, progressListener);
413416
}
414417
}
415418

@@ -464,8 +467,6 @@ public Base(String[] args) throws Exception {
464467
new Thread(new BuiltInCoreIsNewerCheck(this)).start();
465468

466469
selfCheckTimer = new Timer(false);
467-
ContributionInstaller contributionInstaller = new ContributionInstaller(BaseNoGui.indexer, BaseNoGui.getPlatform(), new GPGDetachedSignatureVerifier());
468-
LibraryInstaller libraryInstaller = new LibraryInstaller(BaseNoGui.librariesIndexer, BaseNoGui.getPlatform());
469470
selfCheckTimer.schedule(new ContributionsSelfCheck(this, new UpdatableBoardsLibsFakeURLsHandler(this), BaseNoGui.indexer, contributionInstaller, BaseNoGui.librariesIndexer, libraryInstaller), Constants.BOARDS_LIBS_UPDATABLE_CHECK_START_PERIOD);
470471

471472
} else if (parser.isNoOpMode()) {
@@ -682,13 +683,6 @@ protected int[] nextEditorLocation() {
682683
"jul", "aug", "sep", "oct", "nov", "dec"
683684
};
684685

685-
/**
686-
* Handle creating a sketch folder, return its base .pde file
687-
* or null if the operation was canceled.
688-
*
689-
* @param shift whether shift is pressed, which will invert prompt setting
690-
* @param noPrompt disable prompt, no matter the setting
691-
*/
692686
protected File createNewUntitled() throws IOException {
693687
File newbieDir = null;
694688
String newbieName = null;
@@ -791,20 +785,13 @@ protected void handleNewReplaceImpl() {
791785
activeEditor.handleOpenInternal(file);
792786
activeEditor.untitled = true;
793787
}
794-
// return true;
795788

796789
} catch (IOException e) {
797790
activeEditor.statusError(e);
798-
// return false;
799791
}
800792
}
801793

802794

803-
/**
804-
* Open a sketch, replacing the sketch in the current window.
805-
*
806-
* @param path Location of the primary pde file for the sketch.
807-
*/
808795
public void handleOpenReplace(File file) {
809796
if (!activeEditor.checkModified()) {
810797
return; // sketch was modified, and user canceled
@@ -1245,21 +1232,21 @@ public void openLibraryManager(String dropdownItem) {
12451232
selfCheckTimer.cancel();
12461233
}
12471234
@SuppressWarnings("serial")
1248-
LibraryManagerUI managerUI = new LibraryManagerUI(activeEditor, BaseNoGui.getPlatform()) {
1235+
LibraryManagerUI managerUI = new LibraryManagerUI(activeEditor, BaseNoGui.librariesIndexer, libraryInstaller) {
12491236
@Override
12501237
protected void onIndexesUpdated() throws Exception {
12511238
BaseNoGui.initPackages();
12521239
rebuildBoardsMenu();
12531240
rebuildProgrammerMenu();
12541241
onBoardOrPortChange();
1255-
setIndexer(BaseNoGui.librariesIndexer);
1242+
updateUI();
12561243
if (StringUtils.isNotEmpty(dropdownItem)) {
12571244
selectDropdownItemByClassName(dropdownItem);
12581245
}
12591246
}
12601247
};
12611248
managerUI.setLocationRelativeTo(activeEditor);
1262-
managerUI.setIndexer(BaseNoGui.librariesIndexer);
1249+
managerUI.updateUI();
12631250
managerUI.setVisible(true);
12641251
// Manager dialog is modal, waits here until closed
12651252

@@ -1274,13 +1261,13 @@ public void openBoardsManager(final String filterText, String dropdownItem) thro
12741261
selfCheckTimer.cancel();
12751262
}
12761263
@SuppressWarnings("serial")
1277-
ContributionManagerUI managerUI = new ContributionManagerUI(activeEditor, BaseNoGui.getPlatform()) {
1264+
ContributionManagerUI managerUI = new ContributionManagerUI(activeEditor, BaseNoGui.indexer, contributionInstaller) {
12781265
@Override
12791266
protected void onIndexesUpdated() throws Exception {
12801267
BaseNoGui.initPackages();
12811268
rebuildBoardsMenu();
12821269
rebuildProgrammerMenu();
1283-
setIndexer(BaseNoGui.indexer);
1270+
updateUI();
12841271
if (StringUtils.isNotEmpty(dropdownItem)) {
12851272
selectDropdownItemByClassName(dropdownItem);
12861273
}
@@ -1290,7 +1277,7 @@ protected void onIndexesUpdated() throws Exception {
12901277
}
12911278
};
12921279
managerUI.setLocationRelativeTo(activeEditor);
1293-
managerUI.setIndexer(BaseNoGui.indexer);
1280+
managerUI.updateUI();
12941281
managerUI.setVisible(true);
12951282
// Installer dialog is modal, waits here until closed
12961283

arduino-core/src/cc/arduino/Constants.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,24 @@ public class Constants {
4747
public static final long BOARDS_LIBS_UPDATABLE_CHECK_START_PERIOD = 60000;
4848
public static final int NOTIFICATION_POPUP_AUTOCLOSE_DELAY = 10000;
4949

50+
public static final String LIBRARY_INDEX_URL;
51+
public static final String LIBRARY_INDEX_URL_GZ;
52+
5053
static {
5154
String extenalPackageIndexUrl = System.getProperty("PACKAGE_INDEX_URL");
5255
if (extenalPackageIndexUrl != null && !"".equals(extenalPackageIndexUrl)) {
5356
PACKAGE_INDEX_URL = extenalPackageIndexUrl;
5457
} else {
5558
PACKAGE_INDEX_URL = "http://downloads.arduino.cc/packages/package_index.json";
5659
}
60+
61+
String externalLibraryIndexUrl = System.getProperty("LIBRARY_INDEX_URL");
62+
if (externalLibraryIndexUrl != null && !"".equals(externalLibraryIndexUrl)) {
63+
LIBRARY_INDEX_URL = externalLibraryIndexUrl;
64+
} else {
65+
LIBRARY_INDEX_URL = "http://downloads.arduino.cc/libraries/library_index.json";
66+
}
67+
LIBRARY_INDEX_URL_GZ = "http://downloads.arduino.cc/libraries/library_index.json.gz";
5768
}
5869

5970
}

arduino-core/src/cc/arduino/contributions/libraries/LibraryInstaller.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
package cc.arduino.contributions.libraries;
3131

32+
import cc.arduino.Constants;
3233
import cc.arduino.contributions.DownloadableContributionsDownloader;
3334
import cc.arduino.contributions.GZippedJsonDownloader;
3435
import cc.arduino.contributions.ProgressListener;
@@ -46,19 +47,6 @@
4647

4748
public class LibraryInstaller {
4849

49-
private static final String LIBRARY_INDEX_URL;
50-
private static final String LIBRARY_INDEX_URL_GZ;
51-
52-
static {
53-
String externalLibraryIndexUrl = System.getProperty("LIBRARY_INDEX_URL");
54-
if (externalLibraryIndexUrl != null && !"".equals(externalLibraryIndexUrl)) {
55-
LIBRARY_INDEX_URL = externalLibraryIndexUrl;
56-
} else {
57-
LIBRARY_INDEX_URL = "http://downloads.arduino.cc/libraries/library_index.json";
58-
}
59-
LIBRARY_INDEX_URL_GZ = "http://downloads.arduino.cc/libraries/library_index.json.gz";
60-
}
61-
6250
private final LibrariesIndexer indexer;
6351
private final DownloadableContributionsDownloader downloader;
6452
private final Platform platform;
@@ -70,14 +58,14 @@ public LibraryInstaller(LibrariesIndexer indexer, Platform platform) {
7058
downloader = new DownloadableContributionsDownloader(stagingFolder);
7159
}
7260

73-
public void updateIndex(ProgressListener progressListener) throws Exception {
61+
public synchronized void updateIndex(ProgressListener progressListener) throws Exception {
7462
final MultiStepProgress progress = new MultiStepProgress(2);
7563

7664
// Step 1: Download index
7765
File outputFile = indexer.getIndexFile();
7866
File tmpFile = new File(outputFile.getAbsolutePath() + ".tmp");
7967
try {
80-
GZippedJsonDownloader gZippedJsonDownloader = new GZippedJsonDownloader(downloader, new URL(LIBRARY_INDEX_URL), new URL(LIBRARY_INDEX_URL_GZ));
68+
GZippedJsonDownloader gZippedJsonDownloader = new GZippedJsonDownloader(downloader, new URL(Constants.LIBRARY_INDEX_URL), new URL(Constants.LIBRARY_INDEX_URL_GZ));
8169
gZippedJsonDownloader.download(tmpFile, progress, tr("Downloading libraries index..."), progressListener);
8270
} catch (InterruptedException e) {
8371
// Download interrupted... just exit
@@ -97,7 +85,7 @@ public void updateIndex(ProgressListener progressListener) throws Exception {
9785
rescanLibraryIndex(progress, progressListener);
9886
}
9987

100-
public void install(ContributedLibrary lib, ContributedLibrary replacedLib, ProgressListener progressListener) throws Exception {
88+
public synchronized void install(ContributedLibrary lib, ContributedLibrary replacedLib, ProgressListener progressListener) throws Exception {
10189
if (lib.isInstalled()) {
10290
System.out.println(I18n.format(tr("Library is already installed: {0} version {1}"), lib.getName(), lib.getParsedVersion()));
10391
return;
@@ -141,7 +129,7 @@ public void install(ContributedLibrary lib, ContributedLibrary replacedLib, Prog
141129
rescanLibraryIndex(progress, progressListener);
142130
}
143131

144-
public void remove(ContributedLibrary lib, ProgressListener progressListener) throws IOException {
132+
public synchronized void remove(ContributedLibrary lib, ProgressListener progressListener) throws IOException {
145133
if (lib == null || lib.isReadOnly()) {
146134
return;
147135
}

0 commit comments

Comments
 (0)