/** * Initialize a connection to the MongoDB server * * @param server the server url * @return the MongoDB client */ public static MongoClient newMongoClient(String server) { MongoDBConnectionConfig config = new MongoDBConnectionConfig(); config.server = server; return newMongoClient(config); }
/** * @return all configured databases */ @Override public Iterable<MongoDatabase> getDatabases() { return () -> clients.entrySet().stream().map(e -> { MongoDBConnectionConfig c = getDescriptor(XP_CONNECTION, e.getKey()); return MongoDBConnectionHelper.getDatabase(e.getValue(), c.dbname); }).iterator(); }
/** * Checks if the MongoDB server has the collection. * * @param collection the collection name * @return true if the server has the collection, false otherwise */ protected boolean hasCollection(String collection) { return MongoDBConnectionHelper.hasCollection(database, collection); }
protected static SSLContext getSSLContext(MongoDBConnectionConfig config) { try { KeyStore trustStore = loadKeyStore(config.trustStorePath, config.trustStorePassword, config.trustStoreType); KeyStore keyStore = loadKeyStore(config.keyStorePath, config.keyStorePassword, config.keyStoreType); if (trustStore == null && keyStore == null) { return null; } SSLContextBuilder sslContextBuilder = SSLContexts.custom(); if (trustStore != null) { sslContextBuilder.loadTrustMaterial(trustStore, null); } if (keyStore != null) { sslContextBuilder.loadKeyMaterial(keyStore, null); } return sslContextBuilder.build(); } catch (GeneralSecurityException | IOException e) { throw new RuntimeException("Cannot setup SSL context: " + config, e); } }
.socketTimeout(MONGODB_OPTION_SOCKET_TIMEOUT_MS) .description("Nuxeo"); SSLContext sslContext = getSSLContext(config); if (sslContext == null) { if (config.ssl != null) {
@Override public void start(ComponentContext context) { super.start(context); Collection<MongoDBConnectionConfig> confs = getDescriptors(XP_CONNECTION); confs.forEach(c -> { log.debug("Initializing MongoClient with id={}", c::getId); clients.put(c.getId(), MongoDBConnectionHelper.newMongoClient(c)); }); }
/** * @param id the connection id to retrieve. * @return the database configured by {@link MongoDBConnectionConfig} for the input id, or the default one if it * doesn't exist */ @Override public MongoDatabase getDatabase(String id) { MongoDBConnectionConfig config = getDescriptor(XP_CONNECTION, id); MongoClient client = clients.get(id); if (client == null) { config = getDescriptor(XP_CONNECTION, DEFAULT_CONNECTION_ID); client = clients.get(DEFAULT_CONNECTION_ID); } return MongoDBConnectionHelper.getDatabase(client, config.dbname); }
protected void initMongoDB() { String mongoDBServer = defaultProperty(MONGODB_SERVER_PROPERTY, DEFAULT_MONGODB_SERVER); String mongoDBDbName = defaultProperty(MONGODB_DBNAME_PROPERTY, DEFAULT_MONGODB_DBNAME); try (MongoClient mongoClient = MongoDBConnectionHelper.newMongoClient(mongoDBServer)) { MongoDatabase database = mongoClient.getDatabase(mongoDBDbName); database.drop(); } }