1 11 package org.eclipse.update.standalone; 12 13 import java.io.File ; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.util.ArrayList ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.OperationCanceledException; 22 import org.eclipse.core.runtime.SubProgressMonitor; 23 import org.eclipse.osgi.util.NLS; 24 import org.eclipse.update.configuration.IConfiguredSite; 25 import org.eclipse.update.core.IFeature; 26 import org.eclipse.update.core.ISite; 27 import org.eclipse.update.core.SiteManager; 28 import org.eclipse.update.core.Utilities; 29 import org.eclipse.update.core.VersionedIdentifier; 30 import org.eclipse.update.internal.configurator.UpdateURLDecoder; 31 import org.eclipse.update.internal.core.Messages; 32 import org.eclipse.update.internal.core.UpdateCore; 33 import org.eclipse.update.internal.core.UpdateManagerUtils; 34 import org.eclipse.update.internal.operations.DuplicateConflictsValidator; 35 import org.eclipse.update.internal.operations.UpdateUtils; 36 import org.eclipse.update.internal.search.SiteSearchCategory; 37 import org.eclipse.update.operations.IBatchOperation; 38 import org.eclipse.update.operations.IInstallFeatureOperation; 39 import org.eclipse.update.operations.OperationsManager; 40 import org.eclipse.update.search.BackLevelFilter; 41 import org.eclipse.update.search.EnvironmentFilter; 42 import org.eclipse.update.search.IUpdateSearchResultCollector; 43 import org.eclipse.update.search.UpdateSearchRequest; 44 import org.eclipse.update.search.UpdateSearchScope; 45 import org.eclipse.update.search.VersionedIdentifiersFilter; 46 47 57 public class InstallCommand extends ScriptedCommand { 58 59 private IConfiguredSite targetSite; 60 private UpdateSearchRequest searchRequest; 61 private UpdateSearchResultCollector collector; 62 private URL remoteSiteURL; 63 private String featureId; 64 private String version; 65 66 public InstallCommand( 67 String featureId, 68 String version, 69 String fromSite, 70 String toSite, 71 String verifyOnly) 72 throws Exception { 73 74 super(verifyOnly); 75 76 try { 77 this.featureId = featureId; 78 this.version = version; 79 80 this.remoteSiteURL = new URL (UpdateURLDecoder.decode(fromSite, "UTF-8")); 83 targetSite = getTargetSite(toSite); 85 if (targetSite == null) 87 targetSite = UpdateUtils.getSiteWithFeature(getConfiguration(), featureId); 88 if (targetSite == null) { 90 IConfiguredSite[] sites = getConfiguration().getConfiguredSites(); 91 for (int i = 0; i < sites.length; i++) { 92 if (sites[i].isProductSite() && sites[i].isUpdatable()) { 93 targetSite = sites[i]; 94 break; 95 } 96 } 97 } 98 if (targetSite == null) { 100 IConfiguredSite[] sites = getConfiguration().getConfiguredSites(); 101 for (int i = 0; i < sites.length; i++) { 102 if (sites[i].isUpdatable()) { 103 targetSite = sites[i]; 104 break; 105 } 106 } 107 } 108 if (targetSite == null) 110 throw Utilities.newCoreException( 111 Messages.Standalone_cannotInstall + featureId + " " + version, null); 113 114 UpdateSearchScope searchScope = new UpdateSearchScope(); 115 searchScope.addSearchSite( 116 NLS.bind( Messages.InstallCommand_site, remoteSiteURL.toExternalForm()), 117 remoteSiteURL, 118 new String [0]); 119 120 searchRequest = 121 new UpdateSearchRequest(new SiteSearchCategory(), searchScope); 122 VersionedIdentifier vid = 123 new VersionedIdentifier(featureId, version); 124 searchRequest.addFilter( 125 new VersionedIdentifiersFilter( 126 new VersionedIdentifier[] { vid })); 127 searchRequest.addFilter(new EnvironmentFilter()); 128 searchRequest.addFilter(new BackLevelFilter()); 129 130 collector = new UpdateSearchResultCollector(); 131 132 } catch (MalformedURLException e) { 133 throw e; 134 } catch (CoreException e) { 135 throw e; 136 } 137 } 138 139 141 public boolean run(IProgressMonitor monitor) { 142 try { 143 monitor.beginTask(Messages.Standalone_installing, 4); 144 searchRequest.performSearch(collector, new SubProgressMonitor(monitor,1)); 145 IInstallFeatureOperation[] operations = collector.getOperations(); 146 if (operations == null || operations.length == 0) { 147 throw Utilities.newCoreException( 148 Messages.Standalone_feature 149 + featureId 150 + " " + version 152 + Messages.Standalone_notFound 153 + remoteSiteURL 154 + Messages.Standalone_newerInstalled, 155 null); 156 } 157 158 ArrayList conflicts = 160 DuplicateConflictsValidator.computeDuplicateConflicts( 161 operations, 162 getConfiguration()); 163 if (conflicts != null) { 164 throw Utilities.newCoreException(Messages.Standalone_duplicate, null); 165 } 166 167 if (isVerifyOnly()) { 168 if (operations == null || operations.length == 0) 169 return false; 170 IStatus status = OperationsManager.getValidator().validatePendingChanges(operations); 171 if (status != null && status.getCode() == IStatus.ERROR) 172 throw new CoreException(status); 173 else 174 return true; 175 } 176 177 IBatchOperation installOperation = 178 OperationsManager 179 .getOperationFactory() 180 .createBatchInstallOperation( 181 operations); 182 try { 183 installOperation.execute(new SubProgressMonitor(monitor,3), this); 184 System.out.println( 185 Messages.Standalone_feature 186 + featureId 187 + " " + version 189 + Messages.Standalone_installed); 190 return true; 191 } catch (Exception e) { 192 throw Utilities.newCoreException( 193 Messages.Standalone_cannotInstall + featureId + " " + version, e); 195 } 196 } catch (CoreException ce) { 197 StandaloneUpdateApplication.exceptionLogged(); 198 UpdateCore.log(ce); 199 return false; 200 } catch (OperationCanceledException ce) { 201 return true; 202 } finally { 203 monitor.done(); 204 } 205 } 206 207 private IConfiguredSite getTargetSite(String toSite) throws Exception { 208 if (toSite == null) 209 return null; 210 211 IConfiguredSite[] configuredSites = getConfiguration().getConfiguredSites(); 212 File sitePath = new File (toSite); 213 File secondaryPath = sitePath.getName().equals("eclipse") ? null : new File (sitePath, "eclipse"); 216 for (int i = 0; i < configuredSites.length; i++) { 217 IConfiguredSite csite = configuredSites[i]; 218 if (UpdateManagerUtils.sameURL(csite.getSite().getURL(),sitePath.toURL())) 219 return csite; 220 else if (secondaryPath != null && UpdateManagerUtils.sameURL(csite.getSite().getURL(),secondaryPath.toURL())) 221 return csite; 222 } 223 224 if (!sitePath.exists()) 226 sitePath.mkdirs(); 227 URL toSiteURL = sitePath.toURL(); 228 ISite site = SiteManager.getSite(toSiteURL, null); 229 if (site == null) { 230 throw new Exception (Messages.Standalone_noSite + toSite); 231 } 232 IConfiguredSite csite = site.getCurrentConfiguredSite(); 233 if (csite == null) { 234 csite = getConfiguration().createConfiguredSite(sitePath); 235 IStatus status = csite.verifyUpdatableStatus(); 236 if (status.isOK()) 237 getConfiguration().addConfiguredSite(csite); 238 else 239 throw new CoreException(status); 240 241 return csite; 242 } 243 return csite; 244 } 245 class UpdateSearchResultCollector implements IUpdateSearchResultCollector { 246 private ArrayList operations = new ArrayList (); 247 248 public void accept(IFeature feature) { 249 if (feature 250 .getVersionedIdentifier() 251 .getIdentifier() 252 .equals(featureId) 253 && feature 254 .getVersionedIdentifier() 255 .getVersion() 256 .toString() 257 .equals( 258 version)) { 259 operations.add( 260 OperationsManager 261 .getOperationFactory() 262 .createInstallOperation( 263 targetSite, 264 feature, 265 null, 266 null, 267 null)); 268 } 269 } 270 public IInstallFeatureOperation[] getOperations() { 271 IInstallFeatureOperation[] opsArray = 272 new IInstallFeatureOperation[operations.size()]; 273 operations.toArray(opsArray); 274 return opsArray; 275 } 276 } 277 } 278 | Popular Tags |