aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-application/src
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2013-03-26 14:43:01 +0100
committerJulien HENRY <julien.henry@sonarsource.com>2013-03-26 14:43:48 +0100
commit9d37a7de14a8df7d02879865696510b4ebfaba5c (patch)
treeb2e3056da4f369633ffb488cfb9ebc64bf1f5fe0 /sonar-application/src
parentac4d05b40f4667e7c497c37b11808c73536942b8 (diff)
downloadsonarqube-9d37a7de14a8df7d02879865696510b4ebfaba5c.tar.gz
sonarqube-9d37a7de14a8df7d02879865696510b4ebfaba5c.zip
SONAR-4157 Upgrade the embedded Jetty to version 8.1
Diffstat (limited to 'sonar-application/src')
-rw-r--r--sonar-application/src/main/assembly/conf/sonar.properties5
-rw-r--r--sonar-application/src/main/java/org/sonar/application/FilteredLogger.java76
-rw-r--r--sonar-application/src/main/java/org/sonar/application/JettyEmbedder.java48
-rw-r--r--sonar-application/src/main/java/org/sonar/application/ShutdownHandler.java151
-rw-r--r--sonar-application/src/main/java/org/sonar/application/StartServer.java17
-rw-r--r--sonar-application/src/test/java/org/sonar/application/JettyEmbedderTest.java13
-rw-r--r--sonar-application/src/test/resources/org/sonar/application/jetty-test.xml61
7 files changed, 25 insertions, 346 deletions
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:
- *
- * <pre>
- Server server = new Server(8080);
- HandlerList handlers = new HandlerList();
- handlers.setHandlers(new Handler[]
- { someOtherHandler, new ShutdownHandler(server,&quot;secret password&quot;) });
- server.setHandler(handlers);
- server.start();
- </pre>
- *
- <pre>
- 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);
- }
- }
- </pre>
- */
-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 @@
-<?xml version="1.0"?>
-<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
-
-<!-- =============================================================== -->
-<!-- Configure the Jetty Server -->
-<!-- -->
-<!-- Documentation of this file format can be found at: -->
-<!-- http://docs.codehaus.org/display/JETTY/jetty.xml -->
-<!-- -->
-<!-- =============================================================== -->
-
-
-<Configure id="Server" class="org.mortbay.jetty.Server">
-
- <!-- =========================================================== -->
- <!-- Server Thread Pool -->
- <!-- =========================================================== -->
- <Set name="ThreadPool">
- <!-- Default bounded blocking threadpool
- -->
- <New class="org.mortbay.thread.BoundedThreadPool">
- <Set name="minThreads">5</Set>
- <Set name="maxThreads">50</Set>
- <Set name="lowThreads">10</Set>
- </New>
-
- </Set>
-
- <!-- =========================================================== -->
- <!-- Set connectors -->
- <!-- =========================================================== -->
- <!-- One of each type! -->
- <!-- =========================================================== -->
-
- <!-- Use this connector for many frequently idle connections
- and for threadless continuations.
- -->
- <Call name="addConnector">
- <Arg>
- <New class="org.mortbay.jetty.nio.SelectChannelConnector">
- <Set name="host"><SystemProperty name="jetty.host" default="0.0.0.0"/></Set>
- <Set name="port"><SystemProperty name="jetty.port" default="9000"/></Set>
- <Set name="maxIdleTime">30000</Set>
- <Set name="Acceptors">2</Set>
- <Set name="statsOn">false</Set>
- <Set name="confidentialPort">8443</Set>
- <Set name="lowResourcesConnections">5000</Set>
- <Set name="lowResourcesMaxIdleTime">5000</Set>
- </New>
- </Arg>
- </Call>
-
- <!-- =========================================================== -->
- <!-- extra options -->
- <!-- =========================================================== -->
- <Set name="stopAtShutdown">true</Set>
- <Set name="sendServerVersion">true</Set>
- <Set name="sendDateHeader">true</Set>
- <Set name="gracefulShutdown">1000</Set>
-
-</Configure>