*/
package org.sonar.ce;
+import org.sonar.ce.http.CeHttpClient;
import org.sonar.ce.log.CeLogging;
import org.sonar.ce.queue.CeQueueImpl;
import org.sonar.ce.taskprocessor.ReportTaskProcessorDeclaration;
@Override
protected void configureModule() {
add(CeLogging.class,
+ CeHttpClient.class,
// Queue
CeQueueImpl.class,
ReportSubmitter.class,
-
+
// Core tasks processors
ReportTaskProcessorDeclaration.class);
}
--- /dev/null
+/*
+ * 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.http;
+
+import java.io.File;
+import java.net.URI;
+import java.util.Optional;
+import org.apache.commons.io.IOUtils;
+import org.sonar.api.config.Settings;
+import org.sonar.process.DefaultProcessCommands;
+import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
+
+import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH;
+import static org.sonar.process.ProcessId.COMPUTE_ENGINE;
+
+/**
+ * Client for the HTTP server of the Compute Engine.
+ */
+public class CeHttpClient {
+
+ private final File ipcSharedDir;
+
+ public CeHttpClient(Settings props) {
+ this.ipcSharedDir = new File(props.getString(PROPERTY_SHARED_PATH));
+ }
+
+ /**
+ * Connects to the specified JVM process and requests system information.
+ * @return the system info, or absent if the process is not up or if its HTTP URL
+ * is not registered into IPC.
+ */
+ public Optional<ProtobufSystemInfo.SystemInfo> retrieveSystemInfo() {
+ try (DefaultProcessCommands commands = DefaultProcessCommands.secondary(ipcSharedDir, COMPUTE_ENGINE.getIpcIndex())) {
+ if (commands.isUp()) {
+ String url = commands.getHttpUrl() + "/systemInfo";
+ byte[] protobuf = IOUtils.toByteArray(new URI(url));
+ return Optional.of(ProtobufSystemInfo.SystemInfo.parseFrom(protobuf));
+ }
+ return Optional.empty();
+ } catch (Exception e) {
+ throw new IllegalStateException("Can not get system info of process " + COMPUTE_ENGINE, e);
+ }
+ }
+}
--- /dev/null
+/*
+ * 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.http;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+++ /dev/null
-/*
- * 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.server.platform.monitoring;
-
-import com.google.common.base.Optional;
-import java.io.File;
-import java.net.URI;
-import org.apache.commons.io.IOUtils;
-import org.sonar.api.config.Settings;
-import org.sonar.process.DefaultProcessCommands;
-import org.sonar.process.ProcessId;
-import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
-
-import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH;
-
-/**
- * Connects to the System Info HTTP server of another JVM process.
- */
-public class ProcessSystemInfoClient {
-
- private final File ipcSharedDir;
-
- public ProcessSystemInfoClient(Settings props) {
- this.ipcSharedDir = new File(props.getString(PROPERTY_SHARED_PATH));
- }
-
- /**
- * Connects to the specified JVM process and requests system information.
- * @return the system info, or absent if the process is not up or if its HTTP URL
- * is not registered into IPC.
- */
- public Optional<ProtobufSystemInfo.SystemInfo> connect(ProcessId processId) {
- try (DefaultProcessCommands commands = DefaultProcessCommands.secondary(ipcSharedDir, processId.getIpcIndex())) {
- if (commands.isUp()) {
- String url = commands.getHttpUrl() + "/systemInfo";
- byte[] protobuf = IOUtils.toByteArray(new URI(url));
- return Optional.of(ProtobufSystemInfo.SystemInfo.parseFrom(protobuf));
- }
- return Optional.absent();
- } catch (Exception e) {
- throw new IllegalStateException("Can not get system info of process " + processId, e);
- }
- }
-}
import org.sonar.server.platform.monitoring.EsMonitor;
import org.sonar.server.platform.monitoring.JvmPropsMonitor;
import org.sonar.server.platform.monitoring.PluginsMonitor;
-import org.sonar.server.platform.monitoring.ProcessSystemInfoClient;
import org.sonar.server.platform.monitoring.SettingsMonitor;
import org.sonar.server.platform.monitoring.SonarQubeMonitor;
import org.sonar.server.platform.monitoring.SystemMonitor;
ProjectLinksModule.class,
// System
- ProcessSystemInfoClient.class,
ServerLogging.class,
RestartAction.class,
InfoAction.class,
*/
package org.sonar.server.platform.ws;
-import com.google.common.base.Optional;
import java.util.Map;
+import java.util.Optional;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.ce.http.CeHttpClient;
import org.sonar.core.permission.GlobalPermissions;
-import org.sonar.process.ProcessId;
import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
import org.sonar.server.platform.monitoring.Monitor;
-import org.sonar.server.platform.monitoring.ProcessSystemInfoClient;
import org.sonar.server.user.UserSession;
/**
public class InfoAction implements SystemWsAction {
private final UserSession userSession;
- private final ProcessSystemInfoClient processSystemInfoClient;
+ private final CeHttpClient ceHttpClient;
private final Monitor[] monitors;
- public InfoAction(UserSession userSession, ProcessSystemInfoClient processSystemInfoClient, Monitor... monitors) {
+ public InfoAction(UserSession userSession, CeHttpClient ceHttpClient, Monitor... monitors) {
this.userSession = userSession;
- this.processSystemInfoClient = processSystemInfoClient;
+ this.ceHttpClient = ceHttpClient;
this.monitors = monitors;
}
}
json.endObject();
}
- Optional<ProtobufSystemInfo.SystemInfo> ceSysInfo = processSystemInfoClient.connect(ProcessId.COMPUTE_ENGINE);
+ Optional<ProtobufSystemInfo.SystemInfo> ceSysInfo = ceHttpClient.retrieveSystemInfo();
if (ceSysInfo.isPresent()) {
for (ProtobufSystemInfo.Section section : ceSysInfo.get().getSectionsList()) {
json.name(section.getName());
--- /dev/null
+/*
+ * 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.http;
+
+import java.io.File;
+import java.util.Optional;
+import okhttp3.mockwebserver.MockResponse;
+import okhttp3.mockwebserver.MockWebServer;
+import okio.Buffer;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.config.Settings;
+import org.sonar.process.DefaultProcessCommands;
+import org.sonar.process.ProcessEntryPoint;
+import org.sonar.process.ProcessId;
+import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
+
+import static java.lang.String.format;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CeHttpClientTest {
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+ @Rule
+ public MockWebServer server = new MockWebServer();
+
+ private File ipcSharedDir;
+ private CeHttpClient underTest;
+
+ @Before
+ public void setUp() throws Exception {
+ ipcSharedDir = temp.newFolder();
+ Settings settings = new Settings();
+ settings.setProperty(ProcessEntryPoint.PROPERTY_SHARED_PATH, ipcSharedDir.getAbsolutePath());
+ underTest = new CeHttpClient(settings);
+ }
+
+ @Test
+ public void retrieveSystemInfo_returns_absent_if_process_is_down() throws Exception {
+ Optional<ProtobufSystemInfo.SystemInfo> info = underTest.retrieveSystemInfo();
+
+ assertThat(info.isPresent()).isFalse();
+ }
+
+ @Test
+ public void retrieveSystemInfo_get_information_if_process_is_up() throws Exception {
+ Buffer response = new Buffer();
+ response.read(ProtobufSystemInfo.Section.newBuilder().build().toByteArray());
+ server.enqueue(new MockResponse().setBody(response));
+
+ // initialize registration of process
+ try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, ProcessId.COMPUTE_ENGINE.getIpcIndex())) {
+ processCommands.setUp();
+ processCommands.setHttpUrl(format("http://%s:%d", server.getHostName(), server.getPort()));
+ }
+
+ Optional<ProtobufSystemInfo.SystemInfo> info = underTest.retrieveSystemInfo();
+ assertThat(info.get().getSectionsCount()).isEqualTo(0);
+ }
+
+ @Test
+ public void retrieveSystemInfo_throws_ISE_if_http_error() throws Exception {
+ server.enqueue(new MockResponse().setResponseCode(500));
+
+ // initialize registration of process
+ try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, ProcessId.COMPUTE_ENGINE.getIpcIndex())) {
+ processCommands.setUp();
+ processCommands.setHttpUrl(format("http://%s:%d", server.getHostName(), server.getPort()));
+ }
+
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Can not get system info of process " + ProcessId.COMPUTE_ENGINE);
+ underTest.retrieveSystemInfo();
+ }
+}
+++ /dev/null
-/*
- * 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.server.platform.monitoring;
-
-import com.google.common.base.Optional;
-import okhttp3.mockwebserver.MockResponse;
-import okhttp3.mockwebserver.MockWebServer;
-import java.io.File;
-import okio.Buffer;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.api.config.Settings;
-import org.sonar.process.DefaultProcessCommands;
-import org.sonar.process.ProcessEntryPoint;
-import org.sonar.process.ProcessId;
-import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
-
-import static java.lang.String.format;
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class ProcessSystemInfoClientTest {
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- @Rule
- public MockWebServer server = new MockWebServer();
-
- File ipcSharedDir;
- ProcessSystemInfoClient underTest;
-
- @Before
- public void setUp() throws Exception {
- ipcSharedDir = temp.newFolder();
- Settings settings = new Settings();
- settings.setProperty(ProcessEntryPoint.PROPERTY_SHARED_PATH, ipcSharedDir.getAbsolutePath());
- underTest = new ProcessSystemInfoClient(settings);
- }
-
- @Test
- public void connect_returns_absent_if_process_is_down() throws Exception {
- Optional<ProtobufSystemInfo.SystemInfo> info = underTest.connect(ProcessId.COMPUTE_ENGINE);
-
- assertThat(info.isPresent()).isFalse();
- }
-
- @Test
- public void get_information_if_process_is_up() throws Exception {
- Buffer response = new Buffer();
- response.read(ProtobufSystemInfo.Section.newBuilder().build().toByteArray());
- server.enqueue(new MockResponse().setBody(response));
-
- // initialize registration of process
- try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, ProcessId.COMPUTE_ENGINE.getIpcIndex())) {
- processCommands.setUp();
- processCommands.setHttpUrl(format("http://%s:%d", server.getHostName(), server.getPort()));
- }
-
- Optional<ProtobufSystemInfo.SystemInfo> info = underTest.connect(ProcessId.COMPUTE_ENGINE);
- assertThat(info.get().getSectionsCount()).isEqualTo(0);
- }
-
- @Test
- public void throws_ISE_if_http_error() throws Exception {
- server.enqueue(new MockResponse().setResponseCode(500));
-
- // initialize registration of process
- try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, ProcessId.COMPUTE_ENGINE.getIpcIndex())) {
- processCommands.setUp();
- processCommands.setHttpUrl(format("http://%s:%d", server.getHostName(), server.getPort()));
- }
-
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Can not get system info of process " + ProcessId.COMPUTE_ENGINE);
- underTest.connect(ProcessId.COMPUTE_ENGINE);
- }
-}
import java.util.LinkedHashMap;
import java.util.Map;
+import java.util.Optional;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mockito;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.platform.monitoring.Monitor;
-import org.sonar.server.platform.monitoring.ProcessSystemInfoClient;
+import org.sonar.ce.http.CeHttpClient;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
Monitor monitor1 = mock(Monitor.class);
Monitor monitor2 = mock(Monitor.class);
- ProcessSystemInfoClient processSystemInfoClient = mock(ProcessSystemInfoClient.class, Mockito.RETURNS_MOCKS);
+ CeHttpClient ceHttpClient = mock(CeHttpClient.class, Mockito.RETURNS_MOCKS);
- WsActionTester ws = new WsActionTester(new InfoAction(userSessionRule, processSystemInfoClient, monitor1, monitor2));
+ WsActionTester ws = new WsActionTester(new InfoAction(userSessionRule, ceHttpClient, monitor1, monitor2));
@Test
public void test_definition() throws Exception {
when(monitor1.attributes()).thenReturn(attributes1);
when(monitor2.name()).thenReturn("Monitor Two");
when(monitor2.attributes()).thenReturn(attributes2);
+ when(ceHttpClient.retrieveSystemInfo()).thenReturn(Optional.empty());
TestResponse response = ws.newRequest().execute();
// response does not contain empty "Monitor Three"
import org.sonar.server.app.ProcessCommandWrapper;
import org.sonar.server.app.RestartFlagHolder;
import org.sonar.server.platform.Platform;
-import org.sonar.server.platform.monitoring.ProcessSystemInfoClient;
+import org.sonar.ce.http.CeHttpClient;
import org.sonar.server.tester.AnonymousMockUserSession;
import org.sonar.server.user.UserSession;
public class SystemWsTest {
- ProcessSystemInfoClient processSystemInfoClient = mock(ProcessSystemInfoClient.class);
+ CeHttpClient ceHttpClient = mock(CeHttpClient.class);
@Test
public void define() {
RestartAction action1 = new RestartAction(mock(UserSession.class), mock(Settings.class), mock(Platform.class), mock(ProcessCommandWrapper.class), mock(RestartFlagHolder.class));
- InfoAction action2 = new InfoAction(new AnonymousMockUserSession(), processSystemInfoClient);
+ InfoAction action2 = new InfoAction(new AnonymousMockUserSession(), ceHttpClient);
SystemWs ws = new SystemWs(action1, action2);
WebService.Context context = new WebService.Context();