diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-08-22 10:19:18 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-08-23 15:14:18 +0200 |
commit | 087481a53525549b11fe3fce15534b489a44b4cf (patch) | |
tree | 87c6861e5e23ec73ec2228fd2f0f1d5d08301476 /server/sonar-ce/src/main | |
parent | 2b5236702c9c655d20633cfb737eaa86b955fbee (diff) | |
download | sonarqube-087481a53525549b11fe3fce15534b489a44b4cf.tar.gz sonarqube-087481a53525549b11fe3fce15534b489a44b4cf.zip |
SONAR-7825 changeloglevel WS now change CE's log level too
Diffstat (limited to 'server/sonar-ce/src/main')
4 files changed, 101 insertions, 2 deletions
diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/CeHttpModule.java b/server/sonar-ce/src/main/java/org/sonar/ce/CeHttpModule.java index 69afca02955..00d011638a6 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/CeHttpModule.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/CeHttpModule.java @@ -20,6 +20,7 @@ package org.sonar.ce; import org.sonar.ce.httpd.CeHttpServer; +import org.sonar.ce.logging.ChangeLogLevelHttpAction; import org.sonar.ce.systeminfo.SystemInfoHttpAction; import org.sonar.core.platform.Module; @@ -28,6 +29,7 @@ public class CeHttpModule extends Module { protected void configureModule() { add( CeHttpServer.class, - SystemInfoHttpAction.class); + SystemInfoHttpAction.class, + ChangeLogLevelHttpAction.class); } } diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/httpd/HttpAction.java b/server/sonar-ce/src/main/java/org/sonar/ce/httpd/HttpAction.java index c4d633269a1..b5891be7ac2 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/httpd/HttpAction.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/httpd/HttpAction.java @@ -27,7 +27,7 @@ import fi.iki.elonen.NanoHTTPD.Response; * * <p> * Method {@link #register(ActionRegistry)} of the action will be called right before the HTTP server is started (server - * is started by the Pico Container). It's the action's responsability to call the method + * is started by the Pico Container). It's the action's responsibility to call the method * {@link ActionRegistry#register(ActionRegistry)} to register itself for a given path. * </p> * <p> diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/logging/ChangeLogLevelHttpAction.java b/server/sonar-ce/src/main/java/org/sonar/ce/logging/ChangeLogLevelHttpAction.java new file mode 100644 index 00000000000..626eb20c4a6 --- /dev/null +++ b/server/sonar-ce/src/main/java/org/sonar/ce/logging/ChangeLogLevelHttpAction.java @@ -0,0 +1,74 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.ce.logging; + +import fi.iki.elonen.NanoHTTPD; +import org.sonar.api.utils.log.LoggerLevel; +import org.sonar.api.utils.log.Loggers; +import org.sonar.ce.httpd.HttpAction; +import org.sonar.db.Database; +import org.sonar.server.platform.ServerLogging; + +import static fi.iki.elonen.NanoHTTPD.MIME_PLAINTEXT; +import static fi.iki.elonen.NanoHTTPD.Response.Status.BAD_REQUEST; +import static fi.iki.elonen.NanoHTTPD.Response.Status.METHOD_NOT_ALLOWED; +import static fi.iki.elonen.NanoHTTPD.Response.Status.OK; +import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse; +import static java.lang.String.format; + +public class ChangeLogLevelHttpAction implements HttpAction { + + private static final String PATH = "changeLogLevel"; + private static final String PARAM_LEVEL = "level"; + + private final ServerLogging logging; + private final Database db; + + public ChangeLogLevelHttpAction(ServerLogging logging, Database db) { + this.logging = logging; + this.db = db; + } + + @Override + public void register(ActionRegistry registry) { + registry.register(PATH, this); + } + + @Override + public NanoHTTPD.Response serve(NanoHTTPD.IHTTPSession session) { + if (session.getMethod() != NanoHTTPD.Method.POST) { + return newFixedLengthResponse(METHOD_NOT_ALLOWED, MIME_PLAINTEXT, null); + } + + String levelStr = session.getParms().get(PARAM_LEVEL); + if (levelStr == null || levelStr.isEmpty()) { + return newFixedLengthResponse(BAD_REQUEST, MIME_PLAINTEXT, format("Parameter '%s' is missing", PARAM_LEVEL)); + } + try { + LoggerLevel level = LoggerLevel.valueOf(levelStr); + db.enableSqlLogging(level.equals(LoggerLevel.TRACE)); + logging.changeLevel(level); + return newFixedLengthResponse(OK, MIME_PLAINTEXT, null); + } catch (IllegalArgumentException e) { + Loggers.get(ChangeLogLevelHttpAction.class).debug("Value '{}' for parameter '{}' is invalid", levelStr, PARAM_LEVEL, e); + return newFixedLengthResponse(BAD_REQUEST, MIME_PLAINTEXT, format("Value '%s' for parameter '%s' is invalid", levelStr, PARAM_LEVEL)); + } + } +} diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/logging/package-info.java b/server/sonar-ce/src/main/java/org/sonar/ce/logging/package-info.java new file mode 100644 index 00000000000..83bd486343c --- /dev/null +++ b/server/sonar-ce/src/main/java/org/sonar/ce/logging/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +@ParametersAreNonnullByDefault +package org.sonar.ce.logging; + +import javax.annotation.ParametersAreNonnullByDefault; |