Search in sources :

Example 56 with Resolver

use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.

the class MavenProxyServletSupportTest method testDownloadMetadata.

@Test(timeout = 30000)
public void testDownloadMetadata() throws Exception {
    final String old = System.getProperty("karaf.data");
    System.setProperty("karaf.data", new File("target").getCanonicalPath());
    FileUtils.deleteDirectory(new File("target/tmp"));
    Server server = new Server(0);
    server.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            String result = null;
            if ("/repo1/org/apache/camel/camel-core/maven-metadata.xml".equals(target)) {
                result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n" + "  <groupId>org.apache.camel</groupId>\n" + "  <artifactId>camel-core</artifactId>\n" + "  <versioning>\n" + "    <latest>2.14.0</latest>\n" + "    <release>2.14.0</release>\n" + "    <versions>\n" + "      <version>1.6.1</version>\n" + "      <version>1.6.2</version>\n" + "      <version>1.6.3</version>\n" + "      <version>1.6.4</version>\n" + "      <version>2.0-M2</version>\n" + "      <version>2.0-M3</version>\n" + "      <version>2.0.0</version>\n" + "      <version>2.1.0</version>\n" + "      <version>2.2.0</version>\n" + "      <version>2.3.0</version>\n" + "      <version>2.4.0</version>\n" + "      <version>2.5.0</version>\n" + "      <version>2.6.0</version>\n" + "      <version>2.7.0</version>\n" + "      <version>2.7.1</version>\n" + "      <version>2.7.2</version>\n" + "      <version>2.7.3</version>\n" + "      <version>2.7.4</version>\n" + "      <version>2.7.5</version>\n" + "      <version>2.8.0</version>\n" + "      <version>2.8.1</version>\n" + "      <version>2.8.2</version>\n" + "      <version>2.8.3</version>\n" + "      <version>2.8.4</version>\n" + "      <version>2.8.5</version>\n" + "      <version>2.8.6</version>\n" + "      <version>2.9.0-RC1</version>\n" + "      <version>2.9.0</version>\n" + "      <version>2.9.1</version>\n" + "      <version>2.9.2</version>\n" + "      <version>2.9.3</version>\n" + "      <version>2.9.4</version>\n" + "      <version>2.9.5</version>\n" + "      <version>2.9.6</version>\n" + "      <version>2.9.7</version>\n" + "      <version>2.9.8</version>\n" + "      <version>2.10.0</version>\n" + "      <version>2.10.1</version>\n" + "      <version>2.10.2</version>\n" + "      <version>2.10.3</version>\n" + "      <version>2.10.4</version>\n" + "      <version>2.10.5</version>\n" + "      <version>2.10.6</version>\n" + "      <version>2.10.7</version>\n" + "      <version>2.11.0</version>\n" + "      <version>2.11.1</version>\n" + "      <version>2.11.2</version>\n" + "      <version>2.11.3</version>\n" + "      <version>2.11.4</version>\n" + "      <version>2.12.0</version>\n" + "      <version>2.12.1</version>\n" + "      <version>2.12.2</version>\n" + "      <version>2.12.3</version>\n" + "      <version>2.12.4</version>\n" + "      <version>2.13.0</version>\n" + "      <version>2.13.1</version>\n" + "      <version>2.13.2</version>\n" + "      <version>2.14.0</version>\n" + "    </versions>\n" + "    <lastUpdated>20140918132816</lastUpdated>\n" + "  </versioning>\n" + "</metadata>\n" + "\n";
            } else if ("/repo2/org/apache/camel/camel-core/maven-metadata.xml".equals(target)) {
                result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata modelVersion=\"1.1.0\">\n" + "  <groupId>org.apache.camel</groupId>\n" + "  <artifactId>camel-core</artifactId>\n" + "  <versioning>\n" + "    <latest>2.14.0.redhat-620034</latest>\n" + "    <release>2.14.0.redhat-620034</release>\n" + "    <versions>\n" + "      <version>2.10.0.redhat-60074</version>\n" + "      <version>2.12.0.redhat-610312</version>\n" + "      <version>2.12.0.redhat-610328</version>\n" + "      <version>2.12.0.redhat-610355</version>\n" + "      <version>2.12.0.redhat-610378</version>\n" + "      <version>2.12.0.redhat-610396</version>\n" + "      <version>2.12.0.redhat-610399</version>\n" + "      <version>2.12.0.redhat-610401</version>\n" + "      <version>2.12.0.redhat-610402</version>\n" + "      <version>2.12.0.redhat-611403</version>\n" + "      <version>2.12.0.redhat-611405</version>\n" + "      <version>2.12.0.redhat-611406</version>\n" + "      <version>2.12.0.redhat-611408</version>\n" + "      <version>2.12.0.redhat-611409</version>\n" + "      <version>2.12.0.redhat-611410</version>\n" + "      <version>2.12.0.redhat-611411</version>\n" + "      <version>2.12.0.redhat-611412</version>\n" + "      <version>2.14.0.redhat-620031</version>\n" + "      <version>2.14.0.redhat-620033</version>\n" + "      <version>2.14.0.redhat-620034</version>\n" + "    </versions>\n" + "    <lastUpdated>20141019130841</lastUpdated>\n" + "  </versioning>\n" + "</metadata>\n" + "\n";
            }
            if (result == null) {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                baseRequest.setHandled(true);
                response.getOutputStream().close();
            } else {
                response.setStatus(HttpServletResponse.SC_OK);
                baseRequest.setHandled(true);
                response.getOutputStream().write(result.getBytes());
                response.getOutputStream().close();
            }
        }
    });
    server.start();
    try {
        int localPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
        List<String> remoteRepos = Arrays.asList("http://relevant.not/repo1@id=repo1,http://relevant.not/repo2@id=repo2");
        RuntimeProperties props = new MockRuntimeProperties();
        // TODO: local repo should point to target/tmp
        MavenResolver resolver = createResolver("target/tmp", remoteRepos, "http", "localhost", localPort, "fuse", "fuse", null);
        MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, props, projectDeployer, 5, 0);
        AsyncContext context = EasyMock.createMock(AsyncContext.class);
        EasyMock.makeThreadSafe(context, true);
        final Map<String, Object> attributes = new HashMap<>();
        HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
        HttpServletRequest requestWrapper = new HttpServletRequestWrapper(request) {

            @Override
            public Object getAttribute(String name) {
                return attributes.get(name);
            }

            @Override
            public void setAttribute(String name, Object o) {
                attributes.put(name, o);
            }
        };
        EasyMock.makeThreadSafe(request, true);
        EasyMock.expect(request.getMethod()).andReturn("GET");
        EasyMock.expect(request.getPathInfo()).andReturn("org/apache/camel/camel-core/maven-metadata.xml");
        EasyMock.expect(request.startAsync()).andReturn(context);
        context.setTimeout(EasyMock.anyInt());
        EasyMock.expectLastCall();
        context.addListener((AsyncListener) EasyMock.anyObject());
        EasyMock.expectLastCall();
        HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
        EasyMock.makeThreadSafe(response, true);
        response.setStatus(EasyMock.anyInt());
        EasyMock.expectLastCall().anyTimes();
        response.setContentLength(EasyMock.anyInt());
        EasyMock.expectLastCall().anyTimes();
        response.setContentType((String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();
        response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong());
        EasyMock.expectLastCall().anyTimes();
        response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();
        response.flushBuffer();
        EasyMock.expectLastCall().anyTimes();
        final CountDownLatch latchDispatch = new CountDownLatch(1);
        context.dispatch();
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

            @Override
            public Object answer() throws Throwable {
                latchDispatch.countDown();
                return null;
            }
        });
        EasyMock.replay(request, response, context);
        servlet.start();
        servlet.doGet(requestWrapper, response);
        latchDispatch.await();
        EasyMock.verify(request, response, context);
        EasyMock.reset(request, response, context);
        EasyMock.expect(request.getPathInfo()).andReturn("org/apache/camel/camel-core/maven-metadata.xml");
        EasyMock.expect(request.startAsync()).andReturn(context);
        context.setTimeout(EasyMock.anyInt());
        EasyMock.expectLastCall();
        context.addListener((AsyncListener) EasyMock.anyObject());
        EasyMock.expectLastCall();
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        EasyMock.expect(response.getOutputStream()).andReturn(new ServletOutputStream() {

            @Override
            public void write(int b) throws IOException {
                baos.write(b);
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                baos.write(b, off, len);
            }

            @Override
            public boolean isReady() {
                return true;
            }

            @Override
            public void setWriteListener(WriteListener writeListener) {
            }
        }).anyTimes();
        response.flushBuffer();
        EasyMock.expectLastCall().anyTimes();
        final CountDownLatch latchComplete = new CountDownLatch(1);
        context.complete();
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

            @Override
            public Object answer() throws Throwable {
                latchComplete.countDown();
                return null;
            }
        });
        EasyMock.replay(request, response, context);
        servlet.doGet(requestWrapper, response);
        EasyMock.verify(request, response, context);
        org.apache.maven.artifact.repository.metadata.Metadata m = new MetadataXpp3Reader().read(new ByteArrayInputStream(baos.toByteArray()), false);
        assertEquals("2.14.0.redhat-620034", m.getVersioning().getLatest());
        assertTrue(m.getVersioning().getVersions().contains("2.10.4"));
        assertTrue(m.getVersioning().getVersions().contains("2.12.0.redhat-610399"));
        EasyMock.verify(request, response, context);
    } finally {
        server.stop();
        if (old != null) {
            System.setProperty("karaf.data", old);
        }
    }
}
Also used : Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) ServletOutputStream(javax.servlet.ServletOutputStream) MetadataXpp3Reader(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader) AsyncContext(javax.servlet.AsyncContext) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) MavenResolver(io.fabric8.maven.MavenResolver) WriteListener(javax.servlet.WriteListener) AbstractRuntimeProperties(io.fabric8.api.scr.AbstractRuntimeProperties) RuntimeProperties(io.fabric8.api.RuntimeProperties) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File)

