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.classic.net.server.RemoteAppenderClient;
24  import ch.qos.logback.classic.net.server.RemoteAppenderServerListener;
25  import ch.qos.logback.classic.net.server.ServerSocketReceiver;
26  import ch.qos.logback.core.net.server.ServerListener;
27  import ch.qos.logback.core.net.server.ServerRunner;
28  
29  /**
30   * A {@link ServerSocketReceiver} with instrumentation for unit testing.
31   *
32   * @author Carl Harris
33   */
34  public class InstrumentedServerSocketReceiver extends ServerSocketReceiver {
35  
36      private final ServerSocket serverSocket;
37      private final ServerListener<RemoteAppenderClient> listener;
38      private final ServerRunner<RemoteAppenderClient> runner;
39      
40      @SuppressWarnings("rawtypes")
41      private ServerListener lastListener;
42  
43      public InstrumentedServerSocketReceiver(ServerSocket serverSocket) {
44          this(serverSocket, new RemoteAppenderServerListener(serverSocket), null);
45      }
46  
47      public InstrumentedServerSocketReceiver(ServerSocket serverSocket, ServerListener<RemoteAppenderClient> listener, ServerRunner<RemoteAppenderClient> runner) {
48          this.serverSocket = serverSocket;
49          this.listener = listener;
50          this.runner = runner;
51      }
52  
53      @Override
54      protected ServerSocketFactory getServerSocketFactory() throws Exception {
55          return new ServerSocketFactory() {
56  
57              @Override
58              public ServerSocket createServerSocket(int port) throws IOException {
59                  return serverSocket;
60              }
61  
62              @Override
63              public ServerSocket createServerSocket(int port, int backlog) throws IOException {
64                  return serverSocket;
65              }
66  
67              @Override
68              public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
69                  return serverSocket;
70              }
71          };
72      }
73  
74      @SuppressWarnings("rawtypes")
75      @Override
76      protected ServerRunner createServerRunner(ServerListener<RemoteAppenderClient> listener, Executor executor) {
77          lastListener = listener;
78          return runner != null ? runner : super.createServerRunner(listener, executor);
79      }
80  
81      @Override
82      protected ServerListener<RemoteAppenderClient> createServerListener(ServerSocket socket) {
83          return listener;
84      }
85      
86      @SuppressWarnings("rawtypes")
87      public ServerListener getLastListener() {
88          return lastListener;
89      }
90  
91  }