1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.log4j.chainsaw;
18
19 import java.io.EOFException;
20 import java.io.IOException;
21 import java.io.ObjectInputStream;
22 import java.net.ServerSocket;
23 import java.net.Socket;
24 import java.net.SocketException;
25 import org.apache.log4j.Logger;
26 import org.apache.log4j.spi.LoggingEvent;
27
28
29
30
31
32
33
34 class LoggingReceiver extends Thread {
35
36 private static final Logger LOG = Logger.getLogger(LoggingReceiver.class);
37
38
39
40
41
42
43
44 private class Slurper implements Runnable {
45
46 private final Socket mClient;
47
48
49
50
51
52
53 Slurper(Socket aClient) {
54 mClient = aClient;
55 }
56
57
58 public void run() {
59 LOG.debug("Starting to get data");
60 try {
61 final ObjectInputStream ois =
62 new ObjectInputStream(mClient.getInputStream());
63 while (true) {
64 final LoggingEvent event = (LoggingEvent) ois.readObject();
65 mModel.addEvent(new EventDetails(event));
66 }
67 } catch (EOFException e) {
68 LOG.info("Reached EOF, closing connection");
69 } catch (SocketException e) {
70 LOG.info("Caught SocketException, closing connection");
71 } catch (IOException e) {
72 LOG.warn("Got IOException, closing connection", e);
73 } catch (ClassNotFoundException e) {
74 LOG.warn("Got ClassNotFoundException, closing connection", e);
75 }
76
77 try {
78 mClient.close();
79 } catch (IOException e) {
80 LOG.warn("Error closing connection", e);
81 }
82 }
83 }
84
85
86 private MyTableModel mModel;
87
88
89 private ServerSocket mSvrSock;
90
91
92
93
94
95
96
97
98 LoggingReceiver(MyTableModel aModel, int aPort) throws IOException {
99 setDaemon(true);
100 mModel = aModel;
101 mSvrSock = new ServerSocket(aPort);
102 }
103
104
105 public void run() {
106 LOG.info("Thread started");
107 try {
108 while (true) {
109 LOG.debug("Waiting for a connection");
110 final Socket client = mSvrSock.accept();
111 LOG.debug("Got a connection from " +
112 client.getInetAddress().getHostName());
113 final Thread t = new Thread(new Slurper(client));
114 t.setDaemon(true);
115 t.start();
116 }
117 } catch (IOException e) {
118 LOG.error("Error in accepting connections, stopping.", e);
119 }
120 }
121 }