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-2007 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: /*
043: * DebugClientThread.java
044: * Created on January 6, 2004, 1:05 PM
045: */
046: package com.sun.errorhandler;
047:
048: /**
049: * @author Winston Prakash
050: */
051:
052: import java.io.BufferedReader;
053: import java.io.IOException;
054: import java.io.InputStreamReader;
055: import java.io.PrintWriter;
056: import java.net.ServerSocket;
057: import java.net.Socket;
058: import java.net.UnknownHostException;
059: import javax.swing.JTextArea;
060: import javax.swing.SwingUtilities;
061:
062: public class DebugClientThread extends Thread {
063: BufferedReader in = null;
064: PrintWriter out = null;
065:
066: StringBuffer message = new StringBuffer();
067:
068: Socket clientSocket = null;
069:
070: boolean connected = false;
071: boolean connectionFailed = false;
072:
073: public DebugClientThread() {
074: }
075:
076: protected static String errorHost = null;
077: private static int errorPort = 0;
078:
079: protected static void setErrorPort(String newPortVal) {
080: errorPort = 24444;
081: if (newPortVal != null) {
082: try {
083: errorPort = Integer.parseInt(newPortVal);
084: } catch (Exception e) {
085: errorPort = 24444;
086: }
087: if (errorPort < 1 || errorPort > 65536) {
088: errorPort = 24444;
089: }
090: }
091: }
092:
093: protected static int getErrorPort() {
094: return errorPort;
095: }
096:
097: boolean triedToConnect = false;
098:
099: public void run() {
100: triedToConnect = true;
101: String line;
102:
103: if (errorHost == null) {
104: displayText("no errorHost, will not talk to creator."); //NOI18N
105: connected = false;
106: connectionFailed = true;
107: return;
108: }
109:
110: try {
111: clientSocket = new Socket(errorHost, errorPort);
112: displayText("Connected to server " + errorHost + ", port "
113: + errorPort); //NOI18N
114: connected = true;
115: } catch (UnknownHostException exc) {
116: displayText("Could not connect. unknown host " + errorHost
117: + ", port " + errorPort + "; "
118: + exc.getLocalizedMessage()); //NOI18N
119: connectionFailed = true;
120: } catch (IOException exc) {
121: displayText("Couldn't get I/O for the connection to "
122: + errorHost + ", port " + errorPort + "; "
123: + exc.getLocalizedMessage()); //NOI18N
124: connectionFailed = true;
125: }
126:
127: if (connectionFailed) {
128: connected = false;
129: } else {
130: try {
131: out = new PrintWriter(clientSocket.getOutputStream(),
132: true);
133: in = new BufferedReader(new InputStreamReader(
134: clientSocket.getInputStream()));
135: } catch (IOException exc) {
136: displayText("Could not get the socket I/O"
137: + exc.getLocalizedMessage()); //NOI18N
138: connected = false;
139: }
140: }
141:
142: while (connected) {
143: try {
144: line = in.readLine();
145: if (line != null) {
146: displayText(line);
147: message.append(line + "\n"); //NOI18N
148: } else {
149: connected = false;
150: displayText("Connection closed"); //NOI18N
151: }
152: } catch (IOException exc) {
153: displayText(exc.getLocalizedMessage());
154: connected = false;
155: }
156: }
157: }
158:
159: protected boolean testConnected() {
160: // Loop still a connection is established
161: while (!connected) {
162: if (connectionFailed)
163: return false;
164: try {
165: Thread.currentThread().sleep(250);
166: } catch (java.lang.InterruptedException iiee) {
167: break;
168: }
169: }
170: return connected;
171: }
172:
173: protected void disconnect() {
174: if (connected) {
175: out.close();
176:
177: try {
178: in.close();
179: } catch (IOException exc) {
180: // die quietly
181: }
182: try {
183: clientSocket.close();
184: } catch (IOException exc) {
185: displayText("Error occured while disconnecting socket"
186: + exc.getLocalizedMessage()); //NOI18N
187: }
188: out = null;
189: in = null;
190: clientSocket = null;
191: connected = false;
192: }
193: }
194:
195: protected void sendMessage(String text) {
196: if (out != null && testConnected()) {
197: out.println(text);
198: out.flush();
199: }
200: }
201:
202: protected String getMessage() {
203: String str = message.toString();
204: message.delete(0, str.length() - 1);
205: return str;
206: }
207:
208: private static void displayText(final String line) {
209: if (ExceptionHandler.getDebugLevel() > 0) {
210: System.out.println(line);
211: }
212: }
213:
214: }
|