|
9 | 9 |
|
10 | 10 | import java.io.IOException;
|
11 | 11 | import java.util.Collections;
|
| 12 | +import java.util.List; |
12 | 13 |
|
13 | 14 | public class DatabaseClient {
|
14 | 15 |
|
15 |
| - private String dbName; |
16 |
| - private String host; |
17 |
| - private int port; |
18 |
| - private MongoCredential credential; |
| 16 | + private List<ServerAddress> hosts; |
19 | 17 |
|
20 |
| - private MongoClient mongoClient; |
21 |
| - private MongoDatabase mongoDatabase; |
| 18 | + private String dbName; |
| 19 | + private String host; |
| 20 | + private int port; |
| 21 | + private MongoCredential credential; |
22 | 22 |
|
23 |
| - public DatabaseClient(String dbName, String host, int port, String user, char[] pass, String authDatabase) { |
24 |
| - this.dbName = dbName; |
25 |
| - this.host = host; |
26 |
| - this.port = port; |
| 23 | + private MongoClient mongoClient; |
| 24 | + private MongoDatabase mongoDatabase; |
| 25 | + |
| 26 | + public DatabaseClient(String dbName, List<ServerAddress> hosts, String user, char[] pass, String authDatabase) { |
| 27 | + this.dbName = dbName; |
| 28 | + this.hosts = hosts; |
27 | 29 | this.credential = MongoCredential.createScramSha1Credential(user, authDatabase, pass);
|
28 |
| - } |
| 30 | + } |
| 31 | + |
| 32 | + public DatabaseClient(String dbName, String host, int port, String user, char[] pass, String authDatabase) { |
| 33 | + this.dbName = dbName; |
| 34 | + this.host = host; |
| 35 | + this.port = port; |
| 36 | + this.credential = MongoCredential.createScramSha1Credential(user, authDatabase, pass); |
| 37 | + } |
29 | 38 |
|
30 |
| - public int collectionCount() { |
31 |
| - int c = 0; |
32 |
| - for (String ignored : db().listCollectionNames()) { |
33 |
| - c++; |
34 |
| - } |
35 |
| - return c; |
36 |
| - } |
| 39 | + public int collectionCount() { |
| 40 | + int c = 0; |
| 41 | + for (String ignored : db().listCollectionNames()) { |
| 42 | + c++; |
| 43 | + } |
| 44 | + return c; |
| 45 | + } |
37 | 46 |
|
38 |
| - public ServerAddress connect(int timeout) throws IOException { |
39 |
| - if (mongoClient == null) { |
40 |
| - System.out.println("Connecting to MongoDB " + this.host + ":" + this.port + "..."); |
41 |
| - mongoClient = new MongoClient(new ServerAddress(this.host, this.port), Collections.singletonList(this.credential), MongoClientOptions.builder().connectTimeout(timeout).build()); |
42 |
| - } |
43 |
| - return mongoClient.getAddress(); |
44 |
| - } |
| 47 | + public ServerAddress connect(int timeout) throws IOException { |
| 48 | + if (mongoClient == null) { |
| 49 | + if (hosts != null && !hosts.isEmpty()) { |
| 50 | + System.out.println("Connecting to MongoDB..."); |
| 51 | + mongoClient = new MongoClient(this.hosts, this.credential, MongoClientOptions.builder().connectTimeout(timeout).build()); |
| 52 | + } else { |
| 53 | + System.out.println("Connecting to MongoDB " + this.host + ":" + this.port + "..."); |
| 54 | + mongoClient = new MongoClient(new ServerAddress(this.host, this.port), Collections.singletonList(this.credential), MongoClientOptions.builder().connectTimeout(timeout).build()); |
| 55 | + } |
| 56 | + } |
| 57 | + return mongoClient.getAddress(); |
| 58 | + } |
45 | 59 |
|
46 |
| - public void disconnect() throws IOException { |
47 |
| - if (mongoClient != null) { |
48 |
| - mongoClient.close(); |
49 |
| - } |
50 |
| - } |
| 60 | + public void disconnect() throws IOException { |
| 61 | + if (mongoClient != null) { |
| 62 | + mongoClient.close(); |
| 63 | + } |
| 64 | + } |
51 | 65 |
|
52 |
| - public MongoDatabase db() { |
53 |
| - if (mongoDatabase == null) { |
54 |
| - System.out.println("Initializing database '" + dbName + "'"); |
55 |
| - mongoDatabase = mongoClient.getDatabase(dbName); |
56 |
| - } |
57 |
| - return mongoDatabase; |
58 |
| - } |
| 66 | + public MongoDatabase db() { |
| 67 | + if (mongoDatabase == null) { |
| 68 | + System.out.println("Initializing database '" + dbName + "'"); |
| 69 | + mongoDatabase = mongoClient.getDatabase(dbName); |
| 70 | + } |
| 71 | + return mongoDatabase; |
| 72 | + } |
59 | 73 |
|
60 |
| - public Jongo jongo() { |
61 |
| - return new Jongo(mongoClient.getDB(dbName)); |
62 |
| - } |
| 74 | + public Jongo jongo() { |
| 75 | + return new Jongo(mongoClient.getDB(dbName)); |
| 76 | + } |
63 | 77 |
|
64 | 78 | }
|
0 commit comments