mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Matthew Swift
18.28.2013 7d1bbf9b372e41121198be2b9f0f322d58b8d014
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2013 ForgeRock AS.
 */
package com.forgerock.opendj.ldap;
 
import java.util.LinkedList;
import java.util.List;
 
import org.forgerock.opendj.ldap.ConnectionEventListener;
import org.forgerock.opendj.ldap.ErrorResultException;
import org.forgerock.opendj.ldap.responses.ExtendedResult;
 
/**
 * 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:
 * <ul>
 * <li>connection is <b>valid</b> (isClosed()=false, isFailed()=false): can fail
 * or be closed
 * <li>connection has failed due to an <b>error</b> (isClosed()=false,
 * isFailed()=true): can be closed
 * <li>connection has been <b>closed</b> by the application (isClosed()=true,
 * isFailed()=false): terminal state
 * <li>connection has failed due to an <b>error</b> and has been <b>closed</b>
 * by the application (isClosed()=true, isFailed()=true): terminal state
 * </ul>
 * All methods are synchronized and container classes may also synchronize on
 * the state where needed. The state transition methods,
 * {@link #notifyConnectionClosed()} and
 * {@link #notifyConnectionError(boolean, ErrorResultException)}, correspond to
 * methods in the {@link ConnectionEventListener} interface except that they
 * return a boolean indicating whether the transition was successful or not.
 */
public final class ConnectionState {
    /*
     * FIXME: The synchronization in this class has been kept simple for now.
     * However, ideally we should notify listeners without synchronizing on the
     * state in case a listener takes a long time to complete.
     */
 
    /*
     * FIXME: This class should be used by connection pool and ldap connection
     * implementations as well.
     */
 
    /**
     * Use the State design pattern to manage state transitions.
     */
    private enum State {
 
        /**
         * Connection has not encountered an error nor has it been closed
         * (initial state).
         */
        VALID() {
            @Override
            void addConnectionEventListener(final ConnectionState cs,
                    final ConnectionEventListener listener) {
                cs.listeners.add(listener);
            }
 
            @Override
            boolean isClosed() {
                return false;
            }
 
            @Override
            boolean isFailed() {
                return false;
            }
 
            @Override
            boolean isValid() {
                return true;
            }
 
            @Override
            boolean notifyConnectionClosed(final ConnectionState cs) {
                for (final ConnectionEventListener listener : cs.listeners) {
                    listener.handleConnectionClosed();
                }
                cs.state = CLOSED;
                return true;
            }
 
            @Override
            boolean notifyConnectionError(final ConnectionState cs,
                    final boolean isDisconnectNotification, final ErrorResultException error) {
                // Transition from valid to error state.
                cs.failedDueToDisconnect = isDisconnectNotification;
                cs.connectionError = error;
                for (final ConnectionEventListener listener : cs.listeners) {
                    // Use the reason provided in the disconnect notification.
                    listener.handleConnectionError(isDisconnectNotification, error);
                }
                cs.state = ERROR;
                return true;
            }
 
            @Override
            void notifyUnsolicitedNotification(final ConnectionState cs,
                    final ExtendedResult notification) {
                for (final ConnectionEventListener listener : cs.listeners) {
                    listener.handleUnsolicitedNotification(notification);
                }
            }
        },
 
        /**
         * Connection has encountered an error, but has not been closed.
         */
        ERROR() {
            @Override
            void addConnectionEventListener(final ConnectionState cs,
                    final ConnectionEventListener listener) {
                listener.handleConnectionError(cs.failedDueToDisconnect, cs.connectionError);
                cs.listeners.add(listener);
            }
 
            @Override
            boolean isClosed() {
                return false;
            }
 
            @Override
            boolean isFailed() {
                return true;
            }
 
            @Override
            boolean isValid() {
                return false;
            }
 
            @Override
            boolean notifyConnectionClosed(final ConnectionState cs) {
                for (final ConnectionEventListener listener : cs.listeners) {
                    listener.handleConnectionClosed();
                }
                cs.state = ERROR_CLOSED;
                return true;
            }
        },
 
        /**
         * Connection has been closed (terminal state).
         */
        CLOSED() {
            @Override
            void addConnectionEventListener(final ConnectionState cs,
                    final ConnectionEventListener listener) {
                listener.handleConnectionClosed();
            }
 
            @Override
            boolean isClosed() {
                return true;
            }
 
            @Override
            boolean isFailed() {
                return false;
            }
 
            @Override
            boolean isValid() {
                return false;
            }
        },
 
