Class NettyConnection

    • Field Detail

      • LOGGER

        private static final Logger LOGGER
        Logger for this class.
      • channelType

        private final Class<? extends> channelType
        Type of channel.
      • ioWorkerGroup

        private final ioWorkerGroup
        Event worker group used to process I/O.
      • messageWorkerGroup

        private final messageWorkerGroup
        Event worker group used to process inbound messages.
      • shutdownOnClose

        private final boolean shutdownOnClose
        Whether to shutdown the event loop groups on Connection.close().
      • channelOptions

        private final Map<,​Object> channelOptions
        Netty channel configuration options.
      • pendingResponses

        private final HandleMap pendingResponses
        Queue holding requests that haven't received a response.
      • messageID

        private final AtomicInteger messageID
        Message ID counter, incremented as requests are sent.
      • reconnectLock

        private final ReentrantReadWriteLock reconnectLock
        Block operations while a reconnect is occurring.
      • connectionExecutor

        private ExecutorService 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

        private LdapURL ldapURL
        URL derived from the connection strategy.
      • channel

        private channel
        Connection to the LDAP server.
      • connectTime

        private Instant connectTime
        Time this connection was successfully established, null if the connection is not open.
      • inboundException

        private Throwable inboundException
        Last exception received on the inbound pipeline.
    • Constructor Detail

      • NettyConnection

        public NettyConnection​(ConnectionConfig config,
                               Class<? extends> type,
                               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.
        config - connection configuration
        type - type of channel
        ioGroup - event loop group that handles I/O and supports the channel type, cannot be null
        messageGroup - event loop group that handles inbound messages, can be null
        shutdownGroups - whether to shutdown the event loop groups when the connection is closed
    • Method Detail

      • convertChannelOption

        private Object convertChannelOption​(String value)
        Performs a best effort at converting a channel option value to the correct type. Handles Boolean and Integer types.
        value - to convert
        converted value or the supplied value if no conversion occurred
      • createBootstrap

        private io.netty.bootstrap.Bootstrap createBootstrap​(NettyConnection.ClientInitializer initializer)
        Creates a Netty Bootstrap with the supplied client initializer.
        initializer - to provide to the bootstrap
        Netty bootstrap
      • test

        protected boolean test​(LdapURL url)
        Description copied from class: TransportConnection
        Determine whether the supplied URL is acceptable for use.
        Specified by:
        test in class TransportConnection
        url - LDAP URL to test
        whether URL can be become active
      • openInitialize

        private void openInitialize​(LdapURL url)
                             throws LdapException
        Initializes this connection for use after it has been established. If startTLS is configured it will be performed. Any configured connection initializers are invoked.
        url - LDAP URL to connect to
        LdapException - if initializing the connection fails
      • getLdapURL

        public LdapURL 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.
        LDAP URL
      • connectInternal

        private connectInternal()
                                                  throws ConnectException
        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.
        channel for the established connection.
        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.
        config - containing SSL config
        SSL handler
        SSLException - if the SSL engine cannot be initialized
      • connectBootstrap

        private connectBootstrap​(NettyConnection.ClientInitializer initializer)
        Creates a new bootstrap with the supplied initializer and uses that bootstrap to connect to the ldapURL.
        initializer - to create bootstrap with
        channel future produced from the connect invocation
      • waitForConnectionEstablish

        private void waitForConnectionEstablish​(NettyConnection.ClientInitializer initializer,
                                         throws ConnectException
        Waits until the TCP connection has completed.
        initializer - used to determine whether to wait for the SSL handshake to complete
        future - to wait on
        ConnectException - if the connection fails
      • waitForSSLHandshake

        private void waitForSSLHandshake​( ch)
                                  throws SSLException
        Waits until the SSL handshake has completed.
        ch - that the handshake is occurring on
        SSLException - if the handshake fails
      • operation

        Result operation​(StartTLSRequest request)
                  throws LdapException
        Performs a startTLS operation. This method can only be invoked when a connection is opened.
        request - to send
        result of the startTLS operation
        LdapException - if the operation fails
      • operation

        public BindResponse operation​(SaslClientRequest request)
                               throws LdapException
        Performs a SASL bind operation that uses a custom client.
        request - to send
        result of the GSS-API bind operation
        LdapException - if the operation fails or another bind is in progress
      • operation

        public NettyConnection.BindOperationHandle operation​(BindRequest request)
        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 operation handle.
        request - bind request
        operation handle
      • write

        protected void write​(DefaultOperationHandle handle)
        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 class TransportConnection
        handle - for the operation write
      • complete

        protected void complete​(DefaultOperationHandle handle)
        Description copied from class: TransportConnection
        Report that the supplied handle has completed. Allows the connection to clean up any resources associated with the handle.
        Specified by:
        complete in class TransportConnection
        handle - that has completed
      • getAndIncrementMessageID

        int getAndIncrementMessageID()
        Returns the value of the next message ID and increments the counter.
        message ID
      • getMessageID

        int getMessageID()
        Returns the value of the next message ID.
        message ID
      • setMessageID

        void setMessageID​(int i)
        Sets the value of the next message ID.
        i - message ID
      • getChannelOptions

        Map<,​Object> getChannelOptions()
        Returns the channel options.
        channel options
      • close

        public void close​(RequestControl... controls)
        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.
        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 the messageWorkerGroup if it is configured.
      • reconnect

        private void reconnect()
        Attempts to reestablish the channel for this connection.
        IllegalStateException - if the connection is open
      • isOpen

        public boolean isOpen()
        Returns whether the underlying Netty channel is open. See Channel.isOpen().
        whether the Netty channel is open
      • isOpening

        private boolean isOpening()
        Returns whether this connection is currently attempting to open.
        whether the Netty channel is in the process of opening
      • isClosing

        private boolean isClosing()
        Returns whether this connection is currently attempting to close.
        whether the Netty channel is in the process of closing
      • throwIfClosed

        private void throwIfClosed()
                            throws LdapException
        Throws an exception if the Netty channel is closed. See isOpen().
        LdapException - if the connection is closed