use of org.apache.ivy.core.module.descriptor.ModuleDescriptor in project ant-ivy by apache.
the class BasicResolver method cacheModuleDescriptor.
private void cacheModuleDescriptor(ModuleDescriptor systemMd, ModuleRevisionId systemMrid, ResolvedResource ivyRef, ResolvedModuleRevision rmr) {
RepositoryCacheManager cacheManager = getRepositoryCacheManager();
final ModuleDescriptorParser parser = systemMd.getParser();
// the metadata artifact which was used to cache the original metadata file
Artifact requestedMetadataArtifact = (ivyRef == null) ? systemMd.getMetadataArtifact() : parser.getMetadataArtifact(ModuleRevisionId.newInstance(systemMrid, systemMd.getRevision()), ivyRef.getResource());
cacheManager.originalToCachedModuleDescriptor(this, ivyRef, requestedMetadataArtifact, rmr, new ModuleDescriptorWriter() {
public void write(ResolvedResource originalMdResource, ModuleDescriptor md, File src, File dest) throws IOException, ParseException {
if (originalMdResource == null) {
// a basic ivy file is written containing default data
XmlModuleDescriptorWriter.write(md, dest);
} else {
// copy and update ivy file from source to cache
parser.toIvyFile(new FileInputStream(src), originalMdResource.getResource(), dest, md);
long repLastModified = originalMdResource.getLastModified();
if (repLastModified > 0) {
dest.setLastModified(repLastModified);
}
}
}
});
}
use of org.apache.ivy.core.module.descriptor.ModuleDescriptor 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.core.module.descriptor.ModuleDescriptor in project ant-ivy by apache.
the class BasicResolver method getDependency.
public ResolvedModuleRevision getDependency(DependencyDescriptor dd, ResolveData data) throws ParseException {
IvyContext context = IvyContext.pushNewCopyContext();
try {
ResolvedModuleRevision mr = data.getCurrentResolvedModuleRevision();
if (mr != null && shouldReturnResolvedModule(dd, mr)) {
return mr;
}
if (isForce()) {
dd = dd.clone(ModuleRevisionId.newInstance(dd.getDependencyRevisionId(), "latest.integration"));
}
DependencyDescriptor systemDd = dd;
DependencyDescriptor nsDd = fromSystem(dd);
context.setDependencyDescriptor(systemDd);
context.setResolveData(data);
clearIvyAttempts();
clearArtifactAttempts();
ModuleRevisionId systemMrid = systemDd.getDependencyRevisionId();
ModuleRevisionId nsMrid = nsDd.getDependencyRevisionId();
checkRevision(systemMrid);
boolean isDynamic = getAndCheckIsDynamic(systemMrid);
// we first search for the dependency in cache
ResolvedModuleRevision rmr = findModuleInCache(systemDd, data);
if (rmr != null) {
if (rmr.getDescriptor().isDefault() && rmr.getResolver() != this) {
Message.verbose("\t" + getName() + ": found revision in cache: " + systemMrid + " (resolved by " + rmr.getResolver().getName() + "): but it's a default one, maybe we can find a better one");
} else if (isForce() && rmr.getResolver() != this) {
Message.verbose("\t" + getName() + ": found revision in cache: " + systemMrid + " (resolved by " + rmr.getResolver().getName() + "): but we are in force mode, let's try to find one ourselves");
} else {
Message.verbose("\t" + getName() + ": revision in cache: " + systemMrid);
return checkLatest(systemDd, checkForcedResolvedModuleRevision(rmr), data);
}
}
if (data.getOptions().isUseCacheOnly()) {
throw new UnresolvedDependencyException("\t" + getName() + " (useCacheOnly) : no ivy file found for " + systemMrid, false);
}
checkInterrupted();
ResolvedResource ivyRef = findIvyFileRef(nsDd, data);
checkInterrupted();
// get module descriptor
ModuleDescriptor nsMd;
ModuleDescriptor systemMd = null;
if (ivyRef == null) {
if (!isAllownomd()) {
throw new UnresolvedDependencyException("\t" + getName() + ": no ivy file found for " + systemMrid, false);
}
nsMd = DefaultModuleDescriptor.newDefaultInstance(nsMrid, nsDd.getAllDependencyArtifacts());
ResolvedResource artifactRef = findFirstArtifactRef(nsMd, nsDd, data);
checkInterrupted();
if (artifactRef == null) {
throw new UnresolvedDependencyException("\t" + getName() + ": no ivy file nor artifact found for " + systemMrid, false);
}
long lastModified = artifactRef.getLastModified();
if (lastModified != 0 && nsMd instanceof DefaultModuleDescriptor) {
((DefaultModuleDescriptor) nsMd).setLastModified(lastModified);
}
Message.verbose("\t" + getName() + ": no ivy file found for " + systemMrid + ": using default data");
if (isDynamic) {
nsMd.setResolvedModuleRevisionId(ModuleRevisionId.newInstance(nsMrid, artifactRef.getRevision()));
}
systemMd = toSystem(nsMd);
MetadataArtifactDownloadReport madr = new MetadataArtifactDownloadReport(systemMd.getMetadataArtifact());
madr.setDownloadStatus(DownloadStatus.NO);
madr.setSearched(true);
rmr = new ResolvedModuleRevision(this, this, systemMd, madr, isForce());
getRepositoryCacheManager().cacheModuleDescriptor(this, artifactRef, toSystem(dd), systemMd.getAllArtifacts()[0], null, getCacheOptions(data));
} else {
if (ivyRef instanceof MDResolvedResource) {
rmr = ((MDResolvedResource) ivyRef).getResolvedModuleRevision();
}
if (rmr == null) {
rmr = parse(ivyRef, systemDd, data);
if (rmr == null) {
throw new UnresolvedDependencyException();
}
}
if (!rmr.getReport().isDownloaded() && rmr.getReport().getLocalFile() != null) {
return checkLatest(systemDd, checkForcedResolvedModuleRevision(rmr), data);
}
nsMd = rmr.getDescriptor();
// check descriptor data is in sync with resource revision and names
systemMd = toSystem(nsMd);
if (isCheckconsistency()) {
checkDescriptorConsistency(systemMrid, systemMd, ivyRef);
checkDescriptorConsistency(nsMrid, nsMd, ivyRef);
} else {
if (systemMd instanceof DefaultModuleDescriptor) {
DefaultModuleDescriptor defaultMd = (DefaultModuleDescriptor) systemMd;
ModuleRevisionId revision = getRevision(ivyRef, systemMrid, systemMd);
defaultMd.setModuleRevisionId(revision);
defaultMd.setResolvedModuleRevisionId(revision);
} else {
Message.warn("consistency disabled with instance of non DefaultModuleDescriptor... module info can't be updated, so consistency check will be done");
checkDescriptorConsistency(nsMrid, nsMd, ivyRef);
checkDescriptorConsistency(systemMrid, systemMd, ivyRef);
}
}
rmr = new ResolvedModuleRevision(this, this, systemMd, toSystem(rmr.getReport()), isForce());
}
resolveAndCheckRevision(systemMd, systemMrid, ivyRef, isDynamic);
resolveAndCheckPublicationDate(systemDd, systemMd, systemMrid, data);
checkNotConvertedExclusionRule(systemMd, ivyRef, data);
if (ivyRef == null || ivyRef.getResource() != null) {
cacheModuleDescriptor(systemMd, systemMrid, ivyRef, rmr);
}
return checkLatest(systemDd, checkForcedResolvedModuleRevision(rmr), data);
} catch (UnresolvedDependencyException ex) {
if (!ex.getMessage().isEmpty()) {
if (ex.isError()) {
Message.error(ex.getMessage());
} else {
Message.verbose(ex.getMessage());
}
}
return data.getCurrentResolvedModuleRevision();
} finally {
IvyContext.popContext();
}
}
use of org.apache.ivy.core.module.descriptor.ModuleDescriptor 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.core.module.descriptor.ModuleDescriptor in project ant-ivy by apache.
the class DefaultRepositoryCacheManager method originalToCachedModuleDescriptor.
public void originalToCachedModuleDescriptor(DependencyResolver resolver, ResolvedResource originalMetadataRef, Artifact requestedMetadataArtifact, ResolvedModuleRevision rmr, ModuleDescriptorWriter writer) {
ModuleDescriptor md = rmr.getDescriptor();
Artifact originalMetadataArtifact = getOriginalMetadataArtifact(requestedMetadataArtifact);
File mdFileInCache = getIvyFileInCache(md.getResolvedModuleRevisionId());
ModuleRevisionId mrid = requestedMetadataArtifact.getModuleRevisionId();
if (!lockMetadataArtifact(mrid)) {
Message.warn("impossible to acquire lock for: " + mrid);
return;
}
try {
File originalFileInCache = getArchiveFileInCache(originalMetadataArtifact);
writer.write(originalMetadataRef, md, originalFileInCache, mdFileInCache);
getMemoryCache().putInCache(mdFileInCache, new ParserSettingsMonitor(settings), true, md);
saveResolvers(md, resolver.getName(), resolver.getName());
if (!md.isDefault()) {
rmr.getReport().setOriginalLocalFile(originalFileInCache);
}
rmr.getReport().setLocalFile(mdFileInCache);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
String metadataRef;
if (originalMetadataRef == null) {
metadataRef = String.valueOf(md.getResolvedModuleRevisionId());
} else {
metadataRef = String.valueOf(originalMetadataRef);
}
Message.warn("impossible to put metadata file in cache: " + metadataRef, e);
} finally {
unlockMetadataArtifact(mrid);
}
}
Aggregations