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 TypeClassDescriptionprivate static final classInvokes a latch when this handler is added or removed from a pipeline.protected static classInitiates a channel read when an LDAP message has been processed and auto read is false.final classBind specific operation handle that locks other operations until the bind completes.private classSets up the Netty pipeline for this connection.private final classListener for channel close events.private final classSetsinboundExceptionand closes the channel when an exception occurs.private final classMatches an inbound LDAP response message to its operation handle and removes that handle from the response queue.private final classListener that logs the future success state when it occurs.protected static classDecodes byte buffer into a concrete LDAP response message.protected static enumEnum that describes the state of an LDAP message in the pipeline.protected static classEncodes an LDAP request into its DER bytes.private classSchedules a connection validator to run based on its strategy. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ReentrantReadWriteLockOperation lock when a bind occurs.private io.netty.channel.ChannelConnection to the LDAP server.Netty channel configuration options.private final Class<? extends io.netty.channel.Channel>Type of channel.private final NettyConnection.CloseFutureListenerListener notified when the connection is closed.private ExecutorServiceExecutor for scheduling various connection related tasks that cannot or should not be handled by the netty event loop groups.private InstantTime this connection was successfully established, null if the connection is not open.private ThrowableLast exception received on the inbound pipeline.private final io.netty.channel.EventLoopGroupEvent worker group used to process I/O.private LdapURLURL derived from the connection strategy.private static final LoggerLogger for this class.private final AtomicIntegerMessage ID counter, incremented as requests are sent.private final io.netty.channel.EventLoopGroupEvent worker group used to process inbound messages.private final HandleMapQueue holding requests that haven't received a response.private static final NettyConnection.AutoReadEventHandlerInbound handler to read the next message if autoRead is false.private final ReentrantReadWriteLockBlock operations while a reconnect is occurring.private static final NettyConnection.RequestEncoderRequest encoder pipeline handler.private final booleanWhether 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 TypeMethodDescriptionvoidclose(RequestControl... controls) Closes this connection.protected voidcomplete(DefaultOperationHandle<?, ?> handle) Report that the supplied handle has completed.private io.netty.channel.ChannelFutureconnectBootstrap(NettyConnection.ClientInitializer initializer) Creates a new bootstrap with the supplied initializer and uses that bootstrap to connect to the ldapURL.private io.netty.channel.ChannelCreates a Netty bootstrap and connects to the LDAP server.private ObjectconvertChannelOption(String value) Performs a best effort at converting a channel option value to the correct type.private io.netty.bootstrap.BootstrapcreateBootstrap(NettyConnection.ClientInitializer initializer) Creates a NettyBootstrapwith the supplied client initializer.Creates a new client initializer.private io.netty.handler.ssl.SslHandlercreateSslHandler(ConnectionConfig config) Creates a Netty SSL handler using the supplied connection config.(package private) intReturns the value of the next message ID and increments the counter.Returns the channel options.(package private) io.netty.channel.EventLoopGroupReturns the I/O worker group.Returns the URL that was selected for this connection.(package private) intReturns the value of the next message ID.(package private) io.netty.channel.EventLoopGroupReturns the message worker group.private booleanReturns whether this connection is currently attempting to close.booleanisOpen()Returns whether the underlying Netty channel is open.private booleanReturns whether this connection is currently attempting to open.private voidSends an exception notification to all pending responses that the connection has been closed.protected voidAttempt to open a connection to the supplied LDAP URL.private voidopenInitialize(LdapURL url) Initializes this connection for use after it has been established.voidoperation(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 a delete operation.operation(ExtendedRequest request) Creates a handle for an extended operation.(package private) Resultoperation(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 voidoperation(UnbindRequest request) Executes an unbind operation.private voidAttempts to reestablish the channel for this connection.(package private) voidsetMessageID(int i) Sets the value of the next message ID.protected booleanDetermine whether the supplied URL is acceptable for use.private voidThrows an exception if the Netty channel is closed.toString()private voidwaitForConnectionEstablish(NettyConnection.ClientInitializer initializer, io.netty.channel.ChannelFuture future) Waits until the TCP connection has completed.private voidwaitForSSLHandshake(io.netty.channel.Channel ch) Waits until the SSL handshake has completed.protected voidwrite(DefaultOperationHandle<?, ?> handle) Write the request in the supplied handle to the LDAP server.Methods inherited from class org.ldaptive.transport.TransportConnection
open, reopen, strategyOpenMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.ldaptive.Connection
close
-
Field Details
-
LOGGER
Logger 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 final 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
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 NettyBootstrapwith the supplied client initializer.- Parameters:
initializer- to provide to the bootstrap- Returns:
- Netty bootstrap
-
test
Description copied from class:TransportConnectionDetermine whether the supplied URL is acceptable for use.- Specified by:
testin classTransportConnection- Parameters:
url- LDAP URL to test- Returns:
- whether URL can be become active
-
open
Description copied from class:TransportConnectionAttempt to open a connection to the supplied LDAP URL.- Specified by:
openin classTransportConnection- Parameters:
url- LDAP URL to connect to- Throws:
LdapException- if opening the connection fails
-
openInitialize
Initializes this connection for use after it has been established. If startTLS is configured it will be performed. Any configured connection initializers are invoked.- Parameters:
url- LDAP URL to connect to- Throws:
LdapException- if initializing the connection fails
-
getLdapURL
Description copied from interface:ConnectionReturns 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
-
createClientInitializer
Creates a new client initializer. If theldapURLis LDAPS an SSL handler is added to the client initializer.- Returns:
- client initializer
- Throws:
ConnectException- if the SSL engine cannot be initialized
-
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
-
connectBootstrap
private io.netty.channel.ChannelFuture connectBootstrap(NettyConnection.ClientInitializer initializer) Creates a new bootstrap with the supplied initializer and uses that bootstrap to connect to the ldapURL.- Parameters:
initializer- to create bootstrap with- Returns:
- channel future produced from the connect invocation
-
waitForConnectionEstablish
private void waitForConnectionEstablish(NettyConnection.ClientInitializer initializer, io.netty.channel.ChannelFuture future) throws ConnectException Waits until the TCP connection has completed.- Parameters:
initializer- used to determine whether to wait for the SSL handshake to completefuture- to wait on- Throws:
ConnectException- if the connection fails
-
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:TransportConnectionExecutes an unbind operation. Clients should close connections usingConnection.close().- Specified by:
operationin 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:ConnectionExecutes an abandon operation. Clients should execute abandons usingOperationHandle.abandon().- Parameters:
request- abandon request
-
operation
Description copied from interface:ConnectionCreates a handle for an add operation.- Parameters:
request- add request- Returns:
- operation handle
-
operation
Description copied from interface:ConnectionCreates 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 operation handle.- Parameters:
request- bind request- Returns:
- operation handle
-
operation
Description copied from interface:ConnectionCreates a handle for a compare operation.- Parameters:
request- compare request- Returns:
- compare operation handle
-
operation
Description copied from interface:ConnectionCreates a handle for a delete operation.- Parameters:
request- delete request- Returns:
- operation handle
-
operation
Description copied from interface:ConnectionCreates a handle for an extended operation.- Parameters:
request- extended request- Returns:
- extended operation handle
-
operation
Description copied from interface:ConnectionCreates a handle for a modify operation.- Parameters:
request- modify request- Returns:
- operation handle
-
operation
Description copied from interface:ConnectionCreates a handle for a modify dn operation.- Parameters:
request- modify dn request- Returns:
- operation handle
-
operation
Description copied from interface:ConnectionCreates a handle for a search operation.- Parameters:
request- search request- Returns:
- search operation handle
-
write
Description copied from class:TransportConnectionWrite the request in the supplied handle to the LDAP server. This method does not throw, it should report exceptions to the handle.- Specified by:
writein classTransportConnection- Parameters:
handle- for the operation write
-
complete
Description copied from class:TransportConnectionReport that the supplied handle has completed. Allows the connection to clean up any resources associated with the handle.- Specified by:
completein 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
-
getIoWorkerGroup
io.netty.channel.EventLoopGroup getIoWorkerGroup()Returns the I/O worker group.- Returns:
- I/O worker group
-
getMessageWorkerGroup
io.netty.channel.EventLoopGroup getMessageWorkerGroup()Returns the message worker group.- Returns:
- message worker group
-
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
private 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 themessageWorkerGroupif it is configured. -
reconnect
private 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
-