001/* 002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Funsho David 018 * 019 */ 020package org.nuxeo.directory.mongodb; 021 022import java.util.stream.StreamSupport; 023 024import org.apache.commons.lang.StringUtils; 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.bson.Document; 028import org.nuxeo.ecm.core.api.NuxeoException; 029 030import com.mongodb.MongoClient; 031import com.mongodb.MongoClientOptions; 032import com.mongodb.MongoClientURI; 033import com.mongodb.ServerAddress; 034import com.mongodb.client.MongoCollection; 035import com.mongodb.client.MongoDatabase; 036import com.mongodb.client.MongoIterable; 037 038/** 039 * Helper for connection to the MongoDB server 040 * 041 * @since 9.1 042 */ 043public class MongoDBConnectionHelper { 044 045 private static final Log log = LogFactory.getLog(MongoDBConnectionHelper.class); 046 047 private static final String DB_DEFAULT = "nuxeo"; 048 049 private static final int MONGODB_OPTION_CONNECTION_TIMEOUT_MS = 30000; 050 051 private static final int MONGODB_OPTION_SOCKET_TIMEOUT_MS = 60000; 052 053 private MongoDBConnectionHelper() { 054 // Empty 055 } 056 057 /** 058 * Initialize a connection to the MongoDB server 059 * 060 * @param server the server url 061 * @return the MongoDB client 062 */ 063 public static MongoClient newMongoClient(String server) { 064 if (StringUtils.isBlank(server)) { 065 throw new NuxeoException("Missing <server> in MongoDB repository descriptor"); 066 } 067 MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder() 068 // Can help to prevent firewall disconnects 069 // inactive connection, option not available from URI 070 .socketKeepAlive(true) 071 // don't wait for ever by default, 072 // can be overridden using URI options 073 .connectTimeout(MONGODB_OPTION_CONNECTION_TIMEOUT_MS) 074 .socketTimeout(MONGODB_OPTION_SOCKET_TIMEOUT_MS) 075 .description("Nuxeo"); 076 MongoClient client; 077 if (server.startsWith("mongodb://")) { 078 // allow mongodb:// URI syntax for the server, to pass everything in one string 079 client = new MongoClient(new MongoClientURI(server, optionsBuilder)); 080 } else { 081 client = new MongoClient(new ServerAddress(server), optionsBuilder.build()); 082 } 083 if (log.isDebugEnabled()) { 084 log.debug("MongoClient initialized with options: " + client.getMongoClientOptions().toString()); 085 } 086 return client; 087 } 088 089 /** 090 * @return a database representing the specified database 091 */ 092 public static MongoDatabase getDatabase(MongoClient mongoClient, String dbname) { 093 if (StringUtils.isBlank(dbname)) { 094 dbname = DB_DEFAULT; 095 } 096 return mongoClient.getDatabase(dbname); 097 } 098 099 /** 100 * Retrieve a collection from the MongoDB server 101 */ 102 public static MongoCollection<Document> getCollection(MongoClient mongoClient, String dbname, String collection) { 103 MongoDatabase db = getDatabase(mongoClient, dbname); 104 return db.getCollection(collection); 105 } 106 107 /** 108 * Check if the collection exists and if it is not empty 109 * 110 * @param mongoClient the Mongo client 111 * @param dbname the database name 112 * @param collection the collection name 113 * @return true if the collection exists and not empty, false otherwise 114 */ 115 public static boolean hasCollection(MongoClient mongoClient, String dbname, String collection) { 116 MongoIterable<String> collections = getDatabase(mongoClient, dbname).listCollectionNames(); 117 boolean found = StreamSupport.stream(collections.spliterator(), false).anyMatch(collection::equals); 118 return found && getCollection(mongoClient, dbname, collection).count() > 0; 119 } 120}