Search in sources :

Example 1 with RepositoryConnector

use of org.sonatype.aether.spi.connector.RepositoryConnector 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 RepositoryConnector

use of org.sonatype.aether.spi.connector.RepositoryConnector 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 3 with RepositoryConnector

use of org.sonatype.aether.spi.connector.RepositoryConnector in project sonatype-aether by sonatype.

the class DefaultRemoteRepositoryManager method getRepositoryConnector.

public RepositoryConnector getRepositoryConnector(RepositorySystemSession session, RemoteRepository repository) throws NoRepositoryConnectorException {
    if (repository == null) {
        throw new IllegalArgumentException("remote repository has not been specified");
    }
    List<RepositoryConnectorFactory> factories = new ArrayList<RepositoryConnectorFactory>(connectorFactories);
    Collections.sort(factories, COMPARATOR);
    for (RepositoryConnectorFactory factory : factories) {
        try {
            RepositoryConnector connector = factory.newInstance(session, repository);
            if (logger.isDebugEnabled()) {
                StringBuilder buffer = new StringBuilder(256);
                buffer.append("Using connector ").append(connector.getClass().getSimpleName());
                buffer.append(" with priority ").append(factory.getPriority());
                buffer.append(" for ").append(repository.getUrl());
                Authentication auth = repository.getAuthentication();
                if (auth != null) {
                    buffer.append(" as ").append(auth.getUsername());
                }
                Proxy proxy = repository.getProxy();
                if (proxy != null) {
                    buffer.append(" via ").append(proxy.getHost()).append(':').append(proxy.getPort());
                    auth = proxy.getAuthentication();
                    if (auth != null) {
                        buffer.append(" as ").append(auth.getUsername());
                    }
                }
                logger.debug(buffer.toString());
            }
            return connector;
        } catch (NoRepositoryConnectorException e) {
        // continue and try next factory
        }
    }
    StringBuilder buffer = new StringBuilder(256);
    buffer.append("No connector available to access repository ");
    buffer.append(repository.getId());
    buffer.append(" (").append(repository.getUrl());
    buffer.append(") of type ").append(repository.getContentType());
    buffer.append(" using the available factories ");
    for (ListIterator<RepositoryConnectorFactory> it = factories.listIterator(); it.hasNext(); ) {
        RepositoryConnectorFactory factory = it.next();
        buffer.append(factory.getClass().getSimpleName());
        if (it.hasNext()) {
            buffer.append(", ");
        }
    }
    throw new NoRepositoryConnectorException(repository, buffer.toString());
}
Also used : Proxy(org.sonatype.aether.repository.Proxy) Authentication(org.sonatype.aether.repository.Authentication) NoRepositoryConnectorException(org.sonatype.aether.transfer.NoRepositoryConnectorException) ArrayList(java.util.ArrayList) RepositoryConnectorFactory(org.sonatype.aether.spi.connector.RepositoryConnectorFactory) RepositoryConnector(org.sonatype.aether.spi.connector.RepositoryConnector)

Example 4 with RepositoryConnector

use of org.sonatype.aether.spi.connector.RepositoryConnector in project sonatype-aether by sonatype.

the class ResumeGetTest method testResumeInterruptedDownloadUsingRangeRequests.

@Test
public void testResumeInterruptedDownloadUsingRangeRequests() throws Exception {
    FlakyHandler flakyHandler = new FlakyHandler(4);
    server.setHandler(flakyHandler);
    server.start();
    File file = TestFileUtils.createTempFile("");
    file.delete();
    ArtifactDownload download = new ArtifactDownload(artifact, "", file, RepositoryPolicy.CHECKSUM_POLICY_IGNORE);
    RemoteRepository repo = new RemoteRepository("test", "default", url());
    RepositoryConnector connector = factory.newInstance(session, repo);
    try {
        connector.get(Arrays.asList(download), null);
    } finally {
        connector.close();
    }
    assertNull(String.valueOf(download.getException()), download.getException());
    assertTrue("Missing " + file.getAbsolutePath(), file.isFile());
    assertEquals("Bad size of " + file.getAbsolutePath(), flakyHandler.totalSize, file.length());
    assertContentPattern(file);
}
Also used : ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) RepositoryConnector(org.sonatype.aether.spi.connector.RepositoryConnector) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) File(java.io.File) Test(org.junit.Test)

