Search in sources :

Example 21 with Transfer

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

the class MavenModelProcessorTest method resolvePluginVersionFromPropertyInProfile.

@Test
public void resolvePluginVersionFromPropertyInProfile() throws Exception {
    final URI src = new URI("http://nowhere.com/path/to/repo");
    final ProjectVersionRef childRef = new SimpleProjectVersionRef("org.test", "test-pom", "1.0");
    final LinkedHashMap<ProjectVersionRef, String> lineage = new LinkedHashMap<ProjectVersionRef, String>();
    lineage.put(childRef, "test-pom-1.0.pom.xml");
    final Location location = new SimpleLocation("test", src.toString(), false, true, true, false, true, false);
    final String base = PROJ_BASE + "version-expression-in-a-profile/";
    for (final Entry<ProjectVersionRef, String> entry : lineage.entrySet()) {
        final ProjectVersionRef ref = entry.getKey();
        final String filename = entry.getValue();
        final String path = ArtifactPathUtils.formatArtifactPath(ref.asPomArtifact(), fixture.getTypeMapper());
        fixture.getTransport().registerDownload(new ConcreteResource(location, path), new TestDownload(base + filename));
    }
    final Transfer transfer = fixture.getArtifactManager().retrieve(location, childRef.asPomArtifact());
    final MavenPomView pomView = fixture.getPomReader().read(childRef, transfer, Collections.singletonList(location));
    final List<PluginView> buildPlugins = pomView.getAllBuildPlugins();
    assertThat(buildPlugins, notNullValue());
    assertThat(buildPlugins.size(), equalTo(1));
    final PluginView pv = buildPlugins.get(0);
    assertThat(pv, notNullValue());
    assertThat(pv.getVersion(), equalTo("2.0"));
    final ModelProcessorConfig discoveryConfig = new ModelProcessorConfig();
    discoveryConfig.setIncludeManagedDependencies(true);
    discoveryConfig.setIncludeBuildSection(true);
    discoveryConfig.setIncludeManagedPlugins(false);
    EProjectDirectRelationships result = fixture.getModelProcessor().readRelationships(pomView, src, discoveryConfig);
    final Set<ProjectRelationship<?, ?>> rels = result.getExactAllRelationships();
    logger.info("Found {} relationships:\n\n  {}", rels.size(), new JoinString("\n  ", rels));
    boolean seen = false;
    for (final ProjectRelationship<?, ?> rel : rels) {
        if (rel.getType() == RelationshipType.PLUGIN && !rel.isManaged()) {
            if (seen) {
                fail("Multiple plugins found!");
            }
            seen = true;
            assertThat(rel.getTarget().getVersionString(), equalTo("2.0"));
        }
    }
    if (!seen) {
        fail("Plugin relationship not found!");
    }
}
Also used : TestDownload(org.commonjava.maven.galley.testing.core.transport.job.TestDownload) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) ProjectRelationship(org.commonjava.maven.atlas.graph.rel.ProjectRelationship) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Transfer(org.commonjava.maven.galley.model.Transfer) EProjectDirectRelationships(org.commonjava.maven.atlas.graph.model.EProjectDirectRelationships) PluginView(org.commonjava.maven.galley.maven.model.view.PluginView) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) MavenPomView(org.commonjava.maven.galley.maven.model.view.MavenPomView) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) Location(org.commonjava.maven.galley.model.Location) Test(org.junit.Test)

Example 22 with Transfer

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

the class MavenModelProcessorTest method resolveRepositoriesExpressionFromPropertyInProfile.

