View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.net.server;
15  
16  import java.io.IOException;
17  import java.net.InetAddress;
18  import java.net.ServerSocket;
19  import java.util.concurrent.Executor;
20  
21  import javax.net.ServerSocketFactory;
22  
23  import ch.qos.logback.core.net.server.ServerListener;
24  import ch.qos.logback.core.net.server.ServerRunner;
25  
26  /**
27   * A {@link ServerSocketReceiver} with instrumentation for unit testing.
28   *
29   * @author Carl Harris
30   */
31  public class InstrumentedServerSocketReceiver extends ServerSocketReceiver {
32  
33      private final ServerSocket serverSocket;
34      private final ServerListener<RemoteAppenderClient> listener;
35      private final ServerRunner<RemoteAppenderClient> runner;
36  
37      @SuppressWarnings("rawtypes")
38      private ServerListener lastListener;
39  
40      public InstrumentedServerSocketReceiver(ServerSocket serverSocket) {
41          this(serverSocket, new RemoteAppenderServerListener(serverSocket), null);
42      }
43  
44      public InstrumentedServerSocketReceiver(ServerSocket serverSocket, ServerListener<RemoteAppenderClient> listener,
45              ServerRunner<RemoteAppenderClient> runner) {
46          this.serverSocket = serverSocket;
47          this.listener = listener;
48          this.runner = runner;
49      }
50  
51      @Override
52      protected ServerSocketFactory getServerSocketFactory() throws Exception {
53          return new ServerSocketFactory() {
54  
55              @Override
56              public ServerSocket createServerSocket(int port) throws IOException {
57                  return serverSocket;
58              }
59  
60              @Override
61              public ServerSocket createServerSocket(int port, int backlog) throws IOException {
62                  return serverSocket;
63              }
64  
65              @Override
66              public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
67                  return serverSocket;
68              }
69          };
70      }
71  
72      @SuppressWarnings("rawtypes")
73      @Override
74      protected ServerRunner createServerRunner(ServerListener<RemoteAppenderClient> listener, Executor executor) {
75          lastListener = listener;
76          return runner != null ? runner : super.createServerRunner(listener, executor);
77      }
78  
79      @Override
80      protected ServerListener<RemoteAppenderClient> createServerListener(ServerSocket socket) {
81          return listener;
82      }
83  
84      @SuppressWarnings("rawtypes")
85      public ServerListener getLastListener() {
86          return lastListener;
87      }
88  
89  }