/** * 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); }
/** * 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); }
/** * @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); }
@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)); }); }
/** * Cast an object according to its instance * * @param value the object to transform * @return the BSON object */ public static Object valueToBson(Object value) { if (value instanceof Map) { @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) value; return fieldMapToBson(map); } else if (value instanceof List) { @SuppressWarnings("unchecked") List<Object> values = (List<Object>) value; return listToBson(values); } else if (value instanceof Object[]) { return listToBson(Arrays.asList((Object[]) value)); } else { return serializableToBson(value); } }
@Override public void initialize(String blobProviderId, Map<String, String> properties) throws IOException { super.initialize(blobProviderId, properties); this.properties = properties; if (StringUtils.isNotBlank(properties.get(SERVER_PROPERTY)) || StringUtils.isNotBlank(properties.get(DBNAME_PROPERTY))) { throw new NuxeoException("Unable to initialize GridFS Binary Manager, properties " + SERVER_PROPERTY + " and " + DBNAME_PROPERTY + " has been removed. Please configure a connection!"); } String bucket = properties.get(BUCKET_PROPERTY); if (StringUtils.isBlank(bucket)) { bucket = blobProviderId + ".fs"; } MongoDBConnectionService mongoService = Framework.getService(MongoDBConnectionService.class); MongoDatabase database = mongoService.getDatabase(BLOB_PROVIDER_CONNECTION_PREFIX + blobProviderId); gridFSBucket = GridFSBuckets.create(database, bucket); filesColl = database.getCollection(bucket + ".files"); garbageCollector = new GridFSBinaryGarbageCollector(bucket); }
for (Object o : list) { if (o != null) { klass = scalarToSerializableClass(o.getClass()); break; List<Serializable> l = new ArrayList<>(list.size()); for (Object o : list) { l.add((Serializable) bsonToFieldMap((Document) o)); int i = 0; for (Object o : list) { array[i++] = scalarToSerializable(o); return (Serializable) bsonToFieldMap((Document) value); } else { return scalarToSerializable(value);
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(); } }
/** * Backward compatibility for {@link MongoDBRepositoryDescriptor#server descriptor.server} and * {@link MongoDBRepositoryDescriptor#dbname descriptor.dbname} * * @since 9.3 * @deprecated since 9.3 */ @Deprecated protected void handleConnectionContribution(MongoDBRepositoryDescriptor descriptor, BiConsumer<DefaultComponent, MongoDBConnectionConfig> consumer) { if (StringUtils.isNotBlank(descriptor.server)) { String id = "repository/" + descriptor.name; String server = descriptor.server; String dbName = StringUtils.defaultIfBlank(descriptor.dbname, DB_DEFAULT); MongoDBConnectionConfig connection = new MongoDBConnectionConfig(); connection.server = server; connection.dbname = dbName; connection.id = id; DefaultComponent component = (DefaultComponent) Framework.getRuntime() .getComponent(MongoDBComponent.COMPONENT_NAME); consumer.accept(component, connection); } }
protected static List<Object> listToBson(List<Object> values) { List<Object> objects = new ArrayList<>(values.size()); for (Object value : values) { objects.add(valueToBson(value)); } return objects; }
/** * Create a BSON object with a single field from a pair key/value * * @param key the key which corresponds to the field id in the object * @param value the value which corresponds to the field value in the object * @return the new BSON object */ public static Document fieldMapToBson(String key, Object value) { return fieldMapToBson(Collections.singletonMap(key, value)); }
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); } }
/** * Create a map from a BSON object * * @param doc the BSON object to parse * @return the new map */ public static Map<String, Object> bsonToFieldMap(Document doc) { Map<String, Object> fieldMap = new HashMap<>(); for (String key : doc.keySet()) { if (MONGODB_ID.equals(key)) { // skip native id continue; } fieldMap.put(key, bsonToValue(doc.get(key))); } return fieldMap; }
.socketTimeout(MONGODB_OPTION_SOCKET_TIMEOUT_MS) .description("Nuxeo"); SSLContext sslContext = getSSLContext(config); if (sslContext == null) { if (config.ssl != null) {
public MongoDBRepository(ConnectionManager cm, MongoDBRepositoryDescriptor descriptor) { super(cm, descriptor.name, descriptor); MongoDBConnectionService mongoService = Framework.getService(MongoDBConnectionService.class); // prefix with repository/ to group repository connection MongoDatabase database = mongoService.getDatabase(REPOSITORY_CONNECTION_PREFIX + descriptor.name); coll = database.getCollection(descriptor.name); countersColl = database.getCollection(descriptor.name + ".counters"); if (Boolean.TRUE.equals(descriptor.nativeId)) { idKey = MONGODB_ID; } else { idKey = KEY_ID; } useCustomId = KEY_ID.equals(idKey); if (idType == IdType.sequence || DEBUG_UUIDS) { Integer sbs = descriptor.sequenceBlockSize; sequenceBlockSize = sbs == null ? 1 : sbs.longValue(); sequenceLeft = 0; } converter = new MongoDBConverter(useCustomId ? null : KEY_ID); cursorService = new CursorService<>(ob -> (String) ob.get(converter.keyToBson(KEY_ID))); initRepository(); }
/** * @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(); }
/** * Create a BSON object from a map * * @param fieldMap a map of keys/values * @return the new BSON object */ public static Document fieldMapToBson(Map<String, Object> fieldMap) { Document doc = new Document(); for (Map.Entry<String, Object> entry : fieldMap.entrySet()) { Object val = valueToBson(entry.getValue()); if (val != null) { doc.put(entry.getKey(), val); } } return doc; }
database = mongoService.getDatabase(DIRECTORY_CONNECTION_PREFIX + getName()); collection = database.getCollection(getName()); String countersCollectionName = getName() + ".counters";