View Javadoc

1   /*
2    * Sonar, entreprise quality control tool.
3    * Copyright (C) 2007-2008 Hortis-GRC SA
4    * mailto:be_agile HAT hortis DOT ch
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.commons;
21  
22  import ch.hortis.sonar.service.MeasuresDao;
23  import ch.hortis.sonar.service.PropertiesDao;
24  import org.sonar.commons.database.BatchDatabaseManager;
25  import org.sonar.commons.database.DatabaseConnector;
26  import org.sonar.commons.database.DatabaseManager;
27  import org.sonar.commons.rules.RulesDao;
28  
29  public class DaoFacade {
30  
31    private DatabaseManager databaseManager = null;
32    private RulesDao rulesDao = null;
33    private MeasuresDao measuresDao = null;
34    private PropertiesDao propertiesDao = null;
35  
36  
37    public DaoFacade(DatabaseConnector databaseConnector) {
38      databaseManager = new BatchDatabaseManager(databaseConnector);
39      databaseManager.start();
40    }
41  
42    public void start() {
43    }
44  
45    public void stop() {
46      if (databaseManager != null) {
47        databaseManager.stop();
48        rulesDao = null;
49        measuresDao = null;
50      }
51    }
52  
53    public RulesDao getRulesDao() {
54      if (rulesDao == null) {
55        rulesDao = new RulesDao(databaseManager);
56      }
57      return rulesDao;
58    }
59  
60    public MeasuresDao getMeasuresDao() {
61      if (measuresDao == null) {
62        measuresDao = new MeasuresDao(getDatabaseManager());
63      }
64      return measuresDao;
65    }
66  
67    public PropertiesDao getPropertiesDao() {
68      if (propertiesDao == null) {
69        propertiesDao = new PropertiesDao(getDatabaseManager().getEntityManager());
70      }
71      return propertiesDao;
72    }
73  
74    public DatabaseManager getDatabaseManager() {
75      return databaseManager;
76    }
77  
78    protected void finalize() throws Throwable {
79      stop();
80      super.finalize();
81    }
82  }