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

📄 jahiaupgrade.java

📁 java 写的一个新闻发布系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
////                                   ____.//                       __/\ ______|    |__/\.     _______//            __   .____|    |       \   |    +----+       \//    _______|  /--|    |    |    -   \  _    |    :    -   \_________//   \\______: :---|    :    :           |    :    |         \________>//           |__\---\_____________:______:    :____|____:_____\//                                      /_____|////                 . . . i n   j a h i a   w e   t r u s t . . .//////  JahiaUpgrade//  AK      28.02.2001  JahiaUpgrade creation.//package upgrade;import java.util.*;                             // Properties, Enumerationimport java.io.*;                               // Fileimport java.net.*;                              // URLClassLoader, URL[]import java.sql.*;                              // SQLimport java.util.jar.*;                         // JarFileimport java.util.zip.*;                         // ZipFilepublic class JahiaUpgrade{    private static final String          jahiaUpgradeClassFileName  = "upgrade.jar";    private static       String          jahiaUpgradeClassFilePath  = "";    private static       String          launchDirectoryPath        = "";    private static       String          jahiaFilesPath             = "";    private static       String          jahiaPropertiesFilePath    = "";    private static       String          jahiaDBScript              = "";    private static       String          jahiaDBDriver              = "";    private static       String          jahiaDBUrl                 = "";    private static       String          jahiaDBUsername            = "";    private static       String          jahiaDBPassword            = "";    private static final String          SCRIPT_SEPARATOR           = ":";    private static final String          SCRIPT_PREFIX              = "release.";    private static       String          newReleaseNumber           = "";    private static       int             jahiaInstalledRelease;    private static       File            jahiaUpgradeClass;    private static       URLClassLoader  upgradeClassLoader;    /**     * Main default method.     *     * @author  AK     * @param   args      Command line arguments.     */    public static void main( String args[] )    {        // display the Jahia startup message...	    startupConsole();        // check the environment where the user launch this class...	    checkLaunchDirectory();        // create an another class loader...        createDifferentClassLoader();        // check installed jahia release...        checkInstalledRelease();        // extract this jar file and overwrite files...        extractJarFile();        // get database script runtime...        executeDatabaseRuntime();        // change jahia.properties file, add values, remove values, change values...        changeJahiaPropertiesFile();        // save new release number into Jahia properties file...        writeReleaseNumber();        // remove non necessary files like JahiaUpgrade.class, scripts and the manifest...        removeDummiesFiles();        // say goodbye... :o)        shutdownConsole();    } // end main    /**     * Check if the user is in the good directory to launch JahiaUpgrade     * and if he use "jahia -jar upgrade.jar".     *     * @author  AK     */    private static void checkLaunchDirectory()    {        launchDirectoryPath         = System.getProperties().getProperty("user.dir");        jahiaFilesPath              = launchDirectoryPath + File.separator + "WEB-INF" + File.separator + "jahiafiles";        jahiaUpgradeClassFilePath   = launchDirectoryPath + File.separator + jahiaUpgradeClassFileName;        jahiaUpgradeClass           = new File(jahiaUpgradeClassFilePath);        File jahiaFilesDirectory    = new File(jahiaFilesPath);        // check if the jar file is in your current directory...        if(!jahiaUpgradeClass.exists()) {            toConsole(jahiaUpgradeClassFileName + " not found.");            toConsole("You must launch JahiaUpgrade using \"java -jar upgrade.jar\".");            System.exit(0);        }        // check if the jahia files directory exists...        if(!jahiaFilesDirectory.exists()) {            toConsole("You must be in the Jahia root to launch JahiaUpgrade.");            System.exit(0);        }    } // end checkLaunchDirectory    /**     * Create an another class loader using jar files in lib directory.     *     * @author  AK     */    private static void createDifferentClassLoader()    {        String jahiaLibDirectoryPath  = launchDirectoryPath + File.separator + "WEB-INF" + File.separator + "lib";        File   jahiaLibDirectory      = new File(jahiaLibDirectoryPath);        File[] filesInThisDirectory   = jahiaLibDirectory.listFiles();        URL[]  urlsFromJarFiles       = new URL[filesInThisDirectory.length];        // list the content of this directory... filter *.jar		for(int i=0; i<filesInThisDirectory.length; i++) {		    String fileName  = filesInThisDirectory[i].getPath();		    try {    		    if(fileName.substring(fileName.length()-4, fileName.length()).equals(".jar")) {                    File thisJarFile     = new File(fileName);                    URL  thisJarFileURL  = thisJarFile.toURL();                    urlsFromJarFiles[i]  = thisJarFileURL;    			}            } catch (IndexOutOfBoundsException ioobe) {            } catch (MalformedURLException mue) {            }		}        upgradeClassLoader = new URLClassLoader( urlsFromJarFiles );    } // end setClassPath    /**     * Check which release of Jahia is currently installed.     *     * @author  AK     */    private static void checkInstalledRelease()    {        toConsole("Checking current installed Jahia release.");        // try to load the current installed release number...        try        {            jahiaPropertiesFilePath  = jahiaFilesPath + File.separator + "config" + File.separator + "jahia.properties";            Properties      jahiaPropertiesObject  = new Properties();            FileInputStream inputStream            = new FileInputStream(jahiaPropertiesFilePath);            jahiaPropertiesObject.load(inputStream);            inputStream.close();            // get properties values...            String jahiaReleaseNumber  = jahiaPropertiesObject.getProperty("release");            jahiaDBScript              = jahiaPropertiesObject.getProperty("db_script");            jahiaDBDriver              = jahiaPropertiesObject.getProperty("db_driver");            jahiaDBUrl                 = jahiaPropertiesObject.getProperty("db_url");            jahiaDBUsername            = jahiaPropertiesObject.getProperty("db_username");            jahiaDBPassword            = jahiaPropertiesObject.getProperty("db_password");            if((jahiaReleaseNumber == null) || (jahiaDBScript == null) || (jahiaDBDriver == null) || (jahiaDBUrl == null) || (jahiaDBUsername == null) || (jahiaDBPassword == null))            {                //System.out.println("\n");                toConsole("Jahia properties file not found or corrupt.");                toConsole("Have you completed the Jahia installation before?");                System.exit(0);            }            // convert type of values...            jahiaInstalledRelease = Integer.parseInt(jahiaReleaseNumber);        } catch (IOException ioe) {            //System.out.println("\n");            toConsole("Jahia properties file not found or corrupt.");            toConsole("Have you completed the Jahia installation before?");            System.exit(0);        }        toConsole("Upgrading Jahia from release " + jahiaInstalledRelease + "...\n");    } // end checkInstalledRelease    /**     * Extract this jar file into the jahiafiles/upgrade directory.     * -> use NK's unzip() method.     *     * @author  AK     */    private static void extractJarFile()    {        try {            toConsole("Decompressing upgrade files... please wait.");            JarFile             jahiaUpgradeJarFile  = new JarFile(jahiaUpgradeClass);            FileInputStream     fileStream           = new FileInputStream(jahiaUpgradeClassFilePath);            BufferedInputStream bufferedStream       = new BufferedInputStream(fileStream);            ZipInputStream      zipStream            = new ZipInputStream(bufferedStream);            ZipEntry            zipEntry;            try            {                while((zipEntry = zipStream.getNextEntry()) != null)                {                    File fileOutput = new File( launchDirectoryPath + File.separator + generatePathFile(zipEntry.getName()) );                    if(zipEntry.isDirectory()) {                        fileOutput.mkdirs();                    } else {                        File parent = new File(fileOutput.getParent());                        parent.mkdirs();                        FileOutputStream outputStream = new FileOutputStream(fileOutput);                        copyStream(zipStream, outputStream);                    }                    zipStream.closeEntry();                }            } finally {                fileStream.close();                zipStream.close();                bufferedStream.close();            }            toConsole("Decompression is done.\n");        } catch (IOException ioe) {        }    } // end extractJarFile    /**     * Remove non necessary files like JahiaUpgrade.class and the manifest.     * Remove only files requested between the current release and this release.     *     * @author  AK     */    private static void removeDummiesFiles()    {        Enumeration eachLines = readLines(launchDirectoryPath + File.separator + "upgrade" + File.separator + "remove_files.upgrade");        while(eachLines.hasMoreElements()) {            try            {                String fileName  = (String) eachLines.nextElement();                if(fileName.trim().length() > 0)                {                    boolean  doOperation        = false;                    int      separatorPosition  = fileName.indexOf(SCRIPT_SEPARATOR);                    String   releaseNumber      = fileName.substring(SCRIPT_PREFIX.length(), separatorPosition).trim();                        if(releaseNumber.equals("all")) {                        doOperation = true;                    } else {                        try {                            int releaseNumberInt = Integer.parseInt(releaseNumber);                            if(releaseNumberInt > jahiaInstalledRelease) {                                doOperation = true;                            }                        } catch (NumberFormatException nfe) {                        }                    }                        if(doOperation) {                        String completeFileName  = launchDirectoryPath + File.separator + fileName.substring(separatorPosition+1, fileName.length()).trim();                        File   theFile           = new File( completeFileName );                        deleteFile( theFile );                    }                }            } catch (IndexOutOfBoundsException ioobe) {            }        }    } // end removeDummiesFiles    /**     * Get database script runtime and execute the runtime.     *     * @author  AK     */    private static void executeDatabaseRuntime()    {        Enumeration eachLines = readLines(launchDirectoryPath + File.separator + "upgrade" + File.separator + jahiaDBScript);        try        {            toConsole("Upgrading your database... please wait.");                        Class      dbDriverClass  = Class.forName( jahiaDBDriver, false, upgradeClassLoader );            Driver     dbDriver       = (Driver) dbDriverClass.newInstance();            Properties dbProperties   = new Properties();            dbProperties.setProperty("user",     jahiaDBUsername);            dbProperties.setProperty("password", jahiaDBPassword);            Connection dbConnection   = dbDriver.connect( jahiaDBUrl, dbProperties );            Statement  dbStatement    = dbConnection.createStatement();

⌨️ 快捷键说明

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