1 23 package com.sun.enterprise.launcher; 24 25 import java.util.HashMap ; 26 import java.util.ArrayList ; 27 import java.util.StringTokenizer ; 28 import org.w3c.dom.*; 29 import java.io.File ; 30 import javax.xml.parsers.*; 31 32 import java.io.*; 33 import java.net.*; 34 35 41 public class StatusProber { 42 43 String domainConfigFilePath = "domain.xml"; 44 45 public static void main( String [] args ) { 46 if ( args.length != 1 ) { 47 System.out.println("Usage : java StatusProber <s1as-instanceRoot>" ); 48 System.exit (1 ); 49 } 50 String instanceRoot = args[0]; 51 String domainConfigFilePath = instanceRoot + File.separator + 52 "config" + File.separator + 53 "domain.xml"; 54 StatusProber sp = new StatusProber( domainConfigFilePath); 55 56 58 } 59 60 public StatusProber ( String 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 webserverHostname= null; 77 String webserverPort= null; 78 String adminserverHostname= null; 79 String adminserverPort= null; 80 81 NodeList httpListeners = root.getElementsByTagName("http-listener"); 82 for ( int hl=0; hl<httpListeners.getLength(); hl++ ) { 84 httpListenerElement = (Element)httpListeners.item(hl); 85 String server_id = httpListenerElement.getAttribute ("id"); 86 String server_name= httpListenerElement.getAttribute 87 ("server-name"); 88 String 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 106 int webServerPort = new Integer ( webserverPort ).intValue(); 107 int adminServerPort = new Integer ( 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 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 e) { 121 e.printStackTrace(); 122 } 123 } 124 125 126 protected class StatusChecker extends Thread { 127 128 boolean status; 129 String hostName; 130 final long TIMEOUT = 100000; 131 long startTime; 132 long elapsedTime; 133 String startPage="index.html"; 134 135 int timeout=240; 136 137 int port= 8080; 138 139 public StatusChecker (String hostName, int port, String 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 hostName, int port, String startPage ) { 147 this( hostName, port, startPage,120); 148 } 149 150 public StatusChecker (String 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 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 return false; 184 } catch (Exception e) { 185 System.out.println("ERROR "+ e ); 186 return false; 187 } 188 } 189 190 public void setHostName( String 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("Pinging localhost ..."); 213 status=getValue(); 214 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 e) { 224 e.printStackTrace(); 225 status=false; 226 } 227 } 228 } 229 } 230 | Popular Tags |