KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > tools > checkserver > CheckServer


1 //
2
//
3
// ____.
4
// __/\ ______| |__/\. _______
5
// __ .____| | \ | +----+ \
6
// _______| /--| | | - \ _ | : - \_________
7
// \\______: :---| : : | : | \________>
8
// |__\---\_____________:______: :____|____:_____\
9
// /_____|
10
//
11
// . . . i n j a h i a w e t r u s t . . .
12
//
13
// 12.06.2001 POL First release
14

15 package org.jahia.tools.checkserver;
16
17 import java.io.File JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.net.ServerSocket JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Calendar JavaDoc;
23
24 import org.jahia.utils.JahiaConsole;
25 import org.jahia.utils.properties.PropertiesManager;
26
27
28 public class CheckServer
29 {
30     
31     /** time beetwen 2 tests [second] **/
32     static private final int CHECKTIME = 60;
33
34     /** default port **/
35     static private final int PORT = 80;
36     
37     /** lock file **/
38     static private final String JavaDoc LOCKFILE = ".lock";
39     
40     /**
41      * desc: This will check if tomcat is running. If not, then restart tomcat
42      *
43      * Copyright: Copyright (c) 2002
44      * Company: Jahia Ltd
45      *
46      * @author Phillippe Vollenweider
47      * @version 1.0
48      */

49     static void main(String JavaDoc args[])
50     {
51         System.out.println("Jahia LifeControl, version 1.0 started");
52
53         // check command line syntax
54
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         // check if the lockfile exist
60
File JavaDoc lockFile = new File JavaDoc(LOCKFILE);
61         if (! lockFile.exists()){
62             try {
63                 lockFile.createNewFile();
64             } catch (IOException JavaDoc 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         // get checktime var
71
String JavaDoc checkTimeStr = args[0];
72         int checkTime = CHECKTIME;
73         try {
74             checkTime = Integer.parseInt(checkTimeStr);
75         } catch (NumberFormatException JavaDoc nfe){
76         }
77         checkTime *= 1000;
78
79         // get the path to jahia.properties
80
String JavaDoc propertiesFile = args[1];
81         if (! new File JavaDoc(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         // get properties
89
String JavaDoc serverHomeDiskPath = properties.getProperty("serverHomeDiskPath");
90         String JavaDoc jahiaCoreHttpPath = properties.getProperty("jahiaCoreHttpPath");
91         String JavaDoc jahiaHostHttpPath = properties.getProperty("jahiaHostHttpPath");
92         String JavaDoc server = properties.getProperty("server");
93         
94         // exit if the server is not tomcat
95
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         // get the port
101
int i = replacePattern(jahiaHostHttpPath,"://","///").indexOf(":");
102         int port = PORT;
103         String JavaDoc portStr = "";
104         if (i != -1){
105             portStr = jahiaHostHttpPath.substring(i + 1, jahiaHostHttpPath.length());
106             port = Integer.parseInt(portStr);
107         }
108         
109         // get jahiaCoreHttpPath
110
URL JavaDoc uri = null;
111         try {
112             uri = new URL JavaDoc(jahiaCoreHttpPath);
113         } catch(Throwable JavaDoc 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                 // Let's try to get the default web page
125
InputStream JavaDoc inputstream = uri.openStream();
126                 inputstream.close();
127                 System.out.println("[OK]");
128
129             } catch (Throwable JavaDoc throwable1) {
130                 System.out.println("[FAILED]");
131
132                 // check the OS
133
String JavaDoc ext = ".bat"; // windows
134
if (File.separator.equals("/")){
135                     ext = ".sh"; // other
136
}
137                 // String p = ".." + File.separator;
138
String JavaDoc shutdownScript = serverHomeDiskPath + "bin" + File.separator + "shutdown" + ext;
139                 String JavaDoc 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 JavaDoc(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 JavaDoc(serverHomeDiskPath));
152                     } catch (IOException JavaDoc ioe){
153                         JahiaConsole.finalPrintln("CheckServer.main",ioe.toString());
154                     }
155                     try {
156                         Thread.sleep(3000);
157                     } catch (InterruptedException JavaDoc ie) {
158                     }
159                     
160                     // Check if all connections are closed
161
boolean allConnectionClosed = false;
162                     while (! allConnectionClosed){
163                         try {
164                             // If we can open a socket on port PORT, it mean that all connections are closed.
165
ServerSocket JavaDoc s = new ServerSocket JavaDoc(port);
166                             s.close();
167                             allConnectionClosed = true;
168                         } catch (IOException JavaDoc ioe){
169                             // wait for connections to be closed
170
allConnectionClosed = false;
171                             try {
172                                 Thread.sleep(1000);
173                             } catch (InterruptedException JavaDoc ie) {
174                             }
175                         }
176                     }
177                     System.out.println(" [OK]");
178                     JahiaConsole.finalPrintln("CheckServer.main"," * Starting tomcat");
179                     if (! new File JavaDoc(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 JavaDoc(serverHomeDiskPath));
187                     } catch (IOException JavaDoc ioe){
188                         System.out.println(ioe);
189                     }
190                     boolean flag = false;
191                     
192                     // wait the server to be up
193
while(!flag) {
194                         try {
195                             InputStream JavaDoc inputstream = uri.openStream();
196                             flag = true;
197                             inputstream.close();
198                         } catch (Throwable JavaDoc t) {
199                             flag = false;
200                         }
201                     }
202                     System.out.println(" [OK]");
203
204                 } catch (Throwable JavaDoc t) {
205                     t.printStackTrace();
206                 }
207             } // catch
208

209             try {
210                 Thread.sleep(checkTime);
211             } catch (InterruptedException JavaDoc ie) {
212                 //
213
}
214
215         } // while(true)
216
} //main
217

218
219     static public String JavaDoc replacePattern(String JavaDoc str, String JavaDoc oldToken, String JavaDoc newToken) {
220         if (str==null){
221             return str;
222         }
223         String JavaDoc 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