use of org.sonatype.aether.repository.LocalMetadataRequest in project sonatype-aether by sonatype.
the class EnhancedLocalRepositoryManagerTest method testFindLocalMetadata.
@Test
public void testFindLocalMetadata() throws Exception {
addMetadata(metadata, null);
LocalMetadataRequest request = new LocalMetadataRequest(metadata, null, testContext);
LocalMetadataResult result = manager.find(session, request);
assertNotNull(result.getFile());
}
use of org.sonatype.aether.repository.LocalMetadataRequest in project sonatype-aether by sonatype.
the class DefaultMetadataResolver method getLocalFile.
private File getLocalFile(RepositorySystemSession session, Metadata metadata) {
LocalRepositoryManager lrm = session.getLocalRepositoryManager();
LocalMetadataResult localResult = lrm.find(session, new LocalMetadataRequest(metadata, null, null));
File localFile = localResult.getFile();
return localFile;
}
use of org.sonatype.aether.repository.LocalMetadataRequest in project sonatype-aether by sonatype.
the class DefaultArtifactResolverTest method testFindInLocalRepositoryWhenVersionRangeWasResolvedFromLocalRepository.
@Test
public void testFindInLocalRepositoryWhenVersionRangeWasResolvedFromLocalRepository() throws ArtifactResolutionException {
session.setLocalRepositoryManager(new LocalRepositoryManager() {
public LocalRepository getRepository() {
return null;
}
public String getPathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context) {
return null;
}
public String getPathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context) {
return null;
}
public String getPathForLocalMetadata(Metadata metadata) {
return null;
}
public String getPathForLocalArtifact(Artifact artifact) {
return null;
}
public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRequest request) {
LocalArtifactResult result = new LocalArtifactResult(request);
result.setAvailable(false);
try {
result.setFile(TestFileUtils.createTempFile(""));
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public void add(RepositorySystemSession session, LocalArtifactRegistration request) {
}
public LocalMetadataResult find(RepositorySystemSession session, LocalMetadataRequest request) {
LocalMetadataResult result = new LocalMetadataResult(request);
return result;
}
public void add(RepositorySystemSession session, LocalMetadataRegistration request) {
}
});
ArtifactRequest request = new ArtifactRequest(artifact, null, "");
resolver.setVersionResolver(new VersionResolver() {
public VersionResult resolveVersion(RepositorySystemSession session, VersionRequest request) throws VersionResolutionException {
return new VersionResult(request).setVersion(request.getArtifact().getVersion());
}
});
ArtifactResult result = resolver.resolveArtifact(session, request);
assertTrue(result.getExceptions().isEmpty());
Artifact resolved = result.getArtifact();
assertNotNull(resolved.getFile());
resolved = resolved.setFile(null);
assertEquals(artifact, resolved);
}
use of org.sonatype.aether.repository.LocalMetadataRequest in project sonatype-aether by sonatype.
the class DefaultArtifactResolverTest method testFindInLocalRepositoryWhenVersionWasFoundInLocalRepository.
@Test
public void testFindInLocalRepositoryWhenVersionWasFoundInLocalRepository() throws ArtifactResolutionException {
session.setLocalRepositoryManager(new LocalRepositoryManager() {
public LocalRepository getRepository() {
return null;
}
public String getPathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context) {
return null;
}
public String getPathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context) {
return null;
}
public String getPathForLocalMetadata(Metadata metadata) {
return null;
}
public String getPathForLocalArtifact(Artifact artifact) {
return null;
}
public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRequest request) {
LocalArtifactResult result = new LocalArtifactResult(request);
result.setAvailable(false);
try {
result.setFile(TestFileUtils.createTempFile(""));
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public void add(RepositorySystemSession session, LocalArtifactRegistration request) {
}
public LocalMetadataResult find(RepositorySystemSession session, LocalMetadataRequest request) {
LocalMetadataResult result = new LocalMetadataResult(request);
return result;
}
public void add(RepositorySystemSession session, LocalMetadataRegistration request) {
}
});
ArtifactRequest request = new ArtifactRequest(artifact, null, "");
request.addRepository(new RemoteRepository("id", "default", "file:///"));
resolver.setVersionResolver(new VersionResolver() {
public VersionResult resolveVersion(RepositorySystemSession session, VersionRequest request) throws VersionResolutionException {
return new VersionResult(request).setRepository(new LocalRepository("id")).setVersion(request.getArtifact().getVersion());
}
});
ArtifactResult result = resolver.resolveArtifact(session, request);
assertTrue(result.getExceptions().isEmpty());
Artifact resolved = result.getArtifact();
assertNotNull(resolved.getFile());
resolved = resolved.setFile(null);
assertEquals(artifact, resolved);
}
use of org.sonatype.aether.repository.LocalMetadataRequest in project sonatype-aether by sonatype.
the class DefaultMetadataResolver method resolve.
private List<MetadataResult> resolve(RepositorySystemSession session, Collection<? extends MetadataRequest> requests) {
List<MetadataResult> results = new ArrayList<MetadataResult>(requests.size());
List<ResolveTask> tasks = new ArrayList<ResolveTask>(requests.size());
Map<File, Long> localLastUpdates = new HashMap<File, Long>();
for (MetadataRequest request : requests) {
RequestTrace trace = DefaultRequestTrace.newChild(request.getTrace(), request);
MetadataResult result = new MetadataResult(request);
results.add(result);
Metadata metadata = request.getMetadata();
RemoteRepository repository = request.getRepository();
if (repository == null) {
LocalRepository localRepo = session.getLocalRepositoryManager().getRepository();
metadataResolving(session, trace, metadata, localRepo);
File localFile = getLocalFile(session, metadata);
if (localFile != null) {
metadata = metadata.setFile(localFile);
result.setMetadata(metadata);
} else {
result.setException(new MetadataNotFoundException(metadata, localRepo));
}
metadataResolved(session, trace, metadata, localRepo, result.getException());
continue;
}
List<RemoteRepository> repositories = getEnabledSourceRepositories(repository, metadata.getNature());
if (repositories.isEmpty()) {
continue;
}
metadataResolving(session, trace, metadata, repository);
LocalRepositoryManager lrm = session.getLocalRepositoryManager();
LocalMetadataRequest localRequest = new LocalMetadataRequest(metadata, repository, request.getRequestContext());
LocalMetadataResult lrmResult = lrm.find(session, localRequest);
File metadataFile = lrmResult.getFile();
if (session.isOffline()) {
if (metadataFile != null) {
metadata = metadata.setFile(metadataFile);
result.setMetadata(metadata);
} else {
String msg = "The repository system is offline but the metadata " + metadata + " from " + repository + " is not available in the local repository.";
result.setException(new MetadataNotFoundException(metadata, repository, msg));
}
metadataResolved(session, trace, metadata, repository, result.getException());
continue;
}
Long localLastUpdate = null;
if (request.isFavorLocalRepository()) {
File localFile = getLocalFile(session, metadata);
localLastUpdate = localLastUpdates.get(localFile);
if (localLastUpdate == null) {
localLastUpdate = Long.valueOf(localFile != null ? localFile.lastModified() : 0);
localLastUpdates.put(localFile, localLastUpdate);
}
}
List<UpdateCheck<Metadata, MetadataTransferException>> checks = new ArrayList<UpdateCheck<Metadata, MetadataTransferException>>();
Exception exception = null;
for (RemoteRepository repo : repositories) {
UpdateCheck<Metadata, MetadataTransferException> check = new UpdateCheck<Metadata, MetadataTransferException>();
check.setLocalLastUpdated((localLastUpdate != null) ? localLastUpdate.longValue() : 0);
check.setItem(metadata);
// use 'main' installation file for the check (-> use requested repository)
File checkFile = new File(session.getLocalRepository().getBasedir(), session.getLocalRepositoryManager().getPathForRemoteMetadata(metadata, repository, request.getRequestContext()));
check.setFile(checkFile);
check.setRepository(repository);
check.setAuthoritativeRepository(repo);
check.setPolicy(getPolicy(session, repo, metadata.getNature()).getUpdatePolicy());
if (lrmResult.isStale()) {
checks.add(check);
} else {
updateCheckManager.checkMetadata(session, check);
if (check.isRequired()) {
checks.add(check);
} else if (exception == null) {
exception = check.getException();
}
}
}
if (!checks.isEmpty()) {
RepositoryPolicy policy = getPolicy(session, repository, metadata.getNature());
// install path may be different from lookup path
File installFile = new File(session.getLocalRepository().getBasedir(), session.getLocalRepositoryManager().getPathForRemoteMetadata(metadata, request.getRepository(), request.getRequestContext()));
ResolveTask task = new ResolveTask(session, trace, result, installFile, checks, policy.getChecksumPolicy());
tasks.add(task);
} else {
result.setException(exception);
if (metadataFile != null) {
metadata = metadata.setFile(metadataFile);
result.setMetadata(metadata);
}
metadataResolved(session, trace, metadata, repository, result.getException());
}
}
if (!tasks.isEmpty()) {
int threads = ConfigUtils.getInteger(session, 4, "aether.metadataResolver.threads");
Executor executor = getExecutor(Math.min(tasks.size(), threads));
try {
RunnableErrorForwarder errorForwarder = new RunnableErrorForwarder();
for (ResolveTask task : tasks) {
executor.execute(errorForwarder.wrap(task));
}
errorForwarder.await();
for (ResolveTask task : tasks) {
task.result.setException(task.exception);
}
} finally {
shutdown(executor);
}
for (ResolveTask task : tasks) {
Metadata metadata = task.request.getMetadata();
// re-lookup metadata for resolve
LocalMetadataRequest localRequest = new LocalMetadataRequest(metadata, task.request.getRepository(), task.request.getRequestContext());
File metadataFile = session.getLocalRepositoryManager().find(session, localRequest).getFile();
if (metadataFile != null) {
metadata = metadata.setFile(metadataFile);
task.result.setMetadata(metadata);
}
if (task.result.getException() == null) {
task.result.setUpdated(true);
}
metadataResolved(session, task.trace, metadata, task.request.getRepository(), task.result.getException());
}
}
return results;
}
Aggregations