]> source.dussan.org Git - sonarqube.git/commitdiff
Remove Hibernate from the component org.sonar.api.config.Settings
authorSimon Brandhof <simon.brandhof@gmail.com>
Sun, 11 Mar 2012 20:37:19 +0000 (21:37 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 12 Mar 2012 10:29:00 +0000 (11:29 +0100)
12 files changed:
sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java
sonar-batch/src/main/java/org/sonar/batch/config/BatchDatabaseSettingsLoader.java [new file with mode: 0644]
sonar-batch/src/main/java/org/sonar/batch/config/BatchSettingsEnhancer.java [deleted file]
sonar-batch/src/main/java/org/sonar/batch/config/ProjectSettings.java
sonar-core/src/main/java/org/sonar/core/config/ConfigurationUtils.java
sonar-core/src/test/java/org/sonar/core/config/ConfigurationUtilsTest.java
sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java
sonar-server/src/main/java/org/sonar/server/platform/Platform.java
sonar-server/src/main/java/org/sonar/server/platform/ServerDatabaseSettingsLoader.java [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/platform/ServerSettings.java
sonar-server/src/test/java/org/sonar/server/platform/ServerSettingsTest.java
sonar-server/src/test/resources/org/sonar/server/platform/ServerSettingsTest/db/shared.xml

index 8ca3fe2a087fa2aa889734874dcf06fe04032054..ced7e8a4d34bb3703092b0efbbf1ec514ad895f4 100644 (file)
@@ -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/BatchDatabaseSettingsLoader.java b/sonar-batch/src/main/java/org/sonar/batch/config/BatchDatabaseSettingsLoader.java
new file mode 100644 (file)
index 0000000..5f91446
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * 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());
+      }
+    }
+  }
+}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/config/BatchSettingsEnhancer.java b/sonar-batch/src/main/java/org/sonar/batch/config/BatchSettingsEnhancer.java
deleted file mode 100644 (file)
index dc60521..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * 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());
-      }
-    }
-  }
-}
index 31c668a8cd45ac3ddd9fba44025a23356f9de608..d470a9cdb02135dd0777f836dd69440891067d08 100644 (file)
@@ -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());
     }
   }
index 91c03732bb250338a00fca499cb310e878700026..e81ffff6ece4e83526899c99f3747a36374fd1d4 100644 (file)
@@ -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()) {
index 7552abdfded65cb0252acde7055af8035decf097..6707b019125819b20a3c717a2a00ca1bc614664a 100644 (file)
@@ -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);
 
index 201b5f91bd93106d262361dec6a389deae4f016b..755345be3f567429bea703946a49bf6cb0279f9f 100644 (file)
@@ -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)) {
index ce7193fd5b049f0a2a94578289a3c48c770eeef7..e7591b7324e4a3e08653077b50a85c99e11f24f2 100644 (file)
@@ -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 (file)
index 0000000..2f1d651
--- /dev/null
@@ -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();
+  }
+}
index 86ab480757d82c9a593d166eed8a96c22b842284..edca83e095333547c6e21acb34de1f98d8887cbf 100644 (file)
@@ -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());
       }
     }
   }
index b6582e3e89123292c70be37437d08e1f06c60228..c356cfaab3670aa50a79d67480cb9b6e25cea579 100644 (file)
@@ -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());
   }
 
index c62b04aa28b0521279e621b9b4b873a8f5d4e8ad..55b972c772f7e64800de4c9200e5397e921032cc 100644 (file)
@@ -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 -->