01: /*
02: * Copyright (C) 2005 Jeff Tassin
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18:
19: package com.jeta.swingbuilder.debug;
20:
21: import java.io.IOException;
22: import java.io.ObjectOutputStream;
23: import java.io.OutputStream;
24: import java.net.InetAddress;
25: import java.net.Socket;
26: import java.net.UnknownHostException;
27:
28: import com.jeta.swingbuilder.store.LogRequest;
29:
30: /**
31: * This class implements a client that sends System.out and System.err messages
32: * to a server running on the same maching. This is used for debugging webstart
33: * apps.
34: *
35: * @author Jeff Tassin
36: */
37: public class DebugLogger {
38: /** the socket used for sending/receiving data from the server */
39: private Socket m_sock;
40:
41: /** the server port */
42: private static final int SERVER_PORT = 7000;
43:
44: /**
45: * ctor
46: */
47: public DebugLogger(String host) throws IOException,
48: UnknownHostException {
49: System.out.println("DebugLogger.starting...");
50: InetAddress addr = InetAddress.getByName(host);
51: // let's run the server on some user port, say 7000
52: m_sock = new Socket(addr, SERVER_PORT);
53:
54: sendRequest(new LogRequest("Forms Client Starting..."));
55: }
56:
57: /**
58: * Helper method that sends a message to the logger only when debugging
59: */
60: public void logMessage(String msg) {
61: sendRequest(msg);
62: }
63:
64: /**
65: * Make a request of the server to do something
66: *
67: * @param req
68: * a request object that is sent to the server and determines
69: * what the server should send back to the client
70: * @return the object returned by the server based on the request message
71: */
72: public void sendRequest(String msg) {
73: sendRequest(new LogRequest(msg));
74: }
75:
76: /**
77: * Make a request of the server to do something
78: *
79: * @param req
80: * a request object that is sent to the server and determines
81: * what the server should send back to the client
82: * @return the object returned by the server based on the request message
83: */
84: public void sendRequest(LogRequest req) {
85: try {
86: // send the request to the server
87: OutputStream os = m_sock.getOutputStream();
88: ObjectOutputStream oos = new ObjectOutputStream(os);
89: oos.writeObject(req);
90: oos.flush();
91: } catch (Exception e) {
92: e.printStackTrace();
93: }
94: }
95:
96: }
|