Search in sources :

Example 6 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 7 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 8 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 9 with RepositoryConnector

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

the class ConnectorTestSuite method testBlocking.

@Test
public void testBlocking() throws NoRepositoryConnectorException, IOException {
    RepositoryConnector connector = factory().newInstance(session, repository);
    int count = 10;
    byte[] pattern = "tmpFile".getBytes("UTF-8");
    File tmpFile = TestFileUtils.createTempFile(pattern, 100000);
    List<ArtifactUpload> artUps = ConnectorTestUtils.createTransfers(ArtifactUpload.class, count, tmpFile);
    List<MetadataUpload> metaUps = ConnectorTestUtils.createTransfers(MetadataUpload.class, count, tmpFile);
    List<ArtifactDownload> artDowns = ConnectorTestUtils.createTransfers(ArtifactDownload.class, count, null);
    List<MetadataDownload> metaDowns = ConnectorTestUtils.createTransfers(MetadataDownload.class, count, null);
    // this should block until all transfers are done - racing condition, better way to test this?
    connector.put(artUps, metaUps);
    connector.get(artDowns, metaDowns);
    for (int i = 0; i < count; i++) {
        ArtifactUpload artUp = artUps.get(i);
        MetadataUpload metaUp = metaUps.get(i);
        ArtifactDownload artDown = artDowns.get(i);
        MetadataDownload metaDown = metaDowns.get(i);
        assertTrue(Transfer.State.DONE.equals(artUp.getState()));
        assertTrue(Transfer.State.DONE.equals(artDown.getState()));
        assertTrue(Transfer.State.DONE.equals(metaUp.getState()));
        assertTrue(Transfer.State.DONE.equals(metaDown.getState()));
    }
    connector.close();
}
Also used : ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) ArtifactUpload(org.sonatype.aether.spi.connector.ArtifactUpload) 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)

Example 10 with RepositoryConnector

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

the class ConnectorTestSuite method testProgressEventsDataBuffer.

@Test
public void testProgressEventsDataBuffer() throws UnsupportedEncodingException, IOException, NoSuchAlgorithmException, NoRepositoryConnectorException {
    byte[] bytes = "These are the test contents.\n".getBytes("UTF-8");
    int count = 120000;
    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    for (int i = 0; i < count; i++) {
        digest.update(bytes);
    }
    byte[] hash = digest.digest();
    File file = TestFileUtils.createTempFile(bytes, count);
    Artifact artifact = new StubArtifact("gid:aid:ext:ver");
    ArtifactUpload upA = new ArtifactUpload(artifact, file);
    File dir = TestFileUtils.createTempDir("con-test");
    File downAFile = new File(dir, "downA.file");
    downAFile.deleteOnExit();
    ArtifactDownload downA = new ArtifactDownload(artifact, "", downAFile, RepositoryPolicy.CHECKSUM_POLICY_FAIL);
    Metadata metadata = new StubMetadata("gid", "aid", "ver", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT);
    MetadataUpload upM = new MetadataUpload(metadata, file);
    File downMFile = new File(dir, "downM.file");
    downMFile.deleteOnExit();
    MetadataDownload downM = new MetadataDownload(metadata, "", downMFile, RepositoryPolicy.CHECKSUM_POLICY_FAIL);
    DigestingTransferListener listener = new DigestingTransferListener();
    session.setTransferListener(listener);
    RepositoryConnector connector = factory().newInstance(session, repository);
    connector.put(Arrays.asList(upA), null);
    assertArrayEquals(hash, listener.getHash());
    listener.rewind();
    connector.put(null, Arrays.asList(upM));
    assertArrayEquals(hash, listener.getHash());
    listener.rewind();
    connector.get(Arrays.asList(downA), null);
    assertArrayEquals(hash, listener.getHash());
    listener.rewind();
    connector.get(null, Arrays.asList(downM));
    assertArrayEquals(hash, listener.getHash());
    listener.rewind();
    connector.close();
}
Also used : ArtifactUpload(org.sonatype.aether.spi.connector.ArtifactUpload) StubMetadata(org.sonatype.aether.test.util.impl.StubMetadata) Metadata(org.sonatype.aether.metadata.Metadata) MetadataUpload(org.sonatype.aether.spi.connector.MetadataUpload) StubArtifact(org.sonatype.aether.test.util.impl.StubArtifact) Artifact(org.sonatype.aether.artifact.Artifact) ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) StubArtifact(org.sonatype.aether.test.util.impl.StubArtifact) StubMetadata(org.sonatype.aether.test.util.impl.StubMetadata) RepositoryConnector(org.sonatype.aether.spi.connector.RepositoryConnector) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) MessageDigest(java.security.MessageDigest) 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