1 19 20 package com.sslexplorer.upgrade; 21 22 import java.io.File ; 23 import java.sql.Connection ; 24 import java.sql.DriverManager ; 25 26 public abstract class AbstractDatabaseUpgrade { 27 28 protected String oldDbName; 29 protected String newDbName; 30 protected File oldDbDir; 31 protected File newDbDir; 32 protected String name; 33 protected String description; 34 protected boolean selectedByDefault; 35 36 AbstractDatabaseUpgrade(String name, String description, boolean selectedByDefault, String oldDbName, String newDbName, File oldDbDir, File newDbDir) { 37 this.name = name; 38 this.description = description; 39 this.selectedByDefault = selectedByDefault; 40 this.oldDbName = oldDbName; 41 this.newDbName = newDbName; 42 this.oldDbDir = oldDbDir; 43 this.newDbDir = newDbDir; 44 } 45 46 47 public String getDescription() { 48 return description; 49 } 50 51 public String getName() { 52 return name; 53 } 54 55 public boolean isSelectedByDefault() { 56 return selectedByDefault; 57 } 58 59 public void upgrade(Upgrader upgrader) throws Exception { 60 if (!new File (oldDbDir, oldDbName + ".data").exists()) { 61 throw new Exception ("Old " + oldDbName + " database does not appear to exist."); 62 } 63 if (!new File (newDbDir, newDbName + ".data").exists()) { 64 throw new Exception ( 65 "New " + newDbName + " database does not appear to exist, you must run the installation wizard on your new installation and select 'Built in' user database"); 66 } 67 Class.forName("org.hsqldb.jdbcDriver"); 68 upgrader.info("Connecting to old " + oldDbName + " database"); 69 Connection oldConx = DriverManager.getConnection("jdbc:hsqldb:" + oldDbDir.getAbsolutePath() + File.separator 70 + oldDbName, "sa", ""); 71 oldConx.setAutoCommit(true); 72 try { 73 upgrader.info("Connecting to new " + newDbName + " database"); 74 Connection newConx = DriverManager.getConnection("jdbc:hsqldb:" + newDbDir.getAbsolutePath() + File.separator 75 + newDbName, "sa", ""); 76 newConx.setAutoCommit(true); 77 try { 78 doUpgrade(upgrader, oldConx, newConx); 79 } 80 finally { 81 newConx.close(); 82 } 83 } 84 finally { 85 oldConx.close(); 86 } 87 88 } 89 90 public abstract void doUpgrade(Upgrader upgrader, Connection oldConx, Connection newConx) throws Exception ; 91 92 93 } 94 | Popular Tags |