KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > wizards > InstallWizard2


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.ui.wizards;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.core.runtime.SubProgressMonitor;
22 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
23 import org.eclipse.core.runtime.jobs.IJobChangeListener;
24 import org.eclipse.core.runtime.jobs.Job;
25 import org.eclipse.core.runtime.jobs.JobChangeAdapter;
26 import org.eclipse.jface.dialogs.ErrorDialog;
27 import org.eclipse.jface.dialogs.MessageDialog;
28 import org.eclipse.jface.wizard.IWizardPage;
29 import org.eclipse.jface.wizard.Wizard;
30 import org.eclipse.osgi.util.NLS;
31 import org.eclipse.swt.widgets.Display;
32 import org.eclipse.update.configuration.IInstallConfiguration;
33 import org.eclipse.update.core.IFeature;
34 import org.eclipse.update.core.IFeatureReference;
35 import org.eclipse.update.core.IIncludedFeatureReference;
36 import org.eclipse.update.core.ISite;
37 import org.eclipse.update.core.ISiteFeatureReference;
38 import org.eclipse.update.core.IVerificationListener;
39 import org.eclipse.update.core.SiteManager;
40 import org.eclipse.update.core.model.InstallAbortedException;
41 import org.eclipse.update.internal.core.FeatureDownloadException;
42 import org.eclipse.update.internal.core.LiteFeature;
43 import org.eclipse.update.internal.core.UpdateCore;
44 import org.eclipse.update.internal.operations.DuplicateConflictsValidator;
45 import org.eclipse.update.internal.operations.InstallOperation;
46 import org.eclipse.update.internal.operations.UpdateUtils;
47 import org.eclipse.update.internal.ui.UpdateUI;
48 import org.eclipse.update.internal.ui.UpdateUIImages;
49 import org.eclipse.update.internal.ui.UpdateUIMessages;
50 import org.eclipse.update.internal.ui.security.JarVerificationService;
51 import org.eclipse.update.operations.IBatchOperation;
52 import org.eclipse.update.operations.IFeatureOperation;
53 import org.eclipse.update.operations.IInstallFeatureOperation;
54 import org.eclipse.update.operations.IOperation;
55 import org.eclipse.update.operations.IOperationListener;
56 import org.eclipse.update.operations.OperationsManager;
57 import org.eclipse.update.search.UpdateSearchRequest;
58
59
60 public class InstallWizard2
61     extends Wizard
62     implements IOperationListener, ISearchProvider {
63     private ReviewPage reviewPage;
64     private LicensePage licensePage;
65     private OptionalFeaturesPage optionalFeaturesPage;
66     private TargetPage targetPage;
67     private IInstallConfiguration config;
68     private int installCount = 0;
69     private UpdateSearchRequest searchRequest;
70     private ArrayList JavaDoc jobs;
71     private boolean needsRestart;
72     private static boolean isRunning;
73     private IBatchOperation installOperation;
74     private Job job;
75     public static final Object JavaDoc jobFamily = new Object JavaDoc();
76     private IJobChangeListener jobListener;
77     private boolean isUpdate;
78
79     public InstallWizard2(UpdateSearchRequest searchRequest, IInstallFeatureOperation[] jobs, boolean isUpdate) {
80         this (searchRequest, new ArrayList JavaDoc(Arrays.asList(jobs)), isUpdate);
81     }
82
83     public InstallWizard2(UpdateSearchRequest searchRequest, ArrayList JavaDoc jobs, boolean isUpdate) {
84         this.isUpdate = isUpdate;
85         this.searchRequest = searchRequest;
86         this.jobs = jobs;
87         isRunning = true;
88         setDialogSettings(UpdateUI.getDefault().getDialogSettings());
89         setDefaultPageImageDescriptor(UpdateUIImages.DESC_UPDATE_WIZ);
90         setForcePreviousAndNextButtons(true);
91         setNeedsProgressMonitor(true);
92         setWindowTitle(UpdateUIMessages.InstallWizard_wtitle);
93     }
94     
95     public int getInstallCount() {
96         return installCount;
97     }
98     public boolean isRestartNeeded() {
99         return installCount > 0 && needsRestart; // or == selectedJobs.length
100
}
101
102     public boolean performCancel() {
103         isRunning = false;
104         if (targetPage != null)
105             targetPage.removeAddedSites();
106         return super.performCancel();
107     }
108     /**
109      * @see Wizard#performFinish()
110      */

111     public boolean performFinish() {
112         IInstallFeatureOperation[] selectedJobs = reviewPage.getSelectedJobs();
113         
114         // Check for duplication conflicts
115
ArrayList JavaDoc conflicts = DuplicateConflictsValidator.computeDuplicateConflicts(selectedJobs, config);
116         if (conflicts != null) {
117             DuplicateConflictsDialog dialog = new DuplicateConflictsDialog(getShell(), conflicts);
118             if (dialog.open() != 0)
119                 return false;
120         }
121         
122         if (Job.getJobManager().find(jobFamily).length > 0) {
123             // another update/install job is running, need to wait to finish or cancel old job
124
boolean proceed = MessageDialog.openQuestion(
125                     UpdateUI.getActiveWorkbenchShell(),
126                     UpdateUIMessages.InstallWizard_anotherJobTitle,
127                     UpdateUIMessages.InstallWizard_anotherJob);
128             if (!proceed)
129                 return false; // cancel this job, and let the old one go on
130
}
131         
132         // set the install operation
133
installOperation = getBatchInstallOperation(selectedJobs);
134         if (installOperation != null)
135             launchInBackground();
136         return true;
137     }
138
139     public void addPages() {
140         reviewPage = new ReviewPage(isUpdate, searchRequest, jobs);
141         addPage(reviewPage);
142
143         try {
144 // config = UpdateUtils.createInstallConfiguration();
145
config = SiteManager.getLocalSite().getCurrentConfiguration();
146         } catch (CoreException e) {
147             UpdateUI.logException(e);
148         }
149
150         licensePage = new LicensePage(true);
151         addPage(licensePage);
152         optionalFeaturesPage = new OptionalFeaturesPage(config);
153         addPage(optionalFeaturesPage);
154         targetPage = new TargetPage(config, isUpdate);
155         addPage(targetPage);
156     }
157
158
159     private boolean isPageRequired(IWizardPage page) {
160         if (page == null)
161             return false;
162             
163         if (page.equals(licensePage)) {
164             return OperationsManager.hasSelectedJobsWithLicenses(
165                 reviewPage.getSelectedJobs());
166         }
167         if (page.equals(optionalFeaturesPage)) {
168             return OperationsManager.hasSelectedJobsWithOptionalFeatures(
169                 reviewPage.getSelectedJobs());
170         }
171         if (page.equals(targetPage)) {
172             return reviewPage.getSelectedJobs().length > 0;
173         }
174         return true;
175     }
176
177     public IWizardPage getNextPage(IWizardPage page) {
178         IWizardPage[] pages = getPages();
179         boolean start = false;
180         IWizardPage nextPage = null;
181
182         if (page.equals(reviewPage)) {
183             updateDynamicPages();
184         }
185
186         for (int i = 0; i < pages.length; i++) {
187             if (pages[i].equals(page)) {
188                 start = true;
189             } else if (start) {
190                 if (isPageRequired(pages[i])) {
191                     nextPage = pages[i];
192                     break;
193                 }
194             }
195         }
196         return nextPage;
197     }
198
199     private void updateDynamicPages() {
200         if (licensePage != null) {
201             IInstallFeatureOperation[] licenseJobs =
202                 OperationsManager.getSelectedJobsWithLicenses(
203                     reviewPage.getSelectedJobs());
204             licensePage.setJobs(licenseJobs);
205         }
206         if (optionalFeaturesPage != null) {
207             IInstallFeatureOperation[] optionalJobs =
208                 OperationsManager.getSelectedJobsWithOptionalFeatures(
209                     reviewPage.getSelectedJobs());
210             optionalFeaturesPage.setJobs(optionalJobs);
211         }
212         if (targetPage != null) {
213             IInstallFeatureOperation[] installJobs =
214                 reviewPage.getSelectedJobs();
215             targetPage.setJobs(installJobs);
216         }
217     }
218
219     public boolean canFinish() {
220         IWizardPage page = getContainer().getCurrentPage();
221         return page.equals(targetPage) && page.isPageComplete();
222     }
223
224     private void preserveOriginatingURLs(
225         IFeature feature,
226         IFeatureReference[] optionalFeatures) {
227         // walk the hierarchy and preserve the originating URL
228
// for all the optional features that are not chosen to
229
// be installed.
230
URL JavaDoc url = feature.getSite().getURL();
231         try {
232             IIncludedFeatureReference[] irefs =
233                 feature.getIncludedFeatureReferences();
234             for (int i = 0; i < irefs.length; i++) {
235                 IIncludedFeatureReference iref = irefs[i];
236                 boolean preserve = false;
237                 if (iref.isOptional()) {
238                     boolean onTheList = false;
239                     for (int j = 0; j < optionalFeatures.length; j++) {
240                         if (optionalFeatures[j].equals(iref)) {
241                             //was on the list
242
onTheList = true;
243                             break;
244                         }
245                     }
246                     if (!onTheList)
247                         preserve = true;
248                 }
249                 if (preserve) {
250                     try {
251                         String JavaDoc id =
252                             iref.getVersionedIdentifier().getIdentifier();
253                         UpdateUI.setOriginatingURL(id, url);
254                     } catch (CoreException e) {
255                         // Silently ignore
256
}
257                 } else {
258                     try {
259                         IFeature ifeature = iref.getFeature(null);
260                         preserveOriginatingURLs(ifeature, optionalFeatures);
261                     } catch (CoreException e) {
262                         // Silently ignore
263
}
264                 }
265             }
266         } catch (CoreException e) {
267             // Silently ignore
268
}
269     }
270
271     /* (non-Javadoc)
272      * @see org.eclipse.update.operations.IOperationListener#afterExecute(org.eclipse.update.operations.IOperation)
273      */

274     public boolean afterExecute(IOperation operation, Object JavaDoc data) {
275         if (!(operation instanceof IInstallFeatureOperation))
276             return true;
277         IInstallFeatureOperation job = (IInstallFeatureOperation) operation;
278         IFeature oldFeature = job.getOldFeature();
279         if (oldFeature == null && job.getOptionalFeatures() != null)
280             preserveOriginatingURLs(
281                 job.getFeature(),
282                 job.getOptionalFeatures());
283
284         installCount++;
285         return true;
286     }
287
288     /* (non-Javadoc)
289      * @see org.eclipse.update.operations.IOperationListener#beforeExecute(org.eclipse.update.operations.IOperation)
290      */

291     public boolean beforeExecute(IOperation operation, Object JavaDoc data) {
292         return true;
293     }
294     /* (non-Javadoc)
295      * @see org.eclipse.update.internal.ui.wizards.ISearchProvider2#getSearchRequest()
296      */

297     public UpdateSearchRequest getSearchRequest() {
298         return searchRequest;
299     }
300
301     public static synchronized boolean isRunning() {
302         return isRunning || Job.getJobManager().find(jobFamily).length > 0;
303     }
304     
305     private IBatchOperation getBatchInstallOperation(final IInstallFeatureOperation[] selectedJobs) {
306         final IVerificationListener verificationListener =new JarVerificationService(
307                 //InstallWizard.this.getShell());
308
UpdateUI.getActiveWorkbenchShell());
309         
310         // setup jobs with the correct environment
311
IInstallFeatureOperation[] operations = new IInstallFeatureOperation[selectedJobs.length];
312         for (int i = 0; i < selectedJobs.length; i++) {
313             IInstallFeatureOperation job = selectedJobs[i];
314             IFeature[] unconfiguredOptionalFeatures = null;
315             IFeatureReference[] optionalFeatures = null;
316             if (UpdateUtils.hasOptionalFeatures(job.getFeature())) {
317                 optionalFeatures = optionalFeaturesPage.getCheckedOptionalFeatures(job);
318                 unconfiguredOptionalFeatures = optionalFeaturesPage.getUnconfiguredOptionalFeatures(job, job.getTargetSite());
319             }
320             IInstallFeatureOperation op =
321                 OperationsManager
322                     .getOperationFactory()
323                     .createInstallOperation(
324                     job.getTargetSite(),
325                     job.getFeature(),
326                     optionalFeatures,
327                     unconfiguredOptionalFeatures,
328                     verificationListener);
329             operations[i] = op;
330         }
331         return OperationsManager.getOperationFactory().createBatchInstallOperation(operations);
332     }
333
334
335
336     private void launchInBackground() {
337         // Downloads the feature content in the background.
338
// The job listener will then install the feature when download is finished.
339

340         // TODO: should we cancel existing jobs?
341
if (jobListener != null)
342             Job.getJobManager().removeJobChangeListener(jobListener);
343         if (job != null)
344             Job.getJobManager().cancel(job);
345         jobListener = new UpdateJobChangeListener();
346         Job.getJobManager().addJobChangeListener(jobListener);
347         
348         job = new Job(UpdateUIMessages.InstallWizard_jobName) {
349             public IStatus run(IProgressMonitor monitor) {
350                 if (download(monitor))
351                     return Status.OK_STATUS;
352                 else {
353                     isRunning = false;
354                     return Status.CANCEL_STATUS;
355                 }
356             }
357             public boolean belongsTo(Object JavaDoc family) {
358                 return InstallWizard2.jobFamily == family;
359             }
360         };
361
362         job.setUser(true);
363         job.setPriority(Job.INTERACTIVE);
364 // if (wait) {
365
// progressService.showInDialog(workbench.getActiveWorkbenchWindow().getShell(), job);
366
// }
367
job.schedule();
368     }
369     
370     private boolean install(IProgressMonitor monitor) {
371         // Installs the (already downloaded) features and prompts for restart
372
try {
373             needsRestart = installOperation.execute(monitor, InstallWizard2.this);
374             UpdateUI.getStandardDisplay().asyncExec(new Runnable JavaDoc() {
375                 public void run() {
376                     UpdateUI.requestRestart(InstallWizard2.this.isRestartNeeded());
377                 }
378             });
379         } catch (final InvocationTargetException JavaDoc e) {
380             final Throwable JavaDoc targetException = e.getTargetException();
381             if (!(targetException instanceof InstallAbortedException)){
382                 UpdateUI.getStandardDisplay().syncExec(new Runnable JavaDoc() {
383                     public void run() {
384                         UpdateUI.logException(targetException);
385                     }
386                 });
387             }
388             return false;
389         } catch (CoreException e) {
390             return false;
391         } finally {
392             isRunning = false;
393         }
394         return true;
395     }
396     
397     private boolean download(final IProgressMonitor monitor) {
398         // Downloads the feature content.
399
// This method is called from a background job.
400
// If download fails, the user is prompted to retry.
401
try {
402             IFeatureOperation[] ops = installOperation.getOperations();
403             monitor.beginTask(UpdateUIMessages.InstallWizard_download, 5 * ops.length);
404             for (int i = 0; i < ops.length; i++) {
405                 IInstallFeatureOperation op = (IInstallFeatureOperation)ops[i];
406                 
407                 try {
408                     String JavaDoc featureName = op.getFeature().getLabel();
409                     if ((featureName == null ) || (featureName.trim() == "") ) { //$NON-NLS-1$
410
featureName = op.getFeature().getVersionedIdentifier().getIdentifier();
411                     }
412                     SubProgressMonitor featureDownloadMonitor = new SubProgressMonitor(monitor, 2);
413                     
414                     featureDownloadMonitor.beginTask(featureName, 2);
415                     featureDownloadMonitor.subTask(NLS.bind(UpdateUIMessages.InstallWizard_downloadingFeatureJar, featureName));
416                     
417                     if (op.getFeature() instanceof LiteFeature) {
418                         ISiteFeatureReference featureReference = getFeatureReference(op.getFeature());
419                         IFeature feature = featureReference.getFeature(featureDownloadMonitor);
420                         if (op instanceof InstallOperation) {
421                             ((InstallOperation)op).setFeature(feature);
422                         }
423                     }
424                     //featureDownloadMonitor.worked(1);
425
SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 3);
426                     UpdateUtils.downloadFeatureContent(op.getTargetSite(), op.getFeature(), op.getOptionalFeatures(), subMonitor);
427                 } catch (final CoreException e) {
428                     if(e instanceof FeatureDownloadException){
429                         boolean retry = retryDownload((FeatureDownloadException)e);
430                         if (retry) {
431                             // redownload current feature
432
i--;
433                             continue;
434                         }
435                     } else {
436                         UpdateCore.log(e);
437                         if ( !monitor.isCanceled()) {
438                             Display.getDefault().syncExec( new Runnable JavaDoc () {
439                                 public void run() {
440                                     IStatus status = new Status( IStatus.ERROR, UpdateUI.getPluginId(), IStatus.OK, UpdateUIMessages.InstallWizard2_updateOperationHasFailed, e);
441                                     ErrorDialog.openError(null, null, null, status);
442                                 
443                                 }
444                             });
445                         }
446                     }
447                     return false;
448                 }
449             }
450             return true;
451         } finally {
452             monitor.done();
453         }
454     }
455     
456     private boolean retryDownload(final FeatureDownloadException e) {
457
458         final boolean retry[] = new boolean[1];
459         UpdateUI.getStandardDisplay().syncExec(new Runnable JavaDoc() {
460             public void run() {
461                 retry[0] = MessageDialog.openQuestion(
462                     UpdateUI.getActiveWorkbenchShell(),
463                     UpdateUIMessages.InstallWizard_retryTitle,
464                     e.getMessage()+"\n" //$NON-NLS-1$
465
+ UpdateUIMessages.InstallWizard_retry);
466             }
467         });
468         return retry[0];
469     }
470     
471
472     private class UpdateJobChangeListener extends JobChangeAdapter {
473         public void done(final IJobChangeEvent event) {
474             // the job listener is triggered when the download job is done, and it proceeds
475
// with the actual install
476
if (event.getJob() == InstallWizard2.this.job && event.getResult() == Status.OK_STATUS) {
477                 Job.getJobManager().removeJobChangeListener(this);
478                 Job.getJobManager().cancel(job);
479                 
480                 Job installJob = new Job(UpdateUIMessages.InstallWizard_jobName) {
481                     public IStatus run(IProgressMonitor monitor) {
482                         //install(monitor);
483
if (install(monitor)) {
484                             return Status.OK_STATUS;
485                         } else {
486                             isRunning = false;
487                             return Status.CANCEL_STATUS;
488                         }
489                     }
490                     public boolean belongsTo(Object JavaDoc family) {
491                         return InstallWizard2.jobFamily == family;
492                     }
493                 };
494
495                 installJob.setUser(true);
496                 installJob.setPriority(Job.INTERACTIVE);
497 // if (wait) {
498
// progressService.showInDialog(workbench.getActiveWorkbenchWindow().getShell(), job);
499
// }
500
installJob.schedule();
501                 
502                 /*final IProgressService progressService= PlatformUI.getWorkbench().getProgressService();
503                 UpdateUI.getStandardDisplay().asyncExec(new Runnable() {
504                     public void run() {
505                         try {
506                             progressService.busyCursorWhile( new IRunnableWithProgress() {
507                                 public void run(final IProgressMonitor monitor){
508                                     install(monitor);
509                                 }
510                             });
511                         } catch (InvocationTargetException e) {
512                             UpdateUI.logException(e);
513                         } catch (InterruptedException e) {
514                             UpdateUI.logException(e, false);
515                         }
516                     }
517                 }); */

518             } else if (event.getJob() == InstallWizard2.this.job && event.getResult() != Status.OK_STATUS) {
519                 isRunning = false;
520                 Job.getJobManager().removeJobChangeListener(this);
521                 Job.getJobManager().cancel(job);
522                 UpdateUI.getStandardDisplay().syncExec(new Runnable JavaDoc() {
523                     public void run() {
524                         UpdateUI.log(event.getResult(), true);
525                     }
526                 });
527             }
528         }
529     }
530     
531     
532     public ISiteFeatureReference getFeatureReference(IFeature feature) {
533
534         ISite site = feature.getSite();
535         ISiteFeatureReference[] references = site.getFeatureReferences();
536         ISiteFeatureReference currentReference = null;
537         for (int i = 0; i < references.length; i++) {
538             currentReference = references[i];
539             try {
540                 if (feature.getVersionedIdentifier().equals(currentReference.getVersionedIdentifier()))
541                     return currentReference;
542             } catch (CoreException e) {
543                 // TODO Auto-generated catch block
544
e.printStackTrace();
545             }
546         }
547
548         UpdateCore.warn("Feature " + feature + " not found on site" + site.getURL()); //$NON-NLS-1$ //$NON-NLS-2$
549
return null;
550     }
551
552 }
553
Popular Tags