Search in sources :

Example 1 with Resource

use of org.commonjava.maven.galley.model.Resource in project galley by Commonjava.

the class TransferManagerImpl method doBatch.

private <T extends TransferBatch> T doBatch(final Set<Resource> resources, final T batch, final boolean suppressFailures, final EventMetadata eventMetadata) throws TransferException {
    logger.info("Attempting to batch-retrieve {} resources:\n  {}", resources.size(), new JoinString("\n  ", resources));
    final Set<BatchRetriever> retrievers = new HashSet<BatchRetriever>(resources.size());
    for (final Resource resource : resources) {
        retrievers.add(new BatchRetriever(this, resource, suppressFailures, eventMetadata));
    }
    final Map<ConcreteResource, TransferException> errors = new HashMap<ConcreteResource, TransferException>();
    final Map<ConcreteResource, Transfer> transfers = new HashMap<ConcreteResource, Transfer>();
    do {
        for (final BatchRetriever retriever : retrievers) {
            batchExecutor.submit(retriever);
        }
        int count = retrievers.size();
        for (int i = 0; i < count; i++) {
            try {
                Future<BatchRetriever> pending = batchExecutor.take();
                BatchRetriever retriever = pending.get();
                final ConcreteResource resource = retriever.getLastTry();
                final TransferException error = retriever.getError();
                if (error != null) {
                    logger.warn("ERROR: {}...{}", error, resource, error.getMessage());
                    retrievers.remove(retriever);
                    if (!(error instanceof TransferLocationException)) {
                        errors.put(resource, error);
                    }
                    continue;
                }
                final Transfer transfer = retriever.getTransfer();
                if (transfer != null && transfer.exists()) {
                    transfers.put(resource, transfer);
                    retrievers.remove(retriever);
                    logger.debug("Completed: {}", resource);
                    continue;
                }
                if (!retriever.hasMoreTries()) {
                    logger.debug("Not completed, but out of tries: {}", resource);
                    retrievers.remove(retriever);
                }
            } catch (final InterruptedException e) {
                logger.error(String.format("Failed to wait for batch retrieval attempts to complete: %s", e.getMessage()), e);
                break;
            } catch (ExecutionException e) {
                logger.error(String.format("Failed to retrieve next completed retrieval: %s", e.getMessage()), e);
            }
        }
    } while (!retrievers.isEmpty());
    batch.setErrors(errors);
    batch.setTransfers(transfers);
    return batch;
}
Also used : HashMap(java.util.HashMap) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Resource(org.commonjava.maven.galley.model.Resource) VirtualResource(org.commonjava.maven.galley.model.VirtualResource) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) BatchRetriever(org.commonjava.maven.galley.internal.xfer.BatchRetriever) TransferException(org.commonjava.maven.galley.TransferException) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Transfer(org.commonjava.maven.galley.model.Transfer) TransferLocationException(org.commonjava.maven.galley.TransferLocationException) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet)

Example 2 with Resource

use of org.commonjava.maven.galley.model.Resource in project galley by Commonjava.

the class ArtifactBatch method setArtifactToResourceMapping.

public void setArtifactToResourceMapping(final Map<ArtifactRef, Resource> mappings) {
    artifactMappings = new HashMap<ArtifactRef, List<ConcreteResource>>();
    final Set<ConcreteResource> resources = new HashSet<ConcreteResource>();
    for (final Entry<ArtifactRef, Resource> entry : mappings.entrySet()) {
        final ArtifactRef artifact = entry.getKey();
        final Resource resource = entry.getValue();
        if (resource instanceof ConcreteResource) {
            artifactMappings.put(artifact, Collections.singletonList((ConcreteResource) resource));
            resources.add((ConcreteResource) resource);
        } else {
            final List<ConcreteResource> res = ((VirtualResource) resource).toConcreteResources();
            artifactMappings.put(artifact, res);
            resources.addAll(res);
        }
    }
    setResources(resources);
}
Also used : ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Resource(org.commonjava.maven.galley.model.Resource) VirtualResource(org.commonjava.maven.galley.model.VirtualResource) List(java.util.List) VirtualResource(org.commonjava.maven.galley.model.VirtualResource) SimpleArtifactRef(org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef) ArtifactRef(org.commonjava.maven.atlas.ident.ref.ArtifactRef) HashSet(java.util.HashSet)

Example 3 with Resource

use of org.commonjava.maven.galley.model.Resource in project galley by Commonjava.

the class AbstractTransferManagerTest method batchRetrieve_returnEmptyResultIfVirtualResourceIsEmpty.

/**
     * Addresses Issue #27 (https://github.com/Commonjava/galley/issues/27). Batches with virtual resources
     * that contain zero concrete resources should count down the latch when they return immediately, but 
     * instead are causing the countdown latch that watches the batch of transfers to hang.
     */
@Test(timeout = 2000)
public void batchRetrieve_returnEmptyResultIfVirtualResourceIsEmpty() throws Exception {
    final VirtualResource vr = new VirtualResource(Collections.<Location>emptyList(), "/path/to/nowhere");
    final TransferBatch batch = getTransferManagerImpl().batchRetrieve(new TransferBatch(Collections.<Resource>singleton(vr)), new EventMetadata());
    assertThat(batch, notNullValue());
    assertThat(batch.getErrors().isEmpty(), equalTo(true));
    assertThat(batch.getTransfers().isEmpty(), equalTo(true));
}
Also used : ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Resource(org.commonjava.maven.galley.model.Resource) VirtualResource(org.commonjava.maven.galley.model.VirtualResource) VirtualResource(org.commonjava.maven.galley.model.VirtualResource) TransferBatch(org.commonjava.maven.galley.model.TransferBatch) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Example 4 with Resource

use of org.commonjava.maven.galley.model.Resource in project galley by Commonjava.

the class ArtifactManagerImpl method resolveArtifactMappings.

private void resolveArtifactMappings(final ArtifactBatch batch, final EventMetadata eventMetadata) throws TransferException {
    final Map<ArtifactRef, Resource> resources = new HashMap<ArtifactRef, Resource>(batch.size());
    for (final ArtifactRef artifact : batch) {
        final VirtualResource virt = resolveFirstVirtualResource(batch.getLocations(artifact), artifact, eventMetadata);
        resources.put(artifact, virt);
    }
    batch.setArtifactToResourceMapping(resources);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Resource(org.commonjava.maven.galley.model.Resource) VirtualResource(org.commonjava.maven.galley.model.VirtualResource) VirtualResource(org.commonjava.maven.galley.model.VirtualResource)

Aggregations

ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)4 Resource (org.commonjava.maven.galley.model.Resource)4 VirtualResource (org.commonjava.maven.galley.model.VirtualResource)4 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 ArtifactRef (org.commonjava.maven.atlas.ident.ref.ArtifactRef)1 SimpleArtifactRef (org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef)1 JoinString (org.commonjava.maven.atlas.ident.util.JoinString)1 TransferException (org.commonjava.maven.galley.TransferException)1 TransferLocationException (org.commonjava.maven.galley.TransferLocationException)1 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)1 BatchRetriever (org.commonjava.maven.galley.internal.xfer.BatchRetriever)1 Transfer (org.commonjava.maven.galley.model.Transfer)1 TransferBatch (org.commonjava.maven.galley.model.TransferBatch)1 Test (org.junit.Test)1