1 11 package org.eclipse.update.ui; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 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 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 59 public static final Object FAMILY = new Object (); 60 61 66 public static final Object family = FAMILY; 67 68 private IUpdateSearchResultCollector resultCollector; 69 70 private UpdateSearchRequest searchRequest; 71 72 private ArrayList 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 94 public UpdateJob(String name, boolean isAutomatic, boolean download) { 95 this(name, isAutomatic, download, null); 96 } 97 98 111 public UpdateJob(String 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 (); 118 searchRequest = UpdateUtils.createNewUpdatesRequest(features, isAutomatic); 119 setPriority(Job.DECORATE); 120 mirrorIndex = 0; 121 } 122 123 132 public UpdateJob(String name, UpdateSearchRequest searchRequest) { 133 super(name); 134 this.searchRequest = searchRequest; 135 updates = new ArrayList (); 136 setPriority(Job.DECORATE); 137 } 138 139 145 public boolean isUpdate() { 146 return isUpdate; 147 } 148 149 154 public boolean belongsTo(Object family) { 155 return UpdateJob.FAMILY == family; 156 } 157 158 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."); } 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 - " + updates.size() + " results."); } 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 statusList = new ArrayList (); 197 if (UpdateCore.DEBUG) { 198 if (isAutomatic) 199 UpdateCore.debug("Automatic update search started."); else 201 UpdateCore.debug("Update search started."); } 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 - " + updates.size() + " results."); } 217 if (updates.size() > 0) { 218 if (download) { 220 if (UpdateCore.DEBUG) { 221 UpdateCore.debug("Automatic download of updates started."); } 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."); } 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", ISite.SITE_ACCESS_EXCEPTION, children, 253 Messages.Search_networkProblems, 254 null); 255 } 256 } 257 258 263 public IInstallFeatureOperation[] getUpdates() { 264 return (IInstallFeatureOperation[]) updates 265 .toArray(new IInstallFeatureOperation[updates.size()]); 266 } 267 268 273 public IStatus getStatus() { 274 return jobStatus; 275 } 276 277 282 public UpdateSearchRequest getSearchRequest() { 283 return searchRequest; 284 } 285 286 private class ResultCollectorWithMirrors extends SearchResultCollector 287 implements IUpdateSearchResultCollectorFromMirror { 288 289 private HashMap mirrors = new HashMap (0); 290 291 297 public IURLEntry getMirror(final ISiteWithMirrors site, 298 final String 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 final IURLEntry[] returnValue = new IURLEntry[1]; 314 final boolean [] canceled = new boolean[1]; 315 UpdateUI.getStandardDisplay().syncExec(new Runnable () { 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 |