All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.d0x7.utils.mongodb.MongoDB Maven / Gradle / Ivy

The newest version!
package com.d0x7.utils.mongodb;

import com.d0x7.utils.config.ConfigUtils;
import com.d0x7.utils.database.Database;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.FindOneAndUpdateOptions;
import com.mongodb.client.model.ReturnDocument;
import lombok.Getter;
import org.bson.Document;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Copyright (c) 2015-2017 d0x7.com
 * 

* Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: *

* The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. *

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ public class MongoDB { @Getter private static MongoClient mongoClient; @Getter private static MongoDatabase database; @Getter private static boolean initialized = false; public static ListenableFuture setup() { if (ConfigUtils.get(MongoDBConfiguration.class) == null) try { ConfigUtils.loadConfigurationFile(new File(System.getProperty("user.home"), "mongodb.database.json"), MongoDBConfiguration.class); } catch (InvocationTargetException | IOException | NoSuchMethodException | IllegalAccessException | InstantiationException e) { return Futures.immediateFailedFuture(e); } return setup(ConfigUtils.get(MongoDBConfiguration.class)); } public static ListenableFuture setup(MongoDBConfiguration mongoDBConfiguration) { if (initialized) return Futures.immediateFuture(true); initialized = true; return Database.getExecutorService().submit(() -> { disableLogger(); if (mongoDBConfiguration.getUsername() == null || mongoDBConfiguration.getUsername().isEmpty() || mongoDBConfiguration.getPassword() == null || mongoDBConfiguration.getPassword().isEmpty()) mongoClient = new MongoClient(mongoDBConfiguration.getServerAddresses(), MongoClientOptions.builder().serverSelectionTimeout(3000).connectTimeout(3000).build()); else mongoClient = new MongoClient(mongoDBConfiguration.getServerAddresses(), Lists.newArrayList(MongoCredential.createCredential(mongoDBConfiguration.getUsername(), mongoDBConfiguration.getDatabase(), mongoDBConfiguration.getPassword().toCharArray())), MongoClientOptions.builder().serverSelectionTimeout(3000).connectTimeout(3000).build()); disableLogger(); database = mongoClient.getDatabase(mongoDBConfiguration.getDatabase()); return database.runCommand(new BasicDBObject("ping", 1)) != null; }); } public static void shutdown() { mongoClient.close(); } public static boolean isConnected() { return database.runCommand(new BasicDBObject("ping", 1)) != null; } public static void disableLogger() { setLoggerLevel(Level.OFF); } public static void setLoggerLevel(Level level) { Logger.getLogger("org.mongodb.driver.connection").setLevel(level); Logger.getLogger("org.mongodb.driver.management").setLevel(level); Logger.getLogger("org.mongodb.driver.cluster").setLevel(level); Logger.getLogger("org.mongodb.driver.protocol.insert").setLevel(level); Logger.getLogger("org.mongodb.driver.protocol.query").setLevel(level); Logger.getLogger("org.mongodb.driver.protocol.update").setLevel(level); } public static Level getLoggerLevel() { return Logger.getLogger("org.mongodb.driver.connection").getLevel(); } public static int getNextSequence(String name) { return getDatabase().getCollection("counters").findOneAndUpdate(new Document("_id", name), new Document("$inc", new Document("seq", 1)), new FindOneAndUpdateOptions().upsert(true).returnDocument(ReturnDocument.AFTER)).getInteger("seq"); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy