Package com.unboundid.ldap.listener
Class LDAPListener
java.lang.Object
java.lang.Thread
com.unboundid.ldap.listener.LDAPListener
- All Implemented Interfaces:
Runnable
This class provides a framework that may be used to accept connections from
LDAP clients and ensure that any requests received on those connections will
be processed appropriately. It can be used to easily allow applications to
accept LDAP requests, to create a simple proxy that can intercept and
examine LDAP requests and responses passing between a client and server, or
helping to test LDAP clients.
Example
The following example demonstrates the process that can be used to create an LDAP listener that will listen for LDAP requests on a randomly-selected port and immediately respond to them with a "success" result:
// Create a canned response request handler that will always return a
// "SUCCESS" result in response to any request.
CannedResponseRequestHandler requestHandler =
new CannedResponseRequestHandler(ResultCode.SUCCESS, null, null,
null);
// A listen port of zero indicates that the listener should
// automatically pick a free port on the system.
int listenPort = 0;
// Create and start an LDAP listener to accept requests and blindly
// return success results.
LDAPListenerConfig listenerConfig = new LDAPListenerConfig(listenPort,
requestHandler);
LDAPListener listener = new LDAPListener(listenerConfig);
listener.startListening();
// Establish a connection to the listener and verify that a search
// request will get a success result.
LDAPConnection connection = new LDAPConnection("localhost",
listener.getListenPort());
SearchResult searchResult = connection.search("dc=example,dc=com",
SearchScope.BASE, Filter.createPresenceFilter("objectClass"));
LDAPTestUtils.assertResultCodeEquals(searchResult,
ResultCode.SUCCESS);
// Close the connection and stop the listener.
connection.close();
listener.shutDown(true);
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.Builder, Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
ConstructorsConstructorDescriptionLDAPListener(LDAPListenerConfig config) Creates a newLDAPListenerobject with the provided configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoidcloseAllConnections(boolean sendNoticeOfDisconnection) Closes all connections that are currently established to this listener.Retrieves the address on which this listener is accepting client connections.intRetrieves the port on which this listener is accepting client connections.voidrun()Operates in a loop, waiting for client connections to arrive and ensuring that they are handled properly.voidshutDown(boolean closeExisting) Indicates that this listener should stop accepting connections.voidCreates the server socket for this listener and starts listening for client connections.Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, isVirtual, join, join, join, join, ofPlatform, ofVirtual, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, sleep, start, startVirtualThread, stop, suspend, threadId, toString, yield
-
Constructor Details
-
LDAPListener
Creates a newLDAPListenerobject with the provided configuration. ThestartListening()method must be called after creating the object to actually start listening for requests.- Parameters:
config- The configuration to use for this listener.
-
-
Method Details
-
startListening
Creates the server socket for this listener and starts listening for client connections. This method will return after the listener has stated.- Throws:
IOException- If a problem occurs while creating the server socket.
-
run
Operates in a loop, waiting for client connections to arrive and ensuring that they are handled properly. This method is for internal use only and must not be called by third-party code. -
closeAllConnections
Closes all connections that are currently established to this listener. This has no effect on the ability to accept new connections.- Parameters:
sendNoticeOfDisconnection- Indicates whether to send the client a notice of disconnection unsolicited notification before closing the connection.
-
shutDown
Indicates that this listener should stop accepting connections. It may optionally also terminate any existing connections that are already established.- Parameters:
closeExisting- Indicates whether to close existing connections that may already be established.
-
getListenAddress
Retrieves the address on which this listener is accepting client connections. Note that if no explicit listen address was configured, then the address returned may not be usable by clients. In the event that theInetAddress.isAnyLocalAddressmethod returnstrue, then clients should generally uselocalhostto attempt to establish connections.- Returns:
- The address on which this listener is accepting client
connections, or
nullif it is not currently listening for client connections.
-
getListenPort
Retrieves the port on which this listener is accepting client connections.- Returns:
- The port on which this listener is accepting client connections, or -1 if it is not currently listening for client connections.
-