org.graylog2.database.MongoCollections Maven / Gradle / Ivy
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package org.graylog2.database;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.MongoCollection;
import org.bson.UuidRepresentation;
import org.graylog.shaded.mongojack4.org.mongojack.JacksonMongoCollection;
import org.graylog.shaded.mongojack4.org.mongojack.internal.MongoJackModule;
import org.graylog2.bindings.providers.CommonMongoJackObjectMapperProvider;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class MongoCollections {
private final ObjectMapper objectMapper;
private final MongoConnection mongoConnection;
@Inject
public MongoCollections(CommonMongoJackObjectMapperProvider objectMapperProvider, MongoConnection mongoConnection) {
this.objectMapper = objectMapperProvider.get();
this.mongoConnection = mongoConnection;
MongoJackModule.configure(this.objectMapper);
}
/**
* Get a MongoCollection configured to use Mongojack for serialization/deserialization of objects.
*
*
* To encourage usage of the mongodb driver API, we are intentionally not returning a {@link JacksonMongoCollection}
* but rather the generic {@link MongoCollection} interface here.
*
*/
public MongoCollection get(String collectionName, Class valueType) {
return JacksonMongoCollection.builder()
.withObjectMapper(objectMapper)
.build(mongoConnection.getMongoDatabase(), collectionName, valueType, UuidRepresentation.UNSPECIFIED);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy