equal
deleted
inserted
replaced
33 import java.util.Arrays; |
33 import java.util.Arrays; |
34 import java.util.List; |
34 import java.util.List; |
35 import java.util.Objects; |
35 import java.util.Objects; |
36 import java.util.concurrent.ExecutorService; |
36 import java.util.concurrent.ExecutorService; |
37 import java.util.concurrent.Executors; |
37 import java.util.concurrent.Executors; |
38 import java.util.concurrent.RejectedExecutionException; |
|
39 |
38 |
40 import static java.lang.System.Logger.Level.INFO; |
39 import static java.lang.System.Logger.Level.INFO; |
41 |
40 |
42 /* |
41 /* |
43 * A bare-bones (testing aid) server for LDAP scenarios. |
42 * A bare-bones (testing aid) server for LDAP scenarios. |
44 * |
43 * |
45 * Override the following methods to provide customized behavior |
44 * Override the following methods to provide customized behavior |
46 * |
45 * |
|
46 * * beforeAcceptingConnections |
47 * * beforeConnectionHandled |
47 * * beforeConnectionHandled |
48 * * handleRequest |
48 * * handleRequest |
49 * |
49 * |
50 * Instances of this class are safe for use by multiple threads. |
50 * Instances of this class are safe for use by multiple threads. |
51 */ |
51 */ |
81 |
81 |
82 private void acceptConnections() { |
82 private void acceptConnections() { |
83 logger().log(INFO, "Server is accepting connections at port {0}", |
83 logger().log(INFO, "Server is accepting connections at port {0}", |
84 getPort()); |
84 getPort()); |
85 try { |
85 try { |
|
86 beforeAcceptingConnections(); |
86 while (isRunning()) { |
87 while (isRunning()) { |
87 Socket socket = serverSocket.accept(); |
88 Socket socket = serverSocket.accept(); |
88 logger().log(INFO, "Accepted new connection at {0}", socket); |
89 logger().log(INFO, "Accepted new connection at {0}", socket); |
89 synchronized (lock) { |
90 synchronized (lock) { |
90 // Recheck if the server is still running |
91 // Recheck if the server is still running |
95 closeSilently(socket); |
96 closeSilently(socket); |
96 } |
97 } |
97 } |
98 } |
98 connectionsPool.submit(() -> handleConnection(socket)); |
99 connectionsPool.submit(() -> handleConnection(socket)); |
99 } |
100 } |
100 } catch (IOException | RejectedExecutionException e) { |
101 } catch (Throwable t) { |
101 if (isRunning()) { |
102 if (isRunning()) { |
102 throw new RuntimeException( |
103 throw new RuntimeException( |
103 "Unexpected exception while accepting connections", e); |
104 "Unexpected exception while accepting connections", t); |
104 } |
105 } |
105 } finally { |
106 } finally { |
106 logger().log(INFO, "Server stopped accepting connections at port {0}", |
107 logger().log(INFO, "Server stopped accepting connections at port {0}", |
107 getPort()); |
108 getPort()); |
108 } |
109 } |
109 } |
110 } |
|
111 |
|
112 /* |
|
113 * Called once immediately preceding the server accepting connections. |
|
114 * |
|
115 * Override to customize the behavior. |
|
116 */ |
|
117 protected void beforeAcceptingConnections() { } |
110 |
118 |
111 /* |
119 /* |
112 * A "Template Method" describing how a connection (represented by a socket) |
120 * A "Template Method" describing how a connection (represented by a socket) |
113 * is handled. |
121 * is handled. |
114 * |
122 * |
238 } |
246 } |
239 |
247 |
240 /** |
248 /** |
241 * Returns the local port this server is listening at. |
249 * Returns the local port this server is listening at. |
242 * |
250 * |
|
251 * This method can be called at any time. |
|
252 * |
243 * @return the port this server is listening at |
253 * @return the port this server is listening at |
244 */ |
254 */ |
245 public int getPort() { |
255 public int getPort() { |
246 return serverSocket.getLocalPort(); |
256 return serverSocket.getLocalPort(); |
|
257 } |
|
258 |
|
259 /** |
|
260 * Returns the address this server is listening at. |
|
261 * |
|
262 * This method can be called at any time. |
|
263 * |
|
264 * @return the address |
|
265 */ |
|
266 public InetAddress getInetAddress() { |
|
267 return serverSocket.getInetAddress(); |
247 } |
268 } |
248 |
269 |
249 /* |
270 /* |
250 * Returns a flag to indicate whether this server is running or not. |
271 * Returns a flag to indicate whether this server is running or not. |
251 * |
272 * |