Package org.forgerock.opendj.ldap.spi
Class ConnectionState
java.lang.Object
org.forgerock.opendj.ldap.spi.ConnectionState
This class can be used to manage the internal state of a connection, ensuring
valid and atomic state transitions, as well as connection event listener
notification. There are 4 states:
- connection is valid (isClosed()=false, isFailed()=false): can fail or be closed
- connection has failed due to an error (isClosed()=false, isFailed()=true): can be closed
- connection has been closed by the application (isClosed()=true, isFailed()=false): terminal state
- connection has failed due to an error and has been closed by the application (isClosed()=true, isFailed()=true): terminal state
notifyConnectionClosed() and
notifyConnectionError(boolean, LdapException), correspond to
methods in the ConnectionEventListener interface except that they
return a boolean indicating whether the transition was successful or not.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidRegisters the provided connection event listener so that it will be notified when this connection is closed by the application, receives an unsolicited notification, or experiences a fatal error.Returns the error that caused the connection to fail, ornullif the connection has not failed.booleanisClosed()Indicates whether this connection has been explicitly closed by callingclose.booleanisValid()Returnstrueif the associated connection has not been closed and no fatal errors have been detected.booleanAttempts to transition this connection state to closed and invokes event listeners if successful.booleannotifyConnectionError(boolean isDisconnectNotification, LdapException error) Attempts to transition this connection state to error and invokes event listeners if successful.voidnotifyUnsolicitedNotification(ExtendedResult notification) Notifies event listeners of the provided unsolicited notification if the state is valid.voidRemoves the provided connection event listener from this connection so that it will no longer be notified when this connection is closed by the application, receives an unsolicited notification, or experiences a fatal error.
-
Constructor Details
-
ConnectionState
public ConnectionState()Creates a new connection state which is initially valid.
-
-
Method Details
-
addConnectionEventListener
Registers the provided connection event listener so that it will be notified when this connection is closed by the application, receives an unsolicited notification, or experiences a fatal error.- Parameters:
listener- The listener which wants to be notified when events occur on this connection.- Throws:
IllegalStateException- If this connection has already been closed, i.e. ifisClosed() == true.NullPointerException- If thelistenerwasnull.
-
getConnectionError
Returns the error that caused the connection to fail, ornullif the connection has not failed.- Returns:
- The error that caused the connection to fail, or
nullif the connection has not failed.
-
isClosed
Indicates whether this connection has been explicitly closed by callingclose. This method will not returntrueif a fatal error has occurred on the connection unlessclosehas been called.- Returns:
trueif this connection has been explicitly closed by callingclose, orfalseotherwise.
-
isValid
Returnstrueif the associated connection has not been closed and no fatal errors have been detected.- Returns:
trueif this connection is valid,falseotherwise.
-
notifyConnectionClosed
Attempts to transition this connection state to closed and invokes event listeners if successful.- Returns:
trueif the state changed to closed, orfalseif the state was already closed.- See Also:
-
notifyConnectionError
Attempts to transition this connection state to error and invokes event listeners if successful.- Parameters:
isDisconnectNotification-trueif the error was triggered by a disconnect notification sent by the server, otherwisefalse.error- The exception that is about to be thrown to the application.- Returns:
trueif the state changed to error, orfalseif the state was already error or closed.- See Also:
-
notifyUnsolicitedNotification
Notifies event listeners of the provided unsolicited notification if the state is valid.- Parameters:
notification- The unsolicited notification.- See Also:
-
removeConnectionEventListener
Removes the provided connection event listener from this connection so that it will no longer be notified when this connection is closed by the application, receives an unsolicited notification, or experiences a fatal error.- Parameters:
listener- The listener which no longer wants to be notified when events occur on this connection.- Throws:
NullPointerException- If thelistenerwasnull.
-