View Javadoc

1   /*
2    * #%L
3    * Bitrepository Integrity Service
4    * %%
5    * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 2.1 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  
23  package org.bitrepository.service.database;
24  
25  import org.bitrepository.common.settings.Settings;
26  import org.bitrepository.common.settings.SettingsProvider;
27  import org.bitrepository.common.settings.XMLFileSettingsLoader;
28  import org.bitrepository.settings.referencesettings.DatabaseSpecifics;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  public class DatabaseCreator extends DatabaseMaintainer {
33      private final Logger log = LoggerFactory.getLogger(getClass());
34  
35      /**
36       * Creates a database by running the supplied script.
37       * @param scriptName The name of the script including path as part of the
38       * classpath.
39       * @param databaseSpecifics Specifies where to create the database.
40       */
41      protected void createDatabase(DatabaseSpecifics databaseSpecifics, String scriptName) {
42          DatabaseSpecifics databaseCreationSpecifics = updateDatabaseSpecificsForDBCreation(databaseSpecifics);
43          log.info("Creating database in " + databaseCreationSpecifics + " from script " + scriptName);
44          try {
45              runScript(databaseCreationSpecifics, scriptName);
46          } catch (Exception e) {
47              throw new RuntimeException(e);
48          }
49      }
50  
51      /**
52       * Appends the specified database url with <code>;create=true</code> to allow creation of the database.
53       */
54      private static DatabaseSpecifics updateDatabaseSpecificsForDBCreation(DatabaseSpecifics databaseSpecifics) {
55          DatabaseSpecifics newDatabaseSpecifics = new DatabaseSpecifics();
56          newDatabaseSpecifics.setDriverClass(databaseSpecifics.getDriverClass());
57          newDatabaseSpecifics.setDatabaseURL(databaseSpecifics.getDatabaseURL() + ";create=true");
58          newDatabaseSpecifics.setUsername(databaseSpecifics.getUsername());
59          newDatabaseSpecifics.setPassword(databaseSpecifics.getPassword());
60          return newDatabaseSpecifics;
61      }
62  
63      protected Settings loadSettings(String pillarID, String pathToSettings) {
64          SettingsProvider settingsLoader =
65              new SettingsProvider(new XMLFileSettingsLoader(pathToSettings), pillarID);
66  
67          return settingsLoader.getSettings();
68      }
69  }