use of org.gradle.internal.resource.ExternalResourceName in project gradle by gradle.
the class MavenVersionLister method listVersions.
public void listVersions(ModuleIdentifier module, List<ResourcePattern> patterns, BuildableModuleVersionListingResolveResult result) {
final Set<ExternalResourceName> searched = Sets.newHashSet();
List<String> versions = Lists.newArrayList();
boolean hasResult = false;
for (ResourcePattern pattern : patterns) {
ExternalResourceName metadataLocation = pattern.toModulePath(module).resolve("maven-metadata.xml");
if (searched.add(metadataLocation)) {
result.attempted(metadataLocation);
try {
MavenMetadata mavenMetaData = mavenMetadataLoader.load(metadataLocation);
versions.addAll(mavenMetaData.versions);
hasResult = true;
} catch (MissingResourceException e) {
// Continue
}
}
}
if (hasResult) {
result.listed(versions);
}
}
use of org.gradle.internal.resource.ExternalResourceName in project gradle by gradle.
the class UrlExternalResource method open.
public static ExternalResource open(URL url) throws IOException {
URI uri;
try {
uri = url.toURI();
} catch (URISyntaxException e) {
throw UncheckedException.throwAsUncheckedException(e);
}
UrlExternalResource connector = new UrlExternalResource();
return new AccessorBackedExternalResource(new ExternalResourceName(uri), connector, connector, connector, false);
}
use of org.gradle.internal.resource.ExternalResourceName in project gradle by gradle.
the class PublicKeyDownloadService method toQuery.
private ExternalResourceName toQuery(URI baseUri, String fingerprint) throws URISyntaxException {
String scheme = baseUri.getScheme();
int port = baseUri.getPort();
if ("hkp".equals(scheme)) {
scheme = "http";
port = 11371;
}
return new ExternalResourceName(new URI(scheme, null, baseUri.getHost(), port, "/pks/lookup", "op=get&options=mr&search=0x" + fingerprint, null));
}
use of org.gradle.internal.resource.ExternalResourceName in project gradle by gradle.
the class PublicKeyDownloadService method tryDownloadKeyFromServer.
@SuppressWarnings("OptionalAssignedToNull")
private void tryDownloadKeyFromServer(String fingerprint, List<URI> baseUris, PublicKeyResultBuilder builder, Consumer<? super PGPPublicKeyRing> onKeyring) {
Deque<URI> serversLeft = new ArrayDeque<>(baseUris);
try {
ExponentialBackoff<ExponentialBackoff.Signal> backoff = ExponentialBackoff.of(5, TimeUnit.SECONDS, 50, TimeUnit.MILLISECONDS);
backoff.retryUntil(() -> {
URI baseUri = serversLeft.poll();
if (baseUri == null) {
// no more servers left despite retries
return IOQuery.Result.successful(false);
}
try {
ExternalResourceName query = toQuery(baseUri, fingerprint);
ExternalResourceReadResult<IOQuery.Result<Boolean>> response = client.resource(query).withContentIfPresent(inputStream -> {
extractKeyRing(inputStream, builder, onKeyring);
return IOQuery.Result.successful(true);
});
if (response != null) {
return response.getResult();
} else {
logKeyDownloadAttempt(fingerprint, baseUri);
// null means the resource is missing from this repo
}
} catch (Exception e) {
logKeyDownloadAttempt(fingerprint, baseUri);
// add for retry
serversLeft.add(baseUri);
}
// retry
return IOQuery.Result.notSuccessful(false);
});
} catch (InterruptedException | IOException e) {
throw UncheckedException.throwAsUncheckedException(e);
}
}
use of org.gradle.internal.resource.ExternalResourceName in project gradle by gradle.
the class ResourceVersionLister method newVisitor.
public VersionPatternVisitor newVisitor(final ModuleIdentifier module, final Collection<String> dest, final ResourceAwareResolveResult result) {
return new VersionPatternVisitor() {
final Set<ExternalResourceName> directories = new HashSet<ExternalResourceName>();
public void visit(ResourcePattern pattern, IvyArtifactName artifact) throws ResourceException {
ExternalResourceName versionListPattern = pattern.toVersionListPattern(module, artifact);
LOGGER.debug("Listing all in {}", versionListPattern);
try {
List<String> versionStrings = listRevisionToken(versionListPattern);
for (String versionString : versionStrings) {
dest.add(versionString);
}
} catch (Exception e) {
throw ResourceExceptions.failure(versionListPattern.getUri(), String.format("Could not list versions using %s.", pattern), e);
}
}
// lists all the values a revision token listed by a given url lister
private List<String> listRevisionToken(ExternalResourceName versionListPattern) {
String pattern = versionListPattern.getPath();
if (!pattern.contains(REVISION_TOKEN)) {
LOGGER.debug("revision token not defined in pattern {}.", pattern);
return Collections.emptyList();
}
String prefix = pattern.substring(0, pattern.indexOf(REVISION_TOKEN));
if (revisionMatchesDirectoryName(pattern)) {
ExternalResourceName parent = versionListPattern.getRoot().resolve(prefix);
return listAll(parent);
} else {
int parentFolderSlashIndex = prefix.lastIndexOf(fileSeparator);
String revisionParentFolder = parentFolderSlashIndex == -1 ? "" : prefix.substring(0, parentFolderSlashIndex + 1);
ExternalResourceName parent = versionListPattern.getRoot().resolve(revisionParentFolder);
LOGGER.debug("using {} to list all in {} ", repository, revisionParentFolder);
if (!directories.add(parent)) {
return Collections.emptyList();
}
result.attempted(parent);
List<String> all = repository.list(parent.getUri());
if (all == null) {
return Collections.emptyList();
}
LOGGER.debug("found {} urls", all.size());
Pattern regexPattern = createRegexPattern(pattern, parentFolderSlashIndex);
List<String> ret = filterMatchedValues(all, regexPattern);
LOGGER.debug("{} matched {}", ret.size(), pattern);
return ret;
}
}
private List<String> filterMatchedValues(List<String> all, final Pattern p) {
List<String> ret = new ArrayList<String>(all.size());
for (String path : all) {
Matcher m = p.matcher(path);
if (m.matches()) {
String value = m.group(1);
ret.add(value);
}
}
return ret;
}
private Pattern createRegexPattern(String pattern, int prefixLastSlashIndex) {
int endNameIndex = pattern.indexOf(fileSeparator, prefixLastSlashIndex + 1);
String namePattern;
if (endNameIndex != -1) {
namePattern = pattern.substring(prefixLastSlashIndex + 1, endNameIndex);
} else {
namePattern = pattern.substring(prefixLastSlashIndex + 1);
}
namePattern = namePattern.replaceAll("\\.", "\\\\.");
String acceptNamePattern = namePattern.replaceAll("\\[revision\\]", "(.+)");
return Pattern.compile(acceptNamePattern);
}
private boolean revisionMatchesDirectoryName(String pattern) {
int startToken = pattern.indexOf(REVISION_TOKEN);
if (startToken > 0 && !pattern.substring(startToken - 1, startToken).equals(fileSeparator)) {
// previous character is not a separator
return false;
}
int endToken = startToken + REV_TOKEN_LENGTH;
if (endToken < pattern.length() && !pattern.substring(endToken, endToken + 1).equals(fileSeparator)) {
// next character is not a separator
return false;
}
return true;
}
private List<String> listAll(ExternalResourceName parent) {
if (!directories.add(parent)) {
return Collections.emptyList();
}
LOGGER.debug("using {} to list all in {}", repository, parent);
result.attempted(parent.toString());
List<String> paths = repository.list(parent.getUri());
if (paths == null) {
return Collections.emptyList();
}
LOGGER.debug("found {} resources", paths.size());
return paths;
}
};
}
Aggregations