1
2
3
4
5
6
7
8
9
10
11
12
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
32
33
34
35 public class ServerSocketReceiver extends ReceiverBase {
36
37
38
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
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
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
100
101
102
103
104
105
106 protected ServerSocketFactory getServerSocketFactory() throws Exception {
107 return ServerSocketFactory.getDefault();
108 }
109
110
111
112
113
114
115
116 protected InetAddress getInetAddress() throws UnknownHostException {
117 if (getAddress() == null)
118 return null;
119 return InetAddress.getByName(getAddress());
120 }
121
122
123
124
125
126
127 public int getPort() {
128 return port;
129 }
130
131
132
133
134
135
136 public void setPort(int port) {
137 this.port = port;
138 }
139
140
141
142
143
144
145
146
147
148
149 public int getBacklog() {
150 return backlog;
151 }
152
153
154
155
156
157
158
159
160
161
162 public void setBacklog(int backlog) {
163 this.backlog = backlog;
164 }
165
166
167
168
169
170
171 public String getAddress() {
172 return address;
173 }
174
175
176
177
178
179
180 public void setAddress(String address) {
181 this.address = address;
182 }
183
184 }