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