You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DatabaseConfiguration.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2008-2011 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * Sonar is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.api.database.configuration;
  21. import org.apache.commons.configuration.BaseConfiguration;
  22. import org.sonar.api.database.DatabaseSession;
  23. import org.sonar.jpa.session.DatabaseSessionFactory;
  24. import java.util.List;
  25. /**
  26. * IMPORTANT : This class can't be moved to org.sonar.jpa.dao for backward-compatibility reasons.
  27. * This class is still used in some plugins.
  28. *
  29. * @since 1.10
  30. */
  31. public class DatabaseConfiguration extends BaseConfiguration {
  32. private DatabaseSessionFactory sessionFactory;
  33. private DatabaseSession session;
  34. public DatabaseConfiguration(DatabaseSessionFactory sessionFactory) {
  35. this.sessionFactory = sessionFactory;
  36. load();
  37. }
  38. public DatabaseConfiguration(DatabaseSession session) {
  39. this.session = session;
  40. load();
  41. }
  42. public void load() {
  43. clear();
  44. // Ugly workaround before the move to myBatis
  45. // Session is not up-to-date when Ruby on Rails inserts new rows in its own transaction. Seems like
  46. // Hibernate keeps a cache...
  47. getSession().commit();
  48. List<Property> properties = getSession()
  49. .createQuery("from " + Property.class.getSimpleName() + " p where p.resourceId is null and p.userId is null")
  50. .getResultList();
  51. if (properties != null) {
  52. for (Property property : properties) {
  53. setProperty(property.getKey(), property.getValue());
  54. }
  55. }
  56. }
  57. private DatabaseSession getSession() {
  58. if (session != null) {
  59. return session;
  60. }
  61. return sessionFactory.getSession();
  62. }
  63. }