@Test
public void resolveRepositoriesExpressionFromPropertyInProfile() throws Exception {
    final URI src = new URI("http://nowhere.com/path/to/repo");
    final ProjectVersionRef childRef = new SimpleProjectVersionRef("org.test", "test-pom", "1.0");
    final LinkedHashMap<ProjectVersionRef, String> lineage = new LinkedHashMap<ProjectVersionRef, String>();
    lineage.put(childRef, "test-pom-1.0.pom.xml");
    final Location location = new SimpleLocation("test", src.toString(), false, true, true, false, true, false);
    final String base = PROJ_BASE + "resolve-expression-in-a-profile/";
    for (final Entry<ProjectVersionRef, String> entry : lineage.entrySet()) {
        final ProjectVersionRef ref = entry.getKey();
        final String filename = entry.getValue();
        final String path = ArtifactPathUtils.formatArtifactPath(ref.asPomArtifact(), fixture.getTypeMapper());
        fixture.getTransport().registerDownload(new ConcreteResource(location, path), new TestDownload(base + filename));
    }
    final Transfer transfer = fixture.getArtifactManager().retrieve(location, childRef.asPomArtifact());
    final MavenPomView pomView = fixture.getPomReader().read(childRef, transfer, Collections.singletonList(location));
    final List<RepositoryView> rvs = pomView.getAllRepositories();
    assertThat(rvs, notNullValue());
    assertThat(rvs.size(), equalTo(3));
    assertThat(rvs.get(0).getName(), equalTo("repo.one"));
    assertThat(rvs.get(0).getUrl(), equalTo("http://repo.one.repository"));
    assertThat(rvs.get(1).getName(), equalTo("test.oracle.repo"));
    assertThat(rvs.get(1).getUrl(), equalTo("http://test.oracle.repository"));
    assertThat(rvs.get(2).getName(), equalTo("test.second.oracle.repo"));
    assertThat(rvs.get(2).getUrl(), equalTo("http://another.test.two.oracle.repository"));
}
Also used : TestDownload(org.commonjava.maven.galley.testing.core.transport.job.TestDownload) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) RepositoryView(org.commonjava.maven.galley.maven.model.view.RepositoryView) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Transfer(org.commonjava.maven.galley.model.Transfer) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) MavenPomView(org.commonjava.maven.galley.maven.model.view.MavenPomView) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) Location(org.commonjava.maven.galley.model.Location) Test(org.junit.Test)

Example 23 with Transfer

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

the class DownloadHandlerConcurrencyTest method concurrentDownloadWaitsThenUsesFirstResult_SmallFile.

@BMRules(rules = { @BMRule(name = "init rendezvous", targetClass = "DownloadHandler", targetMethod = "<init>", targetLocation = "ENTRY", action = "debug(\"Creating rendezvous\"); createRendezvous(\"tsync\", 4);"), @BMRule(name = "rendezvous threads", targetClass = "DownloadHandler", targetMethod = "joinOrStart", targetLocation = "ENTRY", action = "debug(\"waiting for rendezvous\"); rendezvous(\"tsync\"); debug(\"proceeding\");") })
@Test
public void concurrentDownloadWaitsThenUsesFirstResult_SmallFile() throws Exception {
    final NotFoundCache nfc = new MemoryNotFoundCache();
    final TransportManagerConfig mgrConfig = new TransportManagerConfig();
    handler = new DownloadHandler(nfc, mgrConfig, executor);
    // NOTE: Coordinate with "init" @BMRule above!
    final int threads = 4;
    final int timeoutSeconds = 10;
    final String content = "this is a test " + System.currentTimeMillis();
    final String base = "repo";
    final String path = "this/is/the/path.txt";
    final String baseurl = server.formatUrl(base);
    // Serve the content EXACTLY ONCE. It should be cached / cache should be used after that.
    server.expect("GET", server.formatUrl(base, path), new ExpectationHandler() {

        private boolean sent = false;

        @Override
        public void handle(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
            if (!sent) {
                resp.setStatus(HttpServletResponse.SC_OK);
                resp.getWriter().write(content);
                sent = true;
            } else {
                throw new ServletException("Cannot write content more than once.");
            }
        }
    });
    final ConcreteResource resource = new ConcreteResource(new SimpleLocation(base, baseurl), path);
    final Transfer target = cacheProvider.getTransfer(resource);
    final CountDownLatch latch = new CountDownLatch(threads);
    final AtomicInteger successes = new AtomicInteger(0);
    final Logger logger = LoggerFactory.getLogger(getClass());
    // start threads, then wait for each to complete, and
    for (int i = 0; i < threads; i++) {
        executor.execute(new Runnable() {

            @Override
            public void run() {
                String oldName = Thread.currentThread().getName();
                try {
                    Thread.currentThread().setName("Download - " + oldName);
                    Transfer result = handler.download(resource, target, timeoutSeconds, transport, false, new EventMetadata());
                    try (InputStream in = result.openInputStream()) {
                        String resultContent = IOUtils.toString(in);
                        if (resultContent.equals(content)) {
                            successes.incrementAndGet();
                        } else {
                            logger.error("Expected content: '{}'\nActual content: '{}'", content, resultContent);
                        }
                    } catch (IOException e) {
                        logger.error("Failed to read transfer: " + e.getMessage(), e);
                    }
                } catch (TransferException e) {
                    logger.error("Failed to retrieve: " + e.getMessage(), e);
                } finally {
                    latch.countDown();
                    Thread.currentThread().setName(oldName);
                }
            }
        });
    }
}
Also used : InputStream(java.io.InputStream) TransportManagerConfig(org.commonjava.maven.galley.config.TransportManagerConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) MemoryNotFoundCache(org.commonjava.maven.galley.nfc.MemoryNotFoundCache) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Logger(org.slf4j.Logger) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) TransferException(org.commonjava.maven.galley.TransferException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExpectationHandler(org.commonjava.test.http.expect.ExpectationHandler) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Transfer(org.commonjava.maven.galley.model.Transfer) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) MemoryNotFoundCache(org.commonjava.maven.galley.nfc.MemoryNotFoundCache) NotFoundCache(org.commonjava.maven.galley.spi.nfc.NotFoundCache) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 24 with Transfer

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

