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