KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tomcat5 > util > Utils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tomcat5.util;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.net.InetSocketAddress JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.ServerSocket JavaDoc;
30 import java.net.Socket JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import org.openide.filesystems.FileUtil;
37
38 /**
39  * Utility class.
40  *
41  * @author sherold
42  */

43 public class Utils {
44     
45     /** Creates a new instance of Utils */
46     private Utils() {
47     }
48     
49     /** Return URL representation of the specified file. */
50     public static URL JavaDoc fileToUrl(File JavaDoc file) throws MalformedURLException JavaDoc {
51         URL JavaDoc url = file.toURI().toURL();
52         if (FileUtil.isArchiveFile(url)) {
53             url = FileUtil.getArchiveRoot(url);
54         }
55         return url;
56     }
57     
58     /** Return true if the specified port is free, false otherwise. */
59     public static boolean isPortFree(int port) {
60         try {
61             ServerSocket JavaDoc soc = new ServerSocket JavaDoc(port);
62             try {
63                 soc.close();
64             } finally {
65                 return true;
66             }
67         } catch (IOException JavaDoc ioe) {
68             return false;
69         }
70     }
71     
72     /** Return true if a Tomcat server is running on the specifed port */
73     public static boolean pingTomcat(int port, int timeout) {
74         // checking whether a socket can be created is not reliable enough, see #47048
75
Socket JavaDoc socket = new Socket JavaDoc();
76         try {
77             try {
78                 socket.connect(new InetSocketAddress JavaDoc("localhost", port), timeout); // NOI18N
79
socket.setSoTimeout(timeout);
80                 PrintWriter JavaDoc out = new PrintWriter JavaDoc(socket.getOutputStream(), true);
81                 try {
82                     BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(socket.getInputStream()));
83                     try {
84                         // request
85
out.println("GET /netbeans-tomcat-status-test HTTP/1.1\n"); // NOI18N
86

87                         // response
88
String JavaDoc text = in.readLine();
89                         if (text == null || !text.startsWith("HTTP/")) { // NOI18N
90
return false; // not an http response
91
}
92                         Map JavaDoc headerFileds = new HashMap JavaDoc();
93                         while ((text = in.readLine()) != null && text.length() > 0) {
94                             int colon = text.indexOf(':');
95                             if (colon <= 0) {
96                                 return false; // not an http header
97
}
98                             String JavaDoc name = text.substring(0, colon).trim();
99                             String JavaDoc value = text.substring(colon + 1).trim();
100                             List JavaDoc list = (List JavaDoc)headerFileds.get(name);
101                             if (list == null) {
102                                 list = new ArrayList JavaDoc();
103                                 headerFileds.put(name, list);
104                             }
105                             list.add(value);
106                         }
107                         List JavaDoc/*<String>*/ server = (List JavaDoc/*<String>*/)headerFileds.get("Server"); // NIO18N
108
if (server != null) {
109                             if (server.contains("Apache-Coyote/1.1")) { // NOI18N
110
if (headerFileds.get("X-Powered-By") == null) { // NIO18N
111
// if X-Powered-By header is set, it is probably jboss
112
return true;
113                                 }
114                             } else if (server.contains("Sun-Java-System/Web-Services-Pack-1.4")) { // NOI18N
115
// it is probably Tomcat with JWSDP installed
116
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 JavaDoc ioe) {
130             return false;
131         }
132     }
133 }
134
Popular Tags