the class MavenPomReader method getDocRef.

private DocRef<ProjectVersionRef> getDocRef(final ProjectVersionRef ref, final List<? extends Location> locations, final boolean cache, final EventMetadata eventMetadata) throws TransferException, GalleyMavenException {
    DocRef<ProjectVersionRef> dr = getFirstCached(ref, locations);
    if (dr == null) {
        final Transfer transfer = artifacts.retrieveFirst(locations, ref.asPomArtifact(), eventMetadata);
        if (transfer == null) {
            return null;
        }
        final Document doc = xml.parse(transfer, new EventMetadata());
        dr = new DocRef<>(ref, transfer.getLocation().toString(), doc);
        if (cache) {
            cache(dr);
        }
    }
    return dr;
}
Also used : ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) Transfer(org.commonjava.maven.galley.model.Transfer) Document(org.w3c.dom.Document) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 25 with Transfer

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

the class TransferManagerImpl method retrieveFirst.

@Override
public Transfer retrieveFirst(final VirtualResource virt, final EventMetadata eventMetadata) throws TransferException {
    Transfer target;
    TransferException lastError = null;
    int tries = 0;
    for (final ConcreteResource res : virt) {
        tries++;
        if (res == null) {
            continue;
        }
        try {
            target = retrieve(res, true, eventMetadata);
            lastError = null;
            if (target != null && target.exists()) {
                return target;
            }
        } catch (final TransferException e) {
            logger.warn("Failed to retrieve: {}. {} more tries. (Reason: {})", res, (virt.toConcreteResources().size() - tries), e.getMessage());
            lastError = e;
        }
    }
    if (lastError != null) {
        throw lastError;
    }
    fileEventManager.fire(new FileNotFoundEvent(virt, eventMetadata));
    return null;
}
Also used : TransferException(org.commonjava.maven.galley.TransferException) FileNotFoundEvent(org.commonjava.maven.galley.event.FileNotFoundEvent) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource)

Aggregations

Transfer (org.commonjava.maven.galley.model.Transfer)218 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)89 Test (org.junit.Test)80 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)64 IOException (java.io.IOException)63 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)59 InputStream (java.io.InputStream)43 StoreKey (org.commonjava.indy.model.core.StoreKey)43 Logger (org.slf4j.Logger)34 ArrayList (java.util.ArrayList)33 OutputStream (java.io.OutputStream)31 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)31 HostedRepository (org.commonjava.indy.model.core.HostedRepository)29 IndyDataException (org.commonjava.indy.data.IndyDataException)28 SimpleLocation (org.commonjava.maven.galley.model.SimpleLocation)28 Group (org.commonjava.indy.model.core.Group)26 Measure (org.commonjava.o11yphant.metrics.annotation.Measure)23 KeyedLocation (org.commonjava.indy.model.galley.KeyedLocation)21 TransferException (org.commonjava.maven.galley.TransferException)20 Location (org.commonjava.maven.galley.model.Location)19