📄 dbupgrader.java
字号:
/*
* SSL-Explorer
*
* Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.sslexplorer.jdbc;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.prefs.Preferences;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sslexplorer.boot.ContextHolder;
import com.sslexplorer.boot.Util;
import com.sslexplorer.boot.VersionInfo;
/**
* Checks database schemas to see if they are up to date, running the database
* upgrade scripts if they are not.
* <p>
* Current database versions are stored using the Java Preferences API.
* <p>
* TODO This class currently assumes that HSQLDB is in use (to check for the
* existance of databases), although it should be relatively to adapt it for use
* with other database implementations.
*
* @author Brett Smith <a href="mailto: brett@3sp.com"><brett@3sp.com></a>
* @version $Revision: 1.13 $
*/
public class DBUpgrader {
final static Log log = LogFactory.getLog(DBUpgrader.class);
//
private static HashMap removed = new HashMap();
private static HashMap removeProcessed = new HashMap();
// Private instance variables
private JDBCDatabaseEngine engine;
private File dbDir;
private VersionInfo.Version newDbVersion;
private File upgradeDir;
private File versionsFile;
private boolean useDbNameForVersionCheck;
/**
* Constructor
*
* @param newDbVersion the new version of the database
* @param engine the database engine to use to execute the commands
* @param dbDir directory containing databases
* @param upgradeDir directory containing upgrade script
*/
public DBUpgrader(VersionInfo.Version newDbVersion, JDBCDatabaseEngine engine, File dbDir, File upgradeDir) {
this(null, newDbVersion, engine, dbDir, upgradeDir);
}
/**
* Constructor
*
* @param versionsFile file to store database version
* @param newDbVersion the new version of the database
* @param engine the database engine to use to execute the commands
* @param dbDir directory containing databases
* @param upgradeDir directory containing upgrade script
*/
public DBUpgrader(File versionsFile, VersionInfo.Version newDbVersion, JDBCDatabaseEngine engine, File dbDir, File upgradeDir) {
this(versionsFile, newDbVersion, engine, dbDir, upgradeDir, false);
}
/**
* Constructor
*
* @param versionsFile file to store database version
* @param newDbVersion the new version of the database
* @param engine the database engine to use to execute the commands
* @param dbDir directory containing databases
* @param upgradeDir directory containing upgrade script
* @param useDbNameForVersionCheck use database name for version check
* instead of alias
*/
public DBUpgrader(File versionsFile, VersionInfo.Version newDbVersion, JDBCDatabaseEngine engine, File dbDir, File upgradeDir,
boolean useDbNameForVersionCheck) {
super();
this.versionsFile = versionsFile;
this.upgradeDir = upgradeDir;
this.engine = engine;
this.dbDir = dbDir;
this.newDbVersion = newDbVersion;
this.useDbNameForVersionCheck = useDbNameForVersionCheck;
}
/**
* Check the database schema and perform any upgrades.
*
* @throws Exception on any error
*/
public void upgrade() throws Exception {
Properties versions = null;
if (versionsFile == null) {
/* If required, convert from the old preferences node to the new
* file (version 0.2.5)
*/
versionsFile = new File(ContextHolder.getContext().getDBDirectory(), "versions.log");
Preferences p = ContextHolder.getContext().getPreferences().node("dbupgrader");
if (p.nodeExists("currentDataVersion")) {
log.warn("Migrating database versions from preferences to properties file in "
+ ContextHolder.getContext().getDBDirectory().getAbsolutePath() + ".");
versions = new Properties();
p = p.node("currentDataVersion");
String[] c = p.keys();
for (int i = 0; i < c.length; i++) {
versions.put(c[i], p.get(c[i], ""));
}
FileOutputStream fos = new FileOutputStream(versionsFile);
try {
versions.store(fos, "SSL-Explorer Database versions");
} finally {
Util.closeStream(fos);
}
p.removeNode();
}
}
// Load the database versions
if (versions == null) {
versions = new Properties();
if (versionsFile.exists()) {
FileInputStream fin = new FileInputStream(versionsFile);
try {
versions.load(fin);
} finally {
Util.closeStream(fin);
}
}
}
try {
String dbCheckName = useDbNameForVersionCheck ? engine.getDatabase() : engine.getAlias();
if ((!engine.isDatabaseExists() || removed.containsKey(engine.getDatabase()))
&& !removeProcessed.containsKey(dbCheckName)) {
versions.remove(dbCheckName);
removeProcessed.put(dbCheckName, Boolean.TRUE);
if (log.isInfoEnabled())
log.info("Database for " + dbCheckName + " (" + engine.getDatabase()
+ ") has been removed, assuming this is a re-install.");
removed.put(engine.getDatabase(), Boolean.TRUE);
}
// Check for any SQL scripts to run to bring the databases up to
// date
VersionInfo.Version currentDataVersion = new VersionInfo.Version(versions.getProperty(dbCheckName, "0.0.0"));
if (log.isInfoEnabled()) {
log.info("New logical database version for " + engine.getAlias() + " is " + newDbVersion);
log.info("Current logical database version for " + engine.getAlias() + " is " + currentDataVersion);
//
log.info("Upgrade script directory is " + upgradeDir.getAbsolutePath());
}
List upgrades = getSortedUpgrades(upgradeDir);
File oldLog = new File(upgradeDir, "upgrade.log");
if (!dbDir.exists()) {
if (!dbDir.mkdirs()) {
throw new Exception("Failed to create database directory " + dbDir.getAbsolutePath());
}
}
File logFile = new File(dbDir, "upgrade.log");
if (oldLog.exists()) {
if (log.isInfoEnabled())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -