use of org.apache.ivy.plugins.version.VersionMatcher in project ant-ivy by apache.
the class BasicResolver method findResource.
/**
* When the resolver has many choices, this function helps choosing one
*
* @param rress
* the list of resolved resource which the resolver found to fit the requirement
* @param rmdparser
* the parser of module descriptor
* @param mrid
* the module being resolved
* @param date
* the current date
* @return the selected resource
*/
public ResolvedResource findResource(ResolvedResource[] rress, ResourceMDParser rmdparser, ModuleRevisionId mrid, Date date) {
String name = getName();
VersionMatcher versionMatcher = getSettings().getVersionMatcher();
ResolvedResource found = null;
List<ArtifactInfo> sorted = getLatestStrategy().sort(rress);
List<String> rejected = new ArrayList<>();
List<ModuleRevisionId> foundBlacklisted = new ArrayList<>();
IvyContext context = IvyContext.getContext();
ListIterator<ArtifactInfo> iter = sorted.listIterator(sorted.size());
while (iter.hasPrevious()) {
ResolvedResource rres = (ResolvedResource) iter.previous();
// name, blacklisting and first level version matching
if (filterNames(new ArrayList<>(Collections.singleton(rres.getRevision()))).isEmpty()) {
Message.debug("\t" + name + ": filtered by name: " + rres);
continue;
}
ModuleRevisionId foundMrid = ModuleRevisionId.newInstance(mrid, rres.getRevision());
ResolveData data = context.getResolveData();
if (data != null && data.getReport() != null && data.isBlacklisted(data.getReport().getConfiguration(), foundMrid)) {
Message.debug("\t" + name + ": blacklisted: " + rres);
rejected.add(rres.getRevision() + " (blacklisted)");
foundBlacklisted.add(foundMrid);
continue;
}
if (!versionMatcher.accept(mrid, foundMrid)) {
Message.debug("\t" + name + ": rejected by version matcher: " + rres);
rejected.add(rres.getRevision());
continue;
}
if (rres.getResource() != null && !rres.getResource().exists()) {
Message.debug("\t" + name + ": unreachable: " + rres + "; res=" + rres.getResource());
rejected.add(rres.getRevision() + " (unreachable)");
continue;
}
if (date != null && rres.getLastModified() > date.getTime()) {
Message.verbose("\t" + name + ": too young: " + rres);
rejected.add(rres.getRevision() + " (" + rres.getLastModified() + ")");
continue;
}
if (versionMatcher.needModuleDescriptor(mrid, foundMrid)) {
ResolvedResource r = rmdparser.parse(rres.getResource(), rres.getRevision());
if (r == null) {
Message.debug("\t" + name + ": impossible to get module descriptor resource: " + rres);
rejected.add(rres.getRevision() + " (no or bad MD)");
continue;
}
ModuleDescriptor md = ((MDResolvedResource) r).getResolvedModuleRevision().getDescriptor();
if (md.isDefault()) {
Message.debug("\t" + name + ": default md rejected by version matcher" + "requiring module descriptor: " + rres);
rejected.add(rres.getRevision() + " (MD)");
continue;
}
if (!versionMatcher.accept(mrid, md)) {
Message.debug("\t" + name + ": md rejected by version matcher: " + rres);
rejected.add(rres.getRevision() + " (MD)");
continue;
}
found = r;
} else {
found = rres;
}
if (found != null) {
break;
}
}
if (found == null && !rejected.isEmpty()) {
logAttempt(rejected.toString());
}
if (found == null && !foundBlacklisted.isEmpty()) {
// all acceptable versions have been blacklisted, this means that an unsolvable conflict
// has been found
DependencyDescriptor dd = context.getDependencyDescriptor();
IvyNode parentNode = context.getResolveData().getNode(dd.getParentRevisionId());
ConflictManager cm = parentNode.getConflictManager(mrid.getModuleId());
cm.handleAllBlacklistedRevisions(dd, foundBlacklisted);
}
return found;
}
use of org.apache.ivy.plugins.version.VersionMatcher in project ant-ivy by apache.
the class AbstractWorkspaceResolver method checkCandidate.
protected ResolvedModuleRevision checkCandidate(DependencyDescriptor dd, ModuleDescriptor md, String workspaceModuleName) {
if (workspaceModuleName == null) {
workspaceModuleName = dd.getDependencyId().toString();
}
ModuleRevisionId dependencyMrid = dd.getDependencyRevisionId();
String org = dependencyMrid.getModuleId().getOrganisation();
String module = dependencyMrid.getModuleId().getName();
VersionMatcher versionMatcher = getSettings().getVersionMatcher();
ModuleRevisionId candidateMrid = md.getModuleRevisionId();
switch(org) {
case BundleInfo.BUNDLE_TYPE:
// looking for an OSGi bundle via its symbolic name
String sn = md.getExtraInfoContentByTagName("Bundle-SymbolicName");
if (sn == null || !module.equals(sn)) {
// not found, skip to next
return null;
}
break;
case BundleInfo.PACKAGE_TYPE:
// looking for an OSGi bundle via its exported package
String exportedPackages = md.getExtraInfoContentByTagName("Export-Package");
if (exportedPackages == null) {
// not found, skip to next
return null;
}
boolean found = false;
String version = null;
ManifestHeaderValue exportElements;
try {
exportElements = new ManifestHeaderValue(exportedPackages);
} catch (ParseException e) {
// wrong OSGi header: skip it
return null;
}
for (ManifestHeaderElement exportElement : exportElements.getElements()) {
if (exportElement.getValues().contains(module)) {
found = true;
version = exportElement.getAttributes().get("version");
break;
}
}
if (!found) {
// not found, skip to next
return null;
}
if (version == null) {
// no version means anything can match. Let's trick the version matcher by
// setting the exact expected version
version = dependencyMrid.getRevision();
}
md.setResolvedModuleRevisionId(ModuleRevisionId.newInstance(org, module, version));
break;
default:
if (!candidateMrid.getModuleId().equals(dependencyMrid.getModuleId())) {
// it doesn't match org#module, skip to next
return null;
}
break;
}
Message.verbose("Workspace resolver found potential matching workspace module " + workspaceModuleName + " with module " + candidateMrid + " for module " + dependencyMrid);
if (!ignoreBranch) {
ModuleId mid = dependencyMrid.getModuleId();
String defaultBranch = getSettings().getDefaultBranch(mid);
String dependencyBranch = dependencyMrid.getBranch();
String candidateBranch = candidateMrid.getBranch();
if (dependencyBranch == null) {
dependencyBranch = defaultBranch;
}
if (candidateBranch == null) {
candidateBranch = defaultBranch;
}
if (dependencyBranch != candidateBranch) {
// Both cannot be null
if (dependencyBranch == null || candidateBranch == null) {
Message.verbose("\t\trejected since branches doesn't match (one is set, the other isn't)");
return null;
}
if (!dependencyBranch.equals(candidateBranch)) {
Message.verbose("\t\trejected since branches doesn't match");
return null;
}
}
}
// Found one; check if it is for the module we need
if (!ignoreVersion && !md.getModuleRevisionId().getRevision().equals(Ivy.getWorkingRevision()) && !versionMatcher.accept(dd.getDependencyRevisionId(), md)) {
Message.verbose("\t\treject as version didn't match");
return null;
}
if (ignoreVersion) {
Message.verbose("\t\tmatched (version are ignored)");
} else {
Message.verbose("\t\tversion matched");
}
WorkspaceModuleDescriptor workspaceMd = createWorkspaceMd(md);
Artifact mdaf = md.getMetadataArtifact();
if (mdaf == null) {
mdaf = new DefaultArtifact(md.getModuleRevisionId(), md.getPublicationDate(), workspaceModuleName, "ivy", "");
}
MetadataArtifactDownloadReport madr = new MetadataArtifactDownloadReport(mdaf);
madr.setDownloadStatus(DownloadStatus.SUCCESSFUL);
madr.setSearched(true);
return new ResolvedModuleRevision(this, this, workspaceMd, madr);
}
use of org.apache.ivy.plugins.version.VersionMatcher in project ant-ivy by apache.
the class RepositoryResolver method findResourceUsingPattern.
@Override
protected ResolvedResource findResourceUsingPattern(ModuleRevisionId mrid, String pattern, Artifact artifact, ResourceMDParser rmdparser, Date date) {
String name = getName();
VersionMatcher versionMatcher = getSettings().getVersionMatcher();
try {
if (!versionMatcher.isDynamic(mrid) || isAlwaysCheckExactRevision()) {
String resourceName = IvyPatternHelper.substitute(pattern, mrid, artifact);
Message.debug("\t trying " + resourceName);
logAttempt(resourceName);
Resource res = repository.getResource(resourceName);
boolean reachable = res.exists();
if (reachable) {
String revision;
if (pattern.contains(IvyPatternHelper.REVISION_KEY)) {
revision = mrid.getRevision();
} else {
if ("ivy".equals(artifact.getType()) || "pom".equals(artifact.getType())) {
// we can't determine the revision from the pattern, get it
// from the module descriptor itself
File temp = File.createTempFile("ivy", artifact.getExt());
temp.deleteOnExit();
repository.get(res.getName(), temp);
ModuleDescriptorParser parser = ModuleDescriptorParserRegistry.getInstance().getParser(res);
ModuleDescriptor md = parser.parseDescriptor(getParserSettings(), temp.toURI().toURL(), res, false);
revision = md.getRevision();
if (isNullOrEmpty(revision)) {
revision = "working@" + name;
}
} else {
revision = "working@" + name;
}
}
return new ResolvedResource(res, revision);
} else if (versionMatcher.isDynamic(mrid)) {
return findDynamicResourceUsingPattern(rmdparser, mrid, pattern, artifact, date);
} else {
Message.debug("\t" + name + ": resource not reachable for " + mrid + ": res=" + res);
return null;
}
} else {
return findDynamicResourceUsingPattern(rmdparser, mrid, pattern, artifact, date);
}
} catch (IOException | ParseException ex) {
throw new RuntimeException(name + ": unable to get resource for " + mrid + ": res=" + IvyPatternHelper.substitute(pattern, mrid, artifact) + ": " + ex, ex);
}
}
use of org.apache.ivy.plugins.version.VersionMatcher in project ant-ivy by apache.
the class LatestCompatibleConflictManager method resolveConflicts.
@Override
public Collection<IvyNode> resolveConflicts(IvyNode parent, Collection<IvyNode> conflicts) {
if (conflicts.size() < 2) {
return conflicts;
}
VersionMatcher versionMatcher = getSettings().getVersionMatcher();
Iterator<IvyNode> iter = conflicts.iterator();
IvyNode node = iter.next();
ModuleRevisionId mrid = node.getResolvedId();
if (versionMatcher.isDynamic(mrid)) {
while (iter.hasNext()) {
IvyNode other = iter.next();
if (versionMatcher.isDynamic(other.getResolvedId()) || !versionMatcher.accept(mrid, other.getResolvedId()) && !handleIncompatibleConflict(parent, conflicts, node, other)) {
// or incompatibility found
return null;
}
}
// no incompatibility nor dynamic version found, let's return the latest static version
if (conflicts.size() == 2) {
// very common special case of only two modules in conflict,
// let's return the second one (static)
Iterator<IvyNode> it = conflicts.iterator();
it.next();
return Collections.singleton(it.next());
}
Collection<IvyNode> newConflicts = new LinkedHashSet<>(conflicts);
newConflicts.remove(node);
return super.resolveConflicts(parent, newConflicts);
} else {
// the first node is a static revision, let's see if all other versions match
while (iter.hasNext()) {
IvyNode other = iter.next();
if (!versionMatcher.accept(other.getResolvedId(), mrid) && !handleIncompatibleConflict(parent, conflicts, node, other)) {
// incompatibility found
return null;
}
}
// no incompatibility found, let's return this static version
return Collections.singleton(node);
}
}
use of org.apache.ivy.plugins.version.VersionMatcher in project ant-ivy by apache.
the class StrictConflictManager method resolveConflicts.
public Collection<IvyNode> resolveConflicts(IvyNode parent, Collection<IvyNode> conflicts) {
VersionMatcher versionMatcher = getSettings().getVersionMatcher();
IvyNode lastNode = null;
for (IvyNode node : conflicts) {
if (versionMatcher.isDynamic(node.getResolvedId())) {
// dynamic revision, not enough information to resolve conflict
return null;
}
if (lastNode != null && !lastNode.equals(node)) {
throw new StrictConflictException(lastNode, node);
}
lastNode = node;
}
return Collections.singleton(lastNode);
}
Aggregations