⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbupgrader.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    log.info("Moving upgrade.log to new location (as of version 0.1.5 it resides in the db directory.");
                if (!oldLog.renameTo(logFile)) {
                    throw new Exception("Failed to move upgrade log file from " + oldLog.getAbsolutePath() + " to "
                                    + logFile.getAbsolutePath());
                }
            }
            HashMap completedUpgrades = new HashMap();
            if (!logFile.exists()) {
                OutputStream out = null;
                try {
                    out = new FileOutputStream(logFile);
                    PrintWriter writer = new PrintWriter(out, true);
                    writer.println("# This file contains a list of database upgrades");
                    writer.println("# that have completed correctly.");
                } finally {
                    if (out != null) {
                        out.flush();
                        out.close();
                    }
                }
            } else {
                InputStream in = null;
                try {
                    in = new FileInputStream(logFile);
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        line = line.trim();
                        if (!line.equals("") && !line.startsWith("#")) {
                            completedUpgrades.put(line, line);
                        }
                    }
                } finally {
                    if (in != null) {
                        in.close();
                    }
                }
            }
            OutputStream out = null;
            try {
                out = new FileOutputStream(logFile, true);
                PrintWriter writer = new PrintWriter(out, true);
                Class.forName("org.hsqldb.jdbcDriver"); // shouldnt be needed,
                // but
                // just in
                // case
                for (Iterator i = upgrades.iterator(); i.hasNext();) {
                    DBUpgradeOp upgrade = (DBUpgradeOp) i.next();
                    boolean runBefore = completedUpgrades.containsKey(upgrade.getFile().getName());
                    if (log.isInfoEnabled())
                        log.info("Checking if upgrade " + upgrade.getFile() + " [" + upgrade.getVersion()
                                        + "] needs to be run. Run before = " + runBefore + ". Current data version = "
                                        + currentDataVersion + ", upgrade version = " + upgrade.getVersion());
                    if ((!runBefore || (currentDataVersion.getMajor() == 0 && currentDataVersion.getMinor() == 0 && currentDataVersion
                                    .getBuild() == 0))
                                    && upgrade.getVersion().compareTo(currentDataVersion) >= 0
                                    && upgrade.getVersion().compareTo(newDbVersion) < 0) {
                        if (log.isInfoEnabled())
                            log.info("Running script " + upgrade.getName() + " [" + upgrade.getVersion() + "] on database "
                                            + engine.getDatabase());

                        // Get a JDBC connection
                        JDBCConnectionImpl conx = engine.aquireConnection();
                        try {
                            runSQLScript(conx, upgrade.getFile());
                            completedUpgrades.put(upgrade.getFile().getName(), upgrade.getFile().getName());
                            writer.println(upgrade.getFile().getName());
                        } finally {
                            engine.releaseConnection(conx);
                        }
                    }
                }
                versions.put(dbCheckName, newDbVersion.toString());

                if (log.isInfoEnabled())
                    log.info("Logical database " + engine.getAlias() + " (" + engine.getDatabase() + ") is now at version "
                                    + newDbVersion);
            } finally {
                if (out != null) {
                    out.flush();
                    out.close();
                }
            }
        } finally {
            FileOutputStream fos = new FileOutputStream(versionsFile);
            try {
                versions.store(fos, "SSL-Explorer Database versions");
            } finally {
                Util.closeStream(fos);
            }
        }
    }

    private List getSortedUpgrades(File upgradeDir) {
        List sortedUpgrades = new ArrayList();
        File[] files = upgradeDir.listFiles();
        for (int i = 0; files != null && i < files.length; i++) {
            String n = files[i].getName();
            if (n.endsWith(engine.getAlias() + ".sql")) {
                sortedUpgrades.add(new DBUpgradeOp(files[i]));
            } else {
                if (log.isDebugEnabled())
                    log.debug("Skipping script " + n);
            }

        }
        Collections.sort(sortedUpgrades);
        return sortedUpgrades;
    }

    private void runSQLScript(JDBCConnectionImpl con, File sqlFile) throws SQLException, IllegalStateException, IOException {

        InputStream in = null;
        try {
            in = new FileInputStream(sqlFile);
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line = null;
            StringBuffer cmdBuffer = new StringBuffer();
            boolean quoted = false;
            char ch;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (!line.equals("") && !line.startsWith("//") && !line.startsWith("--")) {
                    quoted = false;
                    for (int i = 0; i < line.length(); i++) {
                        ch = line.charAt(i);
                        if (ch == '\'') {
                            if (quoted) {
                                if ((i + 1) < line.length() && line.charAt(i + 1) == '\'') {
                                    i++;
                                    cmdBuffer.append(ch);
                                } else {
                                    quoted = false;
                                }
                            } else {
                                quoted = true;
                            }
                            cmdBuffer.append(ch);
                        } else if (ch == ';' && !quoted) {
                            if (cmdBuffer.length() > 0) {
                                executeSQLStatement(con, cmdBuffer.toString());
                                cmdBuffer.setLength(0);
                            }
                        } else {
                            if (i == 0 && ch != ' ' && cmdBuffer.length() > 0 && !quoted) {
                                cmdBuffer.append(' ');
                            }
                            cmdBuffer.append(ch);
                        }
                    }
                }
            }
            if (cmdBuffer.length() > 0) {
                executeSQLStatement(con, cmdBuffer.toString());
                cmdBuffer.setLength(0);
            }
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }

    private void executeSQLStatement(JDBCConnectionImpl con, String cmd) throws SQLException {
        /*
         * A hack to get around the problem of moving the HSQLDB stored
         * procedures. Prior to version 0.1.13, these functions existed in the
         * class com.sslexplorer.DBFunctions. At version 0.1.13, this were moved
         * to com.sslexplorer.server.hsqldb.DBFunctions. This meant that on a
         * fresh install, when the original 'CREATE ALIAS' statement is
         * encountered it can no longer find the class. The 'CREATE ALIAS' in
         * the 0.1.13 upgrade scripts have the correct classname so upgrades are
         * not affected by this.
         * 
         * This then happend *AGAIN* for 0.2.0.
         * 
         * TODO remove this code when we clear out all the database upgrade
         * scripts and start again
         * 
         */
        if (cmd.startsWith("CREATE ALIAS ")) {
            int idx = cmd.indexOf("com.sslexplorer.DBFunctions.");
            if (idx != -1) {
                cmd = cmd.substring(0, idx) + "com.sslexplorer.server.hsqldb.DBFunctions." + cmd.substring(idx + 28);
            }
            idx = cmd.indexOf("com.sslexplorer.server.hsqldb.DBFunctions.");
            if (idx != -1) {
                cmd = cmd.substring(0, idx) + "com.sslexplorer.jdbc.hsqldb.DBFunctions." + cmd.substring(idx + 42);
            }

        }

        if (log.isDebugEnabled())
            log.debug("Executing \"" + cmd + "\"");
        con.execute(cmd);
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -