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

📄 checkserver.java

📁 java 写的一个新闻发布系统
💻 JAVA
字号:
//////                                   ____.//                       __/\ ______|    |__/\.     _______//            __   .____|    |       \   |    +----+       \//    _______|  /--|    |    |    -   \  _    |    :    -   \_________//   \\______: :---|    :    :           |    :    |         \________>//           |__\---\_____________:______:    :____|____:_____\//                                      /_____|////              . . . i n   j a h i a   w e   t r u s t . . .////  12.06.2001  POL  First releasepackage org.jahia.tools.checkserver;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import java.net.*;import java.util.Calendar;import org.jahia.utils.properties.*;import org.jahia.utils.*;public class CheckServer{        /** time beetwen 2 tests [second] **/    static private final int CHECKTIME = 60;    /** default port **/    static private final int PORT = 80;        /** lock file **/    static private final String LOCKFILE = ".lock";        /**     * desc:  This will check if tomcat is running. If not, then restart tomcat     *     * Copyright:    Copyright (c) 2002     * Company:      Jahia Ltd     *     * @author Phillippe Vollenweider     * @version 1.0     */    static void main(String args[])    {        System.out.println("Jahia LifeControl, version 1.0 started");        // check command line syntax        if (args.length < 2){            JahiaConsole.finalPrintln("CheckServer.main","Usage: java [-classpath path_to_jahia_classes] org.jahia.tools.checkserver.CheckServer <delay> <path_to_jahia_properties>");            Runtime.getRuntime().exit(1);        }                // check if the lockfile exist        File lockFile = new File(LOCKFILE);        if (! lockFile.exists()){            try {                lockFile.createNewFile();            } catch (IOException ioe){                JahiaConsole.finalPrintln("CheckServer.main","Could not create the lock file " + LOCKFILE + " in current directory");                Runtime.getRuntime().exit(1);            }        }        // get checktime var        String checkTimeStr = args[0];        int checkTime = CHECKTIME;        try {            checkTime = Integer.parseInt(checkTimeStr);        } catch (NumberFormatException nfe){        }        checkTime *= 1000;        // get the path to jahia.properties        String propertiesFile = args[1];        if (! new File(propertiesFile).exists()){            JahiaConsole.finalPrintln("CheckServer.main","Could not open " + propertiesFile);            Runtime.getRuntime().exit(1);        }         PropertiesManager properties =  new PropertiesManager( propertiesFile );        // get properties        String serverHomeDiskPath = properties.getProperty("serverHomeDiskPath");        String jahiaCoreHttpPath  = properties.getProperty("jahiaCoreHttpPath");        String jahiaHostHttpPath  = properties.getProperty("jahiaHostHttpPath");        String server             = properties.getProperty("server");                // exit if the server is not tomcat        if (server.toLowerCase().indexOf("tomcat")==-1){            JahiaConsole.finalPrintln("CheckServer.main","This service run only with tomcat server");            Runtime.getRuntime().exit(1);                   }                        // get the port        int i = replacePattern(jahiaHostHttpPath,"://","///").indexOf(":");        int port = PORT;        String portStr = "";        if (i != -1){            portStr = jahiaHostHttpPath.substring(i + 1, jahiaHostHttpPath.length());            port = Integer.parseInt(portStr);        }                // get jahiaCoreHttpPath        URL uri = null;        try {            uri = new URL(jahiaCoreHttpPath);        } catch(Throwable throwable) {            return;        }                while(true) {            if (! lockFile.exists()){                JahiaConsole.finalPrintln("CheckServer.main","Lockfile not found: exiting");                Runtime.getRuntime().exit(1);            }            JahiaConsole.finalPrint("CheckServer.main","[" + Calendar.getInstance().getTime().toString() + "] Server status for " + uri.toString() + " : ");            try {                // Let's try to get the default web page                InputStream inputstream = uri.openStream();                inputstream.close();                System.out.println("[OK]");            } catch (Throwable throwable1) {                System.out.println("[FAILED]");                // check the OS                String ext = ".bat"; // windows                if (File.separator.equals("/")){                    ext = ".sh";     // other                }                // String p = ".." + File.separator;                String shutdownScript = serverHomeDiskPath + "bin" + File.separator + "shutdown" + ext;                String startupScript  = serverHomeDiskPath + "bin" + File.separator + "startup"  + ext;                try {                    JahiaConsole.finalPrintln("CheckServer.main","   * Shutdown tomcat");                    JahiaConsole.finalPrintln("CheckServer.main","   * " + shutdownScript);                    JahiaConsole.finalPrint("CheckServer.main","   * Waiting for all conections to be closed : ");                                        if (! new File(shutdownScript).exists()){                        JahiaConsole.finalPrint("CheckServer.main","Couln not execute " + shutdownScript);                        Runtime.getRuntime().exit(1);                    }                    try {                        Runtime.getRuntime().exec(shutdownScript,null,new File(serverHomeDiskPath));                    } catch (IOException ioe){                        JahiaConsole.finalPrintln("CheckServer.main",ioe.toString());                    }                    try {                        Thread.sleep(3000);                    } catch (InterruptedException ie) {                    }                                        // Check if all connections are closed                    boolean allConnectionClosed = false;                    while (! allConnectionClosed){                        try {                            // If we can open a socket on port PORT, it mean that all connections are closed.                            ServerSocket s = new ServerSocket(port);                            s.close();                            allConnectionClosed = true;                        } catch (IOException ioe){                            // wait for connections to be closed                            allConnectionClosed = false;                            try {                                Thread.sleep(1000);                            } catch (InterruptedException ie) {                            }                        }                    }                    System.out.println(" [OK]");                    JahiaConsole.finalPrintln("CheckServer.main","   * Starting tomcat");                    if (! new File(startupScript).exists()){                        JahiaConsole.finalPrintln("CheckServer.main","Couln not execute " + startupScript);                        Runtime.getRuntime().exit(1);                    }                    JahiaConsole.finalPrintln("CheckServer.main","   * " + startupScript);                    JahiaConsole.finalPrint("CheckServer.main","   * waiting for for tomcat to be up : ");                    try {                        Runtime.getRuntime().exec(startupScript,null,new File(serverHomeDiskPath));                    } catch (IOException ioe){                        System.out.println(ioe);                    }                    boolean flag = false;                                        // wait the server to be up                    while(!flag) {                        try {                            InputStream inputstream = uri.openStream();                            flag = true;                            inputstream.close();                        } catch (Throwable t) {                            flag = false;                        }                    }                    System.out.println(" [OK]");                } catch (Throwable t) {                    t.printStackTrace();                 }            } // catch            try {                Thread.sleep(checkTime);            } catch (InterruptedException ie) {                //            }        } // while(true)    } //main	static public String replacePattern(String str, String oldToken, String newToken) {	    if (str==null){	        return str;        }        String result = "";        int i = str.indexOf(oldToken);        while (i != -1) {            result += str.substring(0,i) + newToken;            str = str.substring(i + oldToken.length(), str.length());            i = str.indexOf(oldToken);        }        return result + str;    }}

⌨️ 快捷键说明

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