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.classic.net.server;
015
016import java.io.IOException;
017import java.net.InetAddress;
018import java.net.ServerSocket;
019import java.util.concurrent.Executor;
020
021import javax.net.ServerSocketFactory;
022
023import ch.qos.logback.classic.net.server.RemoteAppenderClient;
024import ch.qos.logback.classic.net.server.RemoteAppenderServerListener;
025import ch.qos.logback.classic.net.server.ServerSocketReceiver;
026import ch.qos.logback.core.net.server.ServerListener;
027import ch.qos.logback.core.net.server.ServerRunner;
028
029/**
030 * A {@link ServerSocketReceiver} with instrumentation for unit testing.
031 *
032 * @author Carl Harris
033 */
034public class InstrumentedServerSocketReceiver extends ServerSocketReceiver {
035
036    private final ServerSocket serverSocket;
037    private final ServerListener<RemoteAppenderClient> listener;
038    private final ServerRunner<RemoteAppenderClient> runner;
039    
040    @SuppressWarnings("rawtypes")
041    private ServerListener lastListener;
042
043    public InstrumentedServerSocketReceiver(ServerSocket serverSocket) {
044        this(serverSocket, new RemoteAppenderServerListener(serverSocket), null);
045    }
046
047    public InstrumentedServerSocketReceiver(ServerSocket serverSocket, ServerListener<RemoteAppenderClient> listener, ServerRunner<RemoteAppenderClient> runner) {
048        this.serverSocket = serverSocket;
049        this.listener = listener;
050        this.runner = runner;
051    }
052
053    @Override
054    protected ServerSocketFactory getServerSocketFactory() throws Exception {
055        return new ServerSocketFactory() {
056
057            @Override
058            public ServerSocket createServerSocket(int port) throws IOException {
059                return serverSocket;
060            }
061
062            @Override
063            public ServerSocket createServerSocket(int port, int backlog) throws IOException {
064                return serverSocket;
065            }
066
067            @Override
068            public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
069                return serverSocket;
070            }
071        };
072    }
073
074    @SuppressWarnings("rawtypes")
075    @Override
076    protected ServerRunner createServerRunner(ServerListener<RemoteAppenderClient> listener, Executor executor) {
077        lastListener = listener;
078        return runner != null ? runner : super.createServerRunner(listener, executor);
079    }
080
081    @Override
082    protected ServerListener<RemoteAppenderClient> createServerListener(ServerSocket socket) {
083        return listener;
084    }
085    
086    @SuppressWarnings("rawtypes")
087    public ServerListener getLastListener() {
088        return lastListener;
089    }
090
091}