diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-01-19 02:20:45 +0300 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-01-24 01:14:07 +0300 |
commit | 6b5035087075113699015583fd4d76b75bf36894 (patch) | |
tree | 97a3a3ef3be082d68db071405f7aab50d4963a19 /sonar-server/src | |
parent | a1fca497333231931a3df3e3973918ade12ab6bc (diff) | |
download | sonarqube-6b5035087075113699015583fd4d76b75bf36894.tar.gz sonarqube-6b5035087075113699015583fd4d76b75bf36894.zip |
SONAR-2106: New Java library to bootstrap project analysis
* Add BatchResourcesServlet to allow downloading libraries from server
* Create in memory POM for non-maven environments
* Provide fake MavenPluginExecutor for non-maven environments
* Add new module sonar-batch-maven-compat with shaded maven-project
Diffstat (limited to 'sonar-server/src')
3 files changed, 172 insertions, 1 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/BatchResourcesServlet.java b/sonar-server/src/main/java/org/sonar/server/plugins/BatchResourcesServlet.java new file mode 100644 index 00000000000..1e8ab666578 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/plugins/BatchResourcesServlet.java @@ -0,0 +1,104 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.server.plugins; + +import com.google.common.collect.Lists; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.util.List; +import java.util.Set; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * This servlet allows to load libraries from directory "WEB-INF/lib" in order to provide them for batch-bootstrapper. + * Most probably this is not a best solution. + */ +public class BatchResourcesServlet extends HttpServlet { + + private static final Logger LOG = LoggerFactory.getLogger(BatchResourcesServlet.class); + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String resource = getResource(request); + if (StringUtils.isEmpty(resource)) { + PrintWriter writer = null; + try { + String libs = StringUtils.join(getLibs(), ","); + response.setContentType("text/html"); + writer = response.getWriter(); + writer.println(libs); + } catch (IOException e) { + LOG.error("Unable to provide list of batch resources", e); + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } finally { + IOUtils.closeQuietly(writer); + } + } else { + InputStream in = null; + OutputStream out = null; + try { + in = getServletContext().getResourceAsStream("/WEB-INF/lib/" + resource); + if (in == null) { + // TODO + } else { + out = response.getOutputStream(); + IOUtils.copy(in, out); + } + } catch (Exception e) { + LOG.error("Unable to load batch resource '" + resource + "'", e); + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } finally { + IOUtils.closeQuietly(in); + IOUtils.closeQuietly(out); + } + } + } + + List<String> getLibs() { + List<String> libs = Lists.newArrayList(); + Set paths = getServletContext().getResourcePaths("/WEB-INF/lib"); + for (Object obj : paths) { + String path = (String) obj; + if (StringUtils.endsWith(path, ".jar")) { + libs.add(StringUtils.removeStart(path, "/WEB-INF/lib/")); + } + } + return libs; + } + + /** + * @return part of request URL after servlet path + */ + String getResource(HttpServletRequest request) { + return StringUtils.substringAfter(request.getRequestURI(), request.getContextPath() + request.getServletPath() + "/"); + } + +} diff --git a/sonar-server/src/main/webapp/WEB-INF/web.xml b/sonar-server/src/main/webapp/WEB-INF/web.xml index 99d68dcc5f6..25869629487 100644 --- a/sonar-server/src/main/webapp/WEB-INF/web.xml +++ b/sonar-server/src/main/webapp/WEB-INF/web.xml @@ -78,6 +78,10 @@ <servlet-name>static</servlet-name> <servlet-class>org.sonar.server.plugins.StaticResourcesServlet</servlet-class> </servlet> + <servlet> + <servlet-name>batch</servlet-name> + <servlet-class>org.sonar.server.plugins.BatchResourcesServlet</servlet-class> + </servlet> <servlet-mapping> <servlet-name>chart</servlet-name> @@ -91,6 +95,10 @@ <servlet-name>static</servlet-name> <url-pattern>/static/*</url-pattern> </servlet-mapping> + <servlet-mapping> + <servlet-name>batch</servlet-name> + <url-pattern>/batch/*</url-pattern> + </servlet-mapping> <listener> <listener-class>org.sonar.server.platform.PlatformLifecycleListener</listener-class> @@ -103,4 +111,4 @@ <session-timeout>30</session-timeout> </session-config> -</web-app>
\ No newline at end of file +</web-app> diff --git a/sonar-server/src/test/java/org/sonar/server/plugins/BatchResourcesServletTest.java b/sonar-server/src/test/java/org/sonar/server/plugins/BatchResourcesServletTest.java new file mode 100644 index 00000000000..3dadef9567f --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/plugins/BatchResourcesServletTest.java @@ -0,0 +1,59 @@ +package org.sonar.server.plugins; + +import com.google.common.collect.Sets; +import org.junit.Before; +import org.junit.Test; + +import java.util.Set; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +public class BatchResourcesServletTest { + private BatchResourcesServlet servlet; + private HttpServletRequest request; + + @Before + public void setUp() throws Exception { + servlet = new BatchResourcesServlet(); + request = mock(HttpServletRequest.class); + } + + @Test + public void shouldDetermineResource() { + when(request.getContextPath()).thenReturn("sonar"); + when(request.getServletPath()).thenReturn("/batch"); + + when(request.getRequestURI()).thenReturn("/sonar/batch/sonar-core-2.6.jar"); + assertThat(servlet.getResource(request), is("sonar-core-2.6.jar")); + + when(request.getRequestURI()).thenReturn("/sonar/batch/"); + assertThat(servlet.getResource(request), is("")); + + when(request.getRequestURI()).thenReturn("/sonar/batch"); + assertThat(servlet.getResource(request), is("")); + } + + @Test + public void shouldDetermineListOfResources() { + ServletContext servletContext = mock(ServletContext.class); + servlet = spy(servlet); + doReturn(servletContext).when(servlet).getServletContext(); + Set<String> libs = Sets.newHashSet(); + libs.add("/WEB-INF/lib/sonar-core-2.6.jar"); + libs.add("/WEB-INF/lib/treemap.rb"); + libs.add("/WEB-INF/lib/directory/"); + when(servletContext.getResourcePaths(anyString())).thenReturn(libs); + + assertThat(servlet.getLibs().size(), is(1)); + assertThat(servlet.getLibs().get(0), is("sonar-core-2.6.jar")); + } +} |