1 package org.objectweb.rentacar.persistance.database; 2 3 import java.io.FileNotFoundException ; 4 import java.io.IOException ; 5 import java.sql.DriverManager ; 6 import java.sql.SQLException ; 7 8 import javax.xml.parsers.DocumentBuilder ; 9 import javax.xml.parsers.DocumentBuilderFactory ; 10 import javax.xml.parsers.ParserConfigurationException ; 11 12 import org.dbunit.DatabaseUnitException; 13 import org.dbunit.database.DatabaseConnection; 14 import org.dbunit.database.IDatabaseConnection; 15 import org.dbunit.dataset.IDataSet; 16 import org.dbunit.dataset.xml.FlatXmlDataSet; 17 import org.dbunit.operation.DatabaseOperation; 18 import org.hibernate.HibernateException; 19 import org.hibernate.cfg.AnnotationConfiguration; 20 import org.hibernate.tool.hbm2ddl.SchemaExport; 21 import org.hsqldb.Server; 22 import org.w3c.dom.Document ; 23 import org.xml.sax.SAXException ; 24 25 28 public class DatabaseManager { 29 30 33 private Server server = new Server(); 34 35 42 public void runServer(String host, String port, String dbName, String dbPath) { 43 server.setAddress(host); 44 server.setDatabaseName(0, dbName); 45 server.setDatabasePath(0, dbPath); 46 server.setPort(Integer.parseInt(port)); 47 server.setSilent(true); 48 server.setTrace(false); 49 server.start(); 50 } 51 52 55 public void shutdownServer() { 56 server.stop(); 57 } 58 59 68 public void buildDatabaseSchema(String url, String driver, String dialect, String userName, String password) throws DatabaseException { 69 AnnotationConfiguration conf = new AnnotationConfiguration(); 70 try { 71 DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); 72 DocumentBuilder builder = fact.newDocumentBuilder(); 73 Document document = builder.parse(DatabaseManager.class.getClassLoader().getResourceAsStream("hibernate.cfg.xml")); 74 conf.configure(document); 75 conf.setProperty("hibernate.connection.username", userName); 76 conf.setProperty("hibernate.connection.password", password); 77 conf.setProperty("hibernate.connection.url", url); 78 conf.setProperty("hibernate.dialect", dialect); 79 conf.setProperty("hibernate.connection.driver_class", driver); 80 SchemaExport export = new SchemaExport(conf); 81 export.create(false, true); 82 } 83 catch (HibernateException e) { 84 throw new DatabaseException("Error building the schema", e); 85 } 86 catch (SAXException e) { 87 throw new DatabaseException("Error building the schema", e); 88 } 89 catch (IOException e) { 90 throw new DatabaseException("Error building the schema", e); 91 } 92 catch (ParserConfigurationException e) { 93 throw new DatabaseException("Error building the schema", e); 94 } 95 } 96 97 104 public void fillDatabase(String host, String port, String dbName, String dsFileName) throws DatabaseException { 105 IDatabaseConnection connection = null; 106 try { 107 Class.forName("org.hsqldb.jdbcDriver"); 108 connection = (IDatabaseConnection) new DatabaseConnection(DriverManager.getConnection("jdbc:hsqldb:hsql://" + host + " :" + port + "/" + dbName, "sa", "")); 109 IDataSet dataSet = new FlatXmlDataSet(DatabaseManager.class.getClassLoader().getResourceAsStream(dsFileName)); 110 DatabaseOperation.DELETE.execute(connection, dataSet); 111 DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet); 112 } 113 catch (DatabaseUnitException e) { 114 throw new DatabaseException("Error filling the database", e); 115 } 116 catch (SQLException e) { 117 throw new DatabaseException("Error filling the database", e); 118 } 119 catch (FileNotFoundException e) { 120 throw new DatabaseException("Error filling the database", e); 121 } 122 catch (IOException e) { 123 throw new DatabaseException("Error filling the database", e); 124 } 125 catch (Exception e) { 126 throw new DatabaseException("Error filling the database", e); 127 } 128 finally { 129 try { 130 if (connection != null) { 131 connection.close(); 132 } 133 } 134 catch (SQLException e) { 135 throw new DatabaseException("Error filling the database", e); 136 } 137 } 138 } 139 140 } 141 | Popular Tags |