From 9d37a7de14a8df7d02879865696510b4ebfaba5c Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Tue, 26 Mar 2013 14:43:01 +0100 Subject: [PATCH] SONAR-4157 Upgrade the embedded Jetty to version 8.1 --- pom.xml | 28 ++-- sonar-application/pom.xml | 12 +- .../src/main/assembly/conf/sonar.properties | 5 +- .../org/sonar/application/FilteredLogger.java | 76 --------- .../org/sonar/application/JettyEmbedder.java | 48 +++--- .../sonar/application/ShutdownHandler.java | 151 ------------------ .../org/sonar/application/StartServer.java | 17 +- .../sonar/application/JettyEmbedderTest.java | 13 +- .../org/sonar/application/jetty-test.xml | 61 ------- sonar-batch/pom.xml | 8 +- .../batch/bootstrap/ServerClientTest.java | 12 +- sonar-plugin-api/pom.xml | 2 +- sonar-server/pom.xml | 10 +- sonar-server/src/main/webapp/WEB-INF/web.xml | 9 +- 14 files changed, 62 insertions(+), 390 deletions(-) delete mode 100644 sonar-application/src/main/java/org/sonar/application/FilteredLogger.java delete mode 100644 sonar-application/src/main/java/org/sonar/application/ShutdownHandler.java delete mode 100644 sonar-application/src/test/resources/org/sonar/application/jetty-test.xml diff --git a/pom.xml b/pom.xml index 5fb410737f7..2f233609a5d 100644 --- a/pom.xml +++ b/pom.xml @@ -74,7 +74,7 @@ 1.3-SNAPSHOT 3.3.1 1.3.167 - 6.1.25 + 8.1.9.v20130131 UTF-8 2.2.1 1.6 @@ -965,33 +965,27 @@ javax.servlet - servlet-api - 2.5 + javax.servlet-api + 3.0.1 - org.mortbay.jetty - jetty-plus + org.eclipse.jetty + jetty-server ${jetty.version} - org.mortbay.jetty - jetty-naming + org.eclipse.jetty + jetty-webapp ${jetty.version} - - - javax.mail - mail - - - org.mortbay.jetty - jetty + org.eclipse.jetty + jetty-util ${jetty.version} - org.mortbay.jetty - jetty-util + org.eclipse.jetty + jetty-servlets ${jetty.version} diff --git a/sonar-application/pom.xml b/sonar-application/pom.xml index e93a9714acd..d4b9b9bd9c3 100644 --- a/sonar-application/pom.xml +++ b/sonar-application/pom.xml @@ -22,16 +22,12 @@ war - org.mortbay.jetty - jetty-plus + org.eclipse.jetty + jetty-server - org.mortbay.jetty - jetty-naming - - - org.mortbay.jetty - jetty + org.eclipse.jetty + jetty-webapp commons-io diff --git a/sonar-application/src/main/assembly/conf/sonar.properties b/sonar-application/src/main/assembly/conf/sonar.properties index 11f0859b6c6..f5a6e4d08b2 100644 --- a/sonar-application/src/main/assembly/conf/sonar.properties +++ b/sonar-application/src/main/assembly/conf/sonar.properties @@ -24,12 +24,11 @@ #sonar.web.jettyRequestLogs: ../../logs/jetty-yyyy_mm_dd.request.log #sonar.web.jetty.threads.min: 5 #sonar.web.jetty.threads.max: 50 -#sonar.web.jetty.threads.low: 10 #----------------------------------------------------------------------- # DATABASE # -# IMPORTANT : the embedded database H2 is used by default. +# IMPORTANT : the embedded database H2 is used by default. # It is recommended for tests only. Please use an external database # for production environment (MySQL, Oracle, Postgresql, SQLServer) # @@ -44,7 +43,7 @@ sonar.jdbc.password: sonar #----- Embedded database H2 # Note : it does not accept connections from remote hosts, so the # sonar server and the maven plugin must be executed on the same host. - + # Comment the following line to deactivate the default embedded database. sonar.jdbc.url: jdbc:h2:tcp://localhost:9092/sonar #sonar.jdbc.driverClassName: org.h2.Driver diff --git a/sonar-application/src/main/java/org/sonar/application/FilteredLogger.java b/sonar-application/src/main/java/org/sonar/application/FilteredLogger.java deleted file mode 100644 index b5c07b996d5..00000000000 --- a/sonar-application/src/main/java/org/sonar/application/FilteredLogger.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * 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.application; - -import org.mortbay.log.Logger; -import org.mortbay.log.StdErrLog; - -public class FilteredLogger implements Logger { - private final String name; - private final Logger delegate; - - public FilteredLogger() { - this(null); - } - - private FilteredLogger(String name) { - this.name = name; - this.delegate = new StdErrLog(name); - } - - public boolean isDebugEnabled() { - return delegate.isDebugEnabled(); - } - - public void setDebugEnabled(boolean enabled) { - delegate.setDebugEnabled(enabled); - } - - public void info(String msg, Object arg0, Object arg1) { - if (msg.contains("JVM BUG(s)")) { - // Ignore, see SONAR-3866 - return; - } - delegate.info(msg, arg0, arg1); - } - - public void debug(String msg, Throwable th) { - delegate.debug(msg, th); - } - - public void debug(String msg, Object arg0, Object arg1) { - delegate.debug(msg, arg0, arg1); - } - - public void warn(String msg, Object arg0, Object arg1) { - delegate.warn(msg, arg0, arg1); - } - - public void warn(String msg, Throwable th) { - delegate.warn(msg, th); - } - - public Logger getLogger(String name) { - if ((name == null && this.name == null) || ((name != null) && name.equals(this.name))) { - return this; - } - return new FilteredLogger(name); - } -} diff --git a/sonar-application/src/main/java/org/sonar/application/JettyEmbedder.java b/sonar-application/src/main/java/org/sonar/application/JettyEmbedder.java index 98da317bedf..b6ca00e5c1f 100644 --- a/sonar-application/src/main/java/org/sonar/application/JettyEmbedder.java +++ b/sonar-application/src/main/java/org/sonar/application/JettyEmbedder.java @@ -20,15 +20,15 @@ package org.sonar.application; import org.apache.commons.io.FileUtils; -import org.mortbay.jetty.Handler; -import org.mortbay.jetty.NCSARequestLog; -import org.mortbay.jetty.Server; -import org.mortbay.jetty.handler.HandlerList; -import org.mortbay.jetty.handler.RequestLogHandler; -import org.mortbay.jetty.nio.SelectChannelConnector; -import org.mortbay.jetty.webapp.WebAppContext; -import org.mortbay.thread.QueuedThreadPool; -import org.mortbay.xml.XmlConfiguration; +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.NCSARequestLog; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.HandlerCollection; +import org.eclipse.jetty.server.handler.RequestLogHandler; +import org.eclipse.jetty.server.handler.ShutdownHandler; +import org.eclipse.jetty.server.nio.SelectChannelConnector; +import org.eclipse.jetty.util.thread.QueuedThreadPool; +import org.eclipse.jetty.webapp.WebAppContext; import java.io.File; import java.io.IOException; @@ -47,29 +47,20 @@ public class JettyEmbedder { private final String contextPath; private final Properties configuration; - public JettyEmbedder(String host, int port, String contextPath, URL configurationURL, Properties configuration) throws Exception { + public JettyEmbedder(String host, int port, String contextPath, Properties configuration) throws Exception { this.host = host.trim(); this.port = port; this.contextPath = contextPath; this.configuration = configuration; server = new Server(); - - if (configurationURL == null) { - configureProgrammatically(); - } else { - System.setProperty("jetty.host", this.host); - System.setProperty("jetty.port", String.valueOf(port)); - System.setProperty("jetty.context", contextPath); - XmlConfiguration xmlConfiguration = new XmlConfiguration(configurationURL); - xmlConfiguration.configure(server); - } + configureProgrammatically(); } /** * for tests */ JettyEmbedder(String host, int port) throws Exception { - this(host, port, null, null, new Properties()); + this(host, port, null, new Properties()); } public void start() throws Exception { @@ -90,23 +81,25 @@ public class JettyEmbedder { private Server configureProgrammatically() throws URISyntaxException { configureServer(); + HandlerCollection handlers = new HandlerCollection(); WebAppContext context = new WebAppContext(getPath("/war/sonar-server"), contextPath); + String filenamePattern = configuration.getProperty("sonar.web.jettyRequestLogs"); + RequestLogHandler requestLogHandler = configureRequestLogHandler(filenamePattern); String shutdownCookie = System.getProperty("sonar.shutdownToken"); if (shutdownCookie != null && !"".equals(shutdownCookie)) { System.out.println("Registering shutdown handler"); ShutdownHandler shutdownHandler = new ShutdownHandler(server, shutdownCookie); shutdownHandler.setExitJvm(true); - HandlerList handlers = new HandlerList(); - handlers.setHandlers(new Handler[] {shutdownHandler, context}); - server.setHandler(handlers); + handlers.setHandlers(new Handler[] {shutdownHandler, context, requestLogHandler}); } else { - server.addHandler(context); + handlers.setHandlers(new Handler[] {context, requestLogHandler}); } + server.setHandler(handlers); return server; } - public void configureRequestLogs(String filenamePattern) { + public RequestLogHandler configureRequestLogHandler(String filenamePattern) { RequestLogHandler requestLogHandler = new RequestLogHandler(); NCSARequestLog requestLog = new NCSARequestLog(filenamePattern); requestLog.setRetainDays(7); @@ -114,14 +107,13 @@ public class JettyEmbedder { requestLog.setExtended(true); requestLog.setLogTimeZone("GMT"); requestLogHandler.setRequestLog(requestLog); - server.addHandler(requestLogHandler); + return requestLogHandler; } private void configureServer() { QueuedThreadPool threadPool = new QueuedThreadPool(); threadPool.setMinThreads(getIntProperty("sonar.web.jetty.threads.min", 5)); threadPool.setMaxThreads(getIntProperty("sonar.web.jetty.threads.max", 50)); - threadPool.setLowThreads(getIntProperty("sonar.web.jetty.threads.low", 10)); server.setThreadPool(threadPool); SelectChannelConnector connector = new SelectChannelConnector(); connector.setHost(host); diff --git a/sonar-application/src/main/java/org/sonar/application/ShutdownHandler.java b/sonar-application/src/main/java/org/sonar/application/ShutdownHandler.java deleted file mode 100644 index 2a184d8ce09..00000000000 --- a/sonar-application/src/main/java/org/sonar/application/ShutdownHandler.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * 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.application; - -import org.mortbay.jetty.Server; -import org.mortbay.jetty.handler.AbstractHandler; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import java.io.IOException; - -/* ------------------------------------------------------------ */ -/** - * TODO Duplicate code from Jetty 8 waiting for upgrade. - * - * - * A handler that shuts the server down on a valid request. Used to do "soft" restarts from Java. If _exitJvm ist set to true a hard System.exit() call is being - * made. - * - * This handler is a contribution from Johannes Brodwall: https://bugs.eclipse.org/bugs/show_bug.cgi?id=357687 - * - * Usage: - * - *
-    Server server = new Server(8080);
-    HandlerList handlers = new HandlerList();
-    handlers.setHandlers(new Handler[]
-    { someOtherHandler, new ShutdownHandler(server,"secret password") });
-    server.setHandler(handlers);
-    server.start();
-   
- * -
-   public static void attemptShutdown(int port, String shutdownCookie) {
-        try {
-            URL url = new URL("http://localhost:" + port + "/shutdown?token=" + shutdownCookie);
-            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
-            connection.setRequestMethod("POST");
-            connection.getResponseCode();
-            logger.info("Shutting down " + url + ": " + connection.getResponseMessage());
-        } catch (SocketException e) {
-            logger.debug("Not running");
-            // Okay - the server is not running
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        }
-    }
-  
- */ -public class ShutdownHandler extends AbstractHandler { - - private final String _shutdownToken; - - private final Server _server; - - private boolean _exitJvm = false; - - /** - * Creates a listener that lets the server be shut down remotely (but only from localhost). - * - * @param server - * the Jetty instance that should be shut down - * @param shutdownToken - * a secret password to avoid unauthorized shutdown attempts - */ - public ShutdownHandler(Server server, String shutdownToken) { - this._server = server; - this._shutdownToken = shutdownToken; - } - - public void handle(String target, HttpServletRequest request, HttpServletResponse response, - int dispatch) throws IOException, ServletException { - if (!target.equals("/shutdown")) { - return; - } - - if (!request.getMethod().equals("POST")) { - response.sendError(HttpServletResponse.SC_BAD_REQUEST); - return; - } - if (!hasCorrectSecurityToken(request)) { - System.err.println("Unauthorized shutdown attempt from " + getRemoteAddr(request)); - response.sendError(HttpServletResponse.SC_UNAUTHORIZED); - return; - } - if (!requestFromLocalhost(request)) { - System.err.println("Unauthorized shutdown attempt from " + getRemoteAddr(request)); - response.sendError(HttpServletResponse.SC_UNAUTHORIZED); - return; - } - - System.out.println("Shutting down by request from " + getRemoteAddr(request)); - - new Thread() { - public void run() { - try { - shutdownServer(); - } catch (InterruptedException e) { - // Ignored - } catch (Exception e) { - throw new RuntimeException("Shutting down server", e); - } - } - }.start(); - } - - private boolean requestFromLocalhost(HttpServletRequest request) { - return "127.0.0.1".equals(getRemoteAddr(request)); - } - - protected String getRemoteAddr(HttpServletRequest request) { - return request.getRemoteAddr(); - } - - private boolean hasCorrectSecurityToken(HttpServletRequest request) { - return _shutdownToken.equals(request.getParameter("token")); - } - - private void shutdownServer() throws Exception { - _server.stop(); - - if (_exitJvm) - { - System.exit(0); - } - } - - public void setExitJvm(boolean exitJvm) { - this._exitJvm = exitJvm; - } - -} diff --git a/sonar-application/src/main/java/org/sonar/application/StartServer.java b/sonar-application/src/main/java/org/sonar/application/StartServer.java index 68816d8237d..590cce20956 100644 --- a/sonar-application/src/main/java/org/sonar/application/StartServer.java +++ b/sonar-application/src/main/java/org/sonar/application/StartServer.java @@ -37,14 +37,12 @@ public final class StartServer { public static void main(String[] args) throws Exception { canCreateTemporaryFiles(); configureHome(); - configureJettyLogger(); Properties configuration = getConfiguration(); String host = configuration.getProperty("sonar.web.host", DEFAULT_WEB_HOST); int port = Integer.parseInt(configuration.getProperty("sonar.web.port", "" + DEFAULT_WEB_PORT)); String context = configuration.getProperty("sonar.web.context", DEFAULT_WEB_CONTEXT); - JettyEmbedder jetty = new JettyEmbedder(host, port, context, StartServer.class.getResource("/jetty.xml"), configuration); - configureRequestLogs(jetty, configuration); + JettyEmbedder jetty = new JettyEmbedder(host, port, context, configuration); jetty.start(); Thread.currentThread().join(); @@ -64,13 +62,6 @@ public final class StartServer { } } - private static void configureRequestLogs(JettyEmbedder jetty, Properties configuration) { - String filenamePattern = configuration.getProperty("sonar.web.jettyRequestLogs"); - if (filenamePattern != null) { - jetty.configureRequestLogs(filenamePattern); - } - } - private static Properties getConfiguration() throws IOException { Properties properties = new Properties(); properties.load(StartServer.class.getResourceAsStream("/conf/sonar.properties")); @@ -80,10 +71,6 @@ public final class StartServer { private static void configureHome() throws URISyntaxException { File confFile = new File(StartServer.class.getResource("/conf/sonar.properties").toURI()); System.setProperty("SONAR_HOME" /* see constant org.sonar.server.platform.SonarHome.PROPERTY */, - confFile.getParentFile().getParentFile().getAbsolutePath()); - } - - private static void configureJettyLogger() { - System.setProperty("org.mortbay.log.class", FilteredLogger.class.getName()); + confFile.getParentFile().getParentFile().getAbsolutePath()); } } diff --git a/sonar-application/src/test/java/org/sonar/application/JettyEmbedderTest.java b/sonar-application/src/test/java/org/sonar/application/JettyEmbedderTest.java index 1804d4cd4ff..188ab357023 100644 --- a/sonar-application/src/test/java/org/sonar/application/JettyEmbedderTest.java +++ b/sonar-application/src/test/java/org/sonar/application/JettyEmbedderTest.java @@ -22,23 +22,12 @@ package org.sonar.application; import org.apache.commons.lang.StringUtils; import org.junit.Test; -import java.util.Properties; - import static org.fest.assertions.Assertions.assertThat; public class JettyEmbedderTest { - @Test - public void xmlConfigurationShouldAccessToSomeSystemProperties() throws Exception { - // useful to set the port into the XML file - new JettyEmbedder("127.0.0.1", 9999, "/", JettyEmbedderTest.class.getResource("/org/sonar/application/jetty-test.xml"), new Properties()); - - assertThat(System.getProperty("jetty.host")).isEqualTo("127.0.0.1"); - assertThat(System.getProperty("jetty.port")).isEqualTo("9999"); - assertThat(System.getProperty("jetty.context")).isEqualTo("/"); - } @Test - public void shouldUseDefaultConfigurationIfNoXml() throws Exception { + public void shouldConfigureProgrammatically() throws Exception { JettyEmbedder jetty = new JettyEmbedder("1.2.3.4", 9999); assertThat(jetty.getServer().getConnectors()).hasSize(1); diff --git a/sonar-application/src/test/resources/org/sonar/application/jetty-test.xml b/sonar-application/src/test/resources/org/sonar/application/jetty-test.xml deleted file mode 100644 index 66b888778e6..00000000000 --- a/sonar-application/src/test/resources/org/sonar/application/jetty-test.xml +++ /dev/null @@ -1,61 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - 5 - 50 - 10 - - - - - - - - - - - - - - - - - 30000 - 2 - false - 8443 - 5000 - 5000 - - - - - - - - true - true - true - 1000 - - diff --git a/sonar-batch/pom.xml b/sonar-batch/pom.xml index 9ce759ab34c..d1b299715a0 100644 --- a/sonar-batch/pom.xml +++ b/sonar-batch/pom.xml @@ -59,6 +59,10 @@ commons-lang commons-lang
+ + commons-configuration + commons-configuration + com.google.code.gson gson @@ -81,8 +85,8 @@ test - org.mortbay.jetty - jetty + org.eclipse.jetty + jetty-server test diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java index 1ac83ef89db..76a7d09a934 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java @@ -22,16 +22,15 @@ package org.sonar.batch.bootstrap; import com.google.common.base.Charsets; import com.google.common.io.Files; import org.apache.commons.io.IOUtils; +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.AbstractHandler; import org.junit.After; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; -import org.mortbay.jetty.Handler; -import org.mortbay.jetty.HttpConnection; -import org.mortbay.jetty.Request; -import org.mortbay.jetty.Server; -import org.mortbay.jetty.handler.AbstractHandler; import org.sonar.batch.bootstrapper.EnvironmentInformation; import javax.servlet.ServletException; @@ -165,8 +164,7 @@ public class ServerClientTest { public Handler getMockHandler() { Handler handler = new AbstractHandler() { - public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException { - Request baseRequest = request instanceof Request ? (Request) request : HttpConnection.getCurrentConnection().getRequest(); + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { setResponseBody(getMockResponseData()); setRequestBody(IOUtils.toString(baseRequest.getInputStream())); response.setStatus(mockResponseStatus); diff --git a/sonar-plugin-api/pom.xml b/sonar-plugin-api/pom.xml index 4c175261a50..69529e7a48a 100644 --- a/sonar-plugin-api/pom.xml +++ b/sonar-plugin-api/pom.xml @@ -127,7 +127,7 @@ javax.servlet - servlet-api + javax.servlet-api true diff --git a/sonar-server/pom.xml b/sonar-server/pom.xml index 1d1c513563a..62d8d60b14b 100644 --- a/sonar-server/pom.xml +++ b/sonar-server/pom.xml @@ -111,7 +111,7 @@ javax.servlet - servlet-api + javax.servlet-api provided @@ -132,8 +132,8 @@ - org.mortbay.jetty - jetty-util + org.eclipse.jetty + jetty-servlets @@ -369,9 +369,9 @@ - org.mortbay.jetty + org.eclipse.jetty jetty-maven-plugin - 7.1.0.v20100505 + ${jetty.version} /dev diff --git a/sonar-server/src/main/webapp/WEB-INF/web.xml b/sonar-server/src/main/webapp/WEB-INF/web.xml index 47c60f5ff48..e154cf32012 100644 --- a/sonar-server/src/main/webapp/WEB-INF/web.xml +++ b/sonar-server/src/main/webapp/WEB-INF/web.xml @@ -1,8 +1,9 @@ - + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" + metadata-complete="true" + version="3.0"> Sonar @@ -45,7 +46,7 @@ GZIPFilter - org.mortbay.servlet.GzipFilter + org.eclipse.jetty.servlets.GzipFilter minGzipSize 1024 -- 2.39.5