Search in sources :

Example 1 with MetadataResult

use of org.sonatype.aether.resolution.MetadataResult in project sonatype-aether by sonatype.

the class DefaultMetadataResolverTest method testOfflineSessionResolveMetadata.

@Test
public void testOfflineSessionResolveMetadata() throws IOException {
    session.setOffline(true);
    String path = session.getLocalRepositoryManager().getPathForRemoteMetadata(metadata, repository, "");
    File file = new File(session.getLocalRepository().getBasedir(), path);
    TestFileUtils.write(file.getAbsolutePath(), file);
    // set file to use in TestLRM find()
    metadata = metadata.setFile(file);
    MetadataRequest request = new MetadataRequest(metadata, repository, "");
    List<MetadataResult> results = resolver.resolveMetadata(session, Arrays.asList(request));
    assertEquals(1, results.size());
    MetadataResult result = results.get(0);
    assertSame(request, result.getRequest());
    assertNull(String.valueOf(result.getException()), result.getException());
    assertNotNull(result.getMetadata());
    assertNotNull(result.getMetadata().getFile());
    assertEquals(file, result.getMetadata().getFile());
    assertEquals(metadata.setFile(null), result.getMetadata().setFile(null));
    connector.assertSeenExpected();
}
Also used : MetadataRequest(org.sonatype.aether.resolution.MetadataRequest) File(java.io.File) MetadataResult(org.sonatype.aether.resolution.MetadataResult) Test(org.junit.Test)

Example 2 with MetadataResult

use of org.sonatype.aether.resolution.MetadataResult in project sonatype-aether by sonatype.

the class DefaultMetadataResolverTest method testResolve.

@Test
public void testResolve() throws IOException {
    connector.setExpectGet(metadata);
    // prepare "download"
    File file = new File(session.getLocalRepository().getBasedir(), session.getLocalRepositoryManager().getPathForRemoteMetadata(metadata, repository, ""));
    TestFileUtils.write(file.getAbsolutePath(), file);
    MetadataRequest request = new MetadataRequest(metadata, repository, "");
    List<MetadataResult> results = resolver.resolveMetadata(session, Arrays.asList(request));
    assertEquals(1, results.size());
    MetadataResult result = results.get(0);
    assertSame(request, result.getRequest());
    assertNull(result.getException());
    assertNotNull(result.getMetadata());
    assertNotNull(result.getMetadata().getFile());
    assertEquals(file, result.getMetadata().getFile());
    assertEquals(metadata, result.getMetadata().setFile(null));
    connector.assertSeenExpected();
    Set<Metadata> metadataRegistration = ((TestLocalRepositoryManager) session.getLocalRepositoryManager()).getMetadataRegistration();
    assertTrue(metadataRegistration.contains(metadata));
    assertEquals(1, metadataRegistration.size());
}
Also used : TestLocalRepositoryManager(org.sonatype.aether.test.impl.TestLocalRepositoryManager) MetadataRequest(org.sonatype.aether.resolution.MetadataRequest) StubMetadata(org.sonatype.aether.test.util.impl.StubMetadata) Metadata(org.sonatype.aether.metadata.Metadata) File(java.io.File) MetadataResult(org.sonatype.aether.resolution.MetadataResult) Test(org.junit.Test)

Example 3 with MetadataResult

use of org.sonatype.aether.resolution.MetadataResult in project sonatype-aether by sonatype.

the class DefaultMetadataResolverTest method testRemoveMetadataIfMissing.

@Test
public void testRemoveMetadataIfMissing() throws IOException {
    connector = new RecordingRepositoryConnector() {

        @Override
        public void get(Collection<? extends ArtifactDownload> artifactDownloads, Collection<? extends MetadataDownload> metadataDownloads) {
            super.get(artifactDownloads, metadataDownloads);
            for (MetadataDownload d : metadataDownloads) {
                d.setException(new MetadataNotFoundException(metadata, repository));
            }
        }
    };
    manager.setConnector(connector);
    File file = new File(session.getLocalRepository().getBasedir(), session.getLocalRepositoryManager().getPathForRemoteMetadata(metadata, repository, ""));
    TestFileUtils.write(file.getAbsolutePath(), file);
    metadata.setFile(file);
    MetadataRequest request = new MetadataRequest(metadata, repository, "");
    request.setDeleteLocalCopyIfMissing(true);
    List<MetadataResult> results = resolver.resolveMetadata(session, Arrays.asList(request));
    assertEquals(1, results.size());
    MetadataResult result = results.get(0);
    assertNotNull(result.getException());
    assertEquals(false, file.exists());
}
Also used : MetadataNotFoundException(org.sonatype.aether.transfer.MetadataNotFoundException) MetadataRequest(org.sonatype.aether.resolution.MetadataRequest) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) File(java.io.File) MetadataResult(org.sonatype.aether.resolution.MetadataResult) Test(org.junit.Test)

