Package org.ldaptive.transport.netty
Class NettyConnection
java.lang.Object
org.ldaptive.transport.TransportConnection
org.ldaptive.transport.netty.NettyConnection
- All Implemented Interfaces:
AutoCloseable
,Connection
Netty based connection implementation.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static class
Initiates a channel read when an LDAP message has been processed and auto read is false.class
Bind specific operation handle that locks other operations until the bind completes.private class
Sets up the Netty pipeline for this connection.private class
Listener for channel close events.private class
SetsinboundException
and closes the channel when an exception occurs.private class
Matches an inbound LDAP response message to it's operation handle and removes that handle from the response queue.private class
Listener that logs the future success state when it occurs.protected static class
Decodes byte buffer into a concrete LDAP response message.protected static enum
Enum that describes the state of an LDAP message in the pipeline.protected static class
Encodes an LDAP request into it's DER bytes.private class
Schedules a connection validator to run based on it's strategy. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ReentrantReadWriteLock
Operation lock when a bind occurs.private io.netty.channel.Channel
Connection to the LDAP server.Netty channel configuration options.private final Class
<? extends io.netty.channel.Channel> Type of channel.private final NettyConnection.CloseFutureListener
Listener notified when the connection is closed.private ExecutorService
Executor for scheduling various connection related tasks that cannot or should not be handled by the netty event loop groups.private Instant
Time this connection was successfully established, null if the connection is not open.private Throwable
Last exception received on the inbound pipeline.private final io.netty.channel.EventLoopGroup
Event worker group used to process I/O.private LdapURL
URL derived from the connection strategy.private static final org.slf4j.Logger
Logger for this class.private final AtomicInteger
Message ID counter, incremented as requests are sent.private final io.netty.channel.EventLoopGroup
Event worker group used to process inbound messages.private final HandleMap
Queue holding requests that haven't received a response.private static final NettyConnection.AutoReadEventHandler
Inbound handler to read the next message if autoRead is false.private final ReentrantReadWriteLock
Block operations while a reconnect is occurring.private static final NettyConnection.RequestEncoder
Request encoder pipeline handler.private boolean
Whether to shutdown the event loop groups onConnection.close()
.Fields inherited from class org.ldaptive.transport.TransportConnection
closeLock, connectionConfig, lastSuccessfulOpen, openLock
-
Constructor Summary
ConstructorsConstructorDescriptionNettyConnection
(ConnectionConfig config, Class<? extends io.netty.channel.Channel> type, io.netty.channel.EventLoopGroup ioGroup, io.netty.channel.EventLoopGroup messageGroup, boolean shutdownGroups) Creates a new connection. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close
(RequestControl... controls) Closes this connection.protected void
complete
(DefaultOperationHandle handle) Report that the supplied handle has completed.private io.netty.channel.Channel
Creates a Netty bootstrap and connects to the LDAP server.private Object
convertChannelOption
(String value) Performs a best effort at converting a channel option value to the correct type.private io.netty.bootstrap.Bootstrap
createBootstrap
(NettyConnection.ClientInitializer initializer) Creates a NettyBootstrap
with the supplied client initializer.private io.netty.handler.ssl.SslHandler
createSslHandler
(ConnectionConfig config) Creates a Netty SSL handler using the supplied connection config.(package private) int
Returns the value of the next message ID and increments the counter.Returns the channel options.Returns the URL that was selected for this connection.(package private) int
Returns the value of the next message ID.private boolean
Returns whether this connection is currently attempting to close.boolean
isOpen()
Returns whether the underlying Netty channel is open.private boolean
Returns whether this connection is currently attempting to open.protected void
Sends an exception notification to all pending responses that the connection has been closed.protected void
Attempt to open a connection to the supplied LDAP URL.void
operation
(AbandonRequest request) Executes an abandon operation.operation
(AddRequest request) Creates a handle for an add operation.operation
(BindRequest request) Creates a handle for a bind operation.operation
(CompareRequest request) Creates a handle for a compare operation.operation
(DeleteRequest request) Creates a handle for an delete operation.operation
(ExtendedRequest request) Creates a handle for an extended operation.(package private) Result
operation
(StartTLSRequest request) Performs a startTLS operation.operation
(ModifyDnRequest request) Creates a handle for a modify dn operation.operation
(ModifyRequest request) Creates a handle for a modify operation.operation
(DefaultSaslClientRequest request) Performs a SASL client bind operation.operation
(SaslClientRequest request) Performs a SASL bind operation that uses a custom client.operation
(SearchRequest request) Creates a handle for a search operation.protected void
operation
(UnbindRequest request) Executes an unbind operation.protected void
Attempts to reestablish the channel for this connection.(package private) void
setMessageID
(int i) Sets the value of the next message ID.protected boolean
Determine whether the supplied URL is acceptable for use.private void
Throws an exception if the Netty channel is closed.toString()
private void
waitForSSLHandshake
(io.netty.channel.Channel ch) Waits until the SSL handshake has completed.protected void
write
(DefaultOperationHandle handle) Write the request in the supplied handle to the LDAP server.Methods inherited from class org.ldaptive.transport.TransportConnection
open, reopen, strategyOpen
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.ldaptive.Connection
close
-
Field Details
-
LOGGER
private static final org.slf4j.Logger LOGGERLogger for this class. -
REQUEST_ENCODER
Request encoder pipeline handler. -
READ_NEXT_MESSAGE
Inbound handler to read the next message if autoRead is false. -
channelType
Type of channel. -
ioWorkerGroup
private final io.netty.channel.EventLoopGroup ioWorkerGroupEvent worker group used to process I/O. -
messageWorkerGroup
private final io.netty.channel.EventLoopGroup messageWorkerGroupEvent worker group used to process inbound messages. -
shutdownOnClose
private boolean shutdownOnCloseWhether to shutdown the event loop groups onConnection.close()
. -
channelOptions
Netty channel configuration options. -
pendingResponses
Queue holding requests that haven't received a response. -
closeListener
Listener notified when the connection is closed. -
messageID
Message ID counter, incremented as requests are sent. -
reconnectLock
Block operations while a reconnect is occurring. -
bindLock
Operation lock when a bind occurs. -
connectionExecutor
Executor for scheduling various connection related tasks that cannot or should not be handled by the netty event loop groups. Reconnects in particular require a dedicated thread as the event loop group may be shared or may not be configured with enough threads to handle the task. -
ldapURL
URL derived from the connection strategy. -
channel
private io.netty.channel.Channel channelConnection to the LDAP server. -
connectTime
Time this connection was successfully established, null if the connection is not open. -
inboundException
Last exception received on the inbound pipeline.
-
-
Constructor Details
-
NettyConnection
public NettyConnection(ConnectionConfig config, Class<? extends io.netty.channel.Channel> type, io.netty.channel.EventLoopGroup ioGroup, io.netty.channel.EventLoopGroup messageGroup, boolean shutdownGroups) Creates a new connection. Netty supports various transport implementations including NIO, EPOLL, KQueue, etc. The class type and event loop group are tightly coupled in this regard.- Parameters:
config
- connection configurationtype
- type of channelioGroup
- event loop group that handles I/O and supports the channel type, cannot be nullmessageGroup
- event loop group that handles inbound messages, can be nullshutdownGroups
- whether to shutdown the event loop groups when the connection is closed
-
-
Method Details
-
convertChannelOption
Performs a best effort at converting a channel option value to the correct type. Handles Boolean and Integer types.- Parameters:
value
- to convert- Returns:
- converted value or the supplied value if no conversion occurred
-
createBootstrap
Creates a NettyBootstrap
with the supplied client initializer.- Parameters:
initializer
- to provide to the bootstrap- Returns:
- Netty bootstrap
-
test
Description copied from class:TransportConnection
Determine whether the supplied URL is acceptable for use.- Specified by:
test
in classTransportConnection
- Parameters:
url
- LDAP URL to test- Returns:
- whether URL can be become active
-
open
Description copied from class:TransportConnection
Attempt to open a connection to the supplied LDAP URL.- Specified by:
open
in classTransportConnection
- Parameters:
url
- LDAP URL to connect to- Throws:
LdapException
- if opening the connection fails
-
getLdapURL
Description copied from interface:Connection
Returns the URL that was selected for this connection. The existence of this value does not indicate a current established connection.- Returns:
- LDAP URL
-
connectInternal
Creates a Netty bootstrap and connects to the LDAP server. Handles the details of adding an SSL handler to the pipeline. This method waits until the connection is established.- Returns:
- channel for the established connection.
- Throws:
ConnectException
- if the connection fails
-
createSslHandler
private io.netty.handler.ssl.SslHandler createSslHandler(ConnectionConfig config) throws SSLException Creates a Netty SSL handler using the supplied connection config.- Parameters:
config
- containing SSL config- Returns:
- SSL handler
- Throws:
SSLException
- if the SSL engine cannot be initialized
-
waitForSSLHandshake
Waits until the SSL handshake has completed.- Parameters:
ch
- that the handshake is occurring on- Throws:
SSLException
- if the handshake fails
-
operation
Performs a startTLS operation. This method can only be invoked when a connection is opened.- Parameters:
request
- to send- Returns:
- result of the startTLS operation
- Throws:
LdapException
- if the operation fails
-
operation
Description copied from class:TransportConnection
Executes an unbind operation. Clients should close connections usingConnection.close()
.- Specified by:
operation
in classTransportConnection
- Parameters:
request
- unbind request
-
operation
Performs a SASL bind operation that uses a custom client.- Parameters:
request
- to send- Returns:
- result of the GSS-API bind operation
- Throws:
LdapException
- if the operation fails or another bind is in progress
-
operation
Performs a SASL client bind operation.- Parameters:
request
- to send- Returns:
- result of the SASL client bind operation
- Throws:
LdapException
- if the operation fails or another bind is in progress
-
operation
Description copied from interface:Connection
Executes an abandon operation. Clients should execute abandons usingOperationHandle.abandon()
.- Parameters:
request
- abandon request
-
operation
Description copied from interface:Connection
Creates a handle for an add operation.- Parameters:
request
- add request- Returns:
- operation handle
-
operation
Description copied from interface:Connection
Creates a handle for a bind operation. Since clients must not send requests while a bind is in progress, some methods may not be supported on the the operation handle.- Parameters:
request
- bind request- Returns:
- operation handle
-
operation
Description copied from interface:Connection
Creates a handle for a compare operation.- Parameters:
request
- compare request- Returns:
- compare operation handle
-
operation
Description copied from interface:Connection
Creates a handle for an delete operation.- Parameters:
request
- delete request- Returns:
- operation handle
-
operation
Description copied from interface:Connection
Creates a handle for an extended operation.- Parameters:
request
- extended request- Returns:
- extended operation handle
-
operation
Description copied from interface:Connection
Creates a handle for a modify operation.- Parameters:
request
- modify request- Returns:
- operation handle
-
operation
Description copied from interface:Connection
Creates a handle for a modify dn operation.- Parameters:
request
- modify dn request- Returns:
- operation handle
-
operation
Description copied from interface:Connection
Creates a handle for a search operation.- Parameters:
request
- search request- Returns:
- search operation handle
-
write
Description copied from class:TransportConnection
Write the request in the supplied handle to the LDAP server. This method does not throw, it should report exceptions to the handle.- Specified by:
write
in classTransportConnection
- Parameters:
handle
- for the operation write
-
complete
Description copied from class:TransportConnection
Report that the supplied handle has completed. Allows the connection to cleanup any resources associated with the handle.- Specified by:
complete
in classTransportConnection
- Parameters:
handle
- that has completed
-
getAndIncrementMessageID
int getAndIncrementMessageID()Returns the value of the next message ID and increments the counter.- Returns:
- message ID
-
getMessageID
int getMessageID()Returns the value of the next message ID.- Returns:
- message ID
-
setMessageID
void setMessageID(int i) Sets the value of the next message ID.- Parameters:
i
- message ID
-
getChannelOptions
Returns the channel options.- Returns:
- channel options
-
close
Closes this connection. Abandons all pending responses and sends an unbind to the LDAP server if the connection is open when this method is invoked.- Parameters:
controls
- to send with the unbind request when closing the connection
-
notifyOperationHandlesOfClose
protected void notifyOperationHandlesOfClose()Sends an exception notification to all pending responses that the connection has been closed. Since this invokes any configured exception handlers, notifications will use themessageWorkerGroup
if it is configured. -
reconnect
protected void reconnect()Attempts to reestablish the channel for this connection.- Throws:
IllegalStateException
- if the connection is open
-
isOpen
public boolean isOpen()Returns whether the underlying Netty channel is open. SeeChannel.isOpen()
.- Returns:
- whether the Netty channel is open
-
isOpening
private boolean isOpening()Returns whether this connection is currently attempting to open.- Returns:
- whether the Netty channel is in the process of opening
-
isClosing
private boolean isClosing()Returns whether this connection is currently attempting to close.- Returns:
- whether the Netty channel is in the process of closing
-
throwIfClosed
Throws an exception if the Netty channel is closed. SeeisOpen()
.- Throws:
LdapException
- if the connection is closed
-
toString
-