aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2011-08-12 12:07:30 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2011-08-12 12:07:30 +0200
commit1e3d7e4dc18b79d21aea4c0ffd4a40cb9a037dd4 (patch)
tree5807b1e12bcc78b3a19f2466d8b69443c5ef4d56
parentdd1f7945b12b787c2f2b6726656734d045ae9c8f (diff)
downloadsonarqube-1e3d7e4dc18b79d21aea4c0ffd4a40cb9a037dd4.tar.gz
sonarqube-1e3d7e4dc18b79d21aea4c0ffd4a40cb9a037dd4.zip
SONAR-2603 generate server key before loading plugins
-rw-r--r--sonar-server/src/main/java/org/sonar/server/platform/Platform.java2
-rw-r--r--sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java36
-rw-r--r--sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java33
-rw-r--r--sonar-server/src/test/resources/org/sonar/server/platform/ServerImplTest/shouldGenerateKey.xml6
4 files changed, 55 insertions, 22 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java
index a03fbdc1828..a587cce3cc3 100644
--- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java
+++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java
@@ -144,6 +144,7 @@ public final class Platform {
coreContainer.as(Characteristics.CACHE).addComponent(UpdateCenterClient.class);
coreContainer.as(Characteristics.CACHE).addComponent(UpdateCenterMatrixFactory.class);
coreContainer.as(Characteristics.CACHE).addComponent(PluginDownloader.class);
+ coreContainer.as(Characteristics.CACHE).addComponent(ServerImpl.class);
coreContainer.as(Characteristics.NO_CACHE).addComponent(FilterExecutor.class);
coreContainer.as(Characteristics.NO_CACHE).addAdapter(new DatabaseSessionProvider());
coreContainer.start();
@@ -161,7 +162,6 @@ public final class Platform {
DefaultServerPluginRepository pluginRepository = servicesContainer.getComponent(DefaultServerPluginRepository.class);
pluginRepository.registerExtensions(servicesContainer);
- servicesContainer.as(Characteristics.CACHE).addComponent(ServerImpl.class);
servicesContainer.as(Characteristics.CACHE).addComponent(DefaultModelFinder.class); // depends on plugins
servicesContainer.as(Characteristics.CACHE).addComponent(DefaultModelManager.class);
servicesContainer.as(Characteristics.CACHE).addComponent(Plugins.class);
diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java
index 3b2508ddc21..d156e567b5c 100644
--- a/sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java
+++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java
@@ -19,11 +19,13 @@
*/
package org.sonar.server.platform;
-import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.CoreProperties;
+import org.sonar.api.database.configuration.Property;
+import org.sonar.api.database.DatabaseSession;
import org.sonar.api.platform.Server;
+import org.sonar.jpa.session.DatabaseSessionFactory;
import java.io.IOException;
import java.io.InputStream;
@@ -37,21 +39,27 @@ public final class ServerImpl extends Server {
private String version;
private final Date startedAt;
private String key;
- private Configuration conf;
- public ServerImpl(Configuration conf) {
- this(conf, new Date());
+ /**
+ * This component can't use Configuration because of startup sequence. It must be started before plugins.
+ */
+ private DatabaseSessionFactory dbSessionFactory;
+ private ServerKeyGenerator keyGenerator;
+
+ public ServerImpl(DatabaseSessionFactory dbSessionFactory) {
+ this(dbSessionFactory, new ServerKeyGenerator(), new Date());
}
- ServerImpl(Configuration conf, Date startedAt) {
- this.conf = conf;
+ ServerImpl(DatabaseSessionFactory dbSessionFactory, ServerKeyGenerator keyGenerator, Date startedAt) {
+ this.dbSessionFactory = dbSessionFactory;
this.startedAt = startedAt;
+ this.keyGenerator = keyGenerator;
}
public void start() {
try {
id = new SimpleDateFormat("yyyyMMddHHmmss").format(startedAt);
- key = initKey(conf);
+ key = initKey();
version = loadVersionFromManifest("/META-INF/maven/org.codehaus.sonar/sonar-plugin-api/pom.properties");
if (StringUtils.isBlank(version)) {
throw new ServerStartException("Unknown Sonar version");
@@ -62,11 +70,15 @@ public final class ServerImpl extends Server {
}
}
- private String initKey(Configuration conf) {
- String organization = conf.getString(CoreProperties.ORGANIZATION);
- String baseUrl = conf.getString(CoreProperties.SERVER_BASE_URL, CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE);
- String previousKey = conf.getString(CoreProperties.SERVER_KEY);
- return new ServerKeyGenerator().generate(organization, baseUrl, previousKey);
+ private String initKey() {
+ DatabaseSession session = dbSessionFactory.getSession();
+ Property organization = session.getSingleResult(Property.class, "key", CoreProperties.ORGANIZATION);
+ Property baseUrl = session.getSingleResult(Property.class, "key", CoreProperties.SERVER_BASE_URL);
+ Property previousKey = session.getSingleResult(Property.class, "key", CoreProperties.SERVER_KEY);
+ return keyGenerator.generate(
+ organization!=null ? organization.getValue() : null,
+ baseUrl != null ? baseUrl.getValue() : null,
+ previousKey!=null ? previousKey.getValue() : null);
}
public String getId() {
diff --git a/sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java b/sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java
index faaf814c846..4d5f0049b59 100644
--- a/sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java
@@ -19,19 +19,22 @@
*/
package org.sonar.server.platform;
-import org.apache.commons.configuration.PropertiesConfiguration;
+import org.hamcrest.core.Is;
import org.junit.Test;
+import org.sonar.jpa.test.AbstractDbUnitTestCase;
import java.io.IOException;
+import java.util.Date;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
-public class ServerImplTest {
+public class ServerImplTest extends AbstractDbUnitTestCase {
@Test
public void alwaysReturnTheSameValues() {
- ServerImpl server = new ServerImpl(new PropertiesConfiguration());
+ ServerImpl server = new ServerImpl(getSessionFactory());
server.start();
assertNotNull(server.getId());
@@ -46,21 +49,33 @@ public class ServerImplTest {
@Test
public void getVersionFromFile() throws IOException {
- assertEquals("1.0", new ServerImpl(new PropertiesConfiguration()).loadVersionFromManifest("/org/sonar/server/platform/ServerImplTest/pom-with-version.properties"));
+ assertEquals("1.0", new ServerImpl(getSessionFactory()).loadVersionFromManifest("/org/sonar/server/platform/ServerImplTest/pom-with-version.properties"));
}
@Test
public void testFileWithNoVersion() throws IOException {
- assertEquals("", new ServerImpl(new PropertiesConfiguration()).loadVersionFromManifest("/org/sonar/server/platform/ServerImplTest/pom-without-version.properties"));
+ assertEquals("", new ServerImpl(getSessionFactory()).loadVersionFromManifest("/org/sonar/server/platform/ServerImplTest/pom-without-version.properties"));
}
@Test
public void testFileWithEmptyVersionParameter() throws IOException {
- assertEquals("", new ServerImpl(new PropertiesConfiguration()).loadVersionFromManifest("/org/sonar/server/platform/ServerImplTest/pom-with-empty-version.properties"));
+ assertEquals("", new ServerImpl(getSessionFactory()).loadVersionFromManifest("/org/sonar/server/platform/ServerImplTest/pom-with-empty-version.properties"));
}
@Test
public void shouldNotFailIfFileNotFound() throws IOException {
- assertEquals("", new ServerImpl(new PropertiesConfiguration()).loadVersionFromManifest("/org/sonar/server/platform/ServerImplTest/unknown-file.properties"));
+ assertEquals("", new ServerImpl(getSessionFactory()).loadVersionFromManifest("/org/sonar/server/platform/ServerImplTest/unknown-file.properties"));
+ }
+
+ @Test
+ public void shouldGenerateKey() {
+ setupData("shouldGenerateKey");
+
+ ServerKeyGenerator keyGenerator = mock(ServerKeyGenerator.class);
+ when(keyGenerator.generate("World Company", "http://192.168.0.1", null)).thenReturn("abcde");
+ ServerImpl server = new ServerImpl(getSessionFactory(), keyGenerator, new Date());
+ server.start();
+
+ assertThat(server.getKey(), Is.is("abcde"));
}
}
diff --git a/sonar-server/src/test/resources/org/sonar/server/platform/ServerImplTest/shouldGenerateKey.xml b/sonar-server/src/test/resources/org/sonar/server/platform/ServerImplTest/shouldGenerateKey.xml
new file mode 100644
index 00000000000..df18318837d
--- /dev/null
+++ b/sonar-server/src/test/resources/org/sonar/server/platform/ServerImplTest/shouldGenerateKey.xml
@@ -0,0 +1,6 @@
+<dataset>
+
+ <properties id="1" prop_key="sonar.organization" text_value="World Company" resource_id="[null]" user_id="[null]"/>
+ <properties id="2" prop_key="sonar.core.serverBaseURL" text_value="http://192.168.0.1" resource_id="[null]" user_id="[null]"/>
+
+</dataset> \ No newline at end of file