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(),
57                      getInetAddress());
58  
59              ServerListener<RemoteAppenderClient> listener = createServerListener(serverSocket);
60  
61              runner = createServerRunner(listener, getContext().getExecutorService());
62              runner.setContext(getContext());
63              return true;
64          } catch (Exception ex) {
65              addError("server startup error: " + ex, ex);
66              CloseUtil.closeQuietly(serverSocket);
67              return false;
68          }
69      }
70  
71      protected ServerListener<RemoteAppenderClient> createServerListener(ServerSocket socket) {
72          return new RemoteAppenderServerListener(socket);
73      }
74  
75      @SuppressWarnings("rawtypes")
76      protected ServerRunner createServerRunner(ServerListener<RemoteAppenderClient> listener, Executor executor) {
77          return new RemoteAppenderServerRunner(listener, executor);
78      }
79  
80      @Override
81      protected Runnable getRunnableTask() {
82          return runner;
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      protected void onStop() {
89          try {
90              if (runner == null)
91                  return;
92              runner.stop();
93          } catch (IOException ex) {
94              addError("server shutdown error: " + ex, ex);
95          }
96      }
97  
98      /**
99       * Gets the server socket factory.
100      * <p>
101      * Subclasses may override to provide a custom factory.
102      * 
103      * @return server socket factory
104      * @throws Exception
105      */
106     protected ServerSocketFactory getServerSocketFactory() throws Exception {
107         return ServerSocketFactory.getDefault();
108     }
109 
110     /**
111      * Gets the local address for the listener.
112      * 
113      * @return an {@link InetAddress} representation of the local address.
114      * @throws UnknownHostException
115      */
116     protected InetAddress getInetAddress() throws UnknownHostException {
117         if (getAddress() == null)
118             return null;
119         return InetAddress.getByName(getAddress());
120     }
121 
122     /**
123      * Gets the local port for the listener.
124      * 
125      * @return local port
126      */
127     public int getPort() {
128         return port;
129     }
130 
131     /**
132      * Sets the local port for the listener.
133      * 
134      * @param port the local port to set
135      */
136     public void setPort(int port) {
137         this.port = port;
138     }
139 
140     /**
141      * Gets the listener queue depth.
142      * <p>
143      * This represents the number of connected clients whose connections have not
144      * yet been accepted.
145      * 
146      * @return queue depth
147      * @see java.net.ServerSocket
148      */
149     public int getBacklog() {
150         return backlog;
151     }
152 
153     /**
154      * Sets the listener queue depth.
155      * <p>
156      * This represents the number of connected clients whose connections have not
157      * yet been accepted.
158      * 
159      * @param backlog the queue depth to set
160      * @see java.net.ServerSocket
161      */
162     public void setBacklog(int backlog) {
163         this.backlog = backlog;
164     }
165 
166     /**
167      * Gets the local address for the listener.
168      * 
169      * @return a string representation of the local address
170      */
171     public String getAddress() {
172         return address;
173     }
174 
175     /**
176      * Sets the local address for the listener.
177      * 
178      * @param address a host name or a string representation of an IP address
179      */
180     public void setAddress(String address) {
181         this.address = address;
182     }
183 
184 }