KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > launcher > StatusProber


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.launcher;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28 import org.w3c.dom.*;
29 import java.io.File JavaDoc;
30 import javax.xml.parsers.*;
31
32 import java.io.*;
33 import java.net.*;
34
35 /**
36  * Create a filter for Appserver to use commons launcher. This filter is used
37  * to parse out the jvm-options from server.xml and use it to launch the vm.
38  *
39  * @author Ramesh Mandava (ramesh.mandava@sun.com)
40  */

41 public class StatusProber {
42
43     String JavaDoc domainConfigFilePath = "domain.xml";
44     
45     public static void main( String JavaDoc[] args ) {
46         if ( args.length != 1 ) {
47             System.out.println("Usage : java StatusProber <s1as-instanceRoot>" );
48             System.exit (1 );
49         }
50         String JavaDoc instanceRoot = args[0];
51         String JavaDoc domainConfigFilePath = instanceRoot + File.separator +
52                 "config" + File.separator +
53                 "domain.xml";
54         StatusProber sp = new StatusProber( domainConfigFilePath);
55
56             //System.out.println("Domain Config Path -> " +domainConfigFilePath);
57

58     }
59
60     public StatusProber ( String JavaDoc configFilePath ) {
61         this.domainConfigFilePath = configFilePath;
62     }
63
64
65
66     public void probeStatus ( ) {
67         try {
68
69         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
70         DocumentBuilder db = dbf.newDocumentBuilder();
71                  
72         Document doc = db.parse(domainConfigFilePath );
73
74         Element root = doc.getDocumentElement();
75             Element httpListenerElement = null;
76         String JavaDoc webserverHostname= null;
77         String JavaDoc webserverPort= null;
78         String JavaDoc adminserverHostname= null;
79         String JavaDoc adminserverPort= null;
80
81             NodeList httpListeners = root.getElementsByTagName("http-listener");
82             // for loop. PENDING : Error condition check
83
for ( int hl=0; hl<httpListeners.getLength(); hl++ ) {
84                 httpListenerElement = (Element)httpListeners.item(hl);
85             String JavaDoc server_id = httpListenerElement.getAttribute ("id");
86                 String JavaDoc server_name= httpListenerElement.getAttribute
87                     ("server-name");
88                 String JavaDoc server_port= httpListenerElement.getAttribute
89                     ("server-port");
90                 if ( server_id.equals("admin-listener") ) {
91                     adminserverHostname = server_name;
92                     adminserverPort = server_port;
93                 } else {
94                     webserverHostname = server_name;
95                     webserverPort = server_port;
96                 }
97
98             }
99             /*
100             System.out.println("Web Server HostName => " + webserverHostname);
101             System.out.println("Web Server Port => " + webserverPort);
102             System.out.println("Admin Server HostName => "+adminserverHostname);
103             System.out.println("Admin Server Port => " +adminserverPort);
104             System.out.println("Trying to check the status of Web Server");
105             */

106             int webServerPort = new Integer JavaDoc( webserverPort ).intValue();
107             int adminServerPort = new Integer JavaDoc( adminserverPort ).intValue();
108             StatusChecker sc = new StatusChecker( webserverHostname, webServerPort );
109             boolean serverUp = sc.probeServer();
110             if ( !serverUp ) {
111                 System.out.println("Error : Web Server is not up in specified time interval");
112                 return;
113             }
114             //System.out.println("Now trying to see if admin server is up");
115
sc = new StatusChecker( adminserverHostname, adminServerPort );
116             serverUp = sc.probeServer();
117             if ( !serverUp ) {
118                 System.out.println("Error : Admin Server is not up in specified time interval");
119             }
120     } catch(Exception JavaDoc e) {
121         e.printStackTrace();
122     }
123     }
124
125
126     protected class StatusChecker extends Thread JavaDoc{
127  
128         boolean status;
129         String JavaDoc hostName;
130         final long TIMEOUT = 100000;
131         long startTime;
132         long elapsedTime;
133         String JavaDoc startPage="index.html";
134
135         int timeout=240;
136
137         int port= 8080;
138
139         public StatusChecker (String JavaDoc hostName, int port, String JavaDoc startPage, int timeout ) {
140             this.hostName= hostName;
141             this.port= port;
142             this.startPage= startPage;
143             this.timeout = timeout;
144         }
145
146         public StatusChecker (String JavaDoc hostName, int port, String JavaDoc startPage ) {
147         this( hostName, port, startPage,120);
148         }
149
150         public StatusChecker (String JavaDoc hostName, int port ) {
151             this( hostName, port, "index.html", 120);
152
153         }
154
155
156         public boolean probeServer ( ) {
157             this.start();
158             try {
159                this .join();
160             } catch ( InterruptedException JavaDoc e ) {
161         System.err.println("Exception : " + e );
162         }
163         if ( getStatus() ) {
164         System.out.println("Server is up");
165             } else {
166                 System.out.println("Error : Server is Not up");
167         }
168             return getStatus();
169         }
170
171         public boolean getValue() {
172             try {
173                 URL url = new URL ("http",hostName,port,"index.html");
174                 HttpURLConnection h = (HttpURLConnection)url.openConnection();
175                 if (h.getResponseCode()!=HttpURLConnection.HTTP_OK) {
176                     return false;
177                 } else {
178                     System.out.println("Server " +hostName +" is Up ");
179                     return true;
180                 }
181             } catch (ConnectException e) {
182                 //System.out.println("Server " +hostName + " is not yet up" );
183
return false;
184             } catch (Exception JavaDoc e) {
185                 System.out.println("ERROR "+ e );
186                 return false;
187             }
188         }
189
190         public void setHostName( String JavaDoc hn ) {
191             hostName = hn;
192         }
193
194         public void setPort( int port ) {
195             this.port = port;
196         }
197
198         public void setTimeout( int timeout ) {
199             this.timeout = timeout;
200         }
201
202         public boolean getStatus () {
203             return status;
204         }
205
206         public void run () {
207             long startTime;
208             long elapsedTime;
209             try {
210                 startTime = System.currentTimeMillis();
211                 //System.out.println("starttime"+startTime);
212
System.out.println("Pinging localhost ...");
213                 status=getValue();
214                 //System.out.println("Status :"+ status);
215
do {
216                     elapsedTime = System.currentTimeMillis();
217                     if ( !status) {
218                         Thread.sleep (1000);
219                         status=getValue();
220                      } else
221                          break;
222                 } while((elapsedTime - startTime) < timeout*1000);
223             } catch (Exception JavaDoc e) {
224                 e.printStackTrace();
225                 status=false;
226             }
227         }
228     }
229 }
230
Popular Tags