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