Example 57 with Resolver

use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.

the class MavenProxyServletSupportTest method testDownload.

private void testDownload(Handler serverHandler) throws Exception {
    final String old = System.getProperty("karaf.data");
    System.setProperty("karaf.data", new File("target").getCanonicalPath());
    FileUtils.deleteDirectory(new File("target/tmp"));
    Server server = new Server(0);
    server.setHandler(serverHandler);
    server.start();
    try {
        int localPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
        List<String> remoteRepos = Arrays.asList("http://relevant.not/maven2@id=central");
        RuntimeProperties props = new MockRuntimeProperties();
        // TODO: local repo should point to target/tmp
        MavenResolver resolver = createResolver("target/tmp", remoteRepos, "http", "localhost", localPort, "fuse", "fuse", null);
        MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, props, projectDeployer, 5, 0);
        AsyncContext context = EasyMock.createMock(AsyncContext.class);
        EasyMock.makeThreadSafe(context, true);
        final Map<String, Object> attributes = new HashMap<>();
        HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
        HttpServletRequest requestWrapper = new HttpServletRequestWrapper(request) {

            @Override
            public Object getAttribute(String name) {
                return attributes.get(name);
            }

            @Override
            public void setAttribute(String name, Object o) {
                attributes.put(name, o);
            }
        };
        EasyMock.makeThreadSafe(request, true);
        EasyMock.expect(request.getMethod()).andReturn("GET");
        EasyMock.expect(request.getPathInfo()).andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
        EasyMock.expect(request.startAsync()).andReturn(context);
        context.setTimeout(EasyMock.anyInt());
        EasyMock.expectLastCall();
        context.addListener((AsyncListener) EasyMock.anyObject());
        EasyMock.expectLastCall();
        HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
        EasyMock.makeThreadSafe(response, true);
        response.setStatus(EasyMock.anyInt());
        EasyMock.expectLastCall();
        response.setContentLength(EasyMock.anyInt());
        EasyMock.expectLastCall();
        response.setContentType((String) EasyMock.anyObject());
        EasyMock.expectLastCall();
        response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong());
        EasyMock.expectLastCall();
        response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();
        final CountDownLatch latchDispatch = new CountDownLatch(1);
        context.dispatch();
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

            @Override
            public Object answer() throws Throwable {
                latchDispatch.countDown();
                return null;
            }
        });
        EasyMock.replay(request, response, context);
        servlet.start();
        servlet.doGet(requestWrapper, response);
        latchDispatch.await();
        EasyMock.verify(request, response, context);
        // 
        // Subsequent call
        // 
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ServletOutputStream outputStream = new ServletOutputStream() {

            @Override
            public void write(int b) throws IOException {
                baos.write(b);
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                baos.write(b, off, len);
            }

            @Override
            public boolean isReady() {
                return true;
            }

            @Override
            public void setWriteListener(WriteListener writeListener) {
            }
        };
        while (true) {
            long size = (Long) attributes.get(AsynchronousFileChannel.class.getName() + ".size");
            long pos = (Long) attributes.get(AsynchronousFileChannel.class.getName() + ".position");
            ByteBuffer buffer = (ByteBuffer) attributes.get(ByteBuffer.class.getName());
            if (pos + buffer.position() >= size) {
                break;
            }
            EasyMock.reset(request, response, context);
            EasyMock.expect(request.getPathInfo()).andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
            EasyMock.expect(request.startAsync()).andReturn(context);
            context.setTimeout(EasyMock.anyInt());
            EasyMock.expectLastCall();
            context.addListener((AsyncListener) EasyMock.anyObject());
            EasyMock.expectLastCall();
            final CountDownLatch latch = new CountDownLatch(1);
            context.dispatch();
            EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

                @Override
                public Object answer() throws Throwable {
                    latch.countDown();
                    return null;
                }
            });
            EasyMock.expect(response.getOutputStream()).andReturn(outputStream);
            EasyMock.replay(request, response, context);
            servlet.doGet(requestWrapper, response);
            latch.await();
            EasyMock.verify(request, response, context);
        }
        // 
        // Last calls
        // 
        EasyMock.reset(request, response, context);
        EasyMock.expect(request.getPathInfo()).andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
        EasyMock.expect(request.startAsync()).andReturn(context);
        context.setTimeout(EasyMock.anyInt());
        EasyMock.expectLastCall();
        context.addListener((AsyncListener) EasyMock.anyObject());
        EasyMock.expectLastCall();
        final CountDownLatch latchComplete = new CountDownLatch(1);
        context.complete();
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

            @Override
            public Object answer() throws Throwable {
                latchComplete.countDown();
                return null;
            }
        });
        EasyMock.expect(response.getOutputStream()).andReturn(outputStream);
        response.flushBuffer();
        EasyMock.expectLastCall();
        EasyMock.replay(request, response, context);
        servlet.doGet(requestWrapper, response);
        latchComplete.await();
        EasyMock.verify(request, response, context);
        Assert.assertArrayEquals(new byte[] { 0x42 }, baos.toByteArray());
    } finally {
        server.stop();
        if (old != null) {
            System.setProperty("karaf.data", old);
        }
    }
}
Also used : Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) ServletOutputStream(javax.servlet.ServletOutputStream) AsyncContext(javax.servlet.AsyncContext) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) MavenResolver(io.fabric8.maven.MavenResolver) WriteListener(javax.servlet.WriteListener) AbstractRuntimeProperties(io.fabric8.api.scr.AbstractRuntimeProperties) RuntimeProperties(io.fabric8.api.RuntimeProperties) HttpServletResponse(javax.servlet.http.HttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) File(java.io.File)