Example 4 with MetadataResult

use of org.sonatype.aether.resolution.MetadataResult in project sonatype-aether by sonatype.

the class DefaultMetadataResolverTest method testNoRepositoryFailing.

@Test
public void testNoRepositoryFailing() {
    MetadataRequest request = new MetadataRequest(metadata, null, "");
    List<MetadataResult> results = resolver.resolveMetadata(session, Arrays.asList(request));
    assertEquals(1, results.size());
    MetadataResult result = results.get(0);
    assertSame(request, result.getRequest());
    assertNotNull("" + (result.getMetadata() != null ? result.getMetadata().getFile() : result.getMetadata()), result.getException());
    assertEquals(MetadataNotFoundException.class, result.getException().getClass());
    assertNull(result.getMetadata());
}
Also used : MetadataRequest(org.sonatype.aether.resolution.MetadataRequest) MetadataResult(org.sonatype.aether.resolution.MetadataResult) Test(org.junit.Test)

Example 5 with MetadataResult

use of org.sonatype.aether.resolution.MetadataResult 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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Metadata(org.sonatype.aether.metadata.Metadata) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) RequestTrace(org.sonatype.aether.RequestTrace) DefaultRequestTrace(org.sonatype.aether.util.DefaultRequestTrace) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Executor(java.util.concurrent.Executor) LocalRepositoryManager(org.sonatype.aether.repository.LocalRepositoryManager) LocalMetadataResult(org.sonatype.aether.repository.LocalMetadataResult) MetadataTransferException(org.sonatype.aether.transfer.MetadataTransferException) RunnableErrorForwarder(org.sonatype.aether.util.concurrency.RunnableErrorForwarder) LocalMetadataRequest(org.sonatype.aether.repository.LocalMetadataRequest) RepositoryPolicy(org.sonatype.aether.repository.RepositoryPolicy) LocalRepository(org.sonatype.aether.repository.LocalRepository) NoRepositoryConnectorException(org.sonatype.aether.transfer.NoRepositoryConnectorException) MetadataNotFoundException(org.sonatype.aether.transfer.MetadataNotFoundException) MetadataTransferException(org.sonatype.aether.transfer.MetadataTransferException) MetadataResult(org.sonatype.aether.resolution.MetadataResult) LocalMetadataResult(org.sonatype.aether.repository.LocalMetadataResult) MetadataNotFoundException(org.sonatype.aether.transfer.MetadataNotFoundException) MetadataRequest(org.sonatype.aether.resolution.MetadataRequest) LocalMetadataRequest(org.sonatype.aether.repository.LocalMetadataRequest) UpdateCheck(org.sonatype.aether.impl.UpdateCheck) File(java.io.File)

Aggregations

MetadataRequest (org.sonatype.aether.resolution.MetadataRequest)7 MetadataResult (org.sonatype.aether.resolution.MetadataResult)7 Test (org.junit.Test)6 File (java.io.File)5 Metadata (org.sonatype.aether.metadata.Metadata)2 MetadataNotFoundException (org.sonatype.aether.transfer.MetadataNotFoundException)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Executor (java.util.concurrent.Executor)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 RepositorySystemSession (org.sonatype.aether.RepositorySystemSession)1 RequestTrace (org.sonatype.aether.RequestTrace)1 UpdateCheck (org.sonatype.aether.impl.UpdateCheck)1 LocalMetadataRegistration (org.sonatype.aether.repository.LocalMetadataRegistration)1 LocalMetadataRequest (org.sonatype.aether.repository.LocalMetadataRequest)1 LocalMetadataResult (org.sonatype.aether.repository.LocalMetadataResult)1 LocalRepository (org.sonatype.aether.repository.LocalRepository)1 LocalRepositoryManager (org.sonatype.aether.repository.LocalRepositoryManager)1 RemoteRepository (org.sonatype.aether.repository.RemoteRepository)1 RepositoryPolicy (org.sonatype.aether.repository.RepositoryPolicy)1