|
Removed
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008, 2009 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.equinox.internal.p2.core.helpers; |
| 12 |
|
| 13 |
import java.lang.ref.SoftReference; |
| 14 |
import java.net.*; |
| 15 |
import java.util.*; |
| 16 |
import org.eclipse.core.runtime.*; |
| 17 |
import org.eclipse.core.runtime.preferences.IPreferencesService; |
| 18 |
import org.eclipse.equinox.internal.p2.core.Activator; |
| 19 |
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException; |
| 20 |
import org.eclipse.equinox.internal.provisional.p2.core.eventbus.IProvisioningEventBus; |
| 21 |
import org.eclipse.equinox.internal.provisional.p2.core.eventbus.ProvisioningListener; |
| 22 |
import org.eclipse.equinox.internal.provisional.p2.core.repository.*; |
| 23 |
import org.eclipse.osgi.util.NLS; |
| 24 |
import org.osgi.service.prefs.BackingStoreException; |
| 25 |
import org.osgi.service.prefs.Preferences; |
| 26 |
|
| 27 |
/** |
| 28 |
* Common code shared between artifact and metadata repository managers. |
| 29 |
*/ |
| 30 |
public abstract class AbstractRepositoryManager implements IRepositoryManager, ProvisioningListener { |
| 31 |
protected static class RepositoryInfo { |
| 32 |
public String description; |
| 33 |
public boolean isEnabled = true; |
| 34 |
public boolean isSystem = false; |
| 35 |
public URI location; |
| 36 |
public String name; |
| 37 |
public String nickname; |
| 38 |
public SoftReference repository; |
| 39 |
public String suffix; |
| 40 |
|
| 41 |
public RepositoryInfo() { |
| 42 |
super(); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public static final String ATTR_SUFFIX = "suffix"; //$NON-NLS-1$ |
| 47 |
public static final String EL_FACTORY = "factory"; //$NON-NLS-1$ |
| 48 |
public static final String EL_FILTER = "filter"; //$NON-NLS-1$ |
| 49 |
public static final String KEY_DESCRIPTION = "description"; //$NON-NLS-1$ |
| 50 |
public static final String KEY_ENABLED = "enabled"; //$NON-NLS-1$ |
| 51 |
public static final String KEY_NAME = "name"; //$NON-NLS-1$ |
| 52 |
public static final String KEY_NICKNAME = "nickname"; //$NON-NLS-1$ |
| 53 |
public static final String KEY_PROVIDER = "provider"; //$NON-NLS-1$ |
| 54 |
public static final String KEY_SUFFIX = "suffix"; //$NON-NLS-1$ |
| 55 |
public static final String KEY_SYSTEM = "isSystem"; //$NON-NLS-1$ |
| 56 |
public static final String KEY_TYPE = "type"; //$NON-NLS-1$ |
| 57 |
public static final String KEY_URI = "uri"; //$NON-NLS-1$ |
| 58 |
public static final String KEY_URL = "url"; //$NON-NLS-1$ |
| 59 |
public static final String KEY_VERSION = "version"; //$NON-NLS-1$ |
| 60 |
|
| 61 |
public static final String NODE_REPOSITORIES = "repositories"; //$NON-NLS-1$ |
| 62 |
|
| 63 |
/** |
| 64 |
* Map of String->RepositoryInfo, where String is the repository key |
| 65 |
* obtained via getKey(URI). |
| 66 |
*/ |
| 67 |
protected Map repositories = null; |
| 68 |
|
| 69 |
//lock object to be held when referring to the repositories field |
| 70 |
protected final Object repositoryLock = new Object(); |
| 71 |
|
| 72 |
/** |
| 73 |
* Cache List of repositories that are not reachable. Maintain cache |
| 74 |
* for short duration because repository may become available at any time. |
| 75 |
*/ |
| 76 |
protected SoftReference unavailableRepositories; |
| 77 |
|
| 78 |
/** |
| 79 |
* Set used to manage exclusive load locks on repository locations. |
| 80 |
*/ |
| 81 |
private Map loadLocks = new HashMap(); |
| 82 |
|
| 83 |
protected AbstractRepositoryManager() { |
| 84 |
IProvisioningEventBus bus = (IProvisioningEventBus) ServiceHelper.getService(Activator.getContext(), IProvisioningEventBus.SERVICE_NAME); |
| 85 |
if (bus != null) |
| 86 |
bus.addListener(this); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Adds a repository to the list of known repositories |
| 91 |
* @param repository the repository object to add |
| 92 |
* @param signalAdd whether a repository change event should be fired |
| 93 |
* @param suffix the suffix used to load the repository, or <code>null</code> if unknown |
| 94 |
*/ |
| 95 |
protected void addRepository(IRepository repository, boolean signalAdd, String suffix) { |
| 96 |
boolean added = false; |
| 97 |
synchronized (repositoryLock) { |
| 98 |
if (repositories == null) |
| 99 |
restoreRepositories(); |
| 100 |
String key = getKey(repository.getLocation()); |
| 101 |
RepositoryInfo info = (RepositoryInfo) repositories.get(key); |
| 102 |
if (info == null) { |
| 103 |
info = new RepositoryInfo(); |
| 104 |
added = true; |
| 105 |
repositories.put(key, info); |
| 106 |
} |
| 107 |
info.repository = new SoftReference(repository); |
| 108 |
info.name = repository.getName(); |
| 109 |
info.description = repository.getDescription(); |
| 110 |
info.location = repository.getLocation(); |
| 111 |
String value = (String) repository.getProperties().get(IRepository.PROP_SYSTEM); |
| 112 |
if (value != null) |
| 113 |
info.isSystem = Boolean.valueOf(value).booleanValue(); |
| 114 |
info.suffix = suffix; |
| 115 |
} |
| 116 |
// save the given repository in the preferences. |
| 117 |
remember(repository, suffix); |
| 118 |
if (added && signalAdd) |
| 119 |
broadcastChangeEvent(repository.getLocation(), IRepository.TYPE_METADATA, RepositoryEvent.ADDED, true); |
| 120 |
} |
| 121 |
|
| 122 |
/* (non-Javadoc) |
| 123 |
* @see org.eclipse.equinox.internal.provisional.p2.core.repository.IRepositoryManager#addRepository(java.net.URI) |
| 124 |
*/ |
| 125 |
public void addRepository(URI location) { |
| 126 |
//add the repository, or enable it if already known |
| 127 |
if (!addRepository(location, true, true)) |
| 128 |
setEnabled(location, true); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Adds the repository to the list of known repositories. |
| 133 |
* @param location The repository location |
| 134 |
* @param isEnabled Whether the repository should be enabled |
| 135 |
* @param signalAdd Whether a repository add event should be broadcast |
| 136 |
* @return <code>true</code> if the repository was actually added, and |
| 137 |
* <code>false</code> otherwise. |
| 138 |
*/ |
| 139 |
private boolean addRepository(URI location, boolean isEnabled, boolean signalAdd) { |
| 140 |
RepositoryInfo info = new RepositoryInfo(); |
| 141 |
info.location = location; |
| 142 |
info.isEnabled = isEnabled; |
| 143 |
boolean added = true; |
| 144 |
synchronized (repositoryLock) { |
| 145 |
if (repositories == null) |
| 146 |
restoreRepositories(); |
| 147 |
if (contains(location)) |
| 148 |
return false; |
| 149 |
added = repositories.put(getKey(location), info) == null; |
| 150 |
// save the given repository in the preferences. |
| 151 |
remember(info, true); |
| 152 |
} |
| 153 |
if (added && signalAdd) |
| 154 |
broadcastChangeEvent(location, getRepositoryType(), RepositoryEvent.ADDED, isEnabled); |
| 155 |
return added; |
| 156 |
} |
| 157 |
|
| 158 |
protected IRepository basicGetRepository(URI location) { |
| 159 |
synchronized (repositoryLock) { |
| 160 |
if (repositories == null) |
| 161 |
restoreRepositories(); |
| 162 |
RepositoryInfo info = (RepositoryInfo) repositories.get(getKey(location)); |
| 163 |
if (info == null || info.repository == null) |
| 164 |
return null; |
| 165 |
IRepository repo = (IRepository) info.repository.get(); |
| 166 |
//update our repository info because the repository may have changed |
| 167 |
if (repo != null) |
| 168 |
addRepository(repo, false, null); |
| 169 |
return repo; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
public IRepository basicRefreshRepository(URI location, IProgressMonitor monitor) throws ProvisionException { |
| 174 |
clearNotFound(location); |
| 175 |
boolean wasEnabled = isEnabled(location); |
| 176 |
//remove the repository so event is broadcast and repositories can clear their caches |
| 177 |
if (!removeRepository(location)) |
| 178 |
fail(location, ProvisionException.REPOSITORY_NOT_FOUND); |
| 179 |
boolean loaded = false; |
| 180 |
try { |
| 181 |
IRepository result = loadRepository(location, monitor, null, 0); |
| 182 |
loaded = true; |
| 183 |
setEnabled(location, wasEnabled); |
| 184 |
return result; |
| 185 |
} finally { |
| 186 |
//if we failed to load, make sure the repository is not lost |
| 187 |
if (!loaded) |
| 188 |
addRepository(location, wasEnabled, true); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
private void broadcastChangeEvent(URI location, int repositoryType, int kind, boolean isEnabled) { |
| 193 |
IProvisioningEventBus bus = (IProvisioningEventBus) ServiceHelper.getService(Activator.getContext(), IProvisioningEventBus.class.getName()); |
| 194 |
if (bus != null) |
| 195 |
bus.publishEvent(new RepositoryEvent(location, repositoryType, kind, isEnabled)); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Check if we recently attempted to load the given location and failed |
| 200 |
* to find anything. Returns <code>true</code> if the repository was not |
| 201 |
* found, and <code>false</code> otherwise. |
| 202 |
*/ |
| 203 |
private boolean checkNotFound(URI location) { |
| 204 |
if (unavailableRepositories == null) |
| 205 |
return false; |
| 206 |
List badRepos = (List) unavailableRepositories.get(); |
| 207 |
if (badRepos == null) |
| 208 |
return false; |
| 209 |
return badRepos.contains(location); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Clear the fact that we tried to load a repository at this location and did not find anything. |
| 214 |
*/ |
| 215 |
protected void clearNotFound(URI location) { |
| 216 |
List badRepos; |
| 217 |
if (unavailableRepositories != null) { |
| 218 |
badRepos = (List) unavailableRepositories.get(); |
| 219 |
if (badRepos != null) { |
| 220 |
badRepos.remove(location); |
| 221 |
return; |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
/* (non-Javadoc) |
| 227 |
* @see org.eclipse.equinox.internal.provisional.p2.core.repository.IRepositoryManager#contains(java.net.URI) |
| 228 |
*/ |
| 229 |
public boolean contains(URI location) { |
| 230 |
synchronized (repositoryLock) { |
| 231 |
if (repositories == null) |
| 232 |
restoreRepositories(); |
| 233 |
return repositories.containsKey(getKey(location)); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/* (non-Javadoc) |
| 238 |
* @see org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager#createRepository(java.net.URL, java.lang.String, java.lang.String, java.util.Map) |
| 239 |
*/ |
| 240 |
protected IRepository doCreateRepository(URI location, String name, String type, Map properties) throws ProvisionException { |
| 241 |
Assert.isNotNull(name); |
| 242 |
Assert.isNotNull(type); |
| 243 |
IRepository result = null; |
| 244 |
try { |
| 245 |
enterLoad(location); |
| 246 |
boolean loaded = false; |
| 247 |
try { |
| 248 |
//repository should not already exist |
| 249 |
loadRepository(location, (IProgressMonitor) null, type, 0); |
| 250 |
loaded = true; |
| 251 |
} catch (ProvisionException e) { |
| 252 |
//expected - fall through and create the new repository |
| 253 |
} |
| 254 |
if (loaded) |
| 255 |
fail(location, ProvisionException.REPOSITORY_EXISTS); |
| 256 |
|
| 257 |
IExtension extension = RegistryFactory.getRegistry().getExtension(getRepositoryProviderExtensionPointId(), type); |
| 258 |
if (extension == null) |
| 259 |
fail(location, ProvisionException.REPOSITORY_UNKNOWN_TYPE); |
| 260 |
// MetadataRepositoryFactory factory = (MetadataRepositoryFactory) createExecutableExtension(extension, EL_FACTORY); |
| 261 |
// if (factory == null) |
| 262 |
// fail(location, ProvisionException.REPOSITORY_FAILED_READ); |
| 263 |
result = factoryCreate(location, name, type, properties, extension); |
| 264 |
if (result == null) |
| 265 |
fail(location, ProvisionException.REPOSITORY_FAILED_READ); |
| 266 |
clearNotFound(location); |
| 267 |
addRepository(result, false, null); |
| 268 |
} finally { |
| 269 |
exitLoad(location); |
| 270 |
} |
| 271 |
//fire event after releasing load lock |
| 272 |
broadcastChangeEvent(location, getRepositoryType(), RepositoryEvent.ADDED, true); |
| 273 |
return result; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Returns the executable extension, or <code>null</code> if there |
| 278 |
* was no corresponding extension, or an error occurred loading it |
| 279 |
*/ |
| 280 |
protected Object createExecutableExtension(IExtension extension, String element) { |
| 281 |
IConfigurationElement[] elements = extension.getConfigurationElements(); |
| 282 |
CoreException failure = null; |
| 283 |
for (int i = 0; i < elements.length; i++) { |
| 284 |
if (elements[i].getName().equals(element)) { |
| 285 |
try { |
| 286 |
return elements[i].createExecutableExtension("class"); //$NON-NLS-1$ |
| 287 |
} catch (CoreException e) { |
| 288 |
log("Error loading repository extension: " + extension.getUniqueIdentifier(), failure); //$NON-NLS-1$ |
| 289 |
return null; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
log("Malformed repository extension: " + extension.getUniqueIdentifier(), null); //$NON-NLS-1$ |
| 294 |
return null; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Obtains an exclusive right to load a repository at the given location. Blocks |
| 299 |
* if another thread is currently loading at that location. Invocation of this |
| 300 |
* method must be followed by a subsequent call to {@link #exitLoad(URI)}. |
| 301 |
* |
| 302 |
* To avoid deadlock between the loadLock and repositoryLock, this method |
| 303 |
* must not be called when repositoryLock is held. |
| 304 |
* |
| 305 |
* @param location The location to lock |
| 306 |
*/ |
| 307 |
private void enterLoad(URI location) { |
| 308 |
Thread current = Thread.currentThread(); |
| 309 |
synchronized (loadLocks) { |
| 310 |
while (true) { |
| 311 |
Thread owner = (Thread) loadLocks.get(location); |
| 312 |
if (owner == null || current.equals(owner)) |
| 313 |
break; |
| 314 |
try { |
| 315 |
loadLocks.wait(); |
| 316 |
} catch (InterruptedException e) { |
| 317 |
//keep trying |
| 318 |
} |
| 319 |
} |
| 320 |
loadLocks.put(location, current); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Relinquishes the exclusive right to load a repository at the given location. Unblocks |
| 326 |
* other threads waiting to load at that location. |
| 327 |
* @param location The location to unlock |
| 328 |
*/ |
| 329 |
private void exitLoad(URI location) { |
| 330 |
synchronized (loadLocks) { |
| 331 |
loadLocks.remove(location); |
| 332 |
loadLocks.notifyAll(); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Creates and returns a repository using the given repository factory extension. Returns |
| 338 |
* null if no factory could be found associated with that extension. |
| 339 |
*/ |
| 340 |
protected abstract IRepository factoryCreate(URI location, String name, String type, Map properties, IExtension extension) throws ProvisionException; |
| 341 |
|
| 342 |
/** |
| 343 |
* Loads and returns a repository using the given repository factory extension. Returns |
| 344 |
* null if no factory could be found associated with that extension. |
| 345 |
*/ |
| 346 |
protected abstract IRepository factoryLoad(URI location, IExtension extension, int flags, SubMonitor monitor) throws ProvisionException; |
| 347 |
|
| 348 |
protected void fail(URI location, int code) throws ProvisionException { |
| 349 |
String msg = null; |
| 350 |
switch (code) { |
| 351 |
case ProvisionException.REPOSITORY_EXISTS : |
| 352 |
msg = NLS.bind(Messages.repoMan_exists, location); |
| 353 |
break; |
| 354 |
case ProvisionException.REPOSITORY_UNKNOWN_TYPE : |
| 355 |
msg = NLS.bind(Messages.repoMan_unknownType, location); |
| 356 |
break; |
| 357 |
case ProvisionException.REPOSITORY_FAILED_READ : |
| 358 |
msg = NLS.bind(Messages.repoMan_failedRead, location); |
| 359 |
break; |
| 360 |
case ProvisionException.REPOSITORY_NOT_FOUND : |
| 361 |
msg = NLS.bind(Messages.repoMan_notExists, location); |
| 362 |
break; |
| 363 |
} |
| 364 |
if (msg == null) |
| 365 |
msg = Messages.repoMan_internalError; |
| 366 |
throw new ProvisionException(new Status(IStatus.ERROR, getBundleId(), code, msg, null)); |
| 367 |
} |
| 368 |
|
| 369 |
protected IExtension[] findMatchingRepositoryExtensions(String suffix, String type) { |
| 370 |
IConfigurationElement[] elt = null; |
| 371 |
if (type != null && type.length() > 0) { |
| 372 |
IExtension ext = RegistryFactory.getRegistry().getExtension(getRepositoryProviderExtensionPointId(), type); |
| 373 |
elt = (ext != null) ? ext.getConfigurationElements() : new IConfigurationElement[0]; |
| 374 |
} else { |
| 375 |
elt = RegistryFactory.getRegistry().getConfigurationElementsFor(getRepositoryProviderExtensionPointId()); |
| 376 |
} |
| 377 |
int count = 0; |
| 378 |
for (int i = 0; i < elt.length; i++) { |
| 379 |
if (EL_FILTER.equals(elt[i].getName())) { |
| 380 |
if (!suffix.equals(elt[i].getAttribute(ATTR_SUFFIX))) { |
| 381 |
elt[i] = null; |
| 382 |
} else { |
| 383 |
count++; |
| 384 |
} |
| 385 |
} else { |
| 386 |
elt[i] = null; |
| 387 |
} |
| 388 |
} |
| 389 |
IExtension[] results = new IExtension[count]; |
| 390 |
for (int i = 0; i < elt.length; i++) { |
| 391 |
if (elt[i] != null) |
| 392 |
results[--count] = elt[i].getDeclaringExtension(); |
| 393 |
} |
| 394 |
return results; |
| 395 |
} |
| 396 |
|
| 397 |
protected String[] getAllSuffixes() { |
| 398 |
IConfigurationElement[] elements = RegistryFactory.getRegistry().getConfigurationElementsFor(getRepositoryProviderExtensionPointId()); |
| 399 |
ArrayList result = new ArrayList(elements.length); |
| 400 |
result.add(getDefaultSuffix()); |
| 401 |
for (int i = 0; i < elements.length; i++) { |
| 402 |
if (elements[i].getName().equals(EL_FILTER)) { |
| 403 |
String suffix = elements[i].getAttribute(ATTR_SUFFIX); |
| 404 |
if (!result.contains(suffix)) |
| 405 |
result.add(suffix); |
| 406 |
} |
| 407 |
} |
| 408 |
return (String[]) result.toArray(new String[result.size()]); |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* Returns the bundle id of the bundle that provides the concrete repository manager |
| 413 |
* @return a symbolic bundle id |
| 414 |
*/ |
| 415 |
protected abstract String getBundleId(); |
| 416 |
|
| 417 |
/** |
| 418 |
* Returns the default repository suffix. This is used to ensure a particular |
| 419 |
* repository type is preferred over all others. |
| 420 |
*/ |
| 421 |
protected abstract String getDefaultSuffix(); |
| 422 |
|
| 423 |
/* |
| 424 |
* Return a string key based on the given repository location which |
| 425 |
* is suitable for use as a preference node name. |
| 426 |
* TODO: convert local file system URI to canonical form |
| 427 |
*/ |
| 428 |
private String getKey(URI location) { |
| 429 |
String key = location.toString().replace('/', '_'); |
| 430 |
//remove trailing slash |
| 431 |
if (key.endsWith("_")) //$NON-NLS-1$ |
| 432 |
key = key.substring(0, key.length() - 1); |
| 433 |
return key; |
| 434 |
} |
| 435 |
|
| 436 |
/* (non-Javadoc) |
| 437 |
* @see org.eclipse.equinox.internal.provisional.p2.core.repository.IRepositoryManager#getKnownRepositories(int) |
| 438 |
*/ |
| 439 |
public URI[] getKnownRepositories(int flags) { |
| 440 |
synchronized (repositoryLock) { |
| 441 |
if (repositories == null) |
| 442 |
restoreRepositories(); |
| 443 |
ArrayList result = new ArrayList(); |
| 444 |
int i = 0; |
| 445 |
for (Iterator it = repositories.values().iterator(); it.hasNext(); i++) { |
| 446 |
RepositoryInfo info = (RepositoryInfo) it.next(); |
| 447 |
if (matchesFlags(info, flags)) |
| 448 |
result.add(info.location); |
| 449 |
} |
| 450 |
return (URI[]) result.toArray(new URI[result.size()]); |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
/** |
| 455 |
* Return the preference node which is the root for where we store the repository information. |
| 456 |
*/ |
| 457 |
Preferences getPreferences() { |
| 458 |
IPreferencesService prefService = (IPreferencesService) ServiceHelper.getService(Activator.getContext(), IPreferencesService.class.getName()); |
| 459 |
|
| 460 |
try { |
| 461 |
return prefService.getRootNode().node("/profile/_SELF_/" + getBundleId() + "/" + NODE_REPOSITORIES); //$NON-NLS-1$ //$NON-NLS-2$ |
| 462 |
} catch (IllegalArgumentException e) { |
| 463 |
return null; |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
/** |
| 468 |
* Restores a repository location from the preferences. |
| 469 |
*/ |
| 470 |
private URI getRepositoryLocation(Preferences node) { |
| 471 |
//prefer the location stored in URI form |
| 472 |
String locationString = node.get(KEY_URI, null); |
| 473 |
try { |
| 474 |
if (locationString != null) |
| 475 |
return new URI(locationString); |
| 476 |
} catch (URISyntaxException e) { |
| 477 |
log("Error while restoring repository: " + locationString, e); //$NON-NLS-1$ |
| 478 |
} |
| 479 |
//we used to store the repository as a URL, so try old key for backwards compatibility |
| 480 |
locationString = node.get(KEY_URL, null); |
| 481 |
try { |
| 482 |
if (locationString != null) |
| 483 |
return URIUtil.toURI(new URL(locationString)); |
| 484 |
} catch (MalformedURLException e) { |
| 485 |
log("Error while restoring repository: " + locationString, e); //$NON-NLS-1$ |
| 486 |
} catch (URISyntaxException e) { |
| 487 |
log("Error while restoring repository: " + locationString, e); //$NON-NLS-1$ |
| 488 |
} |
| 489 |
return null; |
| 490 |
} |
| 491 |
|
| 492 |
/*(non-Javadoc) |
| 493 |
* @see org.eclipse.equinox.internal.provisional.p2.core.repository.IRepositoryManager#getRepositoryProperty(java.net.URI, java.lang.String) |
| 494 |
*/ |
| 495 |
public String getRepositoryProperty(URI location, String key) { |
| 496 |
synchronized (repositoryLock) { |
| 497 |
if (repositories == null) |
| 498 |
restoreRepositories(); |
| 499 |
RepositoryInfo info = (RepositoryInfo) repositories.get(getKey(location)); |
| 500 |
if (info == null) |
| 501 |
return null;// Repository not found |
| 502 |
if (IRepository.PROP_DESCRIPTION.equals(key)) |
| 503 |
return info.description; |
| 504 |
else if (IRepository.PROP_NAME.equals(key)) |
| 505 |
return info.name; |
| 506 |
else if (IRepository.PROP_SYSTEM.equals(key)) |
| 507 |
return Boolean.toString(info.isSystem); |
| 508 |
else if (IRepository.PROP_NICKNAME.equals(key)) |
| 509 |
return info.nickname; |
| 510 |
// Key not known, return null |
| 511 |
return null; |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
/*(non-Javadoc) |
| 516 |
* @see org.eclipse.equinox.internal.provisional.p2.core.repository.IRepositoryManager#getRepositoryProperty(java.net.URI, java.lang.String) |
| 517 |
*/ |
| 518 |
public void setRepositoryProperty(URI location, String key, String value) { |
| 519 |
synchronized (repositoryLock) { |
| 520 |
if (repositories == null) |
| 521 |
restoreRepositories(); |
| 522 |
RepositoryInfo info = (RepositoryInfo) repositories.get(getKey(location)); |
| 523 |
if (info == null) |
| 524 |
return;// Repository not found |
| 525 |
if (IRepository.PROP_DESCRIPTION.equals(key)) |
| 526 |
info.description = value; |
| 527 |
else if (IRepository.PROP_NAME.equals(key)) |
| 528 |
info.name = value; |
| 529 |
else if (IRepository.PROP_NICKNAME.equals(key)) |
| 530 |
info.nickname = value; |
| 531 |
else if (IRepository.PROP_SYSTEM.equals(key)) |
| 532 |
//only true if value.equals("true") which is OK because a repository is only system if it's explicitly set to system. |
| 533 |
info.isSystem = Boolean.valueOf(value).booleanValue(); |
| 534 |
remember(info, true); |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* Returns the fully qualified id of the repository provider extension point. |
| 540 |
*/ |
| 541 |
protected abstract String getRepositoryProviderExtensionPointId(); |
| 542 |
|
| 543 |
/** |
| 544 |
* Returns the system property used to specify additional repositories to be |
| 545 |
* automatically added to the list of known repositories. |
| 546 |
*/ |
| 547 |
protected abstract String getRepositorySystemProperty(); |
| 548 |
|
| 549 |
/** |
| 550 |
* Returns the repository type stored in this manager. |
| 551 |
*/ |
| 552 |
protected abstract int getRepositoryType(); |
| 553 |
|
| 554 |
/* (non-Javadoc) |
| 555 |
* @see org.eclipse.equinox.internal.provisional.p2.core.repository.IRepositoryManager#isEnabled(java.net.URI) |
| 556 |
*/ |
| 557 |
public boolean isEnabled(URI location) { |
| 558 |
synchronized (repositoryLock) { |
| 559 |
if (repositories == null) |
| 560 |
restoreRepositories(); |
| 561 |
RepositoryInfo info = (RepositoryInfo) repositories.get(getKey(location)); |
| 562 |
if (info != null) |
| 563 |
return info.isEnabled; |
| 564 |
// Repository not found, return false |
| 565 |
return false; |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
protected IRepository loadRepository(URI location, IProgressMonitor monitor, String type, int flags) throws ProvisionException { |
| 570 |
boolean added = false; |
| 571 |
IRepository result = null; |
| 572 |
|
| 573 |
try { |
| 574 |
enterLoad(location); |
| 575 |
result = basicGetRepository(location); |
| 576 |
if (result != null) |
| 577 |
return result; |
| 578 |
if (checkNotFound(location)) |
| 579 |
fail(location, ProvisionException.REPOSITORY_NOT_FOUND); |
| 580 |
//add the repository first so that it will be enabled, but don't send add event until after the load |
| 581 |
added = addRepository(location, true, false); |
| 582 |
String[] suffixes = sortSuffixes(getAllSuffixes(), location); |
| 583 |
SubMonitor sub = SubMonitor.convert(monitor, NLS.bind(Messages.repoMan_adding, location), suffixes.length * 100); |
| 584 |
ProvisionException failure = null; |
| 585 |
try { |
| 586 |
for (int i = 0; i < suffixes.length; i++) { |
| 587 |
if (sub.isCanceled()) |
| 588 |
throw new OperationCanceledException(); |
| 589 |
try { |
| 590 |
result = loadRepository(location, suffixes[i], type, flags, sub.newChild(100)); |
| 591 |
} catch (ProvisionException e) { |
| 592 |
failure = e; |
| 593 |
break; |
| 594 |
} |
| 595 |
if (result != null) { |
| 596 |
addRepository(result, false, suffixes[i]); |
| 597 |
break; |
| 598 |
} |
| 599 |
} |
| 600 |
} finally { |
| 601 |
sub.done(); |
| 602 |
} |
| 603 |
if (result == null) { |
| 604 |
//if we just added the repository, remove it because it cannot be loaded |
| 605 |
if (added) |
| 606 |
removeRepository(location, false); |
| 607 |
//eagerly cleanup missing system repositories |
| 608 |
if (Boolean.valueOf(getRepositoryProperty(location, IRepository.PROP_SYSTEM)).booleanValue()) |
| 609 |
removeRepository(location); |
| 610 |
else |
| 611 |
rememberNotFound(location); |
| 612 |
if (failure != null) |
| 613 |
throw failure; |
| 614 |
fail(location, ProvisionException.REPOSITORY_NOT_FOUND); |
| 615 |
} |
| 616 |
} finally { |
| 617 |
exitLoad(location); |
| 618 |
} |
| 619 |
//broadcast the add event after releasing lock |
| 620 |
if (added) |
| 621 |
broadcastChangeEvent(location, IRepository.TYPE_METADATA, RepositoryEvent.ADDED, true); |
| 622 |
return result; |
| 623 |
} |
| 624 |
|
| 625 |
private IRepository loadRepository(URI location, String suffix, String type, int flags, SubMonitor monitor) throws ProvisionException { |
| 626 |
IExtension[] providers = findMatchingRepositoryExtensions(suffix, type); |
| 627 |
// Loop over the candidates and return the first one that successfully loads |
| 628 |
monitor.beginTask("", providers.length * 10); //$NON-NLS-1$ |
| 629 |
for (int i = 0; i < providers.length; i++) |
| 630 |
try { |
| 631 |
IRepository repo = factoryLoad(location, providers[i], flags, monitor); |
| 632 |
if (repo != null) |
| 633 |
return repo; |
| 634 |
} catch (ProvisionException e) { |
| 635 |
if (e.getStatus().getCode() != ProvisionException.REPOSITORY_NOT_FOUND) |
| 636 |
throw e; |
| 637 |
} catch (Exception e) { |
| 638 |
//catch and log unexpected errors and move onto the next factory |
| 639 |
log("Unexpected error loading extension: " + providers[i].getUniqueIdentifier(), e); //$NON-NLS-1$ |
| 640 |
} catch (LinkageError e) { |
| 641 |
//catch and log unexpected errors and move onto the next factory |
| 642 |
log("Unexpected error loading extension: " + providers[i].getUniqueIdentifier(), e); //$NON-NLS-1$ |
| 643 |
} |
| 644 |
return null; |
| 645 |
} |
| 646 |
|
| 647 |
protected void log(String message, Throwable t) { |
| 648 |
LogHelper.log(new Status(IStatus.ERROR, getBundleId(), message, t)); |
| 649 |
} |
| 650 |
|
| 651 |
private boolean matchesFlags(RepositoryInfo info, int flags) { |
| 652 |
if ((flags & REPOSITORIES_SYSTEM) == REPOSITORIES_SYSTEM) |
| 653 |
if (!info.isSystem) |
| 654 |
return false; |
| 655 |
if ((flags & REPOSITORIES_NON_SYSTEM) == REPOSITORIES_NON_SYSTEM) |
| 656 |
if (info.isSystem) |
| 657 |
return false; |
| 658 |
if ((flags & REPOSITORIES_DISABLED) == REPOSITORIES_DISABLED) { |
| 659 |
if (info.isEnabled) |
| 660 |
return false; |
| 661 |
} else { |
| 662 |
//ignore disabled repositories for all other flag types |
| 663 |
if (!info.isEnabled) |
| 664 |
return false; |
| 665 |
} |
| 666 |
if ((flags & REPOSITORIES_LOCAL) == REPOSITORIES_LOCAL) |
| 667 |
return "file".equals(info.location.getScheme()); //$NON-NLS-1$ |
| 668 |
return true; |
| 669 |
} |
| 670 |
|
| 671 |
/*(non-Javadoc) |
| 672 |
* @see org.eclipse.equinox.internal.provisional.p2.core.eventbus.ProvisioningListener#notify(java.util.EventObject) |
| 673 |
*/ |
| 674 |
public void notify(EventObject o) { |
| 675 |
if (o instanceof RepositoryEvent) { |
| 676 |
RepositoryEvent event = (RepositoryEvent) o; |
| 677 |
if (event.getKind() == RepositoryEvent.DISCOVERED && event.getRepositoryType() == getRepositoryType()) |
| 678 |
addRepository(event.getRepositoryLocation(), event.isRepositoryEnabled(), true); |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
/** |
| 683 |
* Sets a preference and returns <code>true</code> if the preference |
| 684 |
* was actually changed. |
| 685 |
*/ |
| 686 |
protected boolean putValue(Preferences node, String key, String newValue) { |
| 687 |
String oldValue = node.get(key, null); |
| 688 |
if (oldValue == newValue || (oldValue != null && oldValue.equals(newValue))) |
| 689 |
return false; |
| 690 |
if (newValue == null) |
| 691 |
node.remove(key); |
| 692 |
else |
| 693 |
node.put(key, newValue); |
| 694 |
return true; |
| 695 |
} |
| 696 |
|
| 697 |
/* |
| 698 |
* Add the given repository object to the preferences and save. |
| 699 |
*/ |
| 700 |
private void remember(IRepository repository, String suffix) { |
| 701 |
boolean changed = false; |
| 702 |
Preferences node = getPreferences(); |
| 703 |
// Ensure we retrieved preferences |
| 704 |
if (node == null) |
| 705 |
return; |
| 706 |
node = node.node(getKey(repository.getLocation())); |
| 707 |
|
| 708 |
try { |
| 709 |
changed |= putValue(node, KEY_URI, repository.getLocation().toString()); |
| 710 |
changed |= putValue(node, KEY_URL, null); |
| 711 |
changed |= putValue(node, KEY_DESCRIPTION, repository.getDescription()); |
| 712 |
changed |= putValue(node, KEY_NAME, repository.getName()); |
| 713 |
changed |= putValue(node, KEY_PROVIDER, repository.getProvider()); |
| 714 |
changed |= putValue(node, KEY_TYPE, repository.getType()); |
| 715 |
changed |= putValue(node, KEY_VERSION, repository.getVersion()); |
| 716 |
changed |= putValue(node, KEY_SYSTEM, (String) repository.getProperties().get(IRepository.PROP_SYSTEM)); |
| 717 |
changed |= putValue(node, KEY_SUFFIX, suffix); |
| 718 |
if (changed) |
| 719 |
saveToPreferences(); |
| 720 |
} catch (IllegalStateException e) { |
| 721 |
//the repository was removed concurrently, so we don't need to save it |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
/** |
| 726 |
* Writes the state of the repository information into the appropriate preference node. |
| 727 |
* |
| 728 |
* @param info The info to write to the preference node |
| 729 |
* @param flush <code>true</code> if the preference node should be flushed to |
| 730 |
* disk, and <code>false</code> otherwise |
| 731 |
*/ |
| 732 |
private boolean remember(RepositoryInfo info, boolean flush) { |
| 733 |
boolean changed = false; |
| 734 |
Preferences node = getPreferences(); |
| 735 |
// Ensure we retrieved preferences |
| 736 |
if (node == null) |
| 737 |
return changed; |
| 738 |
node = node.node(getKey(info.location)); |
| 739 |
try { |
| 740 |
changed |= putValue(node, KEY_URI, info.location.toString()); |
| 741 |
changed |= putValue(node, KEY_URL, null); |
| 742 |
changed |= putValue(node, KEY_SYSTEM, Boolean.toString(info.isSystem)); |
| 743 |
changed |= putValue(node, KEY_DESCRIPTION, info.description); |
| 744 |
changed |= putValue(node, KEY_NAME, info.name); |
| 745 |
changed |= putValue(node, KEY_NICKNAME, info.nickname); |
| 746 |
changed |= putValue(node, KEY_SUFFIX, info.suffix); |
| 747 |
changed |= putValue(node, KEY_ENABLED, Boolean.toString(info.isEnabled)); |
| 748 |
if (changed && flush) |
| 749 |
saveToPreferences(); |
| 750 |
return changed; |
| 751 |
} catch (IllegalStateException e) { |
| 752 |
//the repository was removed concurrently, so we don't need to save it |
| 753 |
return false; |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
/** |
| 758 |
* Cache the fact that we tried to load a repository at this location and did not find anything. |
| 759 |
*/ |
| 760 |
private void rememberNotFound(URI location) { |
| 761 |
List badRepos; |
| 762 |
if (unavailableRepositories != null) { |
| 763 |
badRepos = (List) unavailableRepositories.get(); |
| 764 |
if (badRepos != null) { |
| 765 |
badRepos.add(location); |
| 766 |
return; |
| 767 |
} |
| 768 |
} |
| 769 |
badRepos = new ArrayList(); |
| 770 |
badRepos.add(location); |
| 771 |
unavailableRepositories = new SoftReference(badRepos); |
| 772 |
} |
| 773 |
|
| 774 |
public boolean removeRepository(URI toRemove) { |
| 775 |
return removeRepository(toRemove, true); |
| 776 |
} |
| 777 |
|
| 778 |
private boolean removeRepository(URI toRemove, boolean signalRemove) { |
| 779 |
Assert.isNotNull(toRemove); |
| 780 |
final String repoKey = getKey(toRemove); |
| 781 |
synchronized (repositoryLock) { |
| 782 |
if (repositories == null) |
| 783 |
restoreRepositories(); |
| 784 |
if (repositories.remove(repoKey) == null) |
| 785 |
return false; |
| 786 |
} |
| 787 |
// remove the repository from the preference store |
| 788 |
try { |
| 789 |
if (Tracing.DEBUG_REMOVE_REPO) { |
| 790 |
String msg = "Removing repository: " + toRemove; //$NON-NLS-1$ |
| 791 |
Tracing.debug(msg); |
| 792 |
new Exception(msg).printStackTrace(); |
| 793 |
} |
| 794 |
Preferences node = getPreferences(); |
| 795 |
if (node != null) { |
| 796 |
getPreferences().node(repoKey).removeNode(); |
| 797 |
saveToPreferences(); |
| 798 |
} |
| 799 |
clearNotFound(toRemove); |
| 800 |
} catch (BackingStoreException e) { |
| 801 |
log("Error saving preferences", e); //$NON-NLS-1$ |
| 802 |
} |
| 803 |
//TODO: compute and pass appropriate isEnabled flag |
| 804 |
if (signalRemove) |
| 805 |
broadcastChangeEvent(toRemove, getRepositoryType(), RepositoryEvent.REMOVED, true); |
| 806 |
return true; |
| 807 |
} |
| 808 |
|
| 809 |
/* |
| 810 |
* Load the list of repositories from the preferences. |
| 811 |
*/ |
| 812 |
private void restoreFromPreferences() { |
| 813 |
// restore the list of repositories from the preference store |
| 814 |
Preferences node = getPreferences(); |
| 815 |
if (node == null) |
| 816 |
return; |
| 817 |
String[] children; |
| 818 |
try { |
| 819 |
children = node.childrenNames(); |
| 820 |
} catch (BackingStoreException e) { |
| 821 |
log("Error restoring repositories from preferences", e); //$NON-NLS-1$ |
| 822 |
return; |
| 823 |
} |
| 824 |
for (int i = 0; i < children.length; i++) { |
| 825 |
Preferences child = node.node(children[i]); |
| 826 |
URI location = getRepositoryLocation(child); |
| 827 |
if (location == null) |
| 828 |
continue; |
| 829 |
RepositoryInfo info = new RepositoryInfo(); |
| 830 |
info.location = location; |
| 831 |
info.name = child.get(KEY_NAME, null); |
| 832 |
info.nickname = child.get(KEY_NICKNAME, null); |
| 833 |
info.description = child.get(KEY_DESCRIPTION, null); |
| 834 |
info.isSystem = child.getBoolean(KEY_SYSTEM, false); |
| 835 |
info.isEnabled = child.getBoolean(KEY_ENABLED, true); |
| 836 |
info.suffix = child.get(KEY_SUFFIX, null); |
| 837 |
repositories.put(getKey(info.location), info); |
| 838 |
} |
| 839 |
// now that we have loaded everything, remember them |
| 840 |
saveToPreferences(); |
| 841 |
} |
| 842 |
|
| 843 |
private void restoreFromSystemProperty() { |
| 844 |
String locationString = Activator.getContext().getProperty(getRepositorySystemProperty()); |
| 845 |
if (locationString != null) { |
| 846 |
StringTokenizer tokenizer = new StringTokenizer(locationString, ","); //$NON-NLS-1$ |
| 847 |
while (tokenizer.hasMoreTokens()) { |
| 848 |
try { |
| 849 |
addRepository(new URI(tokenizer.nextToken()), true, true); |
| 850 |
} catch (URISyntaxException e) { |
| 851 |
log("Error while restoring repository " + locationString, e); //$NON-NLS-1$ |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
/** |
| 858 |
* Restores the repository list. |
| 859 |
*/ |
| 860 |
private void restoreRepositories() { |
| 861 |
synchronized (repositoryLock) { |
| 862 |
repositories = new HashMap(); |
| 863 |
restoreSpecialRepositories(); |
| 864 |
restoreFromSystemProperty(); |
| 865 |
restoreFromPreferences(); |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
/** |
| 870 |
* Hook method to restore special additional repositories. |
| 871 |
*/ |
| 872 |
protected void restoreSpecialRepositories() { |
| 873 |
//by default no special repositories |
| 874 |
} |
| 875 |
|
| 876 |
/* |
| 877 |
* Save the list of repositories to the file-system. |
| 878 |
*/ |
| 879 |
private void saveToPreferences() { |
| 880 |
try { |
| 881 |
Preferences node = getPreferences(); |
| 882 |
if (node != null) |
| 883 |
node.flush(); |
| 884 |
} catch (BackingStoreException e) { |
| 885 |
log("Error while saving repositories in preferences", e); //$NON-NLS-1$ |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
/* (non-Javadoc) |
| 890 |
* @see org.eclipse.equinox.internal.provisional.p2.core.repository.IRepositoryManager#setEnabled(java.net.URI, boolean) |
| 891 |
*/ |
| 892 |
public void setEnabled(URI location, boolean enablement) { |
| 893 |
synchronized (repositoryLock) { |
| 894 |
if (repositories == null) |
| 895 |
restoreRepositories(); |
| 896 |
RepositoryInfo info = (RepositoryInfo) repositories.get(getKey(location)); |
| 897 |
if (info == null || info.isEnabled == enablement) |
| 898 |
return; |
| 899 |
info.isEnabled = enablement; |
| 900 |
remember(info, true); |
| 901 |
} |
| 902 |
broadcastChangeEvent(location, getRepositoryType(), RepositoryEvent.ENABLEMENT, enablement); |
| 903 |
} |
| 904 |
|
| 905 |
/** |
| 906 |
* Shuts down the repository manager. |
| 907 |
*/ |
| 908 |
public void shutdown() { |
| 909 |
IProvisioningEventBus bus = (IProvisioningEventBus) ServiceHelper.getService(Activator.getContext(), IProvisioningEventBus.SERVICE_NAME); |
| 910 |
if (bus != null) |
| 911 |
bus.removeListener(this); |
| 912 |
//ensure all repository state in memory is written to disk |
| 913 |
boolean changed = false; |
| 914 |
synchronized (repositoryLock) { |
| 915 |
if (repositories != null) { |
| 916 |
for (Iterator it = repositories.values().iterator(); it.hasNext();) { |
| 917 |
RepositoryInfo info = (RepositoryInfo) it.next(); |
| 918 |
changed |= remember(info, false); |
| 919 |
} |
| 920 |
} |
| 921 |
} |
| 922 |
if (changed) { |
| 923 |
if (Tracing.DEBUG) |
| 924 |
Tracing.debug("Unsaved preferences when shutting down " + getClass().getName()); //$NON-NLS-1$ |
| 925 |
saveToPreferences(); |
| 926 |
} |
| 927 |
repositories = null; |
| 928 |
unavailableRepositories = null; |
| 929 |
} |
| 930 |
|
| 931 |
/** |
| 932 |
* Optimize the order in which repository suffixes are searched by trying |
| 933 |
* the last successfully loaded suffix first. |
| 934 |
*/ |
| 935 |
private String[] sortSuffixes(String[] suffixes, URI location) { |
| 936 |
synchronized (repositoryLock) { |
| 937 |
if (repositories == null) |
| 938 |
restoreRepositories(); |
| 939 |
RepositoryInfo info = (RepositoryInfo) repositories.get(getKey(location)); |
| 940 |
if (info == null || info.suffix == null) |
| 941 |
return suffixes; |
| 942 |
//move lastSuffix to the front of the list but preserve order of remaining entries |
| 943 |
String lastSuffix = info.suffix; |
| 944 |
for (int i = 0; i < suffixes.length; i++) { |
| 945 |
if (lastSuffix.equals(suffixes[i])) { |
| 946 |
System.arraycopy(suffixes, 0, suffixes, 1, i); |
| 947 |
suffixes[0] = lastSuffix; |
| 948 |
return suffixes; |
| 949 |
} |
| 950 |
} |
| 951 |
} |
| 952 |
return suffixes; |
| 953 |
} |
| 954 |
|
| 955 |
} |