Example 58 with Resolver

use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.

the class MavenProxySnapshotResolutionTest method snapshotIsAvailableInTwoDefaultRepositories.

@Test
public void snapshotIsAvailableInTwoDefaultRepositories() throws IOException, InvalidMavenArtifactRequest {
    File defaultRepository1 = initFileRepository("dr1");
    File defaultRepository2 = initFileRepository("dr2");
    MavenResolver resolver = new ResolverBuilder().withDefaultRepositories(Arrays.asList(defaultRepository1, defaultRepository2)).build();
    MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, runtime, null, 1, 0);
    servlet.start();
    mvnInstall(defaultRepository1, "io.fabric8.test", "universalis-api", "0.1.0-SNAPSHOT", at("10:00"), "a");
    mvnInstall(defaultRepository2, "io.fabric8.test", "universalis-api", "0.1.0-SNAPSHOT", at("11:00"), "b");
    File file = servlet.download("io/fabric8/test/universalis-api/0.1.0-SNAPSHOT/maven-metadata.xml");
    Metadata metadata = readMetadata(file);
    assertThat("Resolution done in first default repository, even if second one has newer artifact", metadata.getVersioning().getSnapshotVersions().get(0).getUpdated(), is("20170101100000"));
}
Also used : MavenResolver(io.fabric8.maven.MavenResolver) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) File(java.io.File) Test(org.junit.Test)

