--- /dev/null
+/*
+ * 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.batch;
+
+import org.sonar.api.BatchComponent;
+import org.sonar.api.platform.Server;
+import org.sonar.batch.bootstrap.ServerClient;
+
+import java.util.Date;
+
+/**
+ * @deprecated replaced by ServerClient since version 3.4. Plugins should use org.sonar.api.platform.Server
+ */
+@Deprecated
+public class ServerMetadata extends Server implements BatchComponent {
+ private ServerClient server;
+
+ public ServerMetadata(ServerClient server) {
+ this.server = server;
+ }
+
+ @Override
+ public String getId() {
+ return server.getId();
+ }
+
+ @Override
+ public String getVersion() {
+ return server.getVersion();
+ }
+
+ @Override
+ public Date getStartedAt() {
+ return server.getStartedAt();
+ }
+
+ /**
+ * @return the server URL when executed from batch, else null.
+ * @since 2.4
+ */
+ @Override
+ public String getURL() {
+ return server.getURL();
+ }
+
+ /**
+ * @since 2.10
+ */
+ @Override
+ public String getPermanentServerId() {
+ return server.getPermanentServerId();
+ }
+}
import org.sonar.api.utils.UriReader;
import org.sonar.batch.FakeMavenPluginExecutor;
import org.sonar.batch.MavenPluginExecutor;
+import org.sonar.batch.ServerMetadata;
import org.sonar.batch.config.BatchDatabaseSettingsLoader;
import org.sonar.batch.config.BootstrapSettings;
import org.sonar.batch.local.DryRunDatabase;
import org.sonar.jpa.session.DatabaseSessionProvider;
import org.sonar.jpa.session.DefaultDatabaseConnector;
import org.sonar.jpa.session.ThreadLocalDatabaseSessionFactory;
+import org.sonar.wsclient.Sonar;
/**
* Level 1 components
container.addSingleton(DryRunExporter.class);
container.addSingleton(Logback.class);
container.addSingleton(ServerClient.class);
+ container.addSingleton(ServerMetadata.class);
+ container.addSingleton(WsConnector.class);
+ container.addSingleton(Sonar.class);
container.addSingleton(TempDirectories.class);
container.addSingleton(HttpDownloader.class);
container.addSingleton(UriReader.class);
}
private void checkCorrectServerId() {
- String remoteServerId;
- try {
- remoteServerId = server.getServerId();
- } catch (IOException e) {
- throw new SonarException("Impossible to get the ID of the remote server: " + server.getURL(), e);
- }
-
- if (!version.getSonarCoreId().equals(remoteServerId)) {
+ if (!version.getSonarCoreId().equals(server.getServerId())) {
StringBuilder message = new StringBuilder("The current batch process and the configured remote server do not share the same DB configuration.\n");
message.append("\t- Batch side: ");
message.append(settings.getString(DatabaseProperties.PROP_URL));
*/
public class ServerClient extends Server implements BatchComponent {
private Settings settings;
- private EnvironmentInformation env;
+ private HttpDownloader.BaseHttpDownloader downloader;
public ServerClient(Settings settings, EnvironmentInformation env) {
this.settings = settings;
- this.env = env;
+ this.downloader = new HttpDownloader.BaseHttpDownloader(settings, env.toString());
}
@Override
return settings.getString(CoreProperties.PERMANENT_SERVER_ID);
}
- public String getServerId() throws IOException {
+ public String getServerId() {
String remoteServerInfo = request("/api/server");
// don't use JSON utilities to extract ID from such a small string
return extractServerId(remoteServerInfo);
String login = settings.getString(CoreProperties.LOGIN);
try {
- HttpDownloader.BaseHttpDownloader downloader = new HttpDownloader.BaseHttpDownloader(settings, env.toString());
InputSupplier<InputStream> inputSupplier;
if (Strings.isNullOrEmpty(login)) {
inputSupplier = downloader.newInputSupplier(uri);
--- /dev/null
+/*
+ * 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.batch.bootstrap;
+
+import org.sonar.wsclient.connectors.Connector;
+import org.sonar.wsclient.services.CreateQuery;
+import org.sonar.wsclient.services.DeleteQuery;
+import org.sonar.wsclient.services.Query;
+import org.sonar.wsclient.services.UpdateQuery;
+
+/**
+ * @since 3.4
+ */
+public class WsConnector extends Connector {
+
+ private ServerClient server;
+
+ public WsConnector(ServerClient server) {
+ this.server = server;
+ }
+
+ /**
+ * @return JSON response or null if 404 NOT FOUND error
+ * @throws org.sonar.wsclient.connectors.ConnectionException
+ * if connection error or HTTP status not in (200, 404)
+ */
+ @Override
+ public String execute(Query<?> query) {
+ return server.request(query.getUrl());
+ }
+
+ /**
+ * @return JSON response or null if 404 NOT FOUND error
+ * @since 2.2
+ */
+ @Override
+ public String execute(CreateQuery<?> query) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * @return JSON response or null if 404 NOT FOUND error
+ * @since 2.2
+ */
+ @Override
+ public String execute(DeleteQuery query) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * @return JSON response or null if 404 NOT FOUND error
+ * @since 2.6
+ */
+ @Override
+ public String execute(UpdateQuery<?> query) {
+ throw new UnsupportedOperationException();
+ }
+}
load();
}
- public BootstrapSettings load() {
+ private BootstrapSettings load() {
clear();
// order is important -> bottom-up. The last one overrides all the others.
--- /dev/null
+/*
+ * 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.batch;
+
+import org.junit.Test;
+import org.sonar.api.utils.DateUtils;
+import org.sonar.batch.bootstrap.ServerClient;
+
+import java.util.Date;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class ServerMetadataTest {
+ @Test
+ public void should_proxy_server_client() {
+ ServerClient client = mock(ServerClient.class);
+ when(client.getId()).thenReturn("id1");
+ when(client.getPermanentServerId()).thenReturn("pid1");
+ Date startedAt = DateUtils.parseDate("2012-05-18");
+ when(client.getStartedAt()).thenReturn(startedAt);
+ when(client.getURL()).thenReturn("http://foo");
+ when(client.getVersion()).thenReturn("v1");
+
+ ServerMetadata metadata = new ServerMetadata(client);
+
+ assertThat(metadata.getId()).isEqualTo("id1");
+ assertThat(metadata.getPermanentServerId()).isEqualTo("pid1");
+ assertThat(metadata.getStartedAt()).isEqualTo(startedAt);
+ assertThat(metadata.getURL()).isEqualTo("http://foo");
+ assertThat(metadata.getVersion()).isEqualTo("v1");
+
+ }
+}
@Test
public void shouldFailIfCantGetServerId() throws Exception {
- when(server.getServerId()).thenThrow(new IOException());
+ when(server.getServerId()).thenThrow(new IllegalStateException());
- thrown.expect(SonarException.class);
- thrown.expectMessage("Impossible to get the ID of the remote server: http://localhost:9000");
+ thrown.expect(IllegalStateException.class);
new DatabaseBatchCompatibility(mock(DatabaseVersion.class), server, settings).start();
}