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: package org.netbeans.modules.visualweb.errorhandler;
043:
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.IOException;
047: import java.net.ServerSocket;
048: import java.net.Socket;
049: import java.util.Date;
050: import java.util.Properties;
051: import org.openide.modules.InstalledFileLocator;
052:
053: /*
054: * DebugServerThread.java
055: * Created on January 6, 2004, 1:05 PM
056: */
057:
058: public class DebugServerThread extends Thread {
059: StringBuffer message = new StringBuffer();
060:
061: boolean connected = false;
062: boolean stopServer = false;
063: Socket clientSocket = null;
064: private static final String installPropsThresher = "config/com-sun-rave-install.properties"; //NOI18N
065:
066: Date date = new Date();
067:
068: ThreadGroup clientSocketThreads = new ThreadGroup(
069: "Debug Client Threads");
070:
071: public DebugServerThread() {
072: }
073:
074: public void run() {
075: Thread.currentThread()
076: .setName("Creator Error Handler Listener"); // NOI18N
077: ServerSocket server = null;
078:
079: String line;
080:
081: // get the properties written by the installer.
082: // first look in the new thresher location. If not found, check the old location.
083:
084: File installPropsFile = InstalledFileLocator.getDefault()
085: .locate(installPropsThresher, null, false);
086:
087: int portNum = 24444; // default port number.
088:
089: // get the port number to use out of install.properties
090: if (installPropsFile != null) {
091: try {
092: Properties installProps = new Properties();
093: installProps
094: .load(new FileInputStream(installPropsFile));
095: String daPort = installProps
096: .getProperty("errorhandlerPort "); //NOI18N
097: if (daPort != null) {
098: // parse for Int
099: try {
100: portNum = Integer.parseInt(daPort);
101: } catch (java.lang.NumberFormatException wwww) {
102: System.err
103: .println("NumberFormantException with errorhandler portNum of "
104: + daPort);
105: wwww.printStackTrace();
106: }
107: }
108: } catch (IOException exc) {
109: exc.printStackTrace();
110: }
111: }
112:
113: try {
114: server = new ServerSocket(portNum);
115: } catch (IOException exc) {
116: displayText("Could not listen on port" + portNum + " "
117: + exc.getLocalizedMessage());
118: stopServer = true;
119: }
120:
121: while (!stopServer) {
122: try {
123: if (server != null) {
124: clientSocket = server.accept();
125: //displayText("Error Handler Server: Accepted a connection\n");
126: Thread socketThread = new ClientSocketConnectionThread(
127: clientSocketThreads, "connection"
128: + date.getTime(), clientSocket);
129: socketThread.setPriority(Thread.MIN_PRIORITY);
130: socketThread.start();
131: } else {
132: Thread.sleep(1500);
133: }
134: } catch (IOException exc) {
135: displayText("Accept failed: port " + portNum + " "
136: + exc.getLocalizedMessage()); // NOI18N
137: } catch (InterruptedException exc) {
138: }
139: }
140: }
141:
142: public void stopServer() {
143: if (clientSocketThreads.activeCount() > 0) {
144: Thread[] socketThreads = new Thread[clientSocketThreads
145: .activeCount()];
146: clientSocketThreads.enumerate(socketThreads);
147: for (int i = 0; i < socketThreads.length; i++) {
148: ((ClientSocketConnectionThread) socketThreads[i])
149: .disconnect();
150: socketThreads[i].interrupt();
151: }
152: }
153: stopServer = true;
154: }
155:
156: public void sendMessage(String text) {
157: if (clientSocketThreads.activeCount() > 0) {
158: Thread[] socketThreads = new Thread[clientSocketThreads
159: .activeCount()];
160: clientSocketThreads.enumerate(socketThreads);
161: for (int i = 0; i < socketThreads.length; i++) {
162: ((ClientSocketConnectionThread) socketThreads[i])
163: .sendMessage(text);
164: }
165: }
166: }
167:
168: private void displayText(final String line) {
169: //DebugServerTestWindow.displayMessage(line);
170: System.out.println(line);
171: }
172:
173: public static void main(String args[]) {
174: DebugServerThread serverThread = new DebugServerThread();
175: serverThread.start();
176: }
177: }
|