com.thematchbox.db.MongoDBPool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of TheMatchBox-Common Show documentation
Show all versions of TheMatchBox-Common Show documentation
This project contains some common utilities used in different projects from theMatchBox.
package com.thematchbox.db;
/* Copyright 2015 theMatchBox
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
public class MongoDBPool {
private ConcurrentHashMap hostMap = new ConcurrentHashMap<>();
public MongoDBConnection getConnection(DBConnectionInfo connectionInfo) throws MongoDBException {
DBHostInfo hostInfo = connectionInfo.hostInfo;
try {
MongoClient mongo = getMongo(hostInfo, connectionInfo.credentials);
return new MongoDBConnection(mongo.getDB(connectionInfo.databaseName));
} catch (UnknownHostException e) {
throw new MongoDBException(e.getMessage(), e);
}
}
private MongoClient getMongo(DBHostInfo hostInfo) throws UnknownHostException {
return getMongo(hostInfo, null);
}
private MongoClient getMongo(DBHostInfo hostInfo, DBCredentials credentials) throws UnknownHostException {
if (!hostMap.containsKey(hostInfo)) {
MongoClient mongoClient;
if (credentials != null) {
MongoCredential mongoCredential = MongoCredential.createMongoCRCredential(credentials.user, credentials.database, credentials.password.toCharArray());
mongoClient = new MongoClient(new ServerAddress(hostInfo.host, hostInfo.port), Collections.singletonList(mongoCredential));
} else {
mongoClient = new MongoClient(hostInfo.host, hostInfo.port);
}
hostMap.put(hostInfo, mongoClient);
}
return hostMap.get(hostInfo);
}
public List getDatabaseNames(DBHostInfo dbHostInfo) throws MongoDBException {
try {
MongoClient mongo = getMongo(dbHostInfo);
return mongo.getDatabaseNames();
} catch (UnknownHostException e) {
throw new MongoDBException(e.getMessage(), e);
}
}
public void addDatabase(DBHostInfo dbHostInfo, String database) throws MongoDBException {
try {
MongoClient mongo = getMongo(dbHostInfo);
DB db = mongo.getDB(database);
db.getCollectionNames(); // Forces the create database
} catch (UnknownHostException e) {
throw new MongoDBException(e.getMessage(), e);
}
}
public boolean tryConnect(DBHostInfo dbHostInfo) {
try {
MongoClient mongo = getMongo(dbHostInfo);
mongo.getDatabaseNames();
return true;
} catch (Exception e) {
return false;
}
}
}