Search in sources :

Example 1 with RequestTrace

use of org.sonatype.aether.RequestTrace in project sonatype-aether by sonatype.

the class DefaultDeployer method deploy.

private DeployResult deploy(SyncContext syncContext, RepositorySystemSession session, DeployRequest request) throws DeploymentException {
    DeployResult result = new DeployResult(request);
    RequestTrace trace = DefaultRequestTrace.newChild(request.getTrace(), request);
    RemoteRepository repository = request.getRepository();
    RepositoryConnector connector;
    try {
        connector = remoteRepositoryManager.getRepositoryConnector(session, repository);
    } catch (NoRepositoryConnectorException e) {
        throw new DeploymentException("Failed to deploy artifacts/metadata: " + e.getMessage(), e);
    }
    List<MetadataGenerator> generators = getMetadataGenerators(session, request);
    try {
        List<ArtifactUpload> artifactUploads = new ArrayList<ArtifactUpload>();
        List<MetadataUpload> metadataUploads = new ArrayList<MetadataUpload>();
        IdentityHashMap<Metadata, Object> processedMetadata = new IdentityHashMap<Metadata, Object>();
        EventCatapult catapult = new EventCatapult(session, trace, repository, repositoryEventDispatcher);
        List<Artifact> artifacts = new ArrayList<Artifact>(request.getArtifacts());
        List<Metadata> metadatas = Utils.prepareMetadata(generators, artifacts);
        syncContext.acquire(artifacts, Utils.combine(request.getMetadata(), metadatas));
        for (Metadata metadata : metadatas) {
            upload(metadataUploads, session, metadata, repository, connector, catapult);
            processedMetadata.put(metadata, null);
        }
        for (int i = 0; i < artifacts.size(); i++) {
            Artifact artifact = artifacts.get(i);
            for (MetadataGenerator generator : generators) {
                artifact = generator.transformArtifact(artifact);
            }
            artifacts.set(i, artifact);
            artifactUploads.add(new ArtifactUploadEx(artifact, artifact.getFile(), catapult));
        }
        connector.put(artifactUploads, null);
        for (ArtifactUpload upload : artifactUploads) {
            if (upload.getException() != null) {
                throw new DeploymentException("Failed to deploy artifacts: " + upload.getException().getMessage(), upload.getException());
            }
            result.addArtifact(upload.getArtifact());
        }
        metadatas = Utils.finishMetadata(generators, artifacts);
        syncContext.acquire(null, metadatas);
        for (Metadata metadata : metadatas) {
            upload(metadataUploads, session, metadata, repository, connector, catapult);
            processedMetadata.put(metadata, null);
        }
        for (Metadata metadata : request.getMetadata()) {
            if (!processedMetadata.containsKey(metadata)) {
                upload(metadataUploads, session, metadata, repository, connector, catapult);
                processedMetadata.put(metadata, null);
            }
        }
        connector.put(null, metadataUploads);
        for (MetadataUpload upload : metadataUploads) {
            if (upload.getException() != null) {
                throw new DeploymentException("Failed to deploy metadata: " + upload.getException().getMessage(), upload.getException());
            }
            result.addMetadata(upload.getMetadata());
        }
    } finally {
        connector.close();
    }
    return result;
}
Also used : ArtifactUpload(org.sonatype.aether.spi.connector.ArtifactUpload) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) MergeableMetadata(org.sonatype.aether.metadata.MergeableMetadata) Metadata(org.sonatype.aether.metadata.Metadata) MetadataUpload(org.sonatype.aether.spi.connector.MetadataUpload) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) RequestTrace(org.sonatype.aether.RequestTrace) DefaultRequestTrace(org.sonatype.aether.util.DefaultRequestTrace) Artifact(org.sonatype.aether.artifact.Artifact) DeployResult(org.sonatype.aether.deployment.DeployResult) NoRepositoryConnectorException(org.sonatype.aether.transfer.NoRepositoryConnectorException) RepositoryConnector(org.sonatype.aether.spi.connector.RepositoryConnector) DeploymentException(org.sonatype.aether.deployment.DeploymentException) MetadataGenerator(org.sonatype.aether.impl.MetadataGenerator)

Example 2 with RequestTrace

use of org.sonatype.aether.RequestTrace in project sonatype-aether by sonatype.

the class DefaultRequestTraceTest method testParentChaining.

@Test
public void testParentChaining() {
    RequestTrace trace1 = new DefaultRequestTrace(null);
    RequestTrace trace2 = trace1.newChild(this);
    assertSame(null, trace1.getParent());
    assertSame(null, trace1.getData());
    assertSame(trace1, trace2.getParent());
    assertSame(this, trace2.getData());
}
Also used : RequestTrace(org.sonatype.aether.RequestTrace) Test(org.junit.Test)

Example 3 with RequestTrace

use of org.sonatype.aether.RequestTrace in project sonatype-aether by sonatype.

the class DefaultRequestTraceTest method testNewChildRequestTrace.

@Test
public void testNewChildRequestTrace() {
    RequestTrace trace = DefaultRequestTrace.newChild(null, this);
    assertNotNull(trace);
    assertSame(null, trace.getParent());
    assertSame(this, trace.getData());
}
Also used : RequestTrace(org.sonatype.aether.RequestTrace) Test(org.junit.Test)

Example 4 with RequestTrace

use of org.sonatype.aether.RequestTrace 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 5 with RequestTrace

use of org.sonatype.aether.RequestTrace in project sonatype-aether by sonatype.

the class DefaultDependencyCollector method collectDependencies.

public CollectResult collectDependencies(RepositorySystemSession session, CollectRequest request) throws DependencyCollectionException {
    session = optimizeSession(session);
    RequestTrace trace = DefaultRequestTrace.newChild(request.getTrace(), request);
    CollectResult result = new CollectResult(request);
    DependencySelector depSelector = session.getDependencySelector();
    DependencyManager depManager = session.getDependencyManager();
    DependencyTraverser depTraverser = session.getDependencyTraverser();
    Dependency root = request.getRoot();
    List<RemoteRepository> repositories = request.getRepositories();
    List<Dependency> dependencies = request.getDependencies();
    List<Dependency> managedDependencies = request.getManagedDependencies();
    GraphEdge edge = null;
    if (root != null) {
        VersionRangeResult rangeResult;
        try {
            VersionRangeRequest rangeRequest = new VersionRangeRequest(root.getArtifact(), request.getRepositories(), request.getRequestContext());
            rangeRequest.setTrace(trace);
            rangeResult = versionRangeResolver.resolveVersionRange(session, rangeRequest);
            if (rangeResult.getVersions().isEmpty()) {
                throw new VersionRangeResolutionException(rangeResult, "No versions available for " + root.getArtifact() + " within specified range");
            }
        } catch (VersionRangeResolutionException e) {
            result.addException(e);
            throw new DependencyCollectionException(result);
        }
        Version version = rangeResult.getVersions().get(rangeResult.getVersions().size() - 1);
        root = root.setArtifact(root.getArtifact().setVersion(version.toString()));
        ArtifactDescriptorResult descriptorResult;
        try {
            ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
            descriptorRequest.setArtifact(root.getArtifact());
            descriptorRequest.setRepositories(request.getRepositories());
            descriptorRequest.setRequestContext(request.getRequestContext());
            descriptorRequest.setTrace(trace);
            if (isLackingDescriptor(root.getArtifact())) {
                descriptorResult = new ArtifactDescriptorResult(descriptorRequest);
            } else {
                descriptorResult = descriptorReader.readArtifactDescriptor(session, descriptorRequest);
            }
        } catch (ArtifactDescriptorException e) {
            result.addException(e);
            throw new DependencyCollectionException(result);
        }
        root = root.setArtifact(descriptorResult.getArtifact());
        repositories = remoteRepositoryManager.aggregateRepositories(session, repositories, descriptorResult.getRepositories(), true);
        dependencies = mergeDeps(dependencies, descriptorResult.getDependencies());
        managedDependencies = mergeDeps(managedDependencies, descriptorResult.getManagedDependencies());
        GraphNode node = new GraphNode();
        node.setAliases(descriptorResult.getAliases());
        node.setRepositories(request.getRepositories());
        edge = new GraphEdge(node);
        edge.setDependency(root);
        edge.setRequestContext(request.getRequestContext());
        edge.setRelocations(descriptorResult.getRelocations());
        edge.setVersionConstraint(rangeResult.getVersionConstraint());
        edge.setVersion(version);
    } else {
        edge = new GraphEdge(new GraphNode());
    }
    result.setRoot(edge);
    boolean traverse = (root == null) || depTraverser.traverseDependency(root);
    if (traverse && !dependencies.isEmpty()) {
        DataPool pool = new DataPool(session);
        EdgeStack edges = new EdgeStack();
        edges.push(edge);
        DefaultDependencyCollectionContext context = new DefaultDependencyCollectionContext(session, root, managedDependencies);
        Args args = new Args(result, session, trace, pool, edges, context);
        process(args, dependencies, repositories, depSelector.deriveChildSelector(context), depManager.deriveChildManager(context), depTraverser.deriveChildTraverser(context));
    }
    DependencyGraphTransformer transformer = session.getDependencyGraphTransformer();
    try {
        DefaultDependencyGraphTransformationContext context = new DefaultDependencyGraphTransformationContext(session);
        result.setRoot(transformer.transformGraph(edge, context));
    } catch (RepositoryException e) {
        result.addException(e);
    }
    if (!result.getExceptions().isEmpty()) {
        throw new DependencyCollectionException(result);
    }
    return result;
}
Also used : DependencyManager(org.sonatype.aether.collection.DependencyManager) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) RequestTrace(org.sonatype.aether.RequestTrace) DefaultRequestTrace(org.sonatype.aether.util.DefaultRequestTrace) DependencyTraverser(org.sonatype.aether.collection.DependencyTraverser) Version(org.sonatype.aether.version.Version) DependencyGraphTransformer(org.sonatype.aether.collection.DependencyGraphTransformer) VersionRangeRequest(org.sonatype.aether.resolution.VersionRangeRequest) ArtifactDescriptorRequest(org.sonatype.aether.resolution.ArtifactDescriptorRequest) VersionRangeResolutionException(org.sonatype.aether.resolution.VersionRangeResolutionException) DependencyCollectionException(org.sonatype.aether.collection.DependencyCollectionException) VersionRangeResult(org.sonatype.aether.resolution.VersionRangeResult) CollectResult(org.sonatype.aether.collection.CollectResult) RepositoryException(org.sonatype.aether.RepositoryException) Dependency(org.sonatype.aether.graph.Dependency) DependencySelector(org.sonatype.aether.collection.DependencySelector) ArtifactDescriptorResult(org.sonatype.aether.resolution.ArtifactDescriptorResult) ArtifactDescriptorException(org.sonatype.aether.resolution.ArtifactDescriptorException)

Aggregations

RequestTrace (org.sonatype.aether.RequestTrace)9 DefaultRequestTrace (org.sonatype.aether.util.DefaultRequestTrace)7 ArrayList (java.util.ArrayList)4 RemoteRepository (org.sonatype.aether.repository.RemoteRepository)4 Artifact (org.sonatype.aether.artifact.Artifact)3 Metadata (org.sonatype.aether.metadata.Metadata)3 ArtifactRequest (org.sonatype.aether.resolution.ArtifactRequest)3 ArtifactResolutionException (org.sonatype.aether.resolution.ArtifactResolutionException)3 ArtifactResult (org.sonatype.aether.resolution.ArtifactResult)3 NoRepositoryConnectorException (org.sonatype.aether.transfer.NoRepositoryConnectorException)3 File (java.io.File)2 IdentityHashMap (java.util.IdentityHashMap)2 Test (org.junit.Test)2 CollectResult (org.sonatype.aether.collection.CollectResult)2 DependencyCollectionException (org.sonatype.aether.collection.DependencyCollectionException)2 DependencyVisitor (org.sonatype.aether.graph.DependencyVisitor)2 MetadataGenerator (org.sonatype.aether.impl.MetadataGenerator)2 UpdateCheck (org.sonatype.aether.impl.UpdateCheck)2 MergeableMetadata (org.sonatype.aether.metadata.MergeableMetadata)2 LocalRepositoryManager (org.sonatype.aether.repository.LocalRepositoryManager)2