1 package de.dlr.shepard.mongoDB;
2
3 import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
4 import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;
5
6 import java.util.concurrent.TimeUnit;
7
8 import org.bson.Document;
9 import org.bson.codecs.configuration.CodecRegistry;
10 import org.bson.codecs.pojo.PojoCodecProvider;
11
12 import com.mongodb.ConnectionString;
13 import com.mongodb.MongoClientSettings;
14 import com.mongodb.MongoCredential;
15 import com.mongodb.MongoException;
16 import com.mongodb.client.MongoClient;
17 import com.mongodb.client.MongoClients;
18 import com.mongodb.client.MongoCollection;
19 import com.mongodb.client.MongoDatabase;
20 import com.mongodb.client.gridfs.GridFSBucket;
21 import com.mongodb.client.gridfs.GridFSBuckets;
22
23 import de.dlr.shepard.util.IConnector;
24 import de.dlr.shepard.util.PropertiesHelper;
25
26
27
28
29
30
31 public class MongoDBConnector implements IConnector {
32 private static MongoDBConnector instance;
33 private MongoClient mongoClient;
34 private MongoDatabase database;
35 private CodecRegistry pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
36 fromProviders(PojoCodecProvider.builder().automatic(true).build()));
37
38
39
40
41 private MongoDBConnector() {
42 }
43
44
45
46
47
48
49
50 public static MongoDBConnector getInstance() {
51 if (instance == null) {
52 instance = new MongoDBConnector();
53 }
54 return instance;
55 }
56
57
58
59
60
61
62 @Override
63 public boolean connect() {
64 PropertiesHelper helper = new PropertiesHelper();
65 String username = helper.getProperty("mongo.username");
66 String password = helper.getProperty("mongo.password");
67 String authDB = helper.getProperty("mongo.authDB");
68 String host = helper.getProperty("mongo.host");
69
70 MongoCredential credential = MongoCredential.createCredential(username, authDB, password.toCharArray());
71
72 ConnectionString connectionString = new ConnectionString("mongodb://" + host);
73
74 MongoClientSettings settings = MongoClientSettings.builder().credential(credential)
75 .applyConnectionString(connectionString)
76 .applyToClusterSettings(builder -> builder.serverSelectionTimeout(2, TimeUnit.SECONDS))
77 .codecRegistry(pojoCodecRegistry).build();
78
79 mongoClient = MongoClients.create(settings);
80 database = mongoClient.getDatabase("database");
81 return true;
82 }
83
84 @Override
85 public boolean disconnect() {
86 if (mongoClient != null)
87 mongoClient.close();
88 return true;
89 }
90
91 @Override
92 public boolean alive() {
93 Document result;
94 try {
95 result = database.runCommand(new Document("buildInfo", "1"));
96 } catch (MongoException ex) {
97 return false;
98 }
99 return result.containsKey("ok");
100 }
101
102 public MongoDatabase getDatabase() {
103 if (mongoClient == null)
104 connect();
105 return database;
106 }
107
108 public void createCollection(String name) {
109 this.database.createCollection(name);
110 }
111
112 public MongoCollection<Document> getCollection(String name) {
113 if (mongoClient == null)
114 connect();
115 return database.getCollection(name);
116 }
117
118 public GridFSBucket createBucket() {
119 if (mongoClient == null)
120 connect();
121 return GridFSBuckets.create(database);
122 }
123
124 }