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 java.io.BufferedReader;
26  import java.io.InputStream;
27  import java.io.InputStreamReader;
28  import java.io.Reader;
29  import java.sql.Connection;
30  import java.sql.DriverManager;
31  import org.bitrepository.settings.referencesettings.DatabaseSpecifics;
32  
33  public class DatabaseMaintainer {
34  
35      /**
36       * Attempts to find a script with the indicated name in the classpath and run it.
37       * @param scriptName The name of the script in the classpath.
38       */
39      protected static void runScript(DatabaseSpecifics databaseSpecifics, String scriptName) throws Exception {
40          Connection connection = getDBConnection(databaseSpecifics);
41          runScript(connection, scriptName);
42          connection.close();
43      }
44      
45      /**
46       * Attempts to find a script with the indicated name in the classpath and run it.
47       * @param scriptName The name of the script in the classpath.
48       */
49      protected static void runScript(DBConnector connector, String scriptName) throws Exception {
50          Connection connection = connector.getConnection();
51          runScript(connection, scriptName);
52          connection.close();
53      }
54      
55      /**
56       * 
57       * @param connection
58       * @param scriptName
59       * @throws Exception
60       */
61      private static void runScript(Connection connection, String scriptName) throws Exception {
62          SqlScriptRunner scriptRunner = new SqlScriptRunner(connection, false, true);
63          scriptRunner.runScript(getReaderForFile(scriptName));
64      }
65  
66      /**
67       * Creates a reader for the file found at indicated location.
68       */
69      private static Reader getReaderForFile(String filePath) throws java.io.IOException {
70          InputStream is = DatabaseMaintainer.class.getClassLoader().getResourceAsStream(filePath);
71  
72          if (is == null) {
73              throw new RuntimeException("Didn't find any file in classpath corresponding to " + filePath);
74          }
75  
76          return new BufferedReader(new InputStreamReader(is));
77      }
78  
79      /**
80       * Creates a connection based on the supplied <code>databaseSpecifics</code>.
81       */
82      protected static Connection getDBConnection(DatabaseSpecifics databaseSpecifics) throws Exception {
83          Class.forName(databaseSpecifics.getDriverClass());
84          Connection connection = DriverManager.getConnection(databaseSpecifics.getDatabaseURL());
85          return connection;
86      }
87  }