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();
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);
*/
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;
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");
}
}
- 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() {
*/
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());
@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"));
}
}
--- /dev/null
+<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