1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.access.common.net;
15  
16  import java.net.ServerSocket;
17  import java.net.Socket;
18  
19  import ch.qos.logback.access.common.spi.AccessContext;
20  import ch.qos.logback.access.common.joran.JoranConfigurator;
21  import ch.qos.logback.core.joran.spi.JoranException;
22  import ch.qos.logback.core.util.StatusPrinter;
23  
24  /**
25   * A simple {@link SocketNode} based server.
26   * 
27   * <pre>
28   *     &lt;b&gt;Usage:&lt;/b&gt; java ch.qos.logback.access.net.SimpleSocketServer port configFile
29   *    
30   *     where
31   * <em>
32   * port
33   * </em>
34   *     is a part number where the server listens and
35   * <em>
36   * configFile
37   * </em>
38   *     is an xml configuration file fed to {@link JoranConfigurator}.
39   * </pre>
40   * 
41   * @author Ceki G&uuml;lc&uuml;
42   * @author S&eacute;bastien Pennec
43   * 
44   * @since 0.8.4
45   */
46  public class SimpleSocketServer {
47  
48      static int port;
49  
50      private static AccessContext basicContext;
51  
52      public static void main(String argv[]) throws Exception {
53          if (argv.length == 2) {
54              init(argv[0], argv[1]);
55          } else {
56              usage("Wrong number of arguments.");
57          }
58  
59          runServer();
60      }
61  
62      static void runServer() {
63          try {
64              System.out.println("Listening on port " + port);
65              @SuppressWarnings("resource")
66              ServerSocket serverSocket = new ServerSocket(port);
67              while (true) {
68                  System.out.println("Waiting to accept a new client.");
69                  Socket socket = serverSocket.accept();
70                  System.out.println("Connected to client at " + socket.getInetAddress());
71                  System.out.println("Starting new socket node.");
72                  new Thread(new SocketNode(socket, basicContext)).start();
73              }
74          } catch (Exception e) {
75              e.printStackTrace();
76          }
77      }
78  
79      static void usage(String msg) {
80          System.err.println(msg);
81          System.err.println("Usage: java " + SimpleSocketServer.class.getName() + " port configFile");
82          System.exit(1);
83      }
84  
85      static void init(String portStr, String configFile) throws JoranException {
86          try {
87              port = Integer.parseInt(portStr);
88          } catch (java.lang.NumberFormatException e) {
89              e.printStackTrace();
90              usage("Could not interpret port number [" + portStr + "].");
91          }
92  
93          basicContext = new AccessContext();
94          if (configFile.endsWith(".xml")) {
95              JoranConfigurator configurator = new JoranConfigurator();
96              configurator.setContext(basicContext);
97              configurator.doConfigure(configFile);
98              StatusPrinter.print(basicContext);
99          }
100     }
101 }