1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.net;
19
20 import java.io.BufferedInputStream;
21 import java.io.IOException;
22 import java.io.ObjectInputStream;
23 import java.net.Socket;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Iterator;
27 import java.util.List;
28
29 import org.apache.log4j.Logger;
30 import org.apache.log4j.helpers.Constants;
31 import org.apache.log4j.plugins.Pauseable;
32 import org.apache.log4j.plugins.Receiver;
33 import org.apache.log4j.spi.ComponentBase;
34 import org.apache.log4j.spi.LoggerRepository;
35 import org.apache.log4j.spi.LoggingEvent;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class SocketNode13 extends ComponentBase implements Runnable, Pauseable {
58
59
60
61
62 private boolean paused;
63
64
65
66 private boolean closed;
67
68
69
70 private Socket socket;
71
72
73
74 private Receiver receiver;
75
76
77
78 private List listenerList = Collections.synchronizedList(new ArrayList());
79
80
81
82
83
84
85
86
87 public SocketNode13(final Socket s,
88 final LoggerRepository hierarchy) {
89 super();
90 this.socket = s;
91 this.repository = hierarchy;
92 }
93
94
95
96
97
98
99 public SocketNode13(final Socket s, final Receiver r) {
100 super();
101 this.socket = s;
102 this.receiver = r;
103 }
104
105
106
107
108
109
110
111
112
113 public void setListener(final SocketNodeEventListener l) {
114 removeSocketNodeEventListener(l);
115 addSocketNodeEventListener(l);
116 }
117
118
119
120
121
122
123 public void addSocketNodeEventListener(
124 final SocketNodeEventListener listener) {
125 listenerList.add(listener);
126 }
127
128
129
130
131
132
133
134
135 public void removeSocketNodeEventListener(
136 final SocketNodeEventListener listener) {
137 listenerList.remove(listener);
138 }
139
140
141
142
143
144 public void run() {
145 LoggingEvent event;
146 Logger remoteLogger;
147 Exception listenerException = null;
148 ObjectInputStream ois = null;
149
150 try {
151 ois =
152 new ObjectInputStream(
153 new BufferedInputStream(socket.getInputStream()));
154 } catch (Exception e) {
155 ois = null;
156 listenerException = e;
157 getLogger().error("Exception opening ObjectInputStream to " + socket, e);
158 }
159
160 if (ois != null) {
161
162 String hostName = socket.getInetAddress().getHostName();
163 String remoteInfo = hostName + ":" + socket.getPort();
164
165
166
167
168
169 fireSocketOpened(remoteInfo);
170
171 try {
172 while (!isClosed()) {
173
174 event = (LoggingEvent) ois.readObject();
175 event.setProperty(Constants.HOSTNAME_KEY, hostName);
176
177 event.setProperty("log4j.remoteSourceInfo", remoteInfo);
178
179
180 if (!isPaused() && !isClosed()) {
181 if ((receiver != null)) {
182 receiver.doPost(event);
183
184
185 } else {
186
187
188 remoteLogger = repository.getLogger(event.getLoggerName());
189
190
191
192 if (event
193 .getLevel()
194 .isGreaterOrEqual(remoteLogger.getEffectiveLevel())) {
195
196 remoteLogger.callAppenders(event);
197 }
198 }
199 } else {
200
201 }
202 }
203 } catch (java.io.EOFException e) {
204 getLogger().info("Caught java.io.EOFException closing connection.");
205 listenerException = e;
206 } catch (java.net.SocketException e) {
207 getLogger().info("Caught java.net.SocketException closing connection.");
208 listenerException = e;
209 } catch (IOException e) {
210 getLogger().info("Caught java.io.IOException: " + e);
211 getLogger().info("Closing connection.");
212 listenerException = e;
213 } catch (Exception e) {
214 getLogger().error("Unexpected exception. Closing connection.", e);
215 listenerException = e;
216 }
217 }
218
219
220 try {
221 if (ois != null) {
222 ois.close();
223 }
224 } catch (Exception e) {
225
226 }
227
228
229 if (listenerList.size() > 0 && !isClosed()) {
230 fireSocketClosedEvent(listenerException);
231 }
232 }
233
234
235
236
237
238 private void fireSocketClosedEvent(final Exception listenerException) {
239 synchronized (listenerList) {
240 for (Iterator iter = listenerList.iterator(); iter.hasNext();) {
241 SocketNodeEventListener snel =
242 (SocketNodeEventListener) iter.next();
243 if (snel != null) {
244 snel.socketClosedEvent(listenerException);
245 }
246 }
247 }
248 }
249
250
251
252
253
254 private void fireSocketOpened(final String remoteInfo) {
255 synchronized (listenerList) {
256 for (Iterator iter = listenerList.iterator(); iter.hasNext();) {
257 SocketNodeEventListener snel =
258 (SocketNodeEventListener) iter.next();
259 if (snel != null) {
260 snel.socketOpened(remoteInfo);
261 }
262 }
263 }
264 }
265
266
267
268
269
270 public void setPaused(final boolean b) {
271 this.paused = b;
272 }
273
274
275
276
277
278 public boolean isPaused() {
279 return this.paused;
280 }
281
282
283
284
285 public void close() throws IOException {
286 getLogger().debug("closing socket");
287 this.closed = true;
288 socket.close();
289 fireSocketClosedEvent(null);
290 }
291
292
293
294
295
296 public boolean isClosed() {
297 return this.closed;
298 }
299 }