1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.net.server;
18
19 import java.io.BufferedReader;
20 import java.io.EOFException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.io.ObjectInputStream;
25 import java.io.OptionalDataException;
26 import java.net.ServerSocket;
27 import java.net.Socket;
28 import java.nio.charset.Charset;
29 import java.util.Map;
30 import java.util.concurrent.ConcurrentHashMap;
31 import java.util.concurrent.ConcurrentMap;
32
33 import org.apache.logging.log4j.core.config.ConfigurationFactory;
34
35
36
37
38
39
40
41 public class TcpSocketServer<T extends InputStream> extends AbstractSocketServer<T> {
42
43
44
45
46 private class SocketHandler extends Thread {
47
48 private final T inputStream;
49
50 private volatile boolean shutdown = false;
51
52 public SocketHandler(final Socket socket) throws IOException {
53 this.inputStream = logEventInput.wrapStream(socket.getInputStream());
54 }
55
56 @Override
57 public void run() {
58 boolean closed = false;
59 try {
60 try {
61 while (!shutdown) {
62 logEventInput.logEvents(inputStream, TcpSocketServer.this);
63 }
64 } catch (final EOFException e) {
65 closed = true;
66 } catch (final OptionalDataException e) {
67 logger.error("OptionalDataException eof=" + e.eof + " length=" + e.length, e);
68 } catch (final IOException e) {
69 logger.error("IOException encountered while reading from socket", e);
70 }
71 if (!closed) {
72 try {
73 inputStream.close();
74 } catch (final Exception ex) {
75
76 }
77 }
78 } finally {
79 handlers.remove(Long.valueOf(getId()));
80 }
81 }
82
83 public void shutdown() {
84 this.shutdown = true;
85 interrupt();
86 }
87 }
88
89
90
91
92
93
94
95
96
97
98 public static TcpSocketServer<InputStream> createJsonSocketServer(final int port) throws IOException {
99 return new TcpSocketServer<InputStream>(port, new JsonInputStreamLogEventBridge());
100 }
101
102
103
104
105
106
107
108
109
110
111 public static TcpSocketServer<ObjectInputStream> createSerializedSocketServer(final int port) throws IOException {
112 return new TcpSocketServer<ObjectInputStream>(port, new ObjectInputStreamLogEventBridge());
113 }
114
115
116
117
118
119
120
121
122
123
124 public static TcpSocketServer<InputStream> createXmlSocketServer(final int port) throws IOException {
125 return new TcpSocketServer<InputStream>(port, new XmlInputStreamLogEventBridge());
126 }
127
128
129
130
131
132
133
134
135
136 public static void main(final String[] args) throws Exception {
137 if (args.length < 1 || args.length > 2) {
138 System.err.println("Incorrect number of arguments");
139 printUsage();
140 return;
141 }
142 final int port = Integer.parseInt(args[0]);
143 if (port <= 0 || port >= MAX_PORT) {
144 System.err.println("Invalid port number");
145 printUsage();
146 return;
147 }
148 if (args.length == 2 && args[1].length() > 0) {
149 ConfigurationFactory.setConfigurationFactory(new ServerConfigurationFactory(args[1]));
150 }
151 final TcpSocketServer<ObjectInputStream> socketServer = TcpSocketServer.createSerializedSocketServer(port);
152 final Thread serverThread = new Thread(socketServer);
153 serverThread.start();
154 final Charset enc = Charset.defaultCharset();
155 final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, enc));
156 while (true) {
157 final String line = reader.readLine();
158 if (line == null || line.equalsIgnoreCase("Quit") || line.equalsIgnoreCase("Stop")
159 || line.equalsIgnoreCase("Exit")) {
160 socketServer.shutdown();
161 serverThread.join();
162 break;
163 }
164 }
165 }
166
167 private static void printUsage() {
168 System.out.println("Usage: ServerSocket port configFilePath");
169 }
170
171 private final ConcurrentMap<Long, SocketHandler> handlers = new ConcurrentHashMap<Long, SocketHandler>();
172
173 private final ServerSocket serverSocket;
174
175
176
177
178
179
180
181
182
183
184
185 public TcpSocketServer(final int port, final LogEventBridge<T> logEventInput) throws IOException {
186 this(port, logEventInput, new ServerSocket(port));
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201 public TcpSocketServer(final int port, final LogEventBridge<T> logEventInput, final ServerSocket serverSocket)
202 throws IOException {
203 super(port, logEventInput);
204 this.serverSocket = serverSocket;
205 }
206
207
208
209
210 @Override
211 public void run() {
212 while (isActive()) {
213 if (serverSocket.isClosed()) {
214 return;
215 }
216 try {
217
218 final Socket clientSocket = serverSocket.accept();
219 clientSocket.setSoLinger(true, 0);
220
221
222
223
224
225 final SocketHandler handler = new SocketHandler(clientSocket);
226 handlers.put(Long.valueOf(handler.getId()), handler);
227 handler.start();
228 } catch (final IOException ioe) {
229 if (serverSocket.isClosed()) {
230
231 return;
232 }
233 System.out.println("Exception encountered on accept. Ignoring. Stack Trace :");
234 ioe.printStackTrace();
235 }
236 }
237 for (final Map.Entry<Long, SocketHandler> entry : handlers.entrySet()) {
238 final SocketHandler handler = entry.getValue();
239 handler.shutdown();
240 try {
241 handler.join();
242 } catch (final InterruptedException ie) {
243
244 }
245 }
246 }
247
248
249
250
251
252
253 public void shutdown() throws IOException {
254 setActive(false);
255 Thread.currentThread().interrupt();
256 serverSocket.close();
257 }
258 }