/
UpdateDialog.java
482 lines (401 loc) · 15.6 KB
/
UpdateDialog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
* RapidMiner
*
* Copyright (C) 2001-2014 by RapidMiner and the contributors
*
* Complete list of developers available at our web site:
*
* http://rapidminer.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.rapid_i.deployment.update.client;
import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import javax.swing.AbstractAction;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.xml.ws.WebServiceException;
import com.rapidminer.deployment.client.wsimport.PackageDescriptor;
import com.rapidminer.deployment.client.wsimport.UpdateService;
import com.rapidminer.gui.RapidMinerGUI;
import com.rapidminer.gui.tools.ProgressThread;
import com.rapidminer.gui.tools.ResourceAction;
import com.rapidminer.gui.tools.SwingTools;
import com.rapidminer.gui.tools.components.LinkButton;
import com.rapidminer.gui.tools.dialogs.ButtonDialog;
import com.rapidminer.gui.tools.dialogs.ConfirmDialog;
import com.rapidminer.tools.I18N;
import com.rapidminer.tools.LogService;
import com.rapidminer.tools.NetTools;
import com.rapidminer.tools.ProgressListener;
import com.rapidminer.tools.plugin.Dependency;
/**
*
* @author Simon Fischer
*
*/
public class UpdateDialog extends ButtonDialog {
private static final long serialVersionUID = 1L;
static {
NetTools.init();
}
public static final Action UPDATE_ACTION = new ResourceAction("update_manager") {
private static final long serialVersionUID = 1L;
{
setCondition(EDIT_IN_PROGRESS, DONT_CARE);
}
@Override
public void actionPerformed(ActionEvent arg0) {
showUpdateDialog(false);
}
};
private WindowListener windowListener = new WindowListener() {
public void windowActivated(WindowEvent e) {
UpdateServerAccount account = UpdateManager.getUpdateServerAccount();
account.updatePurchasedPackages(updateModel);
}
@Override
public void windowOpened(WindowEvent e) {}
@Override
public void windowClosing(WindowEvent e) {}
@Override
public void windowClosed(WindowEvent e) {}
@Override
public void windowIconified(WindowEvent e) {}
@Override
public void windowDeiconified(WindowEvent e) {}
@Override
public void windowDeactivated(WindowEvent e) {}
};
private final UpdatePanel ulp;
private static UpdatePackagesModel updateModel;
private static class USAcountInfoButton extends LinkButton implements Observer {
private static final long serialVersionUID = 1L;
public USAcountInfoButton() {
super(new AbstractAction("") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
UpdateServerAccount account = UpdateManager.getUpdateServerAccount();
if ("#register".equals(e.getActionCommand())) {
try {
Desktop.getDesktop().browse(new URI(UpdateManager.getBaseUrl() + "/faces/signup.xhtml"));
} catch (Exception ex) {
SwingTools.showSimpleErrorMessage("cannot_open_browser", ex);
}
} else {
if (account.isLoggedIn()) {
account.logout(updateModel);
} else {
account.login(updateModel);
}
}
}
});
Dimension size = new Dimension(300, 24);
this.setSize(size);
this.setMaximumSize(size);
this.setPreferredSize(size);
}
@Override
public void update(Observable obs, Object arg) {
if (obs instanceof UpdateServerAccount) {
UpdateServerAccount account = (UpdateServerAccount) obs;
if (account.isLoggedIn()) {
this.setText(I18N.getMessage(I18N.getGUIBundle(), "gui.dialog.update.account_button.logged_in", account.getUserName()));
} else {
this.setText(I18N.getMessage(I18N.getGUIBundle(), "gui.dialog.update.account_button.logged_out"));
}
}
}
}
private class InstallButton extends JButton implements Observer {
private static final long serialVersionUID = 1L;
InstallButton(Action a) {
super(a);
updateButton();
}
@Override
public void update(Observable o, Object arg) {
if (o instanceof UpdatePackagesModel) {
//UpdatePackagesModel currentModel = (UpdatePackagesModel)o;
updateButton();
}
}
private void updateButton() {
UpdatePackagesModel currentModel = updateModel;
if (currentModel.getInstallationList() != null && currentModel.getInstallationList().size() > 0) {
this.setText(I18N.getMessage(I18N.getGUIBundle(), "gui.action.update.install.label", currentModel.getInstallationList().size()));
this.setEnabled(true);
} else {
this.setText(I18N.getMessage(I18N.getGUIBundle(), "gui.action.update.install.label", 0));
this.setEnabled(false);
}
}
}
private USAcountInfoButton accountInfoButton = new USAcountInfoButton();
private InstallButton installButton;
private final PackageDescriptorCache packageDescriptorCache = new PackageDescriptorCache();
private boolean isConfirmed = false;
private LinkedList<PackageDescriptor> installablePackageList;
private JButton closeButton;
public UpdateDialog(String[] preselectedExtensions) {
super("update");
setModal(true);
UpdateServerAccount usAccount = UpdateManager.getUpdateServerAccount();
usAccount.addObserver(accountInfoButton);
updateModel = new UpdatePackagesModel(packageDescriptorCache, usAccount);
ulp = new UpdatePanel(this, packageDescriptorCache, preselectedExtensions, usAccount, updateModel);
closeButton = makeCloseButton();
layoutDefault(ulp, HUGE, makeOkButton(), closeButton);
this.addWindowListener(windowListener);
}
@Override
protected JButton makeOkButton(String i18nKey) {
Action okAction = new ResourceAction(i18nKey) {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
wasConfirmed = true;
ok();
}
};
installButton = new InstallButton(okAction);
getRootPane().setDefaultButton(installButton);
installButton.setEnabled(false);
updateModel.addObserver(installButton);
return installButton;
}
@Override
/** Overriding makeButtonPanel in order to display account information. **/
protected JPanel makeButtonPanel(AbstractButton... buttons) {
JPanel buttonPanel = new JPanel(new BorderLayout());
JPanel buttonPanelRight = new JPanel(new FlowLayout(FlowLayout.RIGHT, GAP, GAP));
for (AbstractButton button : buttons) {
if (button != null) {
buttonPanelRight.add(button);
}
}
buttonPanel.add(buttonPanelRight, BorderLayout.CENTER);
JPanel buttonPanelLeft = new JPanel(new FlowLayout(FlowLayout.LEFT, GAP, 2 * GAP));
buttonPanelLeft.add(accountInfoButton, false);
buttonPanel.add(buttonPanelLeft, BorderLayout.WEST);
return buttonPanel;
}
/**
* Opens the marketplace dialog and selects the update tab.
*/
public static void showUpdateDialog(final boolean selectUpdateTab, final String... preselectedExtensions) {
new ProgressThread("open_marketplace_dialog", true) {
@Override
public void run() {
getProgressListener().setTotal(100);
getProgressListener().setCompleted(33);
try {
UpdateManager.resetService();
UpdateManager.getService();
} catch (WebServiceException e) {
// thrown when no internet connection is available. Simple error message to not confuse users
SwingTools.showVerySimpleErrorMessage("failed_update_server_simple");
LogService.getRoot().log(Level.WARNING, "com.rapid_i.deployment.update.client.UpdateDialog.could_not_connect", e);
return;
} catch (Exception e) {
SwingTools.showSimpleErrorMessage("failed_update_server", e, UpdateManager.getBaseUrl());
LogService.getRoot().log(Level.WARNING, "com.rapid_i.deployment.update.client.UpdateDialog.could_not_connect", e);
return;
}
getProgressListener().setCompleted(100);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
UpdateDialog updateDialog = new UpdateDialog(preselectedExtensions);
if (selectUpdateTab) {
updateDialog.showUpdateTab();
}
updateDialog.setVisible(true);
}
});
}
}.start();
}
private void showUpdateTab() {
ulp.selectUpdatesTab();
}
public void startUpdate(final List<PackageDescriptor> downloadList) {
installButton.setEnabled(false);
new ProgressThread("resolving_dependencies", true) {
@Override
public void run() {
try {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
closeButton.setEnabled(false);
}
});
getProgressListener().setTotal(100);
final HashMap<PackageDescriptor, HashSet<PackageDescriptor>> dependency = resolveDependency(downloadList, packageDescriptorCache);
installablePackageList = getPackagesforInstallation(dependency);
getProgressListener().setCompleted(30);
final HashMap<String, String> licenseNameToLicenseTextMap = collectLicenses(installablePackageList,getProgressListener(),100,30,100);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
isConfirmed = ConfirmLicensesDialog.confirm(dependency, licenseNameToLicenseTextMap);
new ProgressThread("installing_updates", true) {
@Override
public void run() {
try {
if (isConfirmed) {
getProgressListener().setTotal(100);
getProgressListener().setCompleted(20);
UpdateService service = UpdateManager.getService();
UpdateManager um = new UpdateManager(service);
List<PackageDescriptor> installedPackages = um.performUpdates(installablePackageList, getProgressListener());
getProgressListener().setCompleted(40);
updateModel.clearFromSelectionMap(installedPackages);
ulp.validate();
ulp.repaint();
if (installedPackages.size() > 0) {
int confirmation = SwingTools.showConfirmDialog((installedPackages.size() == 1 ? "update.complete_restart" : "update.complete_restart1"),
ConfirmDialog.YES_NO_OPTION, installedPackages.size());
if (confirmation == ConfirmDialog.YES_OPTION) {
RapidMinerGUI.getMainFrame().exit(true);
} else if (confirmation == ConfirmDialog.NO_OPTION) {
if (installedPackages.size() == installablePackageList.size()) {
dispose();
}
}
}
getProgressListener().setCompleted(100);
}
} catch (Exception e) {
SwingTools.showSimpleErrorMessage("error_installing_update", e, e.getMessage());
} finally {
getProgressListener().complete();
installButton.setEnabled(true);
}
}
}.start();
}
});
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
closeButton.setEnabled(true);
}
});
getProgressListener().complete();
} catch (Exception e) {
SwingTools.showSimpleErrorMessage("error_resolving_dependencies", e, e.getMessage());
} finally {
getProgressListener().complete();
installButton.setEnabled(true);
}
}
}.start();
}
/**
* @param Start, int end
* @param Total
* @param progressListener
* @param installablePackageList2
* @return
* @throws URISyntaxException
* @throws MalformedURLException
*/
protected static HashMap<String, String> collectLicenses(LinkedList<PackageDescriptor> installablePackageList, ProgressListener progressListener, int total, int start, int end) throws MalformedURLException, URISyntaxException {
int range = end - start;
double perIterationPercent = (range * 1.0 / installablePackageList.size());
int iterCnt = 1;
HashMap<String, String> licenseNameToLicenseTextMap = null;
licenseNameToLicenseTextMap = new HashMap<String, String>();
UpdateService service = UpdateManager.getService();
for (PackageDescriptor packageDescriptor : installablePackageList) {
String licenseName = packageDescriptor.getLicenseName();
String licenseText = service.getLicenseTextHtml(licenseName);
licenseNameToLicenseTextMap.put(licenseName, licenseText);
progressListener.setCompleted((int)(start + (iterCnt*perIterationPercent)));
iterCnt++;
}
return licenseNameToLicenseTextMap;
}
@Override
protected void ok() {
ulp.startUpdate();
}
public static LinkedList<PackageDescriptor> getPackagesforInstallation(HashMap<PackageDescriptor, HashSet<PackageDescriptor>> dependency) {
HashSet<PackageDescriptor> installabledPackages = new HashSet<PackageDescriptor>();
for (PackageDescriptor packageDescriptor : dependency.keySet()) {
installabledPackages.add(packageDescriptor);
installabledPackages.addAll(dependency.get(packageDescriptor));
}
LinkedList<PackageDescriptor> installablePackageList = new LinkedList<PackageDescriptor>();
installablePackageList.addAll(installabledPackages);
return installablePackageList;
}
/**
* Recursively collect all the dependent extentions of a given extension *
* @param desc
* @param pluginsSelectedForDownload
* @return
*/
private static HashSet<Dependency> collectDependency(PackageDescriptor desc, HashSet<String> pluginsSelectedForDownload, PackageDescriptorCache packageDescriptorCache) {
HashSet<Dependency> dependencySet = new HashSet<Dependency>();
List<Dependency> dependencies = Dependency.parse(desc.getDependencies());
for (Dependency dependency : dependencies) {
String packageId = dependency.getPluginExtensionId();
PackageDescriptor packageInfo = packageDescriptorCache.getPackageInfo(packageId);
if ((!dependencySet.contains(packageInfo)) && (!pluginsSelectedForDownload.contains(packageId))) {
dependencySet.add(dependency);
dependencySet.addAll(collectDependency(packageInfo, pluginsSelectedForDownload, packageDescriptorCache));
}
}
return dependencySet;
}
public static HashMap<PackageDescriptor, HashSet<PackageDescriptor>> resolveDependency(final List<PackageDescriptor> downloadList, PackageDescriptorCache packageDescriptorCache) {
HashMap<PackageDescriptor, HashSet<PackageDescriptor>> dependentPackageMap = new HashMap<PackageDescriptor, HashSet<PackageDescriptor>>();
HashSet<String> pluginsSelectedForDownload = new HashSet<String>();
for (PackageDescriptor packageDescriptor : downloadList) {
pluginsSelectedForDownload.add(packageDescriptor.getPackageId());
dependentPackageMap.put(packageDescriptor, new HashSet<PackageDescriptor>());
}
for (PackageDescriptor desc : downloadList) {
HashSet<Dependency> dependencySet = collectDependency(desc, pluginsSelectedForDownload, packageDescriptorCache);
for (Dependency dependency : dependencySet) {
dependentPackageMap.get(desc).add(packageDescriptorCache.getPackageInfo(dependency.getPluginExtensionId()));
}
}
return dependentPackageMap;
}
}