KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > debugserver


1 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
2 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
3 * Use is subject to license terms. *
4 * J_LZ_COPYRIGHT_END *********************************************************/

5
6 import java.net.*;
7 import java.io.*;
8
9 public class debugserver {
10     static boolean closed = false;
11
12     public static void main(String JavaDoc[] args) throws IOException {
13
14         ServerSocket serverSocket = null;
15         int port= 5559;
16         if (args.length > 0) {
17             try {
18                 port = Integer.parseInt(args[0]);
19             } catch (NumberFormatException JavaDoc e) {
20                 System.err.println("could not parse port number "+args[0]);
21             }
22         }
23
24         while (true) {
25
26             try {
27                 serverSocket = new ServerSocket(port);
28             } catch (IOException e) {
29                 System.err.println("Could not listen on port "+port);
30                 System.exit(1);
31             }
32
33
34
35             closed = false;
36             try {
37                 System.out.println("Listening on port "+port);
38                 final Socket clientSocket = serverSocket.accept();
39                  
40                 System.out.println("Accepting connection on port "+port);
41
42                 PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
43                 final BufferedReader in = new BufferedReader(
44                     new InputStreamReader(
45                         clientSocket.getInputStream()));
46                 String JavaDoc inputLine, outputLine;
47
48                 int seqnum = 1;
49
50                 // socket reader thread
51
new Thread JavaDoc() {
52                     public void run() {
53                         try {
54                             FileWriter of = new FileWriter("lzdebug.log");
55                             PrintWriter outf = new PrintWriter(of);
56                             while (true) {
57                                 try {
58                                     int ch = in.read();
59                                     if (ch == -1) {
60                                         debugserver.closed = true;
61                                         return;
62                                     }
63                                     if (ch == 0) {
64                                         System.out.println("");
65                                         outf.println("");
66                                         outf.flush();
67                                     } else {
68                                         System.out.write(ch);
69                                         outf.write(ch);
70                                     }
71                                     if (clientSocket.isClosed()) {
72                                         debugserver.closed = true;
73                                         return;
74                                     }
75                                 } catch (IOException e) {
76                                     System.out.println(e);
77                                     debugserver.closed = true;
78                                     return;
79                                 }
80                             }
81                         } catch (IOException e) { }
82                     }
83                 }.start();
84
85                 // If there are command line args, open each one as a filename and send as exec
86
if (args.length > 1) {
87                     for (int n = 1; n < args.length; n++) {
88                         String JavaDoc fname = args[n];
89                         try {
90                             FileInputStream fis = new FileInputStream(fname);
91                             byte b[] = new byte[fis.available()];
92                             fis.read(b);
93                             String JavaDoc cmd = new String JavaDoc(b);
94                             System.out.println("Sent: "+cmd);
95                             out.write("<exec seq='"+(seqnum++)+"'>"+escapeXml(cmd)+"</exec>\000");
96                             out.flush();
97                         } catch (IOException e) {
98                             System.out.println(e);
99                         }
100                     }
101                 }
102
103                 BufferedReader tty = new BufferedReader(new InputStreamReader(System.in));
104                 String JavaDoc expr;
105
106                 while (!closed && (expr = tty.readLine()) != null) {
107                     if (expr.trim().equals("")) continue;
108                     out.write("<eval seq='"+(seqnum++)+"'>"+ escapeXml(expr)+"</eval>\000");
109                     out.flush();
110                 }
111
112
113                 out.close();
114                 in.close();
115                 clientSocket.close();
116                 serverSocket.close();
117                 System.out.println("Connection closed");
118             } catch (RuntimeException JavaDoc e) {
119                 System.out.println("Exception "+e);
120             }
121         }
122     }
123
124     /**
125      * Escape the 5 entities defined by XML.
126      * These are: '<', '>', '\', '&', '"'.
127      *
128      * @param s an xml string
129      * @return an escaped xml string
130      */

131     public static String JavaDoc escapeXml(String JavaDoc s) {
132         if (s == null) return null;
133         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
134         for(int i=0; i<s.length(); i++) {
135             char c = s.charAt(i);
136             if (c == '<') {
137                 sb.append("&lt;");
138             } else if (c == '>') {
139                 sb.append("&gt;");
140             } else if (c == '\'') {
141                 sb.append("&apos;");
142             } else if (c == '&') {
143                 sb.append("&amp;");
144             } else if (c == '"') {
145                 sb.append("&quot;");
146             } else {
147                 sb.append(c);
148             }
149         }
150         return sb.toString();
151     }
152
153
154 }
155
Popular Tags