1
2
3
4
5
6
7
8
9
10 package ch.qos.logback.access.net;
11
12 import java.net.ServerSocket;
13 import java.net.Socket;
14
15 import ch.qos.logback.access.joran.JoranConfigurator;
16 import ch.qos.logback.access.spi.AccessContext;
17 import ch.qos.logback.core.joran.spi.JoranException;
18 import ch.qos.logback.core.util.StatusPrinter;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class SimpleSocketServer {
43
44 static int port;
45
46 private static AccessContext basicContext;
47
48 public static void main(String argv[]) throws Exception {
49 if (argv.length == 2) {
50 init(argv[0], argv[1]);
51 } else {
52 usage("Wrong number of arguments.");
53 }
54
55 runServer();
56 }
57
58 static void runServer() {
59 try {
60 System.out.println("Listening on port " + port);
61 ServerSocket serverSocket = new ServerSocket(port);
62 while (true) {
63 System.out.println("Waiting to accept a new client.");
64 Socket socket = serverSocket.accept();
65 System.out.println("Connected to client at " + socket.getInetAddress());
66 System.out.println("Starting new socket node.");
67 new Thread(new SocketNode(socket, basicContext)).start();
68 }
69 } catch (Exception e) {
70 e.printStackTrace();
71 }
72 }
73
74 static void usage(String msg) {
75 System.err.println(msg);
76 System.err.println("Usage: java " + SimpleSocketServer.class.getName()
77 + " port configFile");
78 System.exit(1);
79 }
80
81 static void init(String portStr, String configFile) throws JoranException {
82 try {
83 port = Integer.parseInt(portStr);
84 } catch (java.lang.NumberFormatException e) {
85 e.printStackTrace();
86 usage("Could not interpret port number [" + portStr + "].");
87 }
88
89 basicContext = new AccessContext();
90 if (configFile.endsWith(".xml")) {
91 JoranConfigurator configurator = new JoranConfigurator();
92 configurator.setContext(basicContext);
93 configurator.doConfigure(configFile);
94 StatusPrinter.print(basicContext);
95 }
96 }
97 }