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;
15  
16  import java.io.IOException;
17  import java.net.InetAddress;
18  import java.net.Socket;
19  
20  import javax.net.SocketFactory;
21  
22  import ch.qos.logback.core.util.DelayStrategy;
23  import ch.qos.logback.core.util.FixedDelay;
24  
25  /**
26   * Default implementation of {@link SocketConnector}.
27   *
28   * @author Carl Harris
29   * @since 1.0.12
30   */
31  public class DefaultSocketConnector implements SocketConnector {
32  
33      private final InetAddress address;
34      private final int port;
35      private final DelayStrategy delayStrategy;
36  
37      private ExceptionHandler exceptionHandler;
38      private SocketFactory socketFactory;
39  
40      /**
41       * Constructs a new connector.
42       *
43       * @param address      address of remote listener
44       * @param port         port of remote listener
45       * @param initialDelay delay before initial connection attempt
46       * @param retryDelay   delay after failed connection attempt
47       */
48      public DefaultSocketConnector(InetAddress address, int port, long initialDelay, long retryDelay) {
49          this(address, port, new FixedDelay(initialDelay, retryDelay));
50      }
51  
52      /**
53       * Constructs a new connector.
54       *
55       * @param address       address of remote listener
56       * @param port          port of remote listener
57       * @param delayStrategy strategy for choosing the delay to impose before
58       *                      each connection attempt
59       */
60      public DefaultSocketConnector(InetAddress address, int port, DelayStrategy delayStrategy) {
61          this.address = address;
62          this.port = port;
63          this.delayStrategy = delayStrategy;
64      }
65  
66      /**
67       * Loops until the desired connection is established and returns the resulting connector.
68       */
69      public Socket call() throws InterruptedException {
70          useDefaultsForMissingFields();
71          Socket socket = createSocket();
72          while (socket == null && !Thread.currentThread().isInterrupted()) {
73              Thread.sleep(delayStrategy.nextDelay());
74              socket = createSocket();
75          }
76          return socket;
77      }
78  
79      private Socket createSocket() {
80          Socket newSocket = null;
81          try {
82              newSocket = socketFactory.createSocket(address, port);
83          } catch (IOException ioex) {
84              exceptionHandler.connectionFailed(this, ioex);
85          }
86          return newSocket;
87      }
88  
89      private void useDefaultsForMissingFields() {
90          if (exceptionHandler == null) {
91              exceptionHandler = new ConsoleExceptionHandler();
92          }
93          if (socketFactory == null) {
94              socketFactory = SocketFactory.getDefault();
95          }
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     public void setExceptionHandler(ExceptionHandler exceptionHandler) {
102         this.exceptionHandler = exceptionHandler;
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     public void setSocketFactory(SocketFactory socketFactory) {
109         this.socketFactory = socketFactory;
110     }
111 
112     /**
113      * A default {@link ExceptionHandler} that writes to {@code System.out}
114      */
115     private static class ConsoleExceptionHandler implements ExceptionHandler {
116 
117         public void connectionFailed(SocketConnector connector, Exception ex) {
118             System.out.println(ex);
119         }
120 
121     }
122 
123 }