001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.net;
015
016import java.net.ServerSocket;
017import java.net.Socket;
018
019import ch.qos.logback.access.joran.JoranConfigurator;
020import ch.qos.logback.access.spi.AccessContext;
021import ch.qos.logback.core.joran.spi.JoranException;
022import ch.qos.logback.core.util.StatusPrinter;
023
024/**
025 * A simple {@link SocketNode} based server.
026 * 
027 * <pre>
028 *     &lt;b&gt;Usage:&lt;/b&gt; java ch.qos.logback.access.net.SimpleSocketServer port configFile
029 *    
030 *     where
031 * <em>
032 * port
033 * </em>
034 *     is a part number where the server listens and
035 * <em>
036 * configFile
037 * </em>
038 *     is an xml configuration file fed to {@link JoranConfigurator}.
039 * </pre>
040 * 
041 * @author Ceki G&uuml;lc&uuml;
042 * @author S&eacute;bastien Pennec
043 * 
044 * @since 0.8.4
045 */
046public class SimpleSocketServer {
047
048    static int port;
049
050    private static AccessContext basicContext;
051
052    public static void main(String argv[]) throws Exception {
053        if (argv.length == 2) {
054            init(argv[0], argv[1]);
055        } else {
056            usage("Wrong number of arguments.");
057        }
058
059        runServer();
060    }
061
062    static void runServer() {
063        try {
064            System.out.println("Listening on port " + port);
065            @SuppressWarnings("resource")
066            ServerSocket serverSocket = new ServerSocket(port);
067            while (true) {
068                System.out.println("Waiting to accept a new client.");
069                Socket socket = serverSocket.accept();
070                System.out.println("Connected to client at " + socket.getInetAddress());
071                System.out.println("Starting new socket node.");
072                new Thread(new SocketNode(socket, basicContext)).start();
073            }
074        } catch (Exception e) {
075            e.printStackTrace();
076        }
077    }
078
079    static void usage(String msg) {
080        System.err.println(msg);
081        System.err.println("Usage: java " + SimpleSocketServer.class.getName() + " port configFile");
082        System.exit(1);
083    }
084
085    static void init(String portStr, String configFile) throws JoranException {
086        try {
087            port = Integer.parseInt(portStr);
088        } catch (java.lang.NumberFormatException e) {
089            e.printStackTrace();
090            usage("Could not interpret port number [" + portStr + "].");
091        }
092
093        basicContext = new AccessContext();
094        if (configFile.endsWith(".xml")) {
095            JoranConfigurator configurator = new JoranConfigurator();
096            configurator.setContext(basicContext);
097            configurator.doConfigure(configFile);
098            StatusPrinter.print(basicContext);
099        }
100    }
101}