]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2603 generate server key before loading plugins
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 12 Aug 2011 10:07:30 +0000 (12:07 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 12 Aug 2011 10:07:30 +0000 (12:07 +0200)
sonar-server/src/main/java/org/sonar/server/platform/Platform.java
sonar-server/src/main/java/org/sonar/server/platform/ServerImpl.java
sonar-server/src/test/java/org/sonar/server/platform/ServerImplTest.java
sonar-server/src/test/resources/org/sonar/server/platform/ServerImplTest/shouldGenerateKey.xml [new file with mode: 0644]

index a03fbdc1828cf31ad8fe164d3b1ce7a1565e034c..a587cce3cc3928e9b2df1e4b96092fe5c93e1b2c 100644 (file)
@@ -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);
index 3b2508ddc21a223d78f5c86cb52dd0261032fef6..d156e567b5c28ebc9bead51af4e29dc6e350257a 100644 (file)
  */
 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() {
index faaf814c84680b4296b69eeb3a54aa55d635207e..4d5f0049b596afdeb1a4b8f6f50a51b44ab993cd 100644 (file)
  */
 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 (file)
index 0000000..df18318
--- /dev/null
@@ -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