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;
addCoreSingleton(BatchPluginRepository.class);
addCoreSingleton(BatchExtensionInstaller.class);
- addCoreSingleton(BatchSettingsEnhancer.class);
+ addCoreSingleton(BatchDatabaseSettingsLoader.class);
}
boolean isMavenPluginExecutorRegistered() {
--- /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.config;
+
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.batch.bootstrap.ProjectReactor;
+import org.sonar.core.properties.PropertiesDao;
+import org.sonar.core.properties.PropertyDto;
+
+import java.util.List;
+
+/**
+ * @since 2.12
+ */
+public final class BatchDatabaseSettingsLoader {
+
+ private PropertiesDao propertiesDao;
+ private BatchSettings settings;
+ private ProjectReactor reactor;
+
+ public BatchDatabaseSettingsLoader(PropertiesDao propertiesDao, BatchSettings settings, ProjectReactor reactor) {
+ this.propertiesDao = propertiesDao;
+ this.settings = settings;
+ this.reactor = reactor;
+ }
+
+ public void start() {
+ 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<PropertyDto> dbProperties) {
+ for (PropertyDto dbProperty : dbProperties) {
+ if (!settings.hasKey(dbProperty.getKey())) {
+ settings.setProperty(dbProperty.getKey(), dbProperty.getValue());
+ }
+ }
+ }
+}
+++ /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.config;
-
-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 java.util.List;
-
-/**
- * @since 2.12
- */
-public final class BatchSettingsEnhancer {
-
- private DatabaseSessionFactory dbFactory;
- private BatchSettings settings;
- private ProjectReactor reactor;
-
- public BatchSettingsEnhancer(DatabaseSessionFactory dbFactory, BatchSettings settings, ProjectReactor reactor) {
- this.dbFactory = dbFactory;
- 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));
- settings.updateDeprecatedCommonsConfiguration();
- }
-
- private void setIfNotDefined(List<Property> dbProperties) {
- for (Property dbProperty : dbProperties) {
- if (!settings.hasKey(dbProperty.getKey())) {
- settings.setProperty(dbProperty.getKey(), dbProperty.getValue());
- }
- }
- }
-}
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;
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();
}
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());
}
}
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
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()) {
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);
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)) {
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;
private void startCoreComponents() {
coreContainer = rootContainer.createChild();
+ coreContainer.addSingleton(ServerDatabaseSettingsLoader.class);
coreContainer.addSingleton(DefaultDatabaseConnector.class);
coreContainer.addSingleton(PluginDeployer.class);
coreContainer.addSingleton(DefaultServerPluginRepository.class);
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();
}
/**
--- /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.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();
+ }
+}
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;
public static final String DEPLOY_DIR = "sonar.web.deployDir";
- private DatabaseSessionFactory sessionFactory;
+ private PropertiesDao propertiesDao;
private Configuration deprecatedConfiguration;
private File deployDir;
load(sonarHome);
}
- public ServerSettings setSessionFactory(DatabaseSessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
+ public ServerSettings activateDatabaseSettings(PropertiesDao dao) {
+ this.propertiesDao = dao;
return this;
}
clear();
setProperty(CoreProperties.SONAR_HOME, sonarHome.getAbsolutePath());
setProperty(DEPLOY_DIR, deployDir.getAbsolutePath());
-
+
// order is important : the last override the first
loadDatabaseSettings();
loadPropertiesFile(sonarHome);
}
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());
}
}
}
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;
}
@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());
}
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 -->