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.core.net.server;
15  
16  import java.io.IOException;
17  import java.io.Serializable;
18  import java.net.InetAddress;
19  import java.net.ServerSocket;
20  import java.util.concurrent.Executor;
21  
22  import javax.net.ServerSocketFactory;
23  
24  import ch.qos.logback.core.spi.PreSerializationTransformer;
25  
26  /**
27   * A {@link AbstractServerSocketAppender} with instrumentation for unit testing.
28   *
29   * @author Carl Harris
30   */
31  public class InstrumentedServerSocketAppenderBase extends AbstractServerSocketAppender<Serializable> {
32  
33      private final ServerSocket serverSocket;
34      private final ServerListener<RemoteReceiverClient> listener;
35      private final ServerRunner<RemoteReceiverClient> runner;
36  
37      @SuppressWarnings("rawtypes")
38      private ServerListener lastListener;
39  
40      public InstrumentedServerSocketAppenderBase(ServerSocket serverSocket) {
41          this(serverSocket, new RemoteReceiverServerListener(serverSocket), null);
42      }
43  
44      public InstrumentedServerSocketAppenderBase(ServerSocket serverSocket,
45              ServerListener<RemoteReceiverClient> listener, ServerRunner<RemoteReceiverClient> runner) {
46          this.serverSocket = serverSocket;
47          this.listener = listener;
48          this.runner = runner;
49      }
50  
51      @Override
52      protected void postProcessEvent(Serializable event) {
53      }
54  
55      @Override
56      protected PreSerializationTransformer<Serializable> getPST() {
57          return new PreSerializationTransformer<Serializable>() {
58              public Serializable transform(Serializable event) {
59                  return event;
60              }
61          };
62      }
63  
64      @Override
65      protected ServerSocketFactory getServerSocketFactory() throws Exception {
66          return new ServerSocketFactory() {
67  
68              @Override
69              public ServerSocket createServerSocket(int port) throws IOException {
70                  return serverSocket;
71              }
72  
73              @Override
74              public ServerSocket createServerSocket(int port, int backlog) throws IOException {
75                  return serverSocket;
76              }
77  
78              @Override
79              public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
80                  return serverSocket;
81              }
82          };
83      }
84  
85      @Override
86      protected ServerRunner<RemoteReceiverClient> createServerRunner(ServerListener<RemoteReceiverClient> listener,
87              Executor executor) {
88          lastListener = listener;
89          return runner != null ? runner : super.createServerRunner(listener, executor);
90      }
91  
92      @Override
93      protected ServerListener<RemoteReceiverClient> createServerListener(ServerSocket socket) {
94          return listener;
95      }
96  
97      @SuppressWarnings("rawtypes")
98      public ServerListener getLastListener() {
99          return lastListener;
100     }
101 
102 }