|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2017 "Neo Technology,"," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import ConnectionHolder from './connection-holder'; |
| 21 | +import {READ} from '../driver'; |
| 22 | +import StreamObserver from './stream-observer'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Verifies connectivity using the given connection provider. |
| 26 | + */ |
| 27 | +export default class ConnectivityVerifier { |
| 28 | + |
| 29 | + /** |
| 30 | + * @constructor |
| 31 | + * @param {ConnectionProvider} connectionProvider the provider to obtain connections from. |
| 32 | + * @param {function} successCallback a callback to invoke when verification succeeds. |
| 33 | + */ |
| 34 | + constructor(connectionProvider, successCallback) { |
| 35 | + this._connectionProvider = connectionProvider; |
| 36 | + this._successCallback = successCallback; |
| 37 | + } |
| 38 | + |
| 39 | + verify() { |
| 40 | + acquireAndReleaseDummyConnection(this._connectionProvider).then(serverInfo => { |
| 41 | + if (this._successCallback) { |
| 42 | + this._successCallback(serverInfo); |
| 43 | + } |
| 44 | + }).catch(ignoredError => { |
| 45 | + }); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * @private |
| 51 | + * @param {ConnectionProvider} connectionProvider the provider to obtain connections from. |
| 52 | + * @return {Promise<object>} promise resolved with server info or rejected with error. |
| 53 | + */ |
| 54 | +function acquireAndReleaseDummyConnection(connectionProvider) { |
| 55 | + const connectionHolder = new ConnectionHolder(READ, connectionProvider); |
| 56 | + connectionHolder.initializeConnection(); |
| 57 | + const dummyObserver = new StreamObserver(); |
| 58 | + const connectionPromise = connectionHolder.getConnection(dummyObserver); |
| 59 | + |
| 60 | + return connectionPromise.then(connection => { |
| 61 | + // able to establish a connection |
| 62 | + return connectionHolder.close().then(() => connection.server); |
| 63 | + }).catch(error => { |
| 64 | + // failed to establish a connection |
| 65 | + return connectionHolder.close().catch(ignoredError => { |
| 66 | + // ignore connection release error |
| 67 | + }).then(() => { |
| 68 | + return Promise.reject(error); |
| 69 | + }); |
| 70 | + }); |
| 71 | +} |
0 commit comments