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.net.UnknownHostException;
20  import java.util.concurrent.Executor;
21  
22  import javax.net.ServerSocketFactory;
23  
24  import ch.qos.logback.classic.net.ReceiverBase;
25  import ch.qos.logback.core.net.AbstractSocketAppender;
26  import ch.qos.logback.core.net.server.ServerListener;
27  import ch.qos.logback.core.net.server.ServerRunner;
28  import ch.qos.logback.core.util.CloseUtil;
29  
30  /**
31   * A logging socket server that is configurable using Joran.
32   *
33   * @author Carl Harris
34   */
35  public class ServerSocketReceiver extends ReceiverBase {
36  
37      /**
38       * Default {@link ServerSocket} backlog
39       */
40      public static final int DEFAULT_BACKLOG = 50;
41  
42      private int port = AbstractSocketAppender.DEFAULT_PORT;
43      private int backlog = DEFAULT_BACKLOG;
44  
45      private String address;
46  
47      private ServerSocket serverSocket;
48      @SuppressWarnings("rawtypes")
49      private ServerRunner runner;
50  
51      /**
52       * Starts the server.
53       */
54      protected boolean shouldStart() {
55          try {
56              ServerSocket serverSocket = getServerSocketFactory().createServerSocket(getPort(), getBacklog(), getInetAddress());
57  
58              ServerListener<RemoteAppenderClient> listener = createServerListener(serverSocket);
59  
60              runner = createServerRunner(listener, getContext().getScheduledExecutorService());
61              runner.setContext(getContext());
62              return true;
63          } catch (Exception ex) {
64              addError("server startup error: " + ex, ex);
65              CloseUtil.closeQuietly(serverSocket);
66              return false;
67          }
68      }
69  
70      protected ServerListener<RemoteAppenderClient> createServerListener(ServerSocket socket) {
71          return new RemoteAppenderServerListener(socket);
72      }
73  
74      @SuppressWarnings("rawtypes")
75      protected ServerRunner createServerRunner(ServerListener<RemoteAppenderClient> listener, Executor executor) {
76          return new RemoteAppenderServerRunner(listener, executor);
77      }
78  
79      @Override
80      protected Runnable getRunnableTask() {
81          return runner;
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      protected void onStop() {
88          try {
89              if (runner == null)
90                  return;
91              runner.stop();
92          } catch (IOException ex) {
93              addError("server shutdown error: " + ex, ex);
94          }
95      }
96  
97      /**
98       * Gets the server socket factory.
99       * <p>
100      * Subclasses may override to provide a custom factory.
101      * @return server socket factory
102      * @throws Exception
103      */
104     protected ServerSocketFactory getServerSocketFactory() throws Exception {
105         return ServerSocketFactory.getDefault();
106     }
107 
108     /**
109      * Gets the local address for the listener.
110      * @return an {@link InetAddress} representation of the local address.
111      * @throws UnknownHostException
112      */
113     protected InetAddress getInetAddress() throws UnknownHostException {
114         if (getAddress() == null)
115             return null;
116         return InetAddress.getByName(getAddress());
117     }
118 
119     /**
120      * Gets the local port for the listener.
121      * @return local port
122      */
123     public int getPort() {
124         return port;
125     }
126 
127     /**
128      * Sets the local port for the listener.
129      * @param port the local port to set
130      */
131     public void setPort(int port) {
132         this.port = port;
133     }
134 
135     /**
136      * Gets the listener queue depth.
137      * <p>
138      * This represents the number of connected clients whose connections 
139      * have not yet been accepted.
140      * @return queue depth
141      * @see java.net.ServerSocket
142      */
143     public int getBacklog() {
144         return backlog;
145     }
146 
147     /**
148      * Sets the listener queue depth.
149      * <p>
150      * This represents the number of connected clients whose connections 
151      * have not yet been accepted.
152      * @param backlog the queue depth to set
153      * @see java.net.ServerSocket
154      */
155     public void setBacklog(int backlog) {
156         this.backlog = backlog;
157     }
158 
159     /**
160      * Gets the local address for the listener.
161      * @return a string representation of the local address
162      */
163     public String getAddress() {
164         return address;
165     }
166 
167     /**
168      * Sets the local address for the listener.
169      * @param address a host name or a string representation of an IP address
170      */
171     public void setAddress(String address) {
172         this.address = address;
173     }
174 
175 }