| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008, 2018 IBM Corporation and others. |
| 3 |
* |
| 4 |
* This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License 2.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: EPL-2.0 |
| 10 |
* |
| 11 |
* Contributors: |
| 12 |
* IBM Corporation - initial API and implementation |
| 13 |
* Sonatype, Inc. - transport split |
| 14 |
*******************************************************************************/ |
| 15 |
package org.eclipse.equinox.internal.p2.updatesite; |
| 16 |
|
| 17 |
import java.io.*; |
| 18 |
import java.lang.ref.SoftReference; |
| 19 |
import java.net.*; |
| 20 |
import java.util.HashMap; |
| 21 |
import java.util.Map; |
| 22 |
import java.util.zip.*; |
| 23 |
import org.eclipse.core.runtime.*; |
| 24 |
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; |
| 25 |
import org.eclipse.equinox.internal.p2.publisher.eclipse.FeatureParser; |
| 26 |
import org.eclipse.equinox.internal.p2.repository.Transport; |
| 27 |
import org.eclipse.equinox.p2.core.ProvisionException; |
| 28 |
import org.eclipse.equinox.p2.publisher.eclipse.*; |
| 29 |
import org.eclipse.osgi.service.resolver.BundleDescription; |
| 30 |
import org.eclipse.osgi.util.NLS; |
| 31 |
import org.xml.sax.SAXException; |
| 32 |
|
| 33 |
/** |
| 34 |
* @since 1.0 |
| 35 |
*/ |
| 36 |
public class UpdateSite { |
| 37 |
|
| 38 |
private static final String VERSION_SEPARATOR = "_"; //$NON-NLS-1$ |
| 39 |
private static final String JAR_EXTENSION = ".jar"; //$NON-NLS-1$ |
| 40 |
private static final String FEATURE_DIR = "features/"; //$NON-NLS-1$ |
| 41 |
private static final String PLUGIN_DIR = "plugins/"; //$NON-NLS-1$ |
| 42 |
private static final String FEATURE_TEMP_FILE = "feature"; //$NON-NLS-1$ |
| 43 |
private static final String SITE_FILE = "site.xml"; //$NON-NLS-1$ |
| 44 |
private static final String PROTOCOL_FILE = "file"; //$NON-NLS-1$ |
| 45 |
private static final int RETRY_COUNT = 2; |
| 46 |
private static final String DOT_XML = ".xml"; //$NON-NLS-1$ |
| 47 |
private static final String SITE = "site"; //$NON-NLS-1$ |
| 48 |
private String checksum; |
| 49 |
private URI location; |
| 50 |
private URI rootLocation; |
| 51 |
private SiteModel site; |
| 52 |
|
| 53 |
/* |
| 54 |
* Some variables for caching. |
| 55 |
*/ |
| 56 |
// map of String (URI.toString()) to UpdateSite |
| 57 |
private static Map<String, SoftReference<UpdateSite>> siteCache = new HashMap<>(); |
| 58 |
// map of String (URI.toString()) to UpdateSite (for category xmls) |
| 59 |
private static Map<String, SoftReference<UpdateSite>> categoryCache = new HashMap<>(); |
| 60 |
// map of String (featureID_featureVersion) to Feature |
| 61 |
private Map<String, Feature> featureCache = new HashMap<>(); |
| 62 |
// map of String (bundleID_featureVersion) to BundleDescriptr |
| 63 |
private Map<String, BundleDescription> bundleCache = new HashMap<>(); |
| 64 |
private Transport transport; |
| 65 |
|
| 66 |
/* |
| 67 |
* Return a URI based on the given URI, which points to a site.xml file. |
| 68 |
*/ |
| 69 |
private static URI getSiteURI(URI baseLocation) { |
| 70 |
String segment = URIUtil.lastSegment(baseLocation); |
| 71 |
if (constainsUpdateSiteFileName(segment)) |
| 72 |
return baseLocation; |
| 73 |
return URIUtil.append(baseLocation, SITE_FILE); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Be lenient about accepting any location with *site*.xml at the end. |
| 78 |
*/ |
| 79 |
private static boolean constainsUpdateSiteFileName(String segment) { |
| 80 |
return segment != null && segment.endsWith(DOT_XML) && segment.contains(SITE); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Loads and returns a category file |
| 85 |
* |
| 86 |
* @param location |
| 87 |
* @param monitor |
| 88 |
* @return A CategoryFile |
| 89 |
* @throws ProvisionException |
| 90 |
*/ |
| 91 |
public static synchronized UpdateSite loadCategoryFile(URI location, Transport transport, IProgressMonitor monitor) |
| 92 |
throws ProvisionException { |
| 93 |
if (location == null) |
| 94 |
return null; |
| 95 |
UpdateSite result = null; |
| 96 |
if (!PROTOCOL_FILE.equals(location.getScheme()) && categoryCache.containsKey(location.toString())) { |
| 97 |
result = categoryCache.get(location.toString()).get(); |
| 98 |
if (result != null) |
| 99 |
return result; |
| 100 |
// else soft reference has been cleared, take it out of the cache |
| 101 |
categoryCache.remove(location.toString()); |
| 102 |
} |
| 103 |
|
| 104 |
InputStream input = null; |
| 105 |
File siteFile = loadActualSiteFile(location, location, transport, monitor); |
| 106 |
try { |
| 107 |
CategoryParser siteParser = new CategoryParser(location); |
| 108 |
Checksum checksum = new CRC32(); |
| 109 |
input = new CheckedInputStream(new BufferedInputStream(new FileInputStream(siteFile)), checksum); |
| 110 |
SiteModel siteModel = siteParser.parse(input); |
| 111 |
String checksumString = Long.toString(checksum.getValue()); |
| 112 |
result = new UpdateSite(siteModel, location, transport, checksumString); |
| 113 |
if (!PROTOCOL_FILE.equals(location.getScheme())) |
| 114 |
categoryCache.put(location.toString(), new SoftReference<>(result)); |
| 115 |
return result; |
| 116 |
} catch (SAXException e) { |
| 117 |
String msg = NLS.bind(Messages.ErrorReadingSite, location); |
| 118 |
throw new ProvisionException( |
| 119 |
new Status(IStatus.ERROR, Activator.ID, ProvisionException.REPOSITORY_FAILED_READ, msg, e)); |
| 120 |
} catch (IOException e) { |
| 121 |
String msg = NLS.bind(Messages.ErrorReadingSite, location); |
| 122 |
throw new ProvisionException( |
| 123 |
new Status(IStatus.ERROR, Activator.ID, ProvisionException.REPOSITORY_FAILED_READ, msg, e)); |
| 124 |
} finally { |
| 125 |
try { |
| 126 |
if (input != null) |
| 127 |
input.close(); |
| 128 |
} catch (IOException e) { |
| 129 |
// ignore |
| 130 |
} |
| 131 |
if (!PROTOCOL_FILE.equals(location.getScheme())) |
| 132 |
siteFile.delete(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/* |
| 137 |
* Load and return an update site object from the given location. |
| 138 |
*/ |
| 139 |
public static synchronized UpdateSite load(URI location, Transport transport, IProgressMonitor monitor) |
| 140 |
throws ProvisionException { |
| 141 |
if (location == null) |
| 142 |
return null; |
| 143 |
|
| 144 |
UpdateSite result = null; |
| 145 |
// only caching remote sites |
| 146 |
if (!PROTOCOL_FILE.equals(location.getScheme()) && siteCache.containsKey(location.toString())) { |
| 147 |
result = siteCache.get(location.toString()).get(); |
| 148 |
if (result != null) |
| 149 |
return result; |
| 150 |
// else soft reference has been cleared, take it out of the cache |
| 151 |
siteCache.remove(location.toString()); |
| 152 |
} |
| 153 |
|
| 154 |
InputStream input = null; |
| 155 |
File siteFile = loadActualSiteFile(location, getSiteURI(location), transport, monitor); |
| 156 |
try { |
| 157 |
DefaultSiteParser siteParser = new DefaultSiteParser(location); |
| 158 |
Checksum checksum = new CRC32(); |
| 159 |
input = new CheckedInputStream(new BufferedInputStream(new FileInputStream(siteFile)), checksum); |
| 160 |
SiteModel siteModel = siteParser.parse(input); |
| 161 |
String checksumString = Long.toString(checksum.getValue()); |
| 162 |
result = new UpdateSite(siteModel, getSiteURI(location), transport, checksumString); |
| 163 |
if (!PROTOCOL_FILE.equals(location.getScheme())) |
| 164 |
siteCache.put(location.toString(), new SoftReference<>(result)); |
| 165 |
return result; |
| 166 |
} catch (SAXException e) { |
| 167 |
String msg = NLS.bind(Messages.ErrorReadingSite, location); |
| 168 |
throw new ProvisionException( |
| 169 |
new Status(IStatus.ERROR, Activator.ID, ProvisionException.REPOSITORY_FAILED_READ, msg, e)); |
| 170 |
} catch (IOException e) { |
| 171 |
String msg = NLS.bind(Messages.ErrorReadingSite, location); |
| 172 |
throw new ProvisionException( |
| 173 |
new Status(IStatus.ERROR, Activator.ID, ProvisionException.REPOSITORY_FAILED_READ, msg, e)); |
| 174 |
} finally { |
| 175 |
try { |
| 176 |
if (input != null) |
| 177 |
input.close(); |
| 178 |
} catch (IOException e) { |
| 179 |
// ignore |
| 180 |
} |
| 181 |
if (!PROTOCOL_FILE.equals(location.getScheme())) |
| 182 |
siteFile.delete(); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Returns a local file containing the contents of the update site at the given |
| 188 |
* location. |
| 189 |
*/ |
| 190 |
private static File loadActualSiteFile(URI location, URI actualLocation, Transport transport, |
| 191 |
IProgressMonitor monitor) throws ProvisionException { |
| 192 |
SubMonitor submonitor = SubMonitor.convert(monitor, 1000); |
| 193 |
try { |
| 194 |
File siteFile = null; |
| 195 |
IStatus transferResult = null; |
| 196 |
boolean deleteSiteFile = false; |
| 197 |
try { |
| 198 |
if (PROTOCOL_FILE.equals(actualLocation.getScheme())) { |
| 199 |
siteFile = URIUtil.toFile(actualLocation); |
| 200 |
if (siteFile.exists()) |
| 201 |
transferResult = Status.OK_STATUS; |
| 202 |
else { |
| 203 |
String msg = NLS.bind(Messages.ErrorReadingSite, location); |
| 204 |
transferResult = new Status(IStatus.ERROR, Activator.ID, ProvisionException.ARTIFACT_NOT_FOUND, |
| 205 |
msg, new FileNotFoundException(siteFile.getAbsolutePath())); |
| 206 |
} |
| 207 |
} else { |
| 208 |
// creating a temp file. In the event of an error we want to delete it. |
| 209 |
deleteSiteFile = true; |
| 210 |
OutputStream destination = null; |
| 211 |
try { |
| 212 |
siteFile = File.createTempFile("site", ".xml"); //$NON-NLS-1$//$NON-NLS-2$ |
| 213 |
destination = new BufferedOutputStream(new FileOutputStream(siteFile)); |
| 214 |
} catch (IOException e) { |
| 215 |
throw new ProvisionException(new Status(IStatus.ERROR, Activator.ID, |
| 216 |
ProvisionException.INTERNAL_ERROR, "Can not create tempfile for site.xml", e)); //$NON-NLS-1$ |
| 217 |
} |
| 218 |
try { |
| 219 |
transferResult = transport.download(actualLocation, destination, submonitor.newChild(999)); |
| 220 |
} finally { |
| 221 |
try { |
| 222 |
destination.close(); |
| 223 |
} catch (IOException e) { |
| 224 |
throw new ProvisionException(new Status(IStatus.ERROR, Activator.ID, |
| 225 |
ProvisionException.INTERNAL_ERROR, "Failing to close tempfile for site.xml", e)); //$NON-NLS-1$ |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
if (monitor.isCanceled()) |
| 230 |
throw new OperationCanceledException(); |
| 231 |
if (transferResult.isOK()) { |
| 232 |
// successful. If the siteFile is the download of a remote site.xml it will get |
| 233 |
// cleaned up later |
| 234 |
deleteSiteFile = false; |
| 235 |
return siteFile; |
| 236 |
} |
| 237 |
|
| 238 |
// The transferStatus from download has a well formatted message that should |
| 239 |
// be used as it contains useful feedback to the user. |
| 240 |
// The only thing needed is to translate the error code ARTIFACT_NOT_FOUND to |
| 241 |
// REPOSITORY_NOT_FOUND as the download does not know what the file represents. |
| 242 |
// |
| 243 |
IStatus ms = null; |
| 244 |
if (transferResult.getException() instanceof FileNotFoundException) |
| 245 |
ms = new MultiStatus(Activator.ID, // |
| 246 |
ProvisionException.REPOSITORY_NOT_FOUND, |
| 247 |
// (code == ProvisionException.ARTIFACT_NOT_FOUND || code == |
| 248 |
// ProvisionException.REPOSITORY_NOT_FOUND ? |
| 249 |
// ProvisionException.REPOSITORY_NOT_FOUND : |
| 250 |
// ProvisionException.REPOSITORY_FAILED_READ), // |
| 251 |
new IStatus[] { transferResult }, // |
| 252 |
NLS.bind(Messages.ErrorReadingSite, location), null); |
| 253 |
else |
| 254 |
ms = transferResult; |
| 255 |
throw new ProvisionException(ms); |
| 256 |
|
| 257 |
} finally { |
| 258 |
if (deleteSiteFile && siteFile != null) |
| 259 |
siteFile.delete(); |
| 260 |
} |
| 261 |
} finally { |
| 262 |
if (monitor != null) |
| 263 |
monitor.done(); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
/* |
| 268 |
* Parse the feature.xml specified by the given input stream and return the |
| 269 |
* feature object. In case of failure, the failure is logged and null is |
| 270 |
* returned |
| 271 |
*/ |
| 272 |
private Feature parseFeature(FeatureParser featureParser, URI featureURI, IProgressMonitor monitor) { |
| 273 |
File featureFile = null; |
| 274 |
if (PROTOCOL_FILE.equals(featureURI.getScheme())) { |
| 275 |
featureFile = URIUtil.toFile(featureURI); |
| 276 |
return featureParser.parse(featureFile); |
| 277 |
} |
| 278 |
try { |
| 279 |
featureFile = File.createTempFile(FEATURE_TEMP_FILE, JAR_EXTENSION); |
| 280 |
IStatus transferResult = null; |
| 281 |
// try the download twice in case of transient network problems |
| 282 |
for (int i = 0; i < RETRY_COUNT; i++) { |
| 283 |
if (monitor.isCanceled()) |
| 284 |
throw new OperationCanceledException(); |
| 285 |
OutputStream destination = new BufferedOutputStream(new FileOutputStream(featureFile)); |
| 286 |
try { |
| 287 |
transferResult = transport.download(featureURI, destination, monitor); |
| 288 |
} finally { |
| 289 |
try { |
| 290 |
destination.close(); |
| 291 |
} catch (IOException e) { |
| 292 |
LogHelper.log(new Status(IStatus.ERROR, Activator.ID, |
| 293 |
NLS.bind(Messages.ErrorReadingFeature, featureURI), e)); |
| 294 |
return null; |
| 295 |
} |
| 296 |
} |
| 297 |
if (transferResult.isOK()) |
| 298 |
break; |
| 299 |
} |
| 300 |
if (monitor.isCanceled()) |
| 301 |
throw new OperationCanceledException(); |
| 302 |
if (!transferResult.isOK()) { |
| 303 |
LogHelper.log(new ProvisionException(transferResult)); |
| 304 |
return null; |
| 305 |
} |
| 306 |
return featureParser.parse(featureFile); |
| 307 |
} catch (IOException e) { |
| 308 |
LogHelper.log( |
| 309 |
new Status(IStatus.ERROR, Activator.ID, NLS.bind(Messages.ErrorReadingFeature, featureURI), e)); |
| 310 |
} finally { |
| 311 |
if (featureFile != null) |
| 312 |
featureFile.delete(); |
| 313 |
} |
| 314 |
return null; |
| 315 |
} |
| 316 |
|
| 317 |
/* |
| 318 |
* Constructor for the class. |
| 319 |
*/ |
| 320 |
private UpdateSite(SiteModel site, URI location, Transport transport, String checksum) { |
| 321 |
super(); |
| 322 |
this.site = site; |
| 323 |
this.location = location; |
| 324 |
this.checksum = checksum; |
| 325 |
this.rootLocation = getRootLocation(); |
| 326 |
this.transport = transport; |
| 327 |
} |
| 328 |
|
| 329 |
private URI getRootLocation() { |
| 330 |
String locationString = location.toString(); |
| 331 |
int slashIndex = locationString.lastIndexOf('/'); |
| 332 |
if (slashIndex == -1 || slashIndex == (locationString.length() - 1)) |
| 333 |
return location; |
| 334 |
|
| 335 |
return URI.create(locationString.substring(0, slashIndex + 1)); |
| 336 |
} |
| 337 |
|
| 338 |
/* |
| 339 |
* Iterate over the archive entries in this site and return the matching URI |
| 340 |
* string for the given identifier, if there is one. |
| 341 |
*/ |
| 342 |
private URI getArchiveURI(URI base, String identifier) { |
| 343 |
URLEntry[] archives = site.getArchives(); |
| 344 |
for (int i = 0; archives != null && i < archives.length; i++) { |
| 345 |
URLEntry entry = archives[i]; |
| 346 |
if (identifier.equals(entry.getAnnotation())) |
| 347 |
return internalGetURI(base, entry.getURL()); |
| 348 |
} |
| 349 |
return null; |
| 350 |
} |
| 351 |
|
| 352 |
/* |
| 353 |
* Return the checksum for this site. |
| 354 |
*/ |
| 355 |
public String getChecksum() { |
| 356 |
return checksum; |
| 357 |
} |
| 358 |
|
| 359 |
/* |
| 360 |
* Return a URI which represents the location of the given feature. |
| 361 |
*/ |
| 362 |
public URI getSiteFeatureURI(SiteFeature siteFeature) { |
| 363 |
URL url = siteFeature.getURL(); |
| 364 |
try { |
| 365 |
if (url != null) |
| 366 |
return URIUtil.toURI(url); |
| 367 |
} catch (URISyntaxException e) { |
| 368 |
// fall through and resolve the URI ourselves |
| 369 |
} |
| 370 |
URI base = getBaseURI(); |
| 371 |
String featureURIString = siteFeature.getURLString(); |
| 372 |
return internalGetURI(base, featureURIString); |
| 373 |
} |
| 374 |
|
| 375 |
/* |
| 376 |
* Return a URI which represents the location of the given bundle. |
| 377 |
*/ |
| 378 |
public URI getSiteBundleURI(SiteBundle siteBundle) { |
| 379 |
URL url = siteBundle.getURL(); |
| 380 |
try { |
| 381 |
if (url != null) |
| 382 |
return URIUtil.toURI(url); |
| 383 |
} catch (URISyntaxException e) { |
| 384 |
// fall through and resolve the URI ourselves |
| 385 |
} |
| 386 |
URI base = getBaseURI(); |
| 387 |
String bundleURIString = siteBundle.getURLString(); |
| 388 |
return internalGetURI(base, bundleURIString); |
| 389 |
} |
| 390 |
|
| 391 |
/* |
| 392 |
* Return a URI which represents the location of the given feature. |
| 393 |
*/ |
| 394 |
public URI getFeatureURI(String id, String version) { |
| 395 |
SiteFeature[] entries = site.getFeatures(); |
| 396 |
for (SiteFeature entry : entries) { |
| 397 |
if (id.equals(entry.getFeatureIdentifier()) && version.equals(entry.getFeatureVersion())) { |
| 398 |
return getSiteFeatureURI(entry); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
URI base = getBaseURI(); |
| 403 |
URI url = getArchiveURI(base, FEATURE_DIR + id + VERSION_SEPARATOR + version + JAR_EXTENSION); |
| 404 |
if (url != null) |
| 405 |
return url; |
| 406 |
return URIUtil.append(base, FEATURE_DIR + id + VERSION_SEPARATOR + version + JAR_EXTENSION); |
| 407 |
} |
| 408 |
|
| 409 |
/* |
| 410 |
* Return a URI which represents the location of the given bundle. |
| 411 |
*/ |
| 412 |
public URI getBundleURI(String id, String version) { |
| 413 |
SiteBundle[] entries = site.getBundles(); |
| 414 |
for (SiteBundle entry : entries) { |
| 415 |
if (id.equals(entry.getBundleIdentifier()) && version.equals(entry.getBundleVersion())) { |
| 416 |
return getSiteBundleURI(entry); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
URI base = getBaseURI(); |
| 421 |
URI url = getArchiveURI(base, FEATURE_DIR + id + VERSION_SEPARATOR + version + JAR_EXTENSION); |
| 422 |
if (url != null) |
| 423 |
return url; |
| 424 |
return URIUtil.append(base, FEATURE_DIR + id + VERSION_SEPARATOR + version + JAR_EXTENSION); |
| 425 |
} |
| 426 |
|
| 427 |
/* |
| 428 |
* Return the location of this site. |
| 429 |
*/ |
| 430 |
public URI getLocation() { |
| 431 |
return location; |
| 432 |
} |
| 433 |
|
| 434 |
public String getMirrorsURI() { |
| 435 |
// copy mirror information from update site to p2 repositories |
| 436 |
String mirrors = site.getMirrorsURI(); |
| 437 |
if (mirrors == null) |
| 438 |
return null; |
| 439 |
// remove site.xml file reference |
| 440 |
int index = mirrors.indexOf("site.xml"); //$NON-NLS-1$ |
| 441 |
if (index != -1) |
| 442 |
mirrors = mirrors.substring(0, index) + mirrors.substring(index + "site.xml".length()); //$NON-NLS-1$ |
| 443 |
return mirrors; |
| 444 |
} |
| 445 |
|
| 446 |
/* |
| 447 |
* Return a URI which represents the location of the given plug-in. |
| 448 |
*/ |
| 449 |
public URI getPluginURI(FeatureEntry plugin) { |
| 450 |
URI base = getBaseURI(); |
| 451 |
String path = PLUGIN_DIR + plugin.getId() + VERSION_SEPARATOR + plugin.getVersion() + JAR_EXTENSION; |
| 452 |
URI url = getArchiveURI(base, path); |
| 453 |
if (url != null) |
| 454 |
return url; |
| 455 |
return URIUtil.append(base, path); |
| 456 |
} |
| 457 |
|
| 458 |
private URI getBaseURI() { |
| 459 |
URI base = null; |
| 460 |
String siteURIString = site.getLocationURIString(); |
| 461 |
if (siteURIString != null) { |
| 462 |
if (!siteURIString.endsWith("/")) //$NON-NLS-1$ |
| 463 |
siteURIString += "/"; //$NON-NLS-1$ |
| 464 |
base = internalGetURI(rootLocation, siteURIString); |
| 465 |
} |
| 466 |
if (base == null) |
| 467 |
base = rootLocation; |
| 468 |
return base; |
| 469 |
} |
| 470 |
|
| 471 |
/* |
| 472 |
* Return the site model. |
| 473 |
*/ |
| 474 |
public SiteModel getSite() { |
| 475 |
return site; |
| 476 |
} |
| 477 |
|
| 478 |
/* |
| 479 |
* The trailing parameter can be either null, relative or absolute. If it is |
| 480 |
* null, then return null. If it is absolute, then create a new url and return |
| 481 |
* it. If it is relative, then make it relative to the given base url. |
| 482 |
*/ |
| 483 |
private URI internalGetURI(URI base, String trailing) { |
| 484 |
if (trailing == null) |
| 485 |
return null; |
| 486 |
return URIUtil.makeAbsolute(URI.create(trailing), base); |
| 487 |
} |
| 488 |
|
| 489 |
/* |
| 490 |
* Load and return the features references in this update site. |
| 491 |
*/ |
| 492 |
public synchronized Feature[] loadFeatures(IProgressMonitor monitor) throws ProvisionException { |
| 493 |
if (!featureCache.isEmpty()) |
| 494 |
return featureCache.values().toArray(new Feature[featureCache.size()]); |
| 495 |
Feature[] result = loadFeaturesFromDigest(monitor); |
| 496 |
return result == null ? loadFeaturesFromSite(monitor) : result; |
| 497 |
} |
| 498 |
|
| 499 |
/* |
| 500 |
* Load and return the bundle references in this update site. |
| 501 |
*/ |
| 502 |
public synchronized BundleDescription[] loadBundles(IProgressMonitor monitor) { |
| 503 |
if (!bundleCache.isEmpty()) |
| 504 |
return bundleCache.values().toArray(new BundleDescription[bundleCache.size()]); |
| 505 |
BundleDescription[] result = null; // TODO loadBundlesFromDigest(monitor); |
| 506 |
return result == null ? loadBundlesFromSite(monitor) : result; |
| 507 |
} |
| 508 |
|
| 509 |
/* |
| 510 |
* Try and load the feature information from the update site's digest file, if |
| 511 |
* it exists. |
| 512 |
*/ |
| 513 |
private Feature[] loadFeaturesFromDigest(IProgressMonitor monitor) { |
| 514 |
File digestFile = null; |
| 515 |
boolean local = false; |
| 516 |
try { |
| 517 |
URI digestURI = getDigestURI(); |
| 518 |
if (PROTOCOL_FILE.equals(digestURI.getScheme())) { |
| 519 |
digestFile = URIUtil.toFile(digestURI); |
| 520 |
if (!digestFile.exists()) |
| 521 |
return null; |
| 522 |
local = true; |
| 523 |
} else { |
| 524 |
digestFile = File.createTempFile("digest", ".zip"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 525 |
BufferedOutputStream destination = new BufferedOutputStream(new FileOutputStream(digestFile)); |
| 526 |
IStatus result = null; |
| 527 |
try { |
| 528 |
result = transport.download(digestURI, destination, monitor); |
| 529 |
} finally { |
| 530 |
try { |
| 531 |
destination.close(); |
| 532 |
} catch (IOException e) { |
| 533 |
LogHelper.log(new Status(IStatus.ERROR, Activator.ID, |
| 534 |
NLS.bind(Messages.ErrorReadingFeature, location), e)); |
| 535 |
return null; |
| 536 |
} |
| 537 |
} |
| 538 |
if (result.getSeverity() == IStatus.CANCEL || monitor.isCanceled()) |
| 539 |
throw new OperationCanceledException(); |
| 540 |
if (!result.isOK()) |
| 541 |
return null; |
| 542 |
} |
| 543 |
Feature[] features = new DigestParser().parse(digestFile, digestURI); |
| 544 |
if (features == null) |
| 545 |
return null; |
| 546 |
Map<String, Feature> tmpFeatureCache = new HashMap<>(features.length); |
| 547 |
for (Feature feature : features) { |
| 548 |
String key = feature.getId() + VERSION_SEPARATOR + feature.getVersion(); |
| 549 |
tmpFeatureCache.put(key, feature); |
| 550 |
} |
| 551 |
featureCache = tmpFeatureCache; |
| 552 |
return features; |
| 553 |
} catch (FileNotFoundException fnfe) { |
| 554 |
// we do not track FNF exceptions as we will fall back to the |
| 555 |
// standard feature parsing from the site itself, see bug 225587. |
| 556 |
} catch (IOException e) { |
| 557 |
LogHelper.log(new Status(IStatus.ERROR, Activator.ID, NLS.bind(Messages.ErrorReadingDigest, location), e)); |
| 558 |
} finally { |
| 559 |
if (!local && digestFile != null) |
| 560 |
digestFile.delete(); |
| 561 |
} |
| 562 |
return null; |
| 563 |
} |
| 564 |
|
| 565 |
private URI getDigestURI() { |
| 566 |
URI digestBase = null; |
| 567 |
String digestURIString = site.getDigestURIString(); |
| 568 |
if (digestURIString != null) { |
| 569 |
if (!digestURIString.endsWith("/")) //$NON-NLS-1$ |
| 570 |
digestURIString += "/"; //$NON-NLS-1$ |
| 571 |
digestBase = internalGetURI(rootLocation, digestURIString); |
| 572 |
} |
| 573 |
|
| 574 |
if (digestBase == null) |
| 575 |
digestBase = rootLocation; |
| 576 |
|
| 577 |
return URIUtil.append(digestBase, "digest.zip"); //$NON-NLS-1$ |
| 578 |
} |
| 579 |
|
| 580 |
/* |
| 581 |
* Load and return the features that are referenced by this update site. Note |
| 582 |
* this requires downloading and parsing the feature manifest locally. |
| 583 |
*/ |
| 584 |
private Feature[] loadFeaturesFromSite(IProgressMonitor monitor) throws ProvisionException { |
| 585 |
SiteFeature[] siteFeatures = site.getFeatures(); |
| 586 |
FeatureParser featureParser = new FeatureParser(); |
| 587 |
Map<String, Feature> tmpFeatureCache = new HashMap<>(siteFeatures.length); |
| 588 |
|
| 589 |
for (SiteFeature siteFeature1 : siteFeatures) { |
| 590 |
if (monitor.isCanceled()) { |
| 591 |
throw new OperationCanceledException(); |
| 592 |
} |
| 593 |
SiteFeature siteFeature = siteFeature1; |
| 594 |
String key = null; |
| 595 |
if (siteFeature.getFeatureIdentifier() != null && siteFeature.getFeatureVersion() != null) { |
| 596 |
key = siteFeature.getFeatureIdentifier() + VERSION_SEPARATOR + siteFeature.getFeatureVersion(); |
| 597 |
if (tmpFeatureCache.containsKey(key)) |
| 598 |
continue; |
| 599 |
} |
| 600 |
URI featureURI = getSiteFeatureURI(siteFeature); |
| 601 |
Feature feature = parseFeature(featureParser, featureURI, new NullProgressMonitor()); |
| 602 |
if (feature == null) { |
| 603 |
LogHelper.log( |
| 604 |
new Status(IStatus.ERROR, Activator.ID, NLS.bind(Messages.ErrorReadingFeature, featureURI))); |
| 605 |
} else { |
| 606 |
if (key == null) { |
| 607 |
siteFeature.setFeatureIdentifier(feature.getId()); |
| 608 |
siteFeature.setFeatureVersion(feature.getVersion()); |
| 609 |
key = siteFeature.getFeatureIdentifier() + VERSION_SEPARATOR + siteFeature.getFeatureVersion(); |
| 610 |
} |
| 611 |
tmpFeatureCache.put(key, feature); |
| 612 |
loadIncludedFeatures(feature, featureParser, tmpFeatureCache, monitor); |
| 613 |
} |
| 614 |
} |
| 615 |
featureCache = tmpFeatureCache; |
| 616 |
return featureCache.values().toArray(new Feature[featureCache.size()]); |
| 617 |
} |
| 618 |
|
| 619 |
/* |
| 620 |
* Load the features that are included by the given feature. |
| 621 |
*/ |
| 622 |
private void loadIncludedFeatures(Feature feature, FeatureParser featureParser, Map<String, Feature> features, |
| 623 |
IProgressMonitor monitor) throws ProvisionException { |
| 624 |
FeatureEntry[] featureEntries = feature.getEntries(); |
| 625 |
for (FeatureEntry featureEntry : featureEntries) { |
| 626 |
if (monitor.isCanceled()) |
| 627 |
throw new OperationCanceledException(); |
| 628 |
FeatureEntry entry = featureEntry; |
| 629 |
if (entry.isRequires() || entry.isPlugin()) |
| 630 |
continue; |
| 631 |
String key = entry.getId() + VERSION_SEPARATOR + entry.getVersion(); |
| 632 |
if (features.containsKey(key)) |
| 633 |
continue; |
| 634 |
URI includedFeatureURI = getFeatureURI(entry.getId(), entry.getVersion()); |
| 635 |
Feature includedFeature = parseFeature(featureParser, includedFeatureURI, monitor); |
| 636 |
if (includedFeature == null) { |
| 637 |
LogHelper.log(new Status(IStatus.ERROR, Activator.ID, |
| 638 |
NLS.bind(Messages.ErrorReadingFeature, includedFeatureURI))); |
| 639 |
} else { |
| 640 |
features.put(key, includedFeature); |
| 641 |
loadIncludedFeatures(includedFeature, featureParser, features, monitor); |
| 642 |
} |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
/* |
| 647 |
* Load and return the bundles that are referenced by this update site. Note |
| 648 |
* this requires downloading and parsing the feature manifest locally. |
| 649 |
*/ |
| 650 |
private BundleDescription[] loadBundlesFromSite(IProgressMonitor monitor) { |
| 651 |
SiteBundle[] siteBundles = site.getBundles(); |
| 652 |
Map<String, BundleDescription> tmpBundleCache = new HashMap<>(siteBundles.length); |
| 653 |
|
| 654 |
for (SiteBundle siteBundle : siteBundles) { |
| 655 |
if (monitor.isCanceled()) { |
| 656 |
throw new OperationCanceledException(); |
| 657 |
} |
| 658 |
String key = null; |
| 659 |
if (siteBundle.getBundleIdentifier() != null && siteBundle.getBundleVersion() != null) { |
| 660 |
key = siteBundle.getBundleIdentifier() + VERSION_SEPARATOR + siteBundle.getBundleVersion(); |
| 661 |
if (tmpBundleCache.containsKey(key)) |
| 662 |
continue; |
| 663 |
} |
| 664 |
URI bundleURI = getSiteBundleURI(siteBundle); |
| 665 |
BundleDescription bundle = parseBundleDescription(bundleURI, monitor); |
| 666 |
if (bundle == null) { |
| 667 |
LogHelper |
| 668 |
.log(new Status(IStatus.ERROR, Activator.ID, NLS.bind(Messages.ErrorReadingBundle, bundleURI))); |
| 669 |
} else { |
| 670 |
if (key == null) { |
| 671 |
siteBundle.setBundleIdentifier(bundle.getSymbolicName()); |
| 672 |
siteBundle.setBundleVersion(bundle.getVersion().toString()); |
| 673 |
key = siteBundle.getBundleIdentifier() + VERSION_SEPARATOR |
| 674 |
+ siteBundle.getBundleVersion().toString(); |
| 675 |
} |
| 676 |
tmpBundleCache.put(key, bundle); |
| 677 |
} |
| 678 |
} |
| 679 |
bundleCache = tmpBundleCache; |
| 680 |
return bundleCache.values().toArray(new BundleDescription[bundleCache.size()]); |
| 681 |
} |
| 682 |
|
| 683 |
/* |
| 684 |
* Reads a bundle and extract its BundleDescription In case of failure, the |
| 685 |
* failure is logged and null is returned |
| 686 |
*/ |
| 687 |
private BundleDescription parseBundleDescription(URI bundleURI, IProgressMonitor monitor) { |
| 688 |
File bundleFile = null; |
| 689 |
if (PROTOCOL_FILE.equals(bundleURI.getScheme())) { |
| 690 |
bundleFile = URIUtil.toFile(bundleURI); |
| 691 |
} |
| 692 |
try { |
| 693 |
bundleFile = File.createTempFile("bundle", JAR_EXTENSION); //$NON-NLS-1$ |
| 694 |
IStatus transferResult = null; |
| 695 |
// try the download twice in case of transient network problems |
| 696 |
for (int i = 0; i < RETRY_COUNT; i++) { |
| 697 |
if (monitor.isCanceled()) |
| 698 |
throw new OperationCanceledException(); |
| 699 |
OutputStream destination = new BufferedOutputStream(new FileOutputStream(bundleFile)); |
| 700 |
try { |
| 701 |
transferResult = transport.download(bundleURI, destination, monitor); |
| 702 |
} finally { |
| 703 |
try { |
| 704 |
destination.close(); |
| 705 |
} catch (IOException e) { |
| 706 |
LogHelper.log(new Status(IStatus.ERROR, Activator.ID, |
| 707 |
NLS.bind(Messages.ErrorReadingFeature, bundleURI), e)); |
| 708 |
return null; |
| 709 |
} |
| 710 |
} |
| 711 |
if (transferResult.isOK()) |
| 712 |
break; |
| 713 |
} |
| 714 |
if (monitor.isCanceled()) |
| 715 |
throw new OperationCanceledException(); |
| 716 |
if (!transferResult.isOK()) { |
| 717 |
LogHelper.log(new ProvisionException(transferResult)); |
| 718 |
return null; |
| 719 |
} |
| 720 |
return BundlesAction.createBundleDescriptionIgnoringExceptions(bundleFile); |
| 721 |
} catch (IOException e) { |
| 722 |
LogHelper.log(new Status(IStatus.ERROR, Activator.ID, NLS.bind(Messages.ErrorReadingBundle, bundleURI), e)); |
| 723 |
} finally { |
| 724 |
if (bundleFile != null) |
| 725 |
bundleFile.delete(); |
| 726 |
} |
| 727 |
return null; |
| 728 |
} |
| 729 |
|
| 730 |
} |