001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.tomcat5.util;
043:
044: import java.io.BufferedReader;
045: import java.io.File;
046: import java.io.IOException;
047: import java.io.InputStreamReader;
048: import java.io.PrintWriter;
049: import java.net.InetSocketAddress;
050: import java.net.MalformedURLException;
051: import java.net.ServerSocket;
052: import java.net.Socket;
053: import java.net.URL;
054: import java.util.ArrayList;
055: import java.util.HashMap;
056: import java.util.List;
057: import java.util.Map;
058: import org.openide.filesystems.FileUtil;
059:
060: /**
061: * Utility class.
062: *
063: * @author sherold
064: */
065: public class Utils {
066:
067: /** Creates a new instance of Utils */
068: private Utils() {
069: }
070:
071: /** Return URL representation of the specified file. */
072: public static URL fileToUrl(File file) throws MalformedURLException {
073: URL url = file.toURI().toURL();
074: if (FileUtil.isArchiveFile(url)) {
075: url = FileUtil.getArchiveRoot(url);
076: }
077: return url;
078: }
079:
080: /** Return true if the specified port is free, false otherwise. */
081: public static boolean isPortFree(int port) {
082: try {
083: ServerSocket soc = new ServerSocket(port);
084: try {
085: soc.close();
086: } finally {
087: return true;
088: }
089: } catch (IOException ioe) {
090: return false;
091: }
092: }
093:
094: /** Return true if a Tomcat server is running on the specifed port */
095: public static boolean pingTomcat(int port, int timeout) {
096: // checking whether a socket can be created is not reliable enough, see #47048
097: Socket socket = new Socket();
098: try {
099: try {
100: socket.connect(
101: new InetSocketAddress("localhost", port),
102: timeout); // NOI18N
103: socket.setSoTimeout(timeout);
104: PrintWriter out = new PrintWriter(socket
105: .getOutputStream(), true);
106: try {
107: BufferedReader in = new BufferedReader(
108: new InputStreamReader(socket
109: .getInputStream()));
110: try {
111: // request
112: out
113: .println("HEAD /netbeans-tomcat-status-test HTTP/1.1\nHost: localhost:"
114: + port + "\n"); // NOI18N
115:
116: // response
117: String text = in.readLine();
118: if (text == null || !text.startsWith("HTTP/")) { // NOI18N
119: return false; // not an http response
120: }
121: Map headerFileds = new HashMap();
122: while ((text = in.readLine()) != null
123: && text.length() > 0) {
124: int colon = text.indexOf(':');
125: if (colon <= 0) {
126: return false; // not an http header
127: }
128: String name = text.substring(0, colon)
129: .trim();
130: String value = text.substring(colon + 1)
131: .trim();
132: List list = (List) headerFileds.get(name);
133: if (list == null) {
134: list = new ArrayList();
135: headerFileds.put(name, list);
136: }
137: list.add(value);
138: }
139: List/*<String>*/server = (List/*<String>*/) headerFileds
140: .get("Server"); // NIO18N
141: if (server != null) {
142: if (server.contains("Apache-Coyote/1.1")) { // NOI18N
143: if (headerFileds.get("X-Powered-By") == null) { // NIO18N
144: // if X-Powered-By header is set, it is probably jboss
145: return true;
146: }
147: } else if (server
148: .contains("Sun-Java-System/Web-Services-Pack-1.4")) { // NOI18N
149: // it is probably Tomcat with JWSDP installed
150: return true;
151: }
152: }
153: return false;
154: } finally {
155: in.close();
156: }
157: } finally {
158: out.close();
159: }
160: } finally {
161: socket.close();
162: }
163: } catch (IOException ioe) {
164: return false;
165: }
166: }
167: }
|