001: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
002: * Copyright 2001-2006 Laszlo Systems, Inc. All Rights Reserved. *
003: * Use is subject to license terms. *
004: * J_LZ_COPYRIGHT_END *********************************************************/
005:
006: package org.openlaszlo.test;
007:
008: import java.net.*;
009: import java.io.*;
010:
011: public class debugserver {
012: static boolean closed = false;
013:
014: public static void main(String[] args) throws IOException {
015:
016: ServerSocket serverSocket = null;
017: int port = 5559;
018: if (args.length > 0) {
019: try {
020: port = Integer.parseInt(args[0]);
021: } catch (NumberFormatException e) {
022: System.err.println("could not parse port number "
023: + args[0]);
024: }
025: }
026:
027: while (true) {
028:
029: try {
030: serverSocket = new ServerSocket(port);
031: } catch (IOException e) {
032: System.err.println("Could not listen on port " + port);
033: System.exit(1);
034: }
035:
036: closed = false;
037: try {
038: System.out.println("Listening on port " + port);
039: final Socket clientSocket = serverSocket.accept();
040:
041: System.out.println("Accepting connection on port "
042: + port);
043:
044: PrintWriter out = new PrintWriter(clientSocket
045: .getOutputStream(), true);
046: final BufferedReader in = new BufferedReader(
047: new InputStreamReader(clientSocket
048: .getInputStream()));
049: String inputLine, outputLine;
050:
051: int seqnum = 1;
052:
053: // socket reader thread
054: new Thread() {
055: public void run() {
056: try {
057: FileWriter of = new FileWriter(
058: "lzdebug.log");
059: PrintWriter outf = new PrintWriter(of);
060: while (true) {
061: try {
062: int ch = in.read();
063: if (ch == -1) {
064: debugserver.closed = true;
065: return;
066: }
067: if (ch == 0) {
068: System.out.println("");
069: outf.println("");
070: outf.flush();
071: } else {
072: System.out.write(ch);
073: outf.write(ch);
074: }
075: if (clientSocket.isClosed()) {
076: debugserver.closed = true;
077: return;
078: }
079: } catch (IOException e) {
080: System.out.println(e);
081: debugserver.closed = true;
082: return;
083: }
084: }
085: } catch (IOException e) {
086: }
087: }
088: }.start();
089:
090: // If there are command line args, open each one as a filename and send as exec
091: if (args.length > 1) {
092: for (int n = 1; n < args.length; n++) {
093: String fname = args[n];
094: try {
095: FileInputStream fis = new FileInputStream(
096: fname);
097: byte b[] = new byte[fis.available()];
098: fis.read(b);
099: String cmd = new String(b);
100: System.out.println("Sent: " + cmd);
101: out.write("<exec seq='" + (seqnum++) + "'>"
102: + escapeXml(cmd) + "</exec>\000");
103: out.flush();
104: } catch (IOException e) {
105: System.out.println(e);
106: }
107: }
108: }
109:
110: BufferedReader tty = new BufferedReader(
111: new InputStreamReader(System.in));
112: String expr;
113:
114: while (!closed && (expr = tty.readLine()) != null) {
115: if (expr.trim().equals(""))
116: continue;
117: out.write("<eval seq='" + (seqnum++) + "'>"
118: + escapeXml(expr) + "</eval>\000");
119: out.flush();
120: }
121:
122: out.close();
123: in.close();
124: clientSocket.close();
125: serverSocket.close();
126: System.out.println("Connection closed");
127: } catch (RuntimeException e) {
128: System.out.println("Exception " + e);
129: }
130: }
131: }
132:
133: /**
134: * Escape the 5 entities defined by XML.
135: * These are: '<', '>', '\', '&', '"'.
136: *
137: * @param s an xml string
138: * @return an escaped xml string
139: */
140: public static String escapeXml(String s) {
141: if (s == null)
142: return null;
143: StringBuffer sb = new StringBuffer();
144: for (int i = 0; i < s.length(); i++) {
145: char c = s.charAt(i);
146: if (c == '<') {
147: sb.append("<");
148: } else if (c == '>') {
149: sb.append(">");
150: } else if (c == '\'') {
151: sb.append("'");
152: } else if (c == '&') {
153: sb.append("&");
154: } else if (c == '"') {
155: sb.append(""");
156: } else {
157: sb.append(c);
158: }
159: }
160: return sb.toString();
161: }
162:
163: }
|