Example 59 with Resolver

use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.

the class Activator method stop.

/**
 * Performs cleanup:<br/>
 * * Unregister handler;<br/>
 * * Unregister managed service;<br/>
 * * Release bundle context.
 *
 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
 */
public void stop(final BundleContext bundleContext) {
    if (m_handlerReg != null) {
        m_handlerReg.unregister();
        m_handlerReg = null;
    }
    if (m_managedServiceReg != null) {
        m_managedServiceReg.unregister();
        m_managedServiceReg = null;
    }
    ServiceRegistration<MavenResolver> registration = m_resolverReg.getAndSet(null);
    if (registration != null) {
        registration.unregister();
    }
    MavenResolver resolver = m_resolver.getAndSet(null);
    if (resolver != null) {
        try {
            resolver.close();
        } catch (IOException e) {
        // Ignore
        }
    }
    m_bundleContext = null;
    LOG.debug("Handler for protocols " + PROTOCOL + " stopped");
}
Also used : MavenResolver(io.fabric8.maven.MavenResolver) IOException(java.io.IOException)

Aggregations

File (java.io.File)23 MavenResolver (io.fabric8.maven.MavenResolver)22 Test (org.junit.Test)21 IOException (java.io.IOException)20 FabricService (io.fabric8.api.FabricService)10 Container (io.fabric8.api.Container)9 RuntimeProperties (io.fabric8.api.RuntimeProperties)7 HashMap (java.util.HashMap)7 PlaceholderResolver (io.fabric8.api.PlaceholderResolver)6 HashSet (java.util.HashSet)6 Hashtable (java.util.Hashtable)6 Metadata (org.apache.maven.artifact.repository.metadata.Metadata)6 Map (java.util.Map)5 DownloadManager (io.fabric8.agent.download.DownloadManager)4 AbstractRuntimeProperties (io.fabric8.api.scr.AbstractRuntimeProperties)4 MavenConfigurationImpl (io.fabric8.maven.util.MavenConfigurationImpl)4 FileInputStream (java.io.FileInputStream)4 URI (java.net.URI)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4