1 15 package org.jahia.tools.checkserver; 16 17 import java.io.File ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.net.ServerSocket ; 21 import java.net.URL ; 22 import java.util.Calendar ; 23 24 import org.jahia.utils.JahiaConsole; 25 import org.jahia.utils.properties.PropertiesManager; 26 27 28 public class CheckServer 29 { 30 31 32 static private final int CHECKTIME = 60; 33 34 35 static private final int PORT = 80; 36 37 38 static private final String LOCKFILE = ".lock"; 39 40 49 static void main(String args[]) 50 { 51 System.out.println("Jahia LifeControl, version 1.0 started"); 52 53 if (args.length < 2){ 55 JahiaConsole.finalPrintln("CheckServer.main","Usage: java [-classpath path_to_jahia_classes] org.jahia.tools.checkserver.CheckServer <delay> <path_to_jahia_properties>"); 56 Runtime.getRuntime().exit(1); 57 } 58 59 File lockFile = new File (LOCKFILE); 61 if (! lockFile.exists()){ 62 try { 63 lockFile.createNewFile(); 64 } catch (IOException ioe){ 65 JahiaConsole.finalPrintln("CheckServer.main","Could not create the lock file " + LOCKFILE + " in current directory"); 66 Runtime.getRuntime().exit(1); 67 } 68 } 69 70 String checkTimeStr = args[0]; 72 int checkTime = CHECKTIME; 73 try { 74 checkTime = Integer.parseInt(checkTimeStr); 75 } catch (NumberFormatException nfe){ 76 } 77 checkTime *= 1000; 78 79 String propertiesFile = args[1]; 81 if (! new File (propertiesFile).exists()){ 82 JahiaConsole.finalPrintln("CheckServer.main","Could not open " + propertiesFile); 83 Runtime.getRuntime().exit(1); 84 } 85 86 PropertiesManager properties = new PropertiesManager( propertiesFile ); 87 88 String serverHomeDiskPath = properties.getProperty("serverHomeDiskPath"); 90 String jahiaCoreHttpPath = properties.getProperty("jahiaCoreHttpPath"); 91 String jahiaHostHttpPath = properties.getProperty("jahiaHostHttpPath"); 92 String server = properties.getProperty("server"); 93 94 if (server.toLowerCase().indexOf("tomcat")==-1){ 96 JahiaConsole.finalPrintln("CheckServer.main","This service run only with tomcat server"); 97 Runtime.getRuntime().exit(1); 98 } 99 100 int i = replacePattern(jahiaHostHttpPath,"://","///").indexOf(":"); 102 int port = PORT; 103 String portStr = ""; 104 if (i != -1){ 105 portStr = jahiaHostHttpPath.substring(i + 1, jahiaHostHttpPath.length()); 106 port = Integer.parseInt(portStr); 107 } 108 109 URL uri = null; 111 try { 112 uri = new URL (jahiaCoreHttpPath); 113 } catch(Throwable throwable) { 114 return; 115 } 116 117 while(true) { 118 if (! lockFile.exists()){ 119 JahiaConsole.finalPrintln("CheckServer.main","Lockfile not found: exiting"); 120 Runtime.getRuntime().exit(1); 121 } 122 JahiaConsole.finalPrint("CheckServer.main","[" + Calendar.getInstance().getTime().toString() + "] Server status for " + uri.toString() + " : "); 123 try { 124 InputStream inputstream = uri.openStream(); 126 inputstream.close(); 127 System.out.println("[OK]"); 128 129 } catch (Throwable throwable1) { 130 System.out.println("[FAILED]"); 131 132 String ext = ".bat"; if (File.separator.equals("/")){ 135 ext = ".sh"; } 137 String shutdownScript = serverHomeDiskPath + "bin" + File.separator + "shutdown" + ext; 139 String startupScript = serverHomeDiskPath + "bin" + File.separator + "startup" + ext; 140 141 try { 142 JahiaConsole.finalPrintln("CheckServer.main"," * Shutdown tomcat"); 143 JahiaConsole.finalPrintln("CheckServer.main"," * " + shutdownScript); 144 JahiaConsole.finalPrint("CheckServer.main"," * Waiting for all conections to be closed : "); 145 146 if (! new File (shutdownScript).exists()){ 147 JahiaConsole.finalPrint("CheckServer.main","Couln not execute " + shutdownScript); 148 Runtime.getRuntime().exit(1); 149 } 150 try { 151 Runtime.getRuntime().exec(shutdownScript,null,new File (serverHomeDiskPath)); 152 } catch (IOException ioe){ 153 JahiaConsole.finalPrintln("CheckServer.main",ioe.toString()); 154 } 155 try { 156 Thread.sleep(3000); 157 } catch (InterruptedException ie) { 158 } 159 160 boolean allConnectionClosed = false; 162 while (! allConnectionClosed){ 163 try { 164 ServerSocket s = new ServerSocket (port); 166 s.close(); 167 allConnectionClosed = true; 168 } catch (IOException ioe){ 169 allConnectionClosed = false; 171 try { 172 Thread.sleep(1000); 173 } catch (InterruptedException ie) { 174 } 175 } 176 } 177 System.out.println(" [OK]"); 178 JahiaConsole.finalPrintln("CheckServer.main"," * Starting tomcat"); 179 if (! new File (startupScript).exists()){ 180 JahiaConsole.finalPrintln("CheckServer.main","Couln not execute " + startupScript); 181 Runtime.getRuntime().exit(1); 182 } 183 JahiaConsole.finalPrintln("CheckServer.main"," * " + startupScript); 184 JahiaConsole.finalPrint("CheckServer.main"," * waiting for for tomcat to be up : "); 185 try { 186 Runtime.getRuntime().exec(startupScript,null,new File (serverHomeDiskPath)); 187 } catch (IOException ioe){ 188 System.out.println(ioe); 189 } 190 boolean flag = false; 191 192 while(!flag) { 194 try { 195 InputStream inputstream = uri.openStream(); 196 flag = true; 197 inputstream.close(); 198 } catch (Throwable t) { 199 flag = false; 200 } 201 } 202 System.out.println(" [OK]"); 203 204 } catch (Throwable t) { 205 t.printStackTrace(); 206 } 207 } 209 try { 210 Thread.sleep(checkTime); 211 } catch (InterruptedException ie) { 212 } 214 215 } } 218 219 static public String replacePattern(String str, String oldToken, String newToken) { 220 if (str==null){ 221 return str; 222 } 223 String result = ""; 224 int i = str.indexOf(oldToken); 225 while (i != -1) { 226 result += str.substring(0,i) + newToken; 227 str = str.substring(i + oldToken.length(), str.length()); 228 i = str.indexOf(oldToken); 229 } 230 return result + str; 231 } 232 233 } 234 | Popular Tags |