| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2007, 2010 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 |
*******************************************************************************/ |
| 14 |
package org.eclipse.equinox.p2.operations; |
| 15 |
|
| 16 |
import java.io.File; |
| 17 |
import java.net.URI; |
| 18 |
import java.net.URISyntaxException; |
| 19 |
import java.util.*; |
| 20 |
import org.eclipse.core.runtime.*; |
| 21 |
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; |
| 22 |
import org.eclipse.equinox.internal.p2.operations.*; |
| 23 |
import org.eclipse.equinox.internal.p2.repository.helpers.RepositoryHelper; |
| 24 |
import org.eclipse.equinox.p2.core.ProvisionException; |
| 25 |
import org.eclipse.equinox.p2.repository.IRepositoryManager; |
| 26 |
import org.eclipse.osgi.util.NLS; |
| 27 |
|
| 28 |
/** |
| 29 |
* RepositoryTracker defines a service that retrieves repositories, tracks their status, and |
| 30 |
* reports errors. |
| 31 |
* |
| 32 |
* @since 2.0 |
| 33 |
*/ |
| 34 |
public abstract class RepositoryTracker { |
| 35 |
|
| 36 |
/** |
| 37 |
* A status code used to indicate that a repository location was not valid. |
| 38 |
*/ |
| 39 |
public static final int STATUS_INVALID_REPOSITORY_LOCATION = IStatusCodes.INVALID_REPOSITORY_LOCATION; |
| 40 |
|
| 41 |
// What repositories to show |
| 42 |
private int artifactRepositoryFlags = IRepositoryManager.REPOSITORIES_NON_SYSTEM; |
| 43 |
private int metadataRepositoryFlags = IRepositoryManager.REPOSITORIES_NON_SYSTEM; |
| 44 |
/** |
| 45 |
* List<URI> of repositories that have already been reported to the user as not found. |
| 46 |
*/ |
| 47 |
private final List<URI> reposNotFound = Collections.synchronizedList(new ArrayList<>()); |
| 48 |
|
| 49 |
/** |
| 50 |
* Return an array of repository locations known for the specified provisioning session. |
| 51 |
* |
| 52 |
* @param session the provisioning session providing the provisioning services |
| 53 |
* @return an array of repository locations known by this tracker |
| 54 |
*/ |
| 55 |
public abstract URI[] getKnownRepositories(ProvisioningSession session); |
| 56 |
|
| 57 |
/** |
| 58 |
* Return a status appropriate for reporting an invalid repository location. |
| 59 |
* @param locationText the text representation of the location |
| 60 |
* @return a status that describes an invalid location |
| 61 |
*/ |
| 62 |
public IStatus getInvalidLocationStatus(String locationText) { |
| 63 |
return new Status(IStatus.ERROR, Constants.BUNDLE_ID, IStatusCodes.INVALID_REPOSITORY_LOCATION, NLS.bind(Messages.RepositoryTracker_InvalidLocation, locationText), null); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Return a repository location represented by the supplied string. The provided |
| 68 |
* string should either be an unencoded string representation of a URI, or a |
| 69 |
* local file system path. This method is generally suitable for converting a |
| 70 |
* location string entered by an end user into a suitable URI representation. |
| 71 |
* |
| 72 |
* @param locationString a text representation of the location |
| 73 |
* @return a repository location URI, or <code>null</code> if the |
| 74 |
* text could not be interpreted. |
| 75 |
*/ |
| 76 |
public URI locationFromString(String locationString) { |
| 77 |
URI userLocation; |
| 78 |
try { |
| 79 |
userLocation = URIUtil.fromString(locationString); |
| 80 |
} catch (URISyntaxException e) { |
| 81 |
return null; |
| 82 |
} |
| 83 |
// If a path separator char was used, interpret as a local file URI |
| 84 |
String uriString = URIUtil.toUnencodedString(userLocation); |
| 85 |
if (uriString.length() > 0 && (uriString.charAt(0) == '/' || uriString.charAt(0) == File.separatorChar)) |
| 86 |
return RepositoryHelper.localRepoURIHelper(userLocation); |
| 87 |
return userLocation; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Validate the specified repository location. |
| 92 |
* |
| 93 |
* @param session the provisioning session providing the repository services |
| 94 |
* @param location the location in question |
| 95 |
* @param contactRepositories <code>true</code> if the appropriate repository manager(s) should be |
| 96 |
* consulted regarding the validity of the location, or <code>false</code> if the repository manager |
| 97 |
* should not be consulted. |
| 98 |
* @param monitor the progress monitor |
| 99 |
* @return a status indicating the current status of the repository |
| 100 |
*/ |
| 101 |
|
| 102 |
public IStatus validateRepositoryLocation(ProvisioningSession session, URI location, boolean contactRepositories, IProgressMonitor monitor) { |
| 103 |
// First validate syntax issues |
| 104 |
IStatus localValidationStatus = RepositoryHelper.checkRepositoryLocationSyntax(location); |
| 105 |
if (!localValidationStatus.isOK()) { |
| 106 |
// bad syntax, but it could just be non-absolute. |
| 107 |
// In this case, use the helper |
| 108 |
String locationString = URIUtil.toUnencodedString(location); |
| 109 |
if (locationString.length() > 0 && (locationString.charAt(0) == '/' || locationString.charAt(0) == File.separatorChar)) { |
| 110 |
location = RepositoryHelper.localRepoURIHelper(location); |
| 111 |
localValidationStatus = RepositoryHelper.checkRepositoryLocationSyntax(location); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
if (!localValidationStatus.isOK()) |
| 116 |
return localValidationStatus; |
| 117 |
|
| 118 |
return Status.OK_STATUS; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Return a boolean indicating whether this tracker already contains the specified |
| 123 |
* repository location. |
| 124 |
* |
| 125 |
* @param location the location in question |
| 126 |
* @param session the provisioning session providing the repository services |
| 127 |
* @since 2.1 |
| 128 |
*/ |
| 129 |
protected boolean contains(URI location, ProvisioningSession session) { |
| 130 |
// This is a fallback implementation in the absence of a repository tracker |
| 131 |
// that would know what to do. |
| 132 |
URI[] knownRepositories = getKnownRepositories(session); |
| 133 |
for (URI repository : knownRepositories) { |
| 134 |
if (URIUtil.sameURI(repository, location)) { |
| 135 |
return true; |
| 136 |
} |
| 137 |
} |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Add the specified location to the list of "not found" repositories. |
| 143 |
* This list is used to ensure that errors are not reported multiple times |
| 144 |
* for the same repository. |
| 145 |
* |
| 146 |
* The caller is already assumed to have reported any errors if necessary. |
| 147 |
* |
| 148 |
* @param location the location of the repository that cannot be found |
| 149 |
*/ |
| 150 |
public void addNotFound(URI location) { |
| 151 |
reposNotFound.add(location); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Answer a boolean indicating whether not found status has already been |
| 156 |
* reported for the specified location. |
| 157 |
* |
| 158 |
* @param location the location in question |
| 159 |
* @return <code>true</code> if the repository has already been reported as |
| 160 |
* being not found, <code>false</code> if no status has been reported for this |
| 161 |
* location. |
| 162 |
*/ |
| 163 |
public boolean hasNotFoundStatusBeenReported(URI location) { |
| 164 |
// We don't check for things like case variants or end slash variants |
| 165 |
// because we know that the repository managers already did this. |
| 166 |
return reposNotFound.contains(location); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Clear the list of repositories that have already been reported as not found. |
| 171 |
*/ |
| 172 |
public void clearRepositoriesNotFound() { |
| 173 |
reposNotFound.clear(); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Remove the specified repository from the list of repositories that |
| 178 |
* have already been reported as not found. This method has no effect |
| 179 |
* if the repository has never been reported as not found. |
| 180 |
* |
| 181 |
* @param location the location in question |
| 182 |
*/ |
| 183 |
public void clearRepositoryNotFound(URI location) { |
| 184 |
reposNotFound.remove(location); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Return the repository flags suitable for retrieving known repositories from |
| 189 |
* a repository manager |
| 190 |
* |
| 191 |
* @return the repository flags |
| 192 |
* |
| 193 |
*/ |
| 194 |
public int getArtifactRepositoryFlags() { |
| 195 |
return artifactRepositoryFlags; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Set the repository flags suitable for retrieving known repositories from |
| 200 |
* a repository manager |
| 201 |
* |
| 202 |
* @param flags the repository flags |
| 203 |
* |
| 204 |
*/ |
| 205 |
public void setArtifactRepositoryFlags(int flags) { |
| 206 |
artifactRepositoryFlags = flags; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Return the repository flags suitable for retrieving known repositories from |
| 211 |
* a repository manager |
| 212 |
* |
| 213 |
* @return the repository flags |
| 214 |
* |
| 215 |
*/ |
| 216 |
public int getMetadataRepositoryFlags() { |
| 217 |
return metadataRepositoryFlags; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Set the repository flags suitable for retrieving known repositories from |
| 222 |
* a repository manager |
| 223 |
* |
| 224 |
* @param flags the repository flags |
| 225 |
* |
| 226 |
*/ |
| 227 |
|
| 228 |
public void setMetadataRepositoryFlags(int flags) { |
| 229 |
metadataRepositoryFlags = flags; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Report a failure to load the specified repository. |
| 234 |
* <p> |
| 235 |
* This default implementation simply logs the failure. Subclasses may override |
| 236 |
* to provide additional error reporting. |
| 237 |
* </p> |
| 238 |
* @param location the location of the failed repository |
| 239 |
* @param exception the failure that occurred |
| 240 |
*/ |
| 241 |
public void reportLoadFailure(final URI location, ProvisionException exception) { |
| 242 |
// special handling when the repo location is bad. We don't want to continually report it |
| 243 |
int code = exception.getStatus().getCode(); |
| 244 |
if (code == IStatusCodes.INVALID_REPOSITORY_LOCATION || code == ProvisionException.REPOSITORY_INVALID_LOCATION || code == ProvisionException.REPOSITORY_NOT_FOUND) { |
| 245 |
if (hasNotFoundStatusBeenReported(location)) |
| 246 |
return; |
| 247 |
addNotFound(location); |
| 248 |
} |
| 249 |
|
| 250 |
LogHelper.log(exception.getStatus()); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Add a repository at the specified location. |
| 255 |
* |
| 256 |
* @param location the location of the new repository |
| 257 |
* @param nickname the nickname for the repository, or <code>null</code> if there is no nickname |
| 258 |
* @param session the session to use for provisioning services |
| 259 |
*/ |
| 260 |
public abstract void addRepository(URI location, String nickname, ProvisioningSession session); |
| 261 |
|
| 262 |
/** |
| 263 |
* Remove the repositories at the specified locations |
| 264 |
* |
| 265 |
* @param locations the locations |
| 266 |
* @param session the session to use for provisioning services |
| 267 |
*/ |
| 268 |
public abstract void removeRepositories(URI[] locations, ProvisioningSession session); |
| 269 |
|
| 270 |
/** |
| 271 |
* Refresh the repositories at the specified locations |
| 272 |
* @param locations the locations |
| 273 |
* @param session the session to use for provisioning services |
| 274 |
* @param monitor the progress monitor to use |
| 275 |
*/ |
| 276 |
public abstract void refreshRepositories(URI[] locations, ProvisioningSession session, IProgressMonitor monitor); |
| 277 |
} |