1 11 package org.eclipse.update.standalone; 12 13 import java.net.*; 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.osgi.util.NLS; 18 import org.eclipse.update.configuration.*; 19 import org.eclipse.update.core.*; 20 import org.eclipse.update.internal.core.*; 21 import org.eclipse.update.internal.operations.*; 22 import org.eclipse.update.operations.*; 23 import org.eclipse.update.search.*; 24 25 35 public class UpdateCommand extends ScriptedCommand { 36 37 private IConfiguredSite targetSite; 38 private UpdateSearchRequest searchRequest; 39 private UpdateSearchResultCollector collector; 40 private String featureId; 41 private IFeature currentFeature; 43 44 50 public UpdateCommand(String featureId, String verifyOnly) 51 throws Exception { 52 this(featureId, null, verifyOnly); 53 } 54 55 62 public UpdateCommand(String featureId, String version, String verifyOnly) 63 throws Exception { 64 65 super(verifyOnly); 66 67 try { 68 this.featureId = featureId; 69 if (featureId != null) { 71 this.targetSite = 72 UpdateUtils.getSiteWithFeature( 73 getConfiguration(), 74 featureId); 75 if (targetSite == null) { 76 throw new Exception (NLS.bind(Messages.Standalone_noConfigSiteForFeature, (new String [] { featureId }))); 77 } 78 IFeature[] currentFeatures = 79 UpdateUtils.searchSite(featureId, targetSite, true); 80 if (currentFeatures == null || currentFeatures.length == 0) { 81 throw new Exception (NLS.bind(Messages.Standalone_noFeatures3, (new String [] { featureId }))); 82 } 83 this.currentFeature = currentFeatures[0]; 84 } else { 85 IConfiguredSite[] sites = 87 getConfiguration().getConfiguredSites(); 88 for (int i = 0; i < sites.length; i++) { 89 if (sites[i].isProductSite()) { 90 targetSite = sites[i]; 91 break; 92 } 93 } 94 } 95 if (currentFeature == null) 96 searchRequest = UpdateUtils.createNewUpdatesRequest(null); 97 else { 98 searchRequest = 99 UpdateUtils.createNewUpdatesRequest( 100 new IFeature[] { currentFeature }); 101 if (version != null) 102 searchRequest.addFilter( 103 new VersionedIdentifiersFilter( 104 new VersionedIdentifier[] { new VersionedIdentifier(featureId, version) })); 105 } 106 107 collector = new UpdateSearchResultCollector(); 108 109 } catch (MalformedURLException e) { 110 StandaloneUpdateApplication.exceptionLogged(); 111 UpdateCore.log(e); 112 } catch (CoreException e) { 113 StandaloneUpdateApplication.exceptionLogged(); 114 UpdateCore.log(e); 115 } 116 } 117 118 120 public boolean run(IProgressMonitor monitor) { 121 IStatus status = OperationsManager.getValidator().validatePlatformConfigValid(); 123 if (status != null) { 124 UpdateCore.log(status); 125 return false; 126 } 127 try { 128 monitor.beginTask(Messages.Standalone_updating, 4); 129 searchRequest.performSearch(collector, new SubProgressMonitor(monitor,1)); 130 IInstallFeatureOperation[] operations = collector.getOperations(); 131 if (operations == null || operations.length == 0) { 132 StandaloneUpdateApplication.exceptionLogged(); 133 UpdateCore.log(Utilities.newCoreException(NLS.bind(Messages.Standalone_noUpdate, (new String [] { featureId })), null)); 134 return false; 135 } 136 137 ArrayList conflicts = 139 DuplicateConflictsValidator.computeDuplicateConflicts( 140 operations, 141 getConfiguration()); 142 if (conflicts != null) { 143 StandaloneUpdateApplication.exceptionLogged(); 144 UpdateCore.log(Utilities.newCoreException(Messages.Standalone_duplicate, null)); 145 return false; 146 } 147 148 if (isVerifyOnly()) { 149 status = OperationsManager.getValidator().validatePendingChanges(operations); 150 if (status != null && status.getCode() == IStatus.ERROR) 151 throw new CoreException(status); 152 else 153 return true; 154 } 155 156 IBatchOperation installOperation = 157 OperationsManager 158 .getOperationFactory() 159 .createBatchInstallOperation( 160 operations); 161 try { 162 installOperation.execute(new SubProgressMonitor(monitor,3), this); 163 System.out.println( 164 Messages.Standalone_feature 165 + featureId 166 + " " + Messages.Standalone_updated); 168 return true; 169 } catch (Exception e) { 170 StandaloneUpdateApplication.exceptionLogged(); 171 UpdateCore.log( 172 Utilities.newCoreException( 173 NLS.bind(Messages.Standalone_noUpdate, (new String [] { featureId })), 174 e)); 175 return false; 176 } 177 } catch (CoreException ce) { 178 status = ce.getStatus(); 179 if (status != null 180 && status.getCode() == ISite.SITE_ACCESS_EXCEPTION) { 181 System.out.println(Messages.Standalone_connection); 184 } else { 185 StandaloneUpdateApplication.exceptionLogged(); 186 UpdateCore.log(ce); 187 } 188 return false; 189 } catch (OperationCanceledException ce) { 190 return true; 191 } finally { 192 monitor.done(); 193 } 194 } 195 196 197 class UpdateSearchResultCollector implements IUpdateSearchResultCollector { 198 private ArrayList operations = new ArrayList (); 199 200 public void accept(IFeature feature) { 201 202 IInstallFeatureOperation op = 203 OperationsManager.getOperationFactory().createInstallOperation( 204 null, 205 feature, 206 null, 207 null, 208 null); 209 210 IConfiguredSite site = 211 UpdateUtils.getDefaultTargetSite(getConfiguration(), op); 212 if (site == null) 213 site = UpdateUtils.getAffinitySite(getConfiguration(), feature); 214 if (site == null) 215 site = targetSite; 216 217 op.setTargetSite(site); 218 operations.add(op); 219 } 220 public IInstallFeatureOperation[] getOperations() { 221 IInstallFeatureOperation[] opsArray = 222 new IInstallFeatureOperation[operations.size()]; 223 operations.toArray(opsArray); 224 return opsArray; 225 } 226 } 227 } 228 | Popular Tags |