KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > upgrade > AbstractDatabaseUpgrade


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.upgrade;
21
22 import java.io.File JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.sql.DriverManager JavaDoc;
25
26 public abstract class AbstractDatabaseUpgrade {
27     
28     protected String JavaDoc oldDbName;
29     protected String JavaDoc newDbName;
30     protected File JavaDoc oldDbDir;
31     protected File JavaDoc newDbDir;
32     protected String JavaDoc name;
33     protected String JavaDoc description;
34     protected boolean selectedByDefault;
35
36     AbstractDatabaseUpgrade(String JavaDoc name, String JavaDoc description, boolean selectedByDefault, String JavaDoc oldDbName, String JavaDoc newDbName, File JavaDoc oldDbDir, File JavaDoc 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 JavaDoc getDescription() {
48         return description;
49     }
50
51     public String JavaDoc getName() {
52         return name;
53     }
54
55     public boolean isSelectedByDefault() {
56         return selectedByDefault;
57     }
58     
59     public void upgrade(Upgrader upgrader) throws Exception JavaDoc {
60         if (!new File JavaDoc(oldDbDir, oldDbName + ".data").exists()) {
61             throw new Exception JavaDoc("Old " + oldDbName + " database does not appear to exist.");
62         }
63         if (!new File JavaDoc(newDbDir, newDbName + ".data").exists()) {
64             throw new Exception JavaDoc(
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 JavaDoc 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 JavaDoc 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 JavaDoc oldConx, Connection JavaDoc newConx) throws Exception JavaDoc;
91
92
93 }
94
Popular Tags