/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache license, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the license for the specific language governing permissions and
 * limitations under the license.
 */
package org.apache.logging.log4j.core.net;

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OptionalDataException;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LogEventListener;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.xml.XMLConfiguration;
import org.apache.logging.log4j.core.config.xml.XMLConfigurationFactory;

/**
 * Listens for events over a socket connection.
 */
public class TCPSocketServer extends AbstractSocketServer implements Runnable {

    private final ServerSocket server;

    private final ConcurrentMap<Long, SocketHandler> handlers = new ConcurrentHashMap<Long, SocketHandler>();

    /**
     * Constructor.
     * @param port to listen on.
     * @throws IOException If an error occurs.
     */
    public TCPSocketServer(final int port) throws IOException {
        super(port);
        this.server = new ServerSocket(port);
    }
     /**
     * Main startup for the server.
     * @param args The command line arguments.
     * @throws Exception if an error occurs.
     */
    public static void main(final String[] args) throws Exception {
        if (args.length < 1 || args.length > 2) {
            System.err.println("Incorrect number of arguments");
            printUsage();
            return;
        }
        final int port = Integer.parseInt(args[0]);
        if (port <= 0 || port >= MAX_PORT) {
            System.err.println("Invalid port number");
            printUsage();
            return;
        }
        if (args.length == 2 && args[1].length() > 0) {
            ConfigurationFactory.setConfigurationFactory(new ServerConfigurationFactory(args[1]));
        }
        final TCPSocketServer socketServer = new TCPSocketServer(port);
        final Thread serverThread = new Thread(socketServer);
        serverThread.start();
        final Charset enc = Charset.defaultCharset();
        final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, enc));
        while (true) {
            final String line = reader.readLine();
            if (line == null || line.equalsIgnoreCase("Quit") || line.equalsIgnoreCase("Stop") || line.equalsIgnoreCase("Exit")) {
                socketServer.shutdown();
                serverThread.join();
                break;
            }
        }
    }

    private static void printUsage() {
        System.out.println("Usage: ServerSocket port configFilePath");
    }

    /**
     * Shutdown the server.
     */
    public void shutdown() {
        setActive(false);
        Thread.currentThread().interrupt();
    }

    /**
     * Accept incoming events and processes them.
     */
    @Override
    public void run() {
        while (isActive()) {
            try {
                // Accept incoming connections.
                final Socket clientSocket = server.accept();
                clientSocket.setSoLinger(true, 0);

                // accept() will block until a client connects to the server.
                // If execution reaches this point, then it means that a client
                // socket has been accepted.

                final SocketHandler handler = new SocketHandler(clientSocket);
                handlers.put(Long.valueOf(handler.getId()), handler);
                handler.start();
            } catch (final IOException ioe) {
                System.out.println("Exception encountered on accept. Ignoring. Stack Trace :");
                ioe.printStackTrace();
            }
        }
        for (final Map.Entry<Long, SocketHandler> entry : handlers.entrySet()) {
            final SocketHandler handler = entry.getValue();
            handler.shutdown();
            try {
                handler.join();
            } catch (final InterruptedException ie) {
                // Ignore the exception
            }
        }
    }

    /**
     * Thread that processes the events.
     */
    private class SocketHandler extends Thread {
        private final ObjectInputStream ois;

        private boolean shutdown = false;

        public SocketHandler(final Socket socket) throws IOException {

            ois = new ObjectInputStream(socket.getInputStream());
        }

        public void shutdown() {
            this.shutdown = true;
            interrupt();
        }

        @Override
        public void run() {
            boolean closed = false;
            try {
                try {
                    while (!shutdown) {
                        final LogEvent event = (LogEvent) ois.readObject();
                        if (event != null) {
                            log(event);
                        }
                    }
                } catch (final EOFException eof) {
                    closed = true;
                } catch (final OptionalDataException opt) {
                    logger.error("OptionalDataException eof=" + opt.eof + " length=" + opt.length, opt);
                } catch (final ClassNotFoundException cnfe) {
                    logger.error("Unable to locate LogEvent class", cnfe);
                } catch (final IOException ioe) {
                    logger.error("IOException encountered while reading from socket", ioe);
                }
                if (!closed) {
                    try {
                        ois.close();
                    } catch (final Exception ex) {
                        // Ignore the exception;
                    }
                }
            } finally {
                handlers.remove(Long.valueOf(getId()));
            }
        }
    }
}

@@ -902,99 +902,8 @@
 on;%0A
-import java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileNotFoundException;%0A
 impo
@@ -1036,47 +1036,8 @@
 on;%0A
-import java.net.MalformedURLException;%0A
 impo
@@ -1090,50 +1090,8 @@
 et;%0A
-import java.net.URI;%0Aimport java.net.URL;%0A
 impo
@@ -1268,404 +1268,67 @@
 g4j.
-LogManager;%0Aimport org.apache.logging.log4j.Logger;%0Aimport org.apache.logging.log4j.core.LogEventListener;%0Aimport org.apache.logging.log4j.core.LogEvent;%0Aimport org.apache.logging.log4j.core.config.Configuration;%0Aimport org.apache.logging.log4j.core.config.ConfigurationFactory;%0Aimport org.apache.logging.log4j.core.config.xml.XMLConfiguration;%0Aimport org.apache.logging.log4j.core.config.xml.XML
+core.LogEvent;%0Aimport org.apache.logging.log4j.core.config.
 Conf

Remove unused imports.