diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryClient.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryClient.java index ac3a6c425293..e977387997c1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryClient.java +++ b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryClient.java @@ -31,17 +31,19 @@ import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import static org.sonar.core.config.TelemetryProperties.PROP_URL; + @ServerSide public class TelemetryClient { private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); private static final Logger LOG = Loggers.get(TelemetryClient.class); private final OkHttpClient okHttpClient; - private final TelemetryUrl serverUrl; + private final Configuration config; public TelemetryClient(OkHttpClient okHttpClient, Configuration config) { this.okHttpClient = okHttpClient; - this.serverUrl = new TelemetryUrl(config); + this.config = config; } void send(String json) { @@ -55,7 +57,7 @@ void send(String json) { void optOut(String json) { Request.Builder request = new Request.Builder(); - request.url(serverUrl.get()); + request.url(serverUrl()); RequestBody body = RequestBody.create(JSON, json); request.delete(body); @@ -68,10 +70,14 @@ void optOut(String json) { private Request buildHttpRequest(String json) { Request.Builder request = new Request.Builder(); - request.url(serverUrl.get()); + request.url(serverUrl()); RequestBody body = RequestBody.create(JSON, json); request.post(body); return request.build(); } + private String serverUrl() { + return config.get(PROP_URL).orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", PROP_URL))); + } + } diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java index 7c5a6c2c89a9..f04afd3e228c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java +++ b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java @@ -38,6 +38,7 @@ import static org.sonar.api.utils.DateUtils.formatDate; import static org.sonar.api.utils.DateUtils.parseDate; import static org.sonar.core.config.TelemetryProperties.PROP_ENABLE; +import static org.sonar.core.config.TelemetryProperties.PROP_FREQUENCY; import static org.sonar.core.config.TelemetryProperties.PROP_URL; @ServerSide @@ -53,14 +54,12 @@ public class TelemetryDaemon implements Startable { private final InternalProperties internalProperties; private final Server server; private final System2 system2; - private final TelemetryFrequency frequencyInSeconds; private ScheduledExecutorService executorService; public TelemetryDaemon(TelemetryClient telemetryClient, Configuration config, InternalProperties internalProperties, Server server, System2 system2) { this.telemetryClient = telemetryClient; this.config = config; - this.frequencyInSeconds = new TelemetryFrequency(config); this.internalProperties = internalProperties; this.server = server; this.system2 = system2; @@ -94,6 +93,7 @@ public void start() { .setNameFormat(THREAD_NAME_PREFIX + "%d") .setPriority(Thread.MIN_PRIORITY) .build()); + int frequencyInSeconds = frequency(); executorService.scheduleWithFixedDelay(() -> { try { Optional lastPing = internalProperties.read(I_PROP_LAST_PING).map(Long::valueOf); @@ -114,7 +114,7 @@ public void start() { // fail silently } // do not check at start up to exclude test instance which are not up for a long time - }, frequencyInSeconds.get(), frequencyInSeconds.get(), TimeUnit.SECONDS); + }, frequencyInSeconds, frequencyInSeconds, TimeUnit.SECONDS); } @Override @@ -130,4 +130,8 @@ public void stop() { private static long startOfDay(long now) { return parseDate(formatDate(now)).getTime(); } + + private int frequency() { + return config.getInt(PROP_FREQUENCY).orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", PROP_FREQUENCY))); + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryFrequency.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryFrequency.java deleted file mode 100644 index 54b574c1c9dc..000000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryFrequency.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info 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.server.telemetry; - -import org.sonar.api.config.Configuration; - -class TelemetryFrequency { - private final Configuration config; - private Long frequency; - - TelemetryFrequency(Configuration config) { - this.config = config; - } - - long get() { - if (frequency == null) { - frequency = config.getLong(org.sonar.core.config.TelemetryProperties.PROP_FREQUENCY) - .orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", org.sonar.core.config.TelemetryProperties.PROP_FREQUENCY))); - } - - return frequency; - } -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryUrl.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryUrl.java deleted file mode 100644 index 8e32b0711709..000000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryUrl.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info 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.server.telemetry; - -import org.sonar.api.config.Configuration; - -import static org.sonar.core.config.TelemetryProperties.PROP_URL; - -class TelemetryUrl { - private final Configuration config; - private String url; - - TelemetryUrl(Configuration config) { - this.config = config; - } - - String get() { - if (url == null) { - url = config.get(PROP_URL).orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", PROP_URL))); - } - - return url; - } -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryUrlTest.java b/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryUrlTest.java deleted file mode 100644 index 7f56216facfb..000000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryUrlTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info 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.server.telemetry; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.api.config.internal.MapSettings; - -import static org.assertj.core.api.Assertions.assertThat; - -public class TelemetryUrlTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - private MapSettings settings = new MapSettings(); - - private TelemetryUrl underTest; - - @Test - public void return_url_as_is_when_no_ending_slash() { - settings.setProperty("sonar.telemetry.url", "http://localhost:9001"); - underTest = new TelemetryUrl(settings.asConfig()); - - assertThat(underTest.get()).isEqualTo("http://localhost:9001"); - } - - @Test - public void fail_when_no_settings_to_define_muppet_url() { - underTest = new TelemetryUrl(settings.asConfig()); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Setting 'sonar.telemetry.url' must be provided."); - - underTest.get(); - } -}