summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-03-11 21:37:19 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2012-03-12 11:29:00 +0100
commitd129cbd866b0242ef6777bb6923d27c852ccf95f (patch)
tree3b678507e2836c31c30b97d9a16436163be5012e
parent48ac6cdb612920154aa4bca46fb4f626173adcbc (diff)
downloadsonarqube-d129cbd866b0242ef6777bb6923d27c852ccf95f.tar.gz
sonarqube-d129cbd866b0242ef6777bb6923d27c852ccf95f.zip
Remove Hibernate from the component org.sonar.api.config.Settings
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java4
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/config/BatchDatabaseSettingsLoader.java (renamed from sonar-batch/src/main/java/org/sonar/batch/config/BatchSettingsEnhancer.java)27
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java23
-rw-r--r--sonar-core/src/main/java/org/sonar/core/config/ConfigurationUtils.java47
-rw-r--r--sonar-core/src/test/java/org/sonar/core/config/ConfigurationUtilsTest.java2
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java2
-rw-r--r--sonar-server/src/main/java/org/sonar/server/platform/Platform.java7
-rw-r--r--sonar-server/src/main/java/org/sonar/server/platform/ServerDatabaseSettingsLoader.java41
-rw-r--r--sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java20
-rw-r--r--sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java9
-rw-r--r--sonar-server/src/test/resources/org/sonar/server/platform/ServerSettingsTest/db/shared.xml8
11 files changed, 100 insertions, 90 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java
index 8ca3fe2a087..ced7e8a4d34 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java
@@ -26,7 +26,7 @@ import org.sonar.batch.FakeMavenPluginExecutor;
import org.sonar.batch.MavenPluginExecutor;
import org.sonar.batch.ServerMetadata;
import org.sonar.batch.config.BatchSettings;
-import org.sonar.batch.config.BatchSettingsEnhancer;
+import org.sonar.batch.config.BatchDatabaseSettingsLoader;
import org.sonar.core.persistence.DatabaseVersion;
import org.sonar.jpa.session.DatabaseSessionProvider;
import org.sonar.jpa.session.DefaultDatabaseConnector;
@@ -88,7 +88,7 @@ public class BootstrapModule extends Module {
addCoreSingleton(BatchPluginRepository.class);
addCoreSingleton(BatchExtensionInstaller.class);
- addCoreSingleton(BatchSettingsEnhancer.class);
+ addCoreSingleton(BatchDatabaseSettingsLoader.class);
}
boolean isMavenPluginExecutorRegistered() {
diff --git a/sonar-batch/src/main/java/org/sonar/batch/config/BatchSettingsEnhancer.java b/sonar-batch/src/main/java/org/sonar/batch/config/BatchDatabaseSettingsLoader.java
index dc605219b88..5f91446ddf7 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/config/BatchSettingsEnhancer.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/config/BatchDatabaseSettingsLoader.java
@@ -19,37 +19,42 @@
*/
package org.sonar.batch.config;
+import org.apache.commons.lang.StringUtils;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectReactor;
-import org.sonar.api.database.configuration.Property;
-import org.sonar.core.config.ConfigurationUtils;
-import org.sonar.jpa.session.DatabaseSessionFactory;
+import org.sonar.core.properties.PropertiesDao;
+import org.sonar.core.properties.PropertyDto;
import java.util.List;
/**
* @since 2.12
*/
-public final class BatchSettingsEnhancer {
+public final class BatchDatabaseSettingsLoader {
- private DatabaseSessionFactory dbFactory;
+ private PropertiesDao propertiesDao;
private BatchSettings settings;
private ProjectReactor reactor;
- public BatchSettingsEnhancer(DatabaseSessionFactory dbFactory, BatchSettings settings, ProjectReactor reactor) {
- this.dbFactory = dbFactory;
+ public BatchDatabaseSettingsLoader(PropertiesDao propertiesDao, BatchSettings settings, ProjectReactor reactor) {
+ this.propertiesDao = propertiesDao;
this.settings = settings;
this.reactor = reactor;
}
public void start() {
- setIfNotDefined(ConfigurationUtils.getProjectProperties(dbFactory, reactor.getRoot().getKey(), settings.getString(CoreProperties.PROJECT_BRANCH_PROPERTY)));
- setIfNotDefined(ConfigurationUtils.getGeneralProperties(dbFactory));
+ String branch = settings.getString(CoreProperties.PROJECT_BRANCH_PROPERTY);
+ String projectKey = reactor.getRoot().getKey();
+ if (StringUtils.isNotBlank(branch)) {
+ projectKey = String.format("%s:%s", projectKey, branch);
+ }
+ setIfNotDefined(propertiesDao.selectProjectProperties(projectKey));
+ setIfNotDefined(propertiesDao.selectGlobalProperties());
settings.updateDeprecatedCommonsConfiguration();
}
- private void setIfNotDefined(List<Property> dbProperties) {
- for (Property dbProperty : dbProperties) {
+ private void setIfNotDefined(List<PropertyDto> dbProperties) {
+ for (PropertyDto dbProperty : dbProperties) {
if (!settings.hasKey(dbProperty.getKey())) {
settings.setProperty(dbProperty.getKey(), dbProperty.getValue());
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java b/sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java
index 31c668a8cd4..d470a9cdb02 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java
@@ -21,14 +21,15 @@ package org.sonar.batch.config;
import com.google.common.collect.Lists;
import org.apache.commons.configuration.Configuration;
+import org.apache.commons.lang.StringUtils;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.Settings;
-import org.sonar.api.database.configuration.Property;
import org.sonar.api.resources.Project;
import org.sonar.core.config.ConfigurationUtils;
-import org.sonar.jpa.session.DatabaseSessionFactory;
+import org.sonar.core.properties.PropertiesDao;
+import org.sonar.core.properties.PropertyDto;
import java.util.List;
@@ -39,13 +40,13 @@ public class ProjectSettings extends Settings {
private Configuration deprecatedCommonsConf;
private ProjectDefinition projectDefinition;
- private DatabaseSessionFactory dbFactory;
+ private PropertiesDao propertiesDao;
- public ProjectSettings(PropertyDefinitions definitions, ProjectDefinition projectDefinition, DatabaseSessionFactory dbFactory, Project project) {
+ public ProjectSettings(PropertyDefinitions definitions, ProjectDefinition projectDefinition, PropertiesDao propertiesDao, Project project) {
super(definitions);
this.deprecatedCommonsConf = project.getConfiguration(); // Configuration is not a parameter to be sure that the project conf is used, not the global one
this.projectDefinition = projectDefinition;
- this.dbFactory = dbFactory;
+ this.propertiesDao = propertiesDao;
load();
}
@@ -82,15 +83,19 @@ public class ProjectSettings extends Settings {
if (projectDef.getParent() != null) {
loadDatabaseProjectSettings(projectDef.getParent(), branch);
}
- List<Property> props = ConfigurationUtils.getProjectProperties(dbFactory, projectDef.getKey(), branch);
- for (Property dbProperty : props) {
+ String projectKey = projectDef.getKey();
+ if (StringUtils.isNotBlank(branch)) {
+ projectKey = String.format("%s:%s", projectKey, branch);
+ }
+ List<PropertyDto> props = propertiesDao.selectProjectProperties(projectKey);
+ for (PropertyDto dbProperty : props) {
setProperty(dbProperty.getKey(), dbProperty.getValue());
}
}
private void loadDatabaseGlobalSettings() {
- List<Property> props = ConfigurationUtils.getGeneralProperties(dbFactory);
- for (Property dbProperty : props) {
+ List<PropertyDto> props = propertiesDao.selectGlobalProperties();
+ for (PropertyDto dbProperty : props) {
setProperty(dbProperty.getKey(), dbProperty.getValue());
}
}
diff --git a/sonar-core/src/main/java/org/sonar/core/config/ConfigurationUtils.java b/sonar-core/src/main/java/org/sonar/core/config/ConfigurationUtils.java
index 91c03732bb2..e81ffff6ece 100644
--- a/sonar-core/src/main/java/org/sonar/core/config/ConfigurationUtils.java
+++ b/sonar-core/src/main/java/org/sonar/core/config/ConfigurationUtils.java
@@ -22,18 +22,15 @@ package org.sonar.core.config;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.text.StrSubstitutor;
-import org.sonar.api.database.DatabaseSession;
-import org.sonar.api.database.configuration.Property;
-import org.sonar.api.database.model.ResourceModel;
-import org.sonar.jpa.session.DatabaseSessionFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.util.*;
+import java.util.Enumeration;
+import java.util.Map;
+import java.util.Properties;
/**
* @since 2.12
@@ -85,43 +82,7 @@ public final class ConfigurationUtils {
return result;
}
- public static List<Property> getProjectProperties(DatabaseSessionFactory dbFactory, String moduleKey, String branch) {
- final String completeKey;
- if (StringUtils.isNotBlank(branch)) {
- completeKey = String.format("%s:%s", moduleKey, branch);
- } else {
- completeKey = moduleKey;
- }
- DatabaseSession session = prepareDbSession(dbFactory);
- ResourceModel resource = session.getSingleResult(ResourceModel.class, "key", completeKey);
- if (resource != null) {
- return session
- .createQuery("from " + Property.class.getSimpleName() + " p where p.resourceId=:resourceId and p.userId is null")
- .setParameter("resourceId", resource.getId())
- .getResultList();
-
- }
- return Collections.emptyList();
- }
-
- public static List<Property> getGeneralProperties(DatabaseSessionFactory dbFactory) {
- DatabaseSession session = prepareDbSession(dbFactory);
- return session
- .createQuery("from " + Property.class.getSimpleName() + " p where p.resourceId is null and p.userId is null")
- .getResultList();
-
- }
-
- private static DatabaseSession prepareDbSession(DatabaseSessionFactory dbFactory) {
- DatabaseSession session = dbFactory.getSession();
- // Ugly workaround before the move to myBatis
- // Session is not up-to-date when Ruby on Rails inserts new rows in its own transaction. Seems like
- // Hibernate keeps a cache...
- session.commit();
- return session;
- }
-
- public static void copyToCommonsConfiguration(Map<String,String> input, Configuration commonsConfig) {
+ public static void copyToCommonsConfiguration(Map<String, String> input, Configuration commonsConfig) {
// update deprecated configuration
commonsConfig.clear();
for (Map.Entry<String, String> entry : input.entrySet()) {
diff --git a/sonar-core/src/test/java/org/sonar/core/config/ConfigurationUtilsTest.java b/sonar-core/src/test/java/org/sonar/core/config/ConfigurationUtilsTest.java
index 7552abdfded..6707b019125 100644
--- a/sonar-core/src/test/java/org/sonar/core/config/ConfigurationUtilsTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/config/ConfigurationUtilsTest.java
@@ -57,7 +57,7 @@ public class ConfigurationUtilsTest {
Properties input = new Properties();
input.setProperty("hello", "world");
input.setProperty("foo", "bar");
- Map<String,String> output = Maps.newHashMap();
+ Map<String, String> output = Maps.newHashMap();
ConfigurationUtils.copyProperties(input, output);
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java
index 201b5f91bd9..755345be3f5 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java
@@ -136,7 +136,7 @@ public class Settings implements BatchComponent, ServerComponent {
return ArrayUtils.EMPTY_STRING_ARRAY;
}
- public List<String> getKeysStartingWith(String prefix) {
+ public final List<String> getKeysStartingWith(String prefix) {
List<String> result = Lists.newArrayList();
for (String key : properties.keySet()) {
if (StringUtils.startsWith(key, prefix)) {
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 ce7193fd5b0..e7591b7324e 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
@@ -35,6 +35,7 @@ import org.sonar.api.rules.XMLRuleParser;
import org.sonar.api.utils.HttpDownloader;
import org.sonar.api.utils.IocContainer;
import org.sonar.api.utils.TimeProfiler;
+import org.sonar.batch.config.BatchDatabaseSettingsLoader;
import org.sonar.core.i18n.GwtI18n;
import org.sonar.core.i18n.I18nManager;
import org.sonar.core.i18n.RuleI18nManager;
@@ -147,6 +148,7 @@ public final class Platform {
private void startCoreComponents() {
coreContainer = rootContainer.createChild();
+ coreContainer.addSingleton(ServerDatabaseSettingsLoader.class);
coreContainer.addSingleton(DefaultDatabaseConnector.class);
coreContainer.addSingleton(PluginDeployer.class);
coreContainer.addSingleton(DefaultServerPluginRepository.class);
@@ -155,11 +157,6 @@ public final class Platform {
coreContainer.addSingleton(ThreadLocalDatabaseSessionFactory.class);
coreContainer.addPicoAdapter(new DatabaseSessionProvider());
coreContainer.startComponents();
-
- DatabaseSessionFactory sessionFactory = coreContainer.getComponentByType(DatabaseSessionFactory.class);
- ServerSettings serverSettings = coreContainer.getComponentByType(ServerSettings.class);
- serverSettings.setSessionFactory(sessionFactory);
- serverSettings.load();
}
/**
diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerDatabaseSettingsLoader.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerDatabaseSettingsLoader.java
new file mode 100644
index 00000000000..2f1d651d72b
--- /dev/null
+++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerDatabaseSettingsLoader.java
@@ -0,0 +1,41 @@
+/*
+ * 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.server.platform;
+
+import org.sonar.core.properties.PropertiesDao;
+
+/**
+ * @since 2.15
+ */
+public final class ServerDatabaseSettingsLoader {
+
+ private PropertiesDao propertiesDao;
+ private ServerSettings settings;
+
+ public ServerDatabaseSettingsLoader(PropertiesDao propertiesDao, ServerSettings settings) {
+ this.propertiesDao = propertiesDao;
+ this.settings = settings;
+ }
+
+ public void start() {
+ settings.activateDatabaseSettings(propertiesDao);
+ settings.load();
+ }
+}
diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java
index 86ab480757d..edca83e0953 100644
--- a/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java
+++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java
@@ -23,9 +23,9 @@ import org.apache.commons.configuration.Configuration;
import org.sonar.api.CoreProperties;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.Settings;
-import org.sonar.api.database.configuration.Property;
import org.sonar.core.config.ConfigurationUtils;
-import org.sonar.jpa.session.DatabaseSessionFactory;
+import org.sonar.core.properties.PropertiesDao;
+import org.sonar.core.properties.PropertyDto;
import javax.servlet.ServletContext;
import java.io.File;
@@ -47,7 +47,7 @@ public class ServerSettings extends Settings {
public static final String DEPLOY_DIR = "sonar.web.deployDir";
- private DatabaseSessionFactory sessionFactory;
+ private PropertiesDao propertiesDao;
private Configuration deprecatedConfiguration;
private File deployDir;
@@ -65,8 +65,8 @@ public class ServerSettings extends Settings {
load(sonarHome);
}
- public ServerSettings setSessionFactory(DatabaseSessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
+ public ServerSettings activateDatabaseSettings(PropertiesDao dao) {
+ this.propertiesDao = dao;
return this;
}
@@ -78,7 +78,7 @@ public class ServerSettings extends Settings {
clear();
setProperty(CoreProperties.SONAR_HOME, sonarHome.getAbsolutePath());
setProperty(DEPLOY_DIR, deployDir.getAbsolutePath());
-
+
// order is important : the last override the first
loadDatabaseSettings();
loadPropertiesFile(sonarHome);
@@ -92,10 +92,10 @@ public class ServerSettings extends Settings {
}
private void loadDatabaseSettings() {
- if (sessionFactory != null) {
- List<Property> properties = ConfigurationUtils.getGeneralProperties(sessionFactory);
- for (Property property : properties) {
- setProperty(property.getKey(), property.getValue());
+ if (propertiesDao != null) {
+ List<PropertyDto> dpProps = propertiesDao.selectGlobalProperties();
+ for (PropertyDto dbProp : dpProps) {
+ setProperty(dbProp.getKey(), dbProp.getValue());
}
}
}
diff --git a/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java b/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java
index b6582e3e891..c356cfaab36 100644
--- a/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java
@@ -22,6 +22,7 @@ package org.sonar.server.platform;
import org.apache.commons.configuration.BaseConfiguration;
import org.junit.Test;
import org.sonar.api.config.PropertyDefinitions;
+import org.sonar.core.properties.PropertiesDao;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
import java.io.File;
@@ -57,15 +58,15 @@ public class ServerSettingsTest extends AbstractDbUnitTestCase {
}
@Test
- public void shouldLoadPersistedGeneralSettings() throws URISyntaxException {
+ public void shouldActivateDatabaseSettings() throws URISyntaxException {
setupData("db/shared");
ServerSettings settings = new ServerSettings(new PropertyDefinitions(), new BaseConfiguration(), new File("."), home);
- settings.setSessionFactory(getSessionFactory());
+ settings.activateDatabaseSettings(new PropertiesDao(getMyBatis()));
settings.load(home);
- assertThat(settings.getString("general_only"), is("is_general"));
- assertThat(settings.getString("general_and_project"), is("is_general"));
+ assertThat(settings.getString("global_only"), is("is_global"));
+ assertThat(settings.getString("global_and_project"), is("is_global"));
assertThat(settings.getString("project_only"), nullValue());
}
diff --git a/sonar-server/src/test/resources/org/sonar/server/platform/ServerSettingsTest/db/shared.xml b/sonar-server/src/test/resources/org/sonar/server/platform/ServerSettingsTest/db/shared.xml
index c62b04aa28b..55b972c772f 100644
--- a/sonar-server/src/test/resources/org/sonar/server/platform/ServerSettingsTest/db/shared.xml
+++ b/sonar-server/src/test/resources/org/sonar/server/platform/ServerSettingsTest/db/shared.xml
@@ -6,12 +6,12 @@
description="[null]"
enabled="true" language="java" copy_resource_id="[null]" person_id="[null]"/>
- <!-- general properties -->
- <properties prop_key="general_only" resource_id="[null]" user_id="[null]" text_value="is_general"/>
- <properties prop_key="general_and_project" resource_id="[null]" user_id="[null]" text_value="is_general"/>
+ <!-- global properties -->
+ <properties prop_key="global_only" resource_id="[null]" user_id="[null]" text_value="is_global"/>
+ <properties prop_key="global_and_project" resource_id="[null]" user_id="[null]" text_value="is_global"/>
<!-- project properties: do not load -->
- <properties prop_key="general_and_project" resource_id="3333" user_id="[null]" text_value="is_project"/>
+ <properties prop_key="global_and_project" resource_id="3333" user_id="[null]" text_value="is_project"/>
<properties prop_key="project_only" resource_id="3333" user_id="[null]" text_value="is_project"/>
<!-- user properties : do not load -->