View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
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   * A simple {@link SocketNode} based server.
22   * 
23   * <pre>
24   *     &lt;b&gt;Usage:&lt;/b&gt; java ch.qos.logback.access.net.SimpleSocketServer port configFile
25   *    
26   *     where
27   * <em>
28   * port
29   * </em>
30   *     is a part number where the server listens and
31   * <em>
32   * configFile
33   * </em>
34   *     is an xml configuration file fed to {@link JoranConfigurator}.
35   * </pre>
36   * 
37   * @author Ceki G&uuml;lc&uuml;
38   * @author S&eacute;bastien Pennec
39   * 
40   * @since 0.8.4
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  }