Example 5 with RepositoryConnector

use of org.sonatype.aether.spi.connector.RepositoryConnector in project sonatype-aether by sonatype.

the class ConnectorTestSuite method testFileHandleLeakage.

@Test
public void testFileHandleLeakage() throws IOException, NoRepositoryConnectorException {
    StubArtifact artifact = new StubArtifact("testGroup", "testArtifact", "", "jar", "1-test");
    StubMetadata metadata = new StubMetadata("testGroup", "testArtifact", "1-test", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT);
    RepositoryConnector connector = factory().newInstance(session, repository);
    File tmpFile = TestFileUtils.createTempFile("testFileHandleLeakage");
    ArtifactUpload artUp = new ArtifactUpload(artifact, tmpFile);
    connector.put(Arrays.asList(artUp), null);
    assertTrue("Leaking file handle in artifact upload", tmpFile.delete());
    tmpFile = TestFileUtils.createTempFile("testFileHandleLeakage");
    MetadataUpload metaUp = new MetadataUpload(metadata, tmpFile);
    connector.put(null, Arrays.asList(metaUp));
    assertTrue("Leaking file handle in metadata upload", tmpFile.delete());
    tmpFile = TestFileUtils.createTempFile("testFileHandleLeakage");
    ArtifactDownload artDown = new ArtifactDownload(artifact, null, tmpFile, null);
    connector.get(Arrays.asList(artDown), null);
    new File(tmpFile.getAbsolutePath() + ".sha1").deleteOnExit();
    assertTrue("Leaking file handle in artifact download", tmpFile.delete());
    tmpFile = TestFileUtils.createTempFile("testFileHandleLeakage");
    MetadataDownload metaDown = new MetadataDownload(metadata, null, tmpFile, null);
    connector.get(null, Arrays.asList(metaDown));
    new File(tmpFile.getAbsolutePath() + ".sha1").deleteOnExit();
    assertTrue("Leaking file handle in metadata download", tmpFile.delete());
    connector.close();
}
Also used : ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) ArtifactUpload(org.sonatype.aether.spi.connector.ArtifactUpload) StubArtifact(org.sonatype.aether.test.util.impl.StubArtifact) StubMetadata(org.sonatype.aether.test.util.impl.StubMetadata) RepositoryConnector(org.sonatype.aether.spi.connector.RepositoryConnector) MetadataUpload(org.sonatype.aether.spi.connector.MetadataUpload) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) File(java.io.File) Test(org.junit.Test)

Aggregations

RepositoryConnector (org.sonatype.aether.spi.connector.RepositoryConnector)13 File (java.io.File)11 ArtifactDownload (org.sonatype.aether.spi.connector.ArtifactDownload)10 Test (org.junit.Test)8 ArtifactUpload (org.sonatype.aether.spi.connector.ArtifactUpload)8 MetadataDownload (org.sonatype.aether.spi.connector.MetadataDownload)8 MetadataUpload (org.sonatype.aether.spi.connector.MetadataUpload)8 Artifact (org.sonatype.aether.artifact.Artifact)5 StubArtifact (org.sonatype.aether.test.util.impl.StubArtifact)4 StubMetadata (org.sonatype.aether.test.util.impl.StubMetadata)4 ArrayList (java.util.ArrayList)3 Metadata (org.sonatype.aether.metadata.Metadata)3 RemoteRepository (org.sonatype.aether.repository.RemoteRepository)3 NoRepositoryConnectorException (org.sonatype.aether.transfer.NoRepositoryConnectorException)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 RequestTrace (org.sonatype.aether.RequestTrace)2 MergeableMetadata (org.sonatype.aether.metadata.MergeableMetadata)2 RecordingTransferListener (org.sonatype.aether.test.impl.RecordingTransferListener)2 TransferEvent (org.sonatype.aether.transfer.TransferEvent)2