< prev index next >
test/jdk/com/sun/jndi/ldap/lib/BaseLdapServer.java
Print this page
@@ -33,19 +33,19 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
-import java.util.concurrent.RejectedExecutionException;
import static java.lang.System.Logger.Level.INFO;
/*
* A bare-bones (testing aid) server for LDAP scenarios.
*
* Override the following methods to provide customized behavior
*
+ * * beforeAcceptingConnections
* * beforeConnectionHandled
* * handleRequest
*
* Instances of this class are safe for use by multiple threads.
*/
@@ -81,10 +81,11 @@
private void acceptConnections() {
logger().log(INFO, "Server is accepting connections at port {0}",
getPort());
try {
+ beforeAcceptingConnections();
while (isRunning()) {
Socket socket = serverSocket.accept();
logger().log(INFO, "Accepted new connection at {0}", socket);
synchronized (lock) {
// Recheck if the server is still running
@@ -95,22 +96,29 @@
closeSilently(socket);
}
}
connectionsPool.submit(() -> handleConnection(socket));
}
- } catch (IOException | RejectedExecutionException e) {
+ } catch (Throwable t) {
if (isRunning()) {
throw new RuntimeException(
- "Unexpected exception while accepting connections", e);
+ "Unexpected exception while accepting connections", t);
}
} finally {
logger().log(INFO, "Server stopped accepting connections at port {0}",
getPort());
}
}
/*
+ * Called once immediately preceding the server accepting connections.
+ *
+ * Override to customize the behavior.
+ */
+ protected void beforeAcceptingConnections() { }
+
+ /*
* A "Template Method" describing how a connection (represented by a socket)
* is handled.
*
* The socket is closed immediately before the method returns (normally or
* abruptly).
@@ -238,16 +246,29 @@
}
/**
* Returns the local port this server is listening at.
*
+ * This method can be called at any time.
+ *
* @return the port this server is listening at
*/
public int getPort() {
return serverSocket.getLocalPort();
}
+ /**
+ * Returns the address this server is listening at.
+ *
+ * This method can be called at any time.
+ *
+ * @return the address
+ */
+ public InetAddress getInetAddress() {
+ return serverSocket.getInetAddress();
+ }
+
/*
* Returns a flag to indicate whether this server is running or not.
*
* @return {@code true} if this server is running, {@code false} otherwise.
*/
< prev index next >