Search in sources :

Example 21 with Metadata

use of org.sonatype.aether.metadata.Metadata 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 22 with Metadata

use of org.sonatype.aether.metadata.Metadata in project sonatype-aether by sonatype.

the class DefaultInstaller method install.

private InstallResult install(SyncContext syncContext, RepositorySystemSession session, InstallRequest request) throws InstallationException {
    InstallResult result = new InstallResult(request);
    RequestTrace trace = DefaultRequestTrace.newChild(request.getTrace(), request);
    List<MetadataGenerator> generators = getMetadataGenerators(session, request);
    List<Artifact> artifacts = new ArrayList<Artifact>(request.getArtifacts());
    IdentityHashMap<Metadata, Object> processedMetadata = new IdentityHashMap<Metadata, Object>();
    List<Metadata> metadatas = Utils.prepareMetadata(generators, artifacts);
    syncContext.acquire(artifacts, Utils.combine(request.getMetadata(), metadatas));
    for (Metadata metadata : metadatas) {
        install(session, trace, metadata);
        processedMetadata.put(metadata, null);
        result.addMetadata(metadata);
    }
    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);
        install(session, trace, artifact);
        result.addArtifact(artifact);
    }
    metadatas = Utils.finishMetadata(generators, artifacts);
    syncContext.acquire(null, metadatas);
    for (Metadata metadata : metadatas) {
        install(session, trace, metadata);
        processedMetadata.put(metadata, null);
        result.addMetadata(metadata);
    }
    for (Metadata metadata : request.getMetadata()) {
        if (!processedMetadata.containsKey(metadata)) {
            install(session, trace, metadata);
            result.addMetadata(metadata);
        }
    }
    return result;
}
Also used : IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) MergeableMetadata(org.sonatype.aether.metadata.MergeableMetadata) Metadata(org.sonatype.aether.metadata.Metadata) RequestTrace(org.sonatype.aether.RequestTrace) DefaultRequestTrace(org.sonatype.aether.util.DefaultRequestTrace) InstallResult(org.sonatype.aether.installation.InstallResult) Artifact(org.sonatype.aether.artifact.Artifact) MetadataGenerator(org.sonatype.aether.impl.MetadataGenerator)

Example 23 with Metadata

use of org.sonatype.aether.metadata.Metadata in project sonatype-aether by sonatype.

the class DefaultMetadataResolver method resolveMetadata.

public List<MetadataResult> resolveMetadata(RepositorySystemSession session, Collection<? extends MetadataRequest> requests) {
    SyncContext syncContext = syncContextFactory.newInstance(session, false);
    try {
        Collection<Metadata> metadata = new ArrayList<Metadata>(requests.size());
        for (MetadataRequest request : requests) {
            metadata.add(request.getMetadata());
        }
        syncContext.acquire(null, metadata);
        return resolve(session, requests);
    } finally {
        syncContext.release();
    }
}
Also used : MetadataRequest(org.sonatype.aether.resolution.MetadataRequest) LocalMetadataRequest(org.sonatype.aether.repository.LocalMetadataRequest) ArrayList(java.util.ArrayList) Metadata(org.sonatype.aether.metadata.Metadata) SyncContext(org.sonatype.aether.SyncContext)

Example 24 with Metadata

use of org.sonatype.aether.metadata.Metadata 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)

Example 25 with Metadata

use of org.sonatype.aether.metadata.Metadata in project sonatype-aether by sonatype.

the class DefaultUpdateCheckManager method touchMetadata.

public void touchMetadata(RepositorySystemSession session, UpdateCheck<Metadata, MetadataTransferException> check) {
    Metadata metadata = check.getItem();
    File metadataFile = check.getFile();
    File touchFile = getTouchFile(metadata, metadataFile);
    String updateKey = getUpdateKey(metadataFile, check.getRepository());
    String dataKey = getDataKey(metadata, metadataFile, check.getAuthoritativeRepository());
    String transferKey = getTransferKey(metadata, metadataFile, check.getRepository());
    setUpdated(session.getData(), updateKey);
    write(touchFile, dataKey, transferKey, check.getException());
}
Also used : Metadata(org.sonatype.aether.metadata.Metadata) File(java.io.File)

Aggregations

Metadata (org.sonatype.aether.metadata.Metadata)27 Test (org.junit.Test)15 Artifact (org.sonatype.aether.artifact.Artifact)10 File (java.io.File)9 StubMetadata (org.sonatype.aether.test.util.impl.StubMetadata)9 MetadataUpload (org.sonatype.aether.spi.connector.MetadataUpload)8 RemoteRepository (org.sonatype.aether.repository.RemoteRepository)7 MetadataTransferException (org.sonatype.aether.transfer.MetadataTransferException)7 IOException (java.io.IOException)6 LocalMetadataResult (org.sonatype.aether.repository.LocalMetadataResult)6 StubArtifact (org.sonatype.aether.test.util.impl.StubArtifact)6 ArrayList (java.util.ArrayList)5 LocalMetadataRequest (org.sonatype.aether.repository.LocalMetadataRequest)5 LocalRepositoryManager (org.sonatype.aether.repository.LocalRepositoryManager)5 MetadataDownload (org.sonatype.aether.spi.connector.MetadataDownload)5 LocalRepository (org.sonatype.aether.repository.LocalRepository)4 MetadataNotFoundException (org.sonatype.aether.transfer.MetadataNotFoundException)4 RepositorySystemSession (org.sonatype.aether.RepositorySystemSession)3 RequestTrace (org.sonatype.aether.RequestTrace)3 LocalArtifactRegistration (org.sonatype.aether.repository.LocalArtifactRegistration)3