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 org.apache.log4j.plugins.Pauseable;
21 import org.apache.log4j.plugins.Plugin;
22 import org.apache.log4j.plugins.Receiver;
23 import org.apache.log4j.spi.LoggerRepository;
24 import org.apache.log4j.spi.LoggingEvent;
25
26 import java.net.ServerSocket;
27 import java.net.Socket;
28 import java.util.List;
29 import java.util.Vector;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class XMLSocketReceiver extends Receiver implements Runnable, PortBased, Pauseable {
53 private boolean paused;
54
55 protected String decoder = "org.apache.log4j.xml.XMLDecoder";
56 private ServerSocket serverSocket;
57 private List<Socket> socketList = new Vector<>();
58 private Thread rThread;
59 public static final int DEFAULT_PORT = 4448;
60 protected int port = DEFAULT_PORT;
61 private boolean advertiseViaMulticastDNS;
62 private ZeroConfSupport zeroConf;
63
64
65
66
67 public static final String ZONE = "_log4j_xml_tcpaccept_receiver.local.";
68
69
70
71
72
73
74 public XMLSocketReceiver() {
75 }
76
77 public XMLSocketReceiver(int _port) {
78 port = _port;
79 }
80
81 public XMLSocketReceiver(int _port, LoggerRepository _repository) {
82 port = _port;
83 repository = _repository;
84 }
85
86
87
88
89 public int getPort() {
90 return port;
91 }
92
93
94
95
96 public void setPort(int _port) {
97 port = _port;
98 }
99
100 public String getDecoder() {
101 return decoder;
102 }
103
104
105
106
107 public void setDecoder(String _decoder) {
108 decoder = _decoder;
109 }
110
111 public boolean isPaused() {
112 return paused;
113 }
114
115 public void setPaused(boolean b) {
116 paused = b;
117 }
118
119
120
121
122
123
124
125
126
127
128 public boolean isEquivalent(Plugin testPlugin) {
129 if ((testPlugin != null) && testPlugin instanceof XMLSocketReceiver) {
130 XMLSocketReceiver sReceiver = (XMLSocketReceiver) testPlugin;
131
132 return (port == sReceiver.getPort() && super.isEquivalent(testPlugin));
133 }
134
135 return false;
136 }
137
138 public int hashCode() {
139
140 int result = 37 * (repository != null ? repository.hashCode() : 0);
141 result = result * 37 + port;
142 return (result * 37 + (getName() != null ? getName().hashCode() : 0));
143 }
144
145
146
147
148
149
150 protected synchronized void setActive(final boolean b) {
151 active = b;
152 }
153
154
155
156
157 public void activateOptions() {
158 if (!isActive()) {
159 rThread = new Thread(this);
160 rThread.setDaemon(true);
161 rThread.start();
162
163 if (advertiseViaMulticastDNS) {
164 zeroConf = new ZeroConfSupport(ZONE, port, getName());
165 zeroConf.advertise();
166 }
167
168 active = true;
169 }
170 }
171
172 public void setAdvertiseViaMulticastDNS(boolean advertiseViaMulticastDNS) {
173 this.advertiseViaMulticastDNS = advertiseViaMulticastDNS;
174 }
175
176 public boolean isAdvertiseViaMulticastDNS() {
177 return advertiseViaMulticastDNS;
178 }
179
180
181
182
183
184 public synchronized void shutdown() {
185
186 active = false;
187
188 if (rThread != null) {
189 rThread.interrupt();
190 rThread = null;
191 }
192 doShutdown();
193 }
194
195
196
197
198
199 private synchronized void doShutdown() {
200 active = false;
201
202 getLogger().debug("{} doShutdown called", getName());
203
204
205 closeServerSocket();
206
207
208 closeAllAcceptedSockets();
209
210 if (advertiseViaMulticastDNS) {
211 zeroConf.unadvertise();
212 }
213 }
214
215
216
217
218 private void closeServerSocket() {
219 getLogger().debug("{} closing server socket", getName());
220
221 try {
222 if (serverSocket != null) {
223 serverSocket.close();
224 }
225 } catch (Exception e) {
226
227 }
228
229 serverSocket = null;
230 }
231
232
233
234
235 private synchronized void closeAllAcceptedSockets() {
236 for (Object aSocketList : socketList) {
237 try {
238 ((Socket) aSocketList).close();
239 } catch (Exception e) {
240
241 }
242 }
243
244
245 socketList.clear();
246 }
247
248
249
250
251 public void run() {
252
253
254
255 getLogger().debug("performing socket cleanup prior to entering loop for {}", name);
256 closeServerSocket();
257 closeAllAcceptedSockets();
258 getLogger().debug("socket cleanup complete for {}", name);
259 active = true;
260
261
262 try {
263 serverSocket = new ServerSocket(port);
264 } catch (Exception e) {
265 getLogger().error(
266 "error starting XMLSocketReceiver (" + this.getName()
267 + "), receiver did not start", e);
268 active = false;
269 doShutdown();
270
271 return;
272 }
273
274 Socket socket = null;
275
276 try {
277 getLogger().debug("in run-about to enter while isactiveloop");
278
279 active = true;
280
281 while (!rThread.isInterrupted()) {
282
283 if (socket != null) {
284 getLogger().debug("socket not null - creating and starting socketnode");
285 socketList.add(socket);
286
287 XMLSocketNode node = new XMLSocketNode(decoder, socket, this);
288 node.setLoggerRepository(this.repository);
289 new Thread(node).start();
290 }
291
292 getLogger().debug("waiting to accept socket");
293
294
295 socket = serverSocket.accept();
296 getLogger().debug("accepted socket");
297 }
298
299
300
301 if (socket != null) {
302 socket.close();
303 }
304 } catch (Exception e) {
305 getLogger().warn(
306 "socket server disconnected, stopping");
307 }
308 }
309
310
311
312
313 public void doPost(LoggingEvent event) {
314 if (!isPaused()) {
315 super.doPost(event);
316 }
317 }
318
319
320 }