KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > ui > UpdateJob


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.ui;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.MultiStatus;
20 import org.eclipse.core.runtime.OperationCanceledException;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.core.runtime.jobs.Job;
23 import org.eclipse.update.core.IFeature;
24 import org.eclipse.update.core.ISite;
25 import org.eclipse.update.core.ISiteWithMirrors;
26 import org.eclipse.update.core.IURLEntry;
27 import org.eclipse.update.core.model.InstallAbortedException;
28 import org.eclipse.update.internal.core.Messages;
29 import org.eclipse.update.internal.core.UpdateCore;
30 import org.eclipse.update.internal.operations.UpdateUtils;
31 import org.eclipse.update.internal.ui.*;
32 import org.eclipse.update.internal.ui.wizards.*;
33 import org.eclipse.update.operations.IInstallFeatureOperation;
34 import org.eclipse.update.operations.OperationsManager;
35 import org.eclipse.update.search.IUpdateSearchResultCollector;
36 import org.eclipse.update.search.IUpdateSearchResultCollectorFromMirror;
37 import org.eclipse.update.search.UpdateSearchRequest;
38
39 /**
40  * An UpdateJob performs the lookup for new features or updates to the existing
41  * features, depending on how you construct it.
42  *
43  * @since 3.1
44  */

45 public class UpdateJob extends Job {
46
47     private class SearchResultCollector implements IUpdateSearchResultCollector {
48         public void accept(IFeature feature) {
49             IInstallFeatureOperation operation = OperationsManager
50                     .getOperationFactory().createInstallOperation(null,
51                             feature, null, null, null);
52             updates.add(operation);
53         }
54     }
55     
56     /**
57      * The job family to use for queries
58      */

59     public static final Object JavaDoc FAMILY = new Object JavaDoc();
60
61     /**
62      * The job family to use for queries
63      *
64      * @deprecated use FAMILY
65      */

66     public static final Object JavaDoc family = FAMILY;
67
68     private IUpdateSearchResultCollector resultCollector;
69
70     private UpdateSearchRequest searchRequest;
71
72     private ArrayList JavaDoc updates;
73
74     private boolean isUpdate;
75
76     private boolean download;
77
78     private boolean isAutomatic;
79
80     private IStatus jobStatus = Status.OK_STATUS;
81     
82     private int mirrorIndex;
83
84     /**
85      * Use this constructor to search for updates to installed features
86      *
87      * @param isAutomatic
88      * true if automatically searching for updates
89      * @param name
90      * the job name
91      * @param download
92      * download updates automatically
93      */

94     public UpdateJob(String JavaDoc name, boolean isAutomatic, boolean download) {
95         this(name, isAutomatic, download, null);
96     }
97
98     /**
99      * Use this constructor to search for updates to installed features
100      *
101      * @param isAutomatic
102      * true if automatically searching for updates
103      * @param name
104      * the job name
105      * @param download
106      * download updates automatically
107      * @param features
108      * the features to search for updates. If you want to search all
109      * features, pass a null array or use the other constructor.
110      */

111     public UpdateJob(String JavaDoc name, boolean isAutomatic, boolean download,
112             IFeature[] features) {
113         super(name);
114         this.isUpdate = true;
115         this.isAutomatic = isAutomatic;
116         this.download = download;
117         updates = new ArrayList JavaDoc();
118         searchRequest = UpdateUtils.createNewUpdatesRequest(features, isAutomatic);
119         setPriority(Job.DECORATE);
120         mirrorIndex = 0;
121     }
122
123     /**
124      * Use this constructor to search for features as indicated by the search
125      * request
126      *
127      * @param name
128      * the job name
129      * @param searchRequest
130      * the search request to execute
131      */

132     public UpdateJob(String JavaDoc name, UpdateSearchRequest searchRequest) {
133         super(name);
134         this.searchRequest = searchRequest;
135         updates = new ArrayList JavaDoc();
136         setPriority(Job.DECORATE);
137     }
138
139     /**
140      * Returns true if the job is performing update lookups, or false when
141      * searching for new features
142      *
143      * @return true when searching for updates of existing features
144      */

145     public boolean isUpdate() {
146         return isUpdate;
147     }
148
149     /*
150      * (non-Javadoc)
151      *
152      * @see org.eclipse.core.internal.jobs.InternalJob#belongsTo(java.lang.Object)
153      */

154     public boolean belongsTo(Object JavaDoc family) {
155         return UpdateJob.FAMILY == family;
156     }
157
158     /**
159      * Runs the job and returns the OK status. Call getStatus() to get the
160      * actual execution status.
161      *
162      * @param monitor
163      * progress monitor
164      * @return IStatus the return code is always OK
165      */

166     public IStatus run(IProgressMonitor monitor) {
167         if (isUpdate)
168             jobStatus = runUpdates(monitor);
169         else
170             jobStatus = runSearchForNew(monitor);
171         return Status.OK_STATUS;
172     }
173
174     private IStatus runSearchForNew(IProgressMonitor monitor) {
175         if (UpdateCore.DEBUG) {
176             UpdateCore.debug("Search for features started."); //$NON-NLS-1$
177
}
178
179         try {
180             if (resultCollector == null)
181                 resultCollector = new ResultCollectorWithMirrors();
182             searchRequest.performSearch(resultCollector, monitor);
183             if (UpdateCore.DEBUG) {
184                 UpdateCore.debug("Automatic update search finished - " //$NON-NLS-1$
185
+ updates.size() + " results."); //$NON-NLS-1$
186
}
187             return Status.OK_STATUS;
188         } catch (CoreException e) {
189             return e.getStatus();
190         } catch (OperationCanceledException ce) {
191             return Status.CANCEL_STATUS;
192         }
193     }
194
195     private IStatus runUpdates(IProgressMonitor monitor) {
196         ArrayList JavaDoc statusList = new ArrayList JavaDoc();
197         if (UpdateCore.DEBUG) {
198             if (isAutomatic)
199                 UpdateCore.debug("Automatic update search started."); //$NON-NLS-1$
200
else
201                 UpdateCore.debug("Update search started."); //$NON-NLS-1$
202
}
203
204         if (resultCollector == null)
205             resultCollector = new ResultCollectorWithMirrors();
206         try {
207             searchRequest.performSearch(resultCollector, monitor);
208         } catch (CoreException e) {
209             statusList.add(e.getStatus());
210         } catch (OperationCanceledException ce) {
211             return Status.CANCEL_STATUS;
212         }
213         if (UpdateCore.DEBUG) {
214             UpdateCore.debug("Automatic update search finished - " //$NON-NLS-1$
215
+ updates.size() + " results."); //$NON-NLS-1$
216
}
217         if (updates.size() > 0) {
218             // silently download if download enabled
219
if (download) {
220                 if (UpdateCore.DEBUG) {
221                     UpdateCore.debug("Automatic download of updates started."); //$NON-NLS-1$
222
}
223                 for (int i = 0; i < updates.size(); i++) {
224                     IInstallFeatureOperation op = (IInstallFeatureOperation) updates
225                             .get(i);
226                     IFeature feature = op.getFeature();
227                     try {
228                         UpdateUtils.downloadFeatureContent(op.getTargetSite(),
229                                 feature, null, monitor);
230                     } catch (InstallAbortedException e) {
231                         return Status.CANCEL_STATUS;
232                     } catch (CoreException e) {
233                         statusList.add(e.getStatus());
234                         updates.remove(i);
235                         i -= 1;
236                     }
237                 }
238                 if (UpdateCore.DEBUG) {
239                     UpdateCore.debug("Automatic download of updates finished."); //$NON-NLS-1$
240
}
241             }
242         }
243
244         if (statusList.size() == 0)
245             return Status.OK_STATUS;
246         else if (statusList.size() == 1)
247             return (IStatus) statusList.get(0);
248         else {
249             IStatus[] children = (IStatus[]) statusList
250                     .toArray(new IStatus[statusList.size()]);
251             return new MultiStatus("org.eclipse.update.ui", //$NON-NLS-1$
252
ISite.SITE_ACCESS_EXCEPTION, children,
253                     Messages.Search_networkProblems,
254                     null);
255         }
256     }
257
258     /**
259      * Returns an array of features to install
260      *
261      * @return IInstallFeatureOperation[]
262      */

263     public IInstallFeatureOperation[] getUpdates() {
264         return (IInstallFeatureOperation[]) updates
265                 .toArray(new IInstallFeatureOperation[updates.size()]);
266     }
267
268     /**
269      * Returns the job status upon termination.
270      *
271      * @return IStatus
272      */

273     public IStatus getStatus() {
274         return jobStatus;
275     }
276
277     /**
278      * Returns the update search request for this job.
279      *
280      * @return UpdateSearchRequest
281      */

282     public UpdateSearchRequest getSearchRequest() {
283         return searchRequest;
284     }
285
286     private class ResultCollectorWithMirrors extends SearchResultCollector
287             implements IUpdateSearchResultCollectorFromMirror {
288
289         private HashMap JavaDoc mirrors = new HashMap JavaDoc(0);
290
291         /*
292          * (non-Javadoc)
293          *
294          * @see org.eclipse.update.search.IUpdateSearchResultCollectorFromMirror#getMirror(org.eclipse.update.core.ISite,
295          * java.lang.String)
296          */

297         public IURLEntry getMirror(final ISiteWithMirrors site,
298                 final String JavaDoc siteName) throws OperationCanceledException {
299             if (isUpdate && isAutomatic)
300                 return null;
301             if (mirrors.containsKey(site))
302                 return (IURLEntry) mirrors.get(site);
303             try {
304                 boolean automaticallyChooseMirrors = UpdateCore.getPlugin().getPluginPreferences().getBoolean(UpdateCore.P_AUTOMATICALLY_CHOOSE_MIRROR);
305                 
306                 IURLEntry[] mirrorURLs = site.getMirrorSiteEntries();
307                 if (mirrorURLs.length == 0)
308                     return null;
309                 else if (automaticallyChooseMirrors){
310                     return mirrorURLs[mirrorIndex];
311                 } else {
312                     // here we need to prompt the user
313
final IURLEntry[] returnValue = new IURLEntry[1];
314                     final boolean [] canceled = new boolean[1];
315                     UpdateUI.getStandardDisplay().syncExec(new Runnable JavaDoc() {
316                         public void run() {
317                             MirrorsDialog dialog = new MirrorsDialog(UpdateUI
318                                     .getActiveWorkbenchShell(), site, siteName);
319                             dialog.create();
320                             int result = dialog.open();
321                             if (result==MirrorsDialog.CANCEL) {
322                                 canceled[0] = true;
323                             }
324                             IURLEntry mirror = dialog.getMirror();
325                             mirrors.put(site, mirror);
326                             returnValue[0] = mirror;
327                         }
328                     });
329                     if (canceled[0])
330                         throw new OperationCanceledException();
331                     return returnValue[0];
332                 }
333             } catch (CoreException e) {
334                 return null;
335             }
336         }
337     }
338 }
339
Popular Tags