        /**
         * Connection has encountered an error and has been closed (terminal
         * state).
         */
        ERROR_CLOSED() {
            @Override
            void addConnectionEventListener(final ConnectionState cs,
                    final ConnectionEventListener listener) {
                listener.handleConnectionError(cs.failedDueToDisconnect, cs.connectionError);
                listener.handleConnectionClosed();
            }
 
            @Override
            boolean isClosed() {
                return true;
            }
 
            @Override
            boolean isFailed() {
                return true;
            }
 
            @Override
            boolean isValid() {
                return false;
            }
        };
 
        abstract void addConnectionEventListener(ConnectionState cs,
                final ConnectionEventListener listener);
 
        abstract boolean isClosed();
 
        abstract boolean isFailed();
 
        abstract boolean isValid();
 
        boolean notifyConnectionClosed(final ConnectionState cs) {
            return false;
        }
 
        boolean notifyConnectionError(final ConnectionState cs,
                final boolean isDisconnectNotification, final ErrorResultException error) {
            return false;
        }
 
        void notifyUnsolicitedNotification(final ConnectionState cs,
                final ExtendedResult notification) {
            // Do nothing by default.
        }
    }
 
    /**
     * Non-{@code null} once the connection has failed due to a connection
     * error. Volatile so that it can be read without synchronization.
     */
    private volatile ErrorResultException connectionError = null;
 
    /**
     * {@code true} if the connection has failed due to a disconnect
     * notification.
     */
    private boolean failedDueToDisconnect = false;
 
    /**
     * Registered event listeners.
     */
    private final List<ConnectionEventListener> listeners =
            new LinkedList<ConnectionEventListener>();
 
    /**
     * Internal state implementation.
     */
    private volatile State state = State.VALID;
 
    /**
     * Creates a new connection state which is initially valid.
     */
    public ConnectionState() {
        // Nothing to do.
    }
 
    /**
     * 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.
     *
     * @param 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. if
     *             {@code isClosed() == true}.
     * @throws NullPointerException
     *             If the {@code listener} was {@code null}.
     */
    public synchronized void addConnectionEventListener(final ConnectionEventListener listener) {
        state.addConnectionEventListener(this, listener);
    }
 
    /**
     * Returns the error that caused the connection to fail, or {@code null} if
     * the connection has not failed.
     *
     * @return The error that caused the connection to fail, or {@code null} if
     *         the connection has not failed.
     */
    public ErrorResultException getConnectionError() {
        return connectionError;
    }
 
    /**
     * Indicates whether or not this connection has been explicitly closed by
     * calling {@code close}. This method will not return {@code true} if a
     * fatal error has occurred on the connection unless {@code close} has been
     * called.
     *
     * @return {@code true} if this connection has been explicitly closed by
     *         calling {@code close}, or {@code false} otherwise.
     */
    public boolean isClosed() {
        return state.isClosed();
    }
 
    /**
     * Returns {@code true} if the associated connection has not been closed and
     * no fatal errors have been detected.
     *
     * @return {@code true} if this connection is valid, {@code false}
     *         otherwise.
     */
    public boolean isValid() {
        return state.isValid();
    }
 
    /**
     * Attempts to transition this connection state to closed and invokes event
     * listeners if successful.
     *
     * @return {@code true} if the state changed to closed, or {@code false} if
     *         the state was already closed.
     * @see ConnectionEventListener#handleConnectionClosed()
     */
    public synchronized boolean notifyConnectionClosed() {
        return state.notifyConnectionClosed(this);
    }
 
    /**
     * Attempts to transition this connection state to error and invokes event
     * listeners if successful.
     *
     * @param isDisconnectNotification
     *            {@code true} if the error was triggered by a disconnect
     *            notification sent by the server, otherwise {@code false}.
     * @param error
     *            The exception that is about to be thrown to the application.
     * @return {@code true} if the state changed to error, or {@code false} if
     *         the state was already error or closed.
     * @see ConnectionEventListener#handleConnectionError(boolean,
     *      ErrorResultException)
     */
    public synchronized boolean notifyConnectionError(final boolean isDisconnectNotification,
            final ErrorResultException error) {
        return state.notifyConnectionError(this, isDisconnectNotification, error);
    }
 
    /**
     * Notifies event listeners of the provided unsolicited notification if the
     * state is valid.
     *
     * @param notification
     *            The unsolicited notification.
     * @see ConnectionEventListener#handleUnsolicitedNotification(ExtendedResult)
     */
    public synchronized void notifyUnsolicitedNotification(final ExtendedResult notification) {
        state.notifyUnsolicitedNotification(this, notification);
    }
 
    /**
     * 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.
     *
     * @param listener
     *            The listener which no longer wants to be notified when events
     *            occur on this connection.
     * @throws NullPointerException
     *             If the {@code listener} was {@code null}.
     */
    public synchronized void removeConnectionEventListener(final ConnectionEventListener listener) {
        listeners.remove(listener);
    }
 
}