Search in sources :

Example 1 with UpdateCheck

use of org.sonatype.aether.impl.UpdateCheck in project sonatype-aether by sonatype.

the class DefaultArtifactResolver method resolve.

private List<ArtifactResult> resolve(RepositorySystemSession session, Collection<? extends ArtifactRequest> requests) throws ArtifactResolutionException {
    List<ArtifactResult> results = new ArrayList<ArtifactResult>(requests.size());
    boolean failures = false;
    LocalRepositoryManager lrm = session.getLocalRepositoryManager();
    WorkspaceReader workspace = session.getWorkspaceReader();
    List<ResolutionGroup> groups = new ArrayList<ResolutionGroup>();
    for (ArtifactRequest request : requests) {
        RequestTrace trace = DefaultRequestTrace.newChild(request.getTrace(), request);
        ArtifactResult result = new ArtifactResult(request);
        results.add(result);
        Artifact artifact = request.getArtifact();
        List<RemoteRepository> repos = request.getRepositories();
        artifactResolving(session, trace, artifact);
        String localPath = artifact.getProperty(ArtifactProperties.LOCAL_PATH, null);
        if (localPath != null) {
            // unhosted artifact, just validate file
            File file = new File(localPath);
            if (!file.isFile()) {
                failures = true;
                result.addException(new ArtifactNotFoundException(artifact, null));
            } else {
                artifact = artifact.setFile(file);
                result.setArtifact(artifact);
                artifactResolved(session, trace, artifact, null, result.getExceptions());
            }
            continue;
        }
        VersionResult versionResult;
        try {
            VersionRequest versionRequest = new VersionRequest(artifact, repos, request.getRequestContext());
            versionRequest.setTrace(trace);
            versionResult = versionResolver.resolveVersion(session, versionRequest);
        } catch (VersionResolutionException e) {
            result.addException(e);
            continue;
        }
        artifact = artifact.setVersion(versionResult.getVersion());
        if (versionResult.getRepository() != null) {
            if (versionResult.getRepository() instanceof RemoteRepository) {
                repos = Collections.singletonList((RemoteRepository) versionResult.getRepository());
            } else {
                repos = Collections.emptyList();
            }
        }
        if (workspace != null) {
            File file = workspace.findArtifact(artifact);
            if (file != null) {
                artifact = artifact.setFile(file);
                result.setArtifact(artifact);
                result.setRepository(workspace.getRepository());
                artifactResolved(session, trace, artifact, result.getRepository(), null);
                continue;
            }
        }
        LocalArtifactResult local = lrm.find(session, new LocalArtifactRequest(artifact, repos, request.getRequestContext()));
        if (isLocallyInstalled(local, versionResult)) {
            if (local.getRepository() != null) {
                result.setRepository(local.getRepository());
            } else {
                result.setRepository(lrm.getRepository());
            }
            try {
                artifact = artifact.setFile(getFile(session, artifact, local.getFile()));
                result.setArtifact(artifact);
                artifactResolved(session, trace, artifact, result.getRepository(), null);
            } catch (ArtifactTransferException e) {
                result.addException(e);
            }
            if (!local.isAvailable()) {
                /*
                     * NOTE: Interop with simple local repository: An artifact installed by a simple local repo manager
                     * will not show up in the repository tracking file of the enhanced local repository. If however the
                     * maven-metadata-local.xml tells us the artifact was installed locally, we sync the repository
                     * tracking file.
                     */
                lrm.add(session, new LocalArtifactRegistration(artifact));
            }
            continue;
        } else if (local.getFile() != null) {
            logger.debug("Verifying availability of " + local.getFile() + " from " + repos);
        }
        if (session.isOffline()) {
            Exception exception = new ArtifactNotFoundException(artifact, null, "The repository system is offline but the artifact " + artifact + " is not available in the local repository.");
            result.addException(exception);
            artifactResolved(session, trace, artifact, null, result.getExceptions());
            continue;
        }
        AtomicBoolean resolved = new AtomicBoolean(false);
        Iterator<ResolutionGroup> groupIt = groups.iterator();
        for (RemoteRepository repo : repos) {
            if (!repo.getPolicy(artifact.isSnapshot()).isEnabled()) {
                continue;
            }
            ResolutionGroup group = null;
            while (groupIt.hasNext()) {
                ResolutionGroup t = groupIt.next();
                if (t.matches(repo)) {
                    group = t;
                    break;
                }
            }
            if (group == null) {
                group = new ResolutionGroup(repo);
                groups.add(group);
                groupIt = Collections.<ResolutionGroup>emptyList().iterator();
            }
            group.items.add(new ResolutionItem(trace, artifact, resolved, result, local, repo));
        }
    }
    for (ResolutionGroup group : groups) {
        List<ArtifactDownload> downloads = new ArrayList<ArtifactDownload>();
        for (ResolutionItem item : group.items) {
            Artifact artifact = item.artifact;
            if (item.resolved.get()) {
                // resolved in previous resolution group
                continue;
            }
            ArtifactDownload download = new ArtifactDownload();
            download.setArtifact(artifact);
            download.setRequestContext(item.request.getRequestContext());
            download.setTrace(item.trace);
            if (item.local.getFile() != null) {
                download.setFile(item.local.getFile());
                download.setExistenceCheck(true);
            } else {
                String path = lrm.getPathForRemoteArtifact(artifact, group.repository, item.request.getRequestContext());
                download.setFile(new File(lrm.getRepository().getBasedir(), path));
            }
            boolean snapshot = artifact.isSnapshot();
            RepositoryPolicy policy = remoteRepositoryManager.getPolicy(session, group.repository, !snapshot, snapshot);
            if (session.isNotFoundCachingEnabled() || session.isTransferErrorCachingEnabled()) {
                UpdateCheck<Artifact, ArtifactTransferException> check = new UpdateCheck<Artifact, ArtifactTransferException>();
                check.setItem(artifact);
                check.setFile(download.getFile());
                check.setFileValid(!download.isExistenceCheck());
                check.setRepository(group.repository);
                check.setPolicy(policy.getUpdatePolicy());
                item.updateCheck = check;
                updateCheckManager.checkArtifact(session, check);
                if (!check.isRequired()) {
                    item.result.addException(check.getException());
                    continue;
                }
            }
            download.setChecksumPolicy(policy.getChecksumPolicy());
            download.setRepositories(item.repository.getMirroredRepositories());
            downloads.add(download);
            item.download = download;
        }
        if (downloads.isEmpty()) {
            continue;
        }
        for (ArtifactDownload download : downloads) {
            artifactDownloading(session, download.getTrace(), download.getArtifact(), group.repository);
        }
        try {
            RepositoryConnector connector = remoteRepositoryManager.getRepositoryConnector(session, group.repository);
            try {
                connector.get(downloads, null);
            } finally {
                connector.close();
            }
        } catch (NoRepositoryConnectorException e) {
            for (ArtifactDownload download : downloads) {
                download.setException(new ArtifactTransferException(download.getArtifact(), group.repository, e));
            }
        }
        for (ResolutionItem item : group.items) {
            ArtifactDownload download = item.download;
            if (download == null) {
                continue;
            }
            if (item.updateCheck != null) {
                item.updateCheck.setException(download.getException());
                updateCheckManager.touchArtifact(session, item.updateCheck);
            }
            if (download.getException() == null) {
                item.resolved.set(true);
                item.result.setRepository(group.repository);
                Artifact artifact = download.getArtifact();
                try {
                    artifact = artifact.setFile(getFile(session, artifact, download.getFile()));
                    item.result.setArtifact(artifact);
                } catch (ArtifactTransferException e) {
                    item.result.addException(e);
                    continue;
                }
                lrm.add(session, new LocalArtifactRegistration(artifact, group.repository, download.getSupportedContexts()));
                artifactDownloaded(session, download.getTrace(), artifact, group.repository, null);
                artifactResolved(session, download.getTrace(), artifact, group.repository, null);
            } else {
                item.result.addException(download.getException());
                artifactDownloaded(session, download.getTrace(), download.getArtifact(), group.repository, download.getException());
            }
        }
    }
    for (ArtifactResult result : results) {
        ArtifactRequest request = result.getRequest();
        Artifact artifact = result.getArtifact();
        if (artifact == null || artifact.getFile() == null) {
            failures = true;
            if (result.getExceptions().isEmpty()) {
                Exception exception = new ArtifactNotFoundException(request.getArtifact(), null);
                result.addException(exception);
            }
            RequestTrace trace = DefaultRequestTrace.newChild(request.getTrace(), request);
            artifactResolved(session, trace, request.getArtifact(), null, result.getExceptions());
        }
    }
    if (failures) {
        throw new ArtifactResolutionException(results);
    }
    return results;
}
Also used : LocalArtifactRegistration(org.sonatype.aether.repository.LocalArtifactRegistration) ArrayList(java.util.ArrayList) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) RequestTrace(org.sonatype.aether.RequestTrace) DefaultRequestTrace(org.sonatype.aether.util.DefaultRequestTrace) ArtifactResolutionException(org.sonatype.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest) ArtifactTransferException(org.sonatype.aether.transfer.ArtifactTransferException) ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) LocalRepositoryManager(org.sonatype.aether.repository.LocalRepositoryManager) RepositoryConnector(org.sonatype.aether.spi.connector.RepositoryConnector) ArtifactNotFoundException(org.sonatype.aether.transfer.ArtifactNotFoundException) RepositoryPolicy(org.sonatype.aether.repository.RepositoryPolicy) LocalArtifactResult(org.sonatype.aether.repository.LocalArtifactResult) VersionRequest(org.sonatype.aether.resolution.VersionRequest) WorkspaceReader(org.sonatype.aether.repository.WorkspaceReader) Artifact(org.sonatype.aether.artifact.Artifact) NoRepositoryConnectorException(org.sonatype.aether.transfer.NoRepositoryConnectorException) ArtifactTransferException(org.sonatype.aether.transfer.ArtifactTransferException) ArtifactNotFoundException(org.sonatype.aether.transfer.ArtifactNotFoundException) ArtifactResolutionException(org.sonatype.aether.resolution.ArtifactResolutionException) VersionResolutionException(org.sonatype.aether.resolution.VersionResolutionException) IOException(java.io.IOException) LocalArtifactResult(org.sonatype.aether.repository.LocalArtifactResult) ArtifactResult(org.sonatype.aether.resolution.ArtifactResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UpdateCheck(org.sonatype.aether.impl.UpdateCheck) VersionResult(org.sonatype.aether.resolution.VersionResult) NoRepositoryConnectorException(org.sonatype.aether.transfer.NoRepositoryConnectorException) VersionResolutionException(org.sonatype.aether.resolution.VersionResolutionException) File(java.io.File) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest)

Example 2 with UpdateCheck

use of org.sonatype.aether.impl.UpdateCheck in project sonatype-aether by sonatype.

the class DefaultDeployer method upload.

private void upload(Collection<MetadataUpload> metadataUploads, RepositorySystemSession session, Metadata metadata, RemoteRepository repository, RepositoryConnector connector, EventCatapult catapult) throws DeploymentException {
    LocalRepositoryManager lrm = session.getLocalRepositoryManager();
    File basedir = lrm.getRepository().getBasedir();
    File dstFile = new File(basedir, lrm.getPathForRemoteMetadata(metadata, repository, ""));
    if (metadata instanceof MergeableMetadata) {
        if (!((MergeableMetadata) metadata).isMerged()) {
            {
                DefaultRepositoryEvent event = new DefaultRepositoryEvent(EventType.METADATA_RESOLVING, session, catapult.getTrace());
                event.setMetadata(metadata);
                event.setRepository(repository);
                repositoryEventDispatcher.dispatch(event);
                event = new DefaultRepositoryEvent(EventType.METADATA_DOWNLOADING, session, catapult.getTrace());
                event.setMetadata(metadata);
                event.setRepository(repository);
                repositoryEventDispatcher.dispatch(event);
            }
            RepositoryPolicy policy = getPolicy(session, repository, metadata.getNature());
            MetadataDownload download = new MetadataDownload();
            download.setMetadata(metadata);
            download.setFile(dstFile);
            download.setChecksumPolicy(policy.getChecksumPolicy());
            connector.get(null, Arrays.asList(download));
            Exception error = download.getException();
            if (error instanceof MetadataNotFoundException) {
                dstFile.delete();
            }
            {
                DefaultRepositoryEvent event = new DefaultRepositoryEvent(EventType.METADATA_DOWNLOADED, session, catapult.getTrace());
                event.setMetadata(metadata);
                event.setRepository(repository);
                event.setException(error);
                event.setFile(dstFile);
                repositoryEventDispatcher.dispatch(event);
                event = new DefaultRepositoryEvent(EventType.METADATA_RESOLVED, session, catapult.getTrace());
                event.setMetadata(metadata);
                event.setRepository(repository);
                event.setException(error);
                event.setFile(dstFile);
                repositoryEventDispatcher.dispatch(event);
            }
            if (error != null && !(error instanceof MetadataNotFoundException)) {
                throw new DeploymentException("Failed to retrieve remote metadata " + metadata + ": " + error.getMessage(), error);
            }
        }
        try {
            ((MergeableMetadata) metadata).merge(dstFile, dstFile);
        } catch (RepositoryException e) {
            throw new DeploymentException("Failed to update metadata " + metadata + ": " + e.getMessage(), e);
        }
    } else {
        if (metadata.getFile() == null) {
            throw new DeploymentException("Failed to update metadata " + metadata + ": No file attached.");
        }
        try {
            fileProcessor.copy(metadata.getFile(), dstFile, null);
        } catch (IOException e) {
            throw new DeploymentException("Failed to update metadata " + metadata + ": " + e.getMessage(), e);
        }
    }
    UpdateCheck<Metadata, MetadataTransferException> check = new UpdateCheck<Metadata, MetadataTransferException>();
    check.setItem(metadata);
    check.setFile(dstFile);
    check.setRepository(repository);
    check.setAuthoritativeRepository(repository);
    updateCheckManager.touchMetadata(session, check);
    metadataUploads.add(new MetadataUploadEx(metadata, dstFile, catapult));
}
Also used : RepositoryPolicy(org.sonatype.aether.repository.RepositoryPolicy) MergeableMetadata(org.sonatype.aether.metadata.MergeableMetadata) MergeableMetadata(org.sonatype.aether.metadata.MergeableMetadata) Metadata(org.sonatype.aether.metadata.Metadata) RepositoryException(org.sonatype.aether.RepositoryException) IOException(java.io.IOException) MetadataNotFoundException(org.sonatype.aether.transfer.MetadataNotFoundException) DeploymentException(org.sonatype.aether.deployment.DeploymentException) NoRepositoryConnectorException(org.sonatype.aether.transfer.NoRepositoryConnectorException) ArtifactTransferException(org.sonatype.aether.transfer.ArtifactTransferException) RepositoryException(org.sonatype.aether.RepositoryException) IOException(java.io.IOException) MetadataTransferException(org.sonatype.aether.transfer.MetadataTransferException) DefaultRepositoryEvent(org.sonatype.aether.util.listener.DefaultRepositoryEvent) MetadataNotFoundException(org.sonatype.aether.transfer.MetadataNotFoundException) UpdateCheck(org.sonatype.aether.impl.UpdateCheck) LocalRepositoryManager(org.sonatype.aether.repository.LocalRepositoryManager) MetadataTransferException(org.sonatype.aether.transfer.MetadataTransferException) DeploymentException(org.sonatype.aether.deployment.DeploymentException) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) File(java.io.File)

Example 3 with UpdateCheck

use of org.sonatype.aether.impl.UpdateCheck 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

File (java.io.File)3 UpdateCheck (org.sonatype.aether.impl.UpdateCheck)3 LocalRepositoryManager (org.sonatype.aether.repository.LocalRepositoryManager)3 RepositoryPolicy (org.sonatype.aether.repository.RepositoryPolicy)3 NoRepositoryConnectorException (org.sonatype.aether.transfer.NoRepositoryConnectorException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 RequestTrace (org.sonatype.aether.RequestTrace)2 Metadata (org.sonatype.aether.metadata.Metadata)2 RemoteRepository (org.sonatype.aether.repository.RemoteRepository)2 ArtifactTransferException (org.sonatype.aether.transfer.ArtifactTransferException)2 MetadataNotFoundException (org.sonatype.aether.transfer.MetadataNotFoundException)2 MetadataTransferException (org.sonatype.aether.transfer.MetadataTransferException)2 DefaultRequestTrace (org.sonatype.aether.util.DefaultRequestTrace)2 HashMap (java.util.HashMap)1 Executor (java.util.concurrent.Executor)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 RepositoryException (org.sonatype.aether.RepositoryException)1 Artifact (org.sonatype.aether.artifact.Artifact)1