1 19 20 package org.netbeans.modules.tomcat5.util; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.InputStreamReader ; 26 import java.io.PrintWriter ; 27 import java.net.InetSocketAddress ; 28 import java.net.MalformedURLException ; 29 import java.net.ServerSocket ; 30 import java.net.Socket ; 31 import java.net.URL ; 32 import java.util.ArrayList ; 33 import java.util.HashMap ; 34 import java.util.List ; 35 import java.util.Map ; 36 import org.openide.filesystems.FileUtil; 37 38 43 public class Utils { 44 45 46 private Utils() { 47 } 48 49 50 public static URL fileToUrl(File file) throws MalformedURLException { 51 URL url = file.toURI().toURL(); 52 if (FileUtil.isArchiveFile(url)) { 53 url = FileUtil.getArchiveRoot(url); 54 } 55 return url; 56 } 57 58 59 public static boolean isPortFree(int port) { 60 try { 61 ServerSocket soc = new ServerSocket (port); 62 try { 63 soc.close(); 64 } finally { 65 return true; 66 } 67 } catch (IOException ioe) { 68 return false; 69 } 70 } 71 72 73 public static boolean pingTomcat(int port, int timeout) { 74 Socket socket = new Socket (); 76 try { 77 try { 78 socket.connect(new InetSocketAddress ("localhost", port), timeout); socket.setSoTimeout(timeout); 80 PrintWriter out = new PrintWriter (socket.getOutputStream(), true); 81 try { 82 BufferedReader in = new BufferedReader (new InputStreamReader (socket.getInputStream())); 83 try { 84 out.println("GET /netbeans-tomcat-status-test HTTP/1.1\n"); 87 String text = in.readLine(); 89 if (text == null || !text.startsWith("HTTP/")) { return false; } 92 Map headerFileds = new HashMap (); 93 while ((text = in.readLine()) != null && text.length() > 0) { 94 int colon = text.indexOf(':'); 95 if (colon <= 0) { 96 return false; } 98 String name = text.substring(0, colon).trim(); 99 String value = text.substring(colon + 1).trim(); 100 List list = (List )headerFileds.get(name); 101 if (list == null) { 102 list = new ArrayList (); 103 headerFileds.put(name, list); 104 } 105 list.add(value); 106 } 107 List server = (List )headerFileds.get("Server"); if (server != null) { 109 if (server.contains("Apache-Coyote/1.1")) { if (headerFileds.get("X-Powered-By") == null) { return true; 113 } 114 } else if (server.contains("Sun-Java-System/Web-Services-Pack-1.4")) { return true; 117 } 118 } 119 return false; 120 } finally { 121 in.close(); 122 } 123 } finally { 124 out.close(); 125 } 126 } finally { 127 socket.close(); 128 } 129 } catch (IOException ioe) { 130 return false; 131 } 132 } 133 } 134 | Popular Tags |