Search in sources :

Example 1 with MergeableMetadata

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

the class DefaultDeployerTest method testStaleLocalMetadataCopyGetsDeletedBeforeMergeWhenMetadataIsNotCurrentlyPresentInRemoteRepo.

@Test
public void testStaleLocalMetadataCopyGetsDeletedBeforeMergeWhenMetadataIsNotCurrentlyPresentInRemoteRepo() throws Exception {
    MergeableMetadata metadata = new MergeableMetadata() {

        public Metadata setFile(File file) {
            return this;
        }

        public String getVersion() {
            return "";
        }

        public String getType() {
            return "test.properties";
        }

        public Nature getNature() {
            return Nature.RELEASE;
        }

        public String getGroupId() {
            return "org";
        }

        public File getFile() {
            return null;
        }

        public String getArtifactId() {
            return "aether";
        }

        public void merge(File current, File result) throws RepositoryException {
            Properties props = new Properties();
            try {
                if (current.isFile()) {
                    TestFileUtils.read(props, current);
                }
                props.setProperty("new", "value");
                TestFileUtils.write(props, result);
            } catch (IOException e) {
                throw new RepositoryException(e.getMessage(), e);
            }
        }

        public boolean isMerged() {
            return false;
        }
    };
    manager.setConnector(new RepositoryConnector() {

        public void put(Collection<? extends ArtifactUpload> artifactUploads, Collection<? extends MetadataUpload> metadataUploads) {
        }

        public void get(Collection<? extends ArtifactDownload> artifactDownloads, Collection<? extends MetadataDownload> metadataDownloads) {
            if (metadataDownloads != null) {
                for (MetadataDownload download : metadataDownloads) {
                    download.setException(new MetadataNotFoundException(download.getMetadata(), null, null));
                }
            }
        }

        public void close() {
        }
    });
    request.addMetadata(metadata);
    File metadataFile = new File(session.getLocalRepository().getBasedir(), session.getLocalRepositoryManager().getPathForRemoteMetadata(metadata, request.getRepository(), ""));
    Properties props = new Properties();
    props.setProperty("old", "value");
    TestFileUtils.write(props, metadataFile);
    deployer.deploy(session, request);
    props = new Properties();
    TestFileUtils.read(props, metadataFile);
    assertNull(props.toString(), props.get("old"));
}
Also used : MetadataNotFoundException(org.sonatype.aether.transfer.MetadataNotFoundException) MergeableMetadata(org.sonatype.aether.metadata.MergeableMetadata) RepositoryConnector(org.sonatype.aether.spi.connector.RepositoryConnector) RepositoryException(org.sonatype.aether.RepositoryException) IOException(java.io.IOException) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) Properties(java.util.Properties) File(java.io.File) Test(org.junit.Test)

Example 2 with MergeableMetadata

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

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

the class DefaultInstaller method install.

private void install(RepositorySystemSession session, RequestTrace trace, Metadata metadata) throws InstallationException {
    LocalRepositoryManager lrm = session.getLocalRepositoryManager();
    File dstFile = new File(lrm.getRepository().getBasedir(), lrm.getPathForLocalMetadata(metadata));
    metadataInstalling(session, trace, metadata, dstFile);
    Exception exception = null;
    try {
        if (metadata instanceof MergeableMetadata) {
            ((MergeableMetadata) metadata).merge(dstFile, dstFile);
        } else {
            fileProcessor.copy(metadata.getFile(), dstFile, null);
        }
        lrm.add(session, new LocalMetadataRegistration(metadata));
    } catch (Exception e) {
        exception = e;
        throw new InstallationException("Failed to install metadata " + metadata + ": " + e.getMessage(), e);
    } finally {
        metadataInstalled(session, trace, metadata, dstFile, exception);
    }
}
Also used : InstallationException(org.sonatype.aether.installation.InstallationException) LocalRepositoryManager(org.sonatype.aether.repository.LocalRepositoryManager) MergeableMetadata(org.sonatype.aether.metadata.MergeableMetadata) LocalMetadataRegistration(org.sonatype.aether.repository.LocalMetadataRegistration) File(java.io.File) InstallationException(org.sonatype.aether.installation.InstallationException)

Aggregations

File (java.io.File)3 MergeableMetadata (org.sonatype.aether.metadata.MergeableMetadata)3 IOException (java.io.IOException)2 RepositoryException (org.sonatype.aether.RepositoryException)2 LocalRepositoryManager (org.sonatype.aether.repository.LocalRepositoryManager)2 MetadataDownload (org.sonatype.aether.spi.connector.MetadataDownload)2 MetadataNotFoundException (org.sonatype.aether.transfer.MetadataNotFoundException)2 Properties (java.util.Properties)1 Test (org.junit.Test)1 DeploymentException (org.sonatype.aether.deployment.DeploymentException)1 UpdateCheck (org.sonatype.aether.impl.UpdateCheck)1 InstallationException (org.sonatype.aether.installation.InstallationException)1 Metadata (org.sonatype.aether.metadata.Metadata)1 LocalMetadataRegistration (org.sonatype.aether.repository.LocalMetadataRegistration)1 RepositoryPolicy (org.sonatype.aether.repository.RepositoryPolicy)1 RepositoryConnector (org.sonatype.aether.spi.connector.RepositoryConnector)1 ArtifactTransferException (org.sonatype.aether.transfer.ArtifactTransferException)1 MetadataTransferException (org.sonatype.aether.transfer.MetadataTransferException)1 NoRepositoryConnectorException (org.sonatype.aether.transfer.NoRepositoryConnectorException)1 DefaultRepositoryEvent (org.sonatype.aether.util.listener.DefaultRepositoryEvent)1