@Override public CompletableFuture<SecurityDefinitionStore> open() { List<ServerAddress> hostList = Arrays.stream(hosts).map(h -> new ServerAddress(h)).collect(Collectors.toList()); ClusterSettings clusterSettings = ClusterSettings.builder().hosts(hostList).build(); MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).build(); mongoClient = MongoClients.create(settings); database = mongoClient.getDatabase(DATABASE_NAME); collection = database.getCollection(SECDEF_COLLECTION_NAME); // In the case of MongoDB, open is synchronous because it doesn't // actually communicate with the server until a query is invoked. return CompletableFuture.completedFuture(this); }
/** * DataWriter constructor * * @throws IllegalStateException if database configuration is not set */ private DatabaseWriter() { try { final MongoClient mongoClient = MongoClients.create(PROPERTIES_MANAGER.getProperty("database.host")); final MongoDatabase mongoDatabase = mongoClient.getDatabase(PROPERTIES_MANAGER.getProperty("database.datasource")); this.mongoCollection = mongoDatabase.getCollection(PROPERTIES_MANAGER.getProperty("database.collection")); } catch (IllegalArgumentException e) { LOGGER.error(e.getMessage()); throw new IllegalStateException(e.getMessage()); } LOGGER.info("DatabaseWriter has been instantiate"); }
/** * Get or create the MongoDB client * @return the MongoDB client */ private MongoClient getMongoClient() { if (mongoClient == null) { mongoClient = MongoClients.create(connectionString); } return mongoClient; }
/** * Create an asynchronous MongoDB client * @return the client */ private com.mongodb.async.client.MongoClient createAsyncClient() { ClusterSettings clusterSettings = ClusterSettings.builder() .hosts(Arrays.asList(mongoConnector.serverAddress)) .build(); MongoClientSettings settings = MongoClientSettings.builder() .clusterSettings(clusterSettings).build(); return MongoClients.create(settings); }
/** * * @param mongi */ public QueryAsync(Mongi mongi){ this.mongi = mongi; mongiDb = mongi.getMongoDatabase(); mongoClient = mongi.getMongoClient(); // Use a Connection String mongoClientAsyn = MongoClients.create("mongodb://localhost"); database = mongoClientAsyn.getDatabase("testingMe"); coll = database.getCollection("test"); }
/** * {@inheritDoc} */ @Override public void onEnable() { saveDefaultConfig(); CodecRegistry codecRegistry = createCodecRegistry(); MongoClientSettings settings = MongoClientSettings.builder() .clusterSettings(ClusterSettings.builder().applyConnectionString(new ConnectionString(getConfig().getString("mongodb.uri"))).build()) .codecRegistry(codecRegistry) .build(); client = MongoClients.create(settings); MongoDatabase database = client.getDatabase(getConfig().getString("mongodb.database")); if (!testConnection(database)) return; RegionStorageAdapter storageAdapter = new RegionStorageAdapter(database); MongoRegionDriver driver = new MongoRegionDriver(getServer(), storageAdapter); WorldGuardPlugin wgPlugin = WorldGuardPlugin.inst(); if (getConfig().getBoolean("mongodb.use_oplog")) { getLogger().info("OpLog usage enabled."); WorldGuardOpLogHandler opLogHandler = new WorldGuardOpLogHandler(codecRegistry.get(ProcessingProtectedRegion.class), storageAdapter, wgPlugin); getServer().getScheduler().runTaskAsynchronously(this, new OpLogRetriever( OpLogUtils.getCollection(client), new OpLogParser(opLogHandler), getConfig().getString("mongodb.database") + "." + RegionStorageAdapter.COLLECTION_NAME )); storageAdapter.setListener(opLogHandler); } ConfigurationManager config = wgPlugin.getGlobalStateManager(); RegionContainer container = wgPlugin.getRegionContainer(); InjectionUtils.injectRegionDriver(container, driver); InjectionUtils.callUnload(container); InjectionUtils.callLoadWorlds(container); config.selectedRegionStoreDriver = driver; }
synchronized com.mongodb.async.client.MongoClient mongo() { if (mongo == null) { MongoClientOptionsParser parser = new MongoClientOptionsParser(config); mongo = MongoClients.create(parser.settings()); db = mongo.getDatabase(parser.database()); } return mongo; }
@Override public void setUp() throws Exception { super.setUp(); JsonObject config = getConfig(); mongoClient = MongoClient.createNonShared(vertx, config); CountDownLatch latch = new CountDownLatch(1); dropCollections(mongoClient, latch); awaitLatch(latch); actualMongo = MongoClients.create("mongodb://localhost:27018"); db = actualMongo.getDatabase(io.vertx.ext.mongo.MongoClient.DEFAULT_DB_NAME); }
@Start public void start() { final ClusterSettings clusterSettings = ClusterSettings.builder().hosts(asList(new ServerAddress(host, port))).build(); final MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).build(); mongoClient = MongoClients.create(settings); db = mongoClient.getDatabase(this.database); launchConsumers(); }
private void connect() throws ActivecheckReporterException { if (mongoClient == null) { logger.debug("Cannot run query. MongoDB is not connected. Trying to (re)connect."); try { // configure credentials List<MongoCredential> credentialsList = new ArrayList<MongoCredential>(); String username = properties .getString("mongodb.username", null); String password = properties .getString("mongodb.password", null); if (username != null && password != null) { credentialsList.add(MongoCredential.createPlainCredential( username, "*", password.toCharArray())); } // configure server addresses List<ServerAddress> addressList = new ArrayList<ServerAddress>(); String socketPath = properties.getString("socket", null); if (socketPath != null) { addressList.add(new ServerAddress(new AFUNIXSocketAddress( new File(socketPath)))); } else { String url = properties.getString("url", ServerAddress.defaultHost()); int port = ServerAddress.defaultPort(); String[] urlParts = url.split(":"); if (urlParts.length > 1) { port = Integer.parseInt(urlParts[1]); } addressList.add(new ServerAddress(urlParts[0], port)); } ServerSelector serverSelector = new ReadPreferenceServerSelector( MONGO_READ_PREFERENCE); ClusterSettings clusterSettings = ClusterSettings.builder() .hosts(addressList).serverSelector(serverSelector) .build(); // actually configure and (re)create mongoClient ConnectionPoolSettings connectionPoolSettings = ConnectionPoolSettings .builder().maxSize(MONGO_POOL_SIZE).build(); MongoClientSettings settings = MongoClientSettings.builder() .readPreference(MONGO_READ_PREFERENCE) .credentialList(credentialsList) .clusterSettings(clusterSettings) .connectionPoolSettings(connectionPoolSettings).build(); mongoClient = MongoClients.create(settings); } catch (Exception e) { mongoClient = null; String errorMessage = "MongodbReporter Configuration Error for service '" + getOverallServiceName() + "': " + e.getMessage(); logger.error(errorMessage); logger.trace(e.getMessage(), e); // set report and status setOverallServiceReport(NagiosServiceStatus.CRITICAL, errorMessage); throw new ActivecheckReporterException(e); } } }