Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Getter private final String host, databaseName;
- @Getter private final int port;
- @Getter private String userName, password;
- private MongoClient mongoClient;
- private MongoDatabase mongoDatabase;
- private ExecutorService executorService;
- public MongoDBManager(String host, String databaseName, int port) {
- this.host = host;
- this.port = port;
- this.databaseName = databaseName;
- this.executorService = Executors.newSingleThreadScheduledExecutor();
- }
- public MongoDBManager(String host, String databaseName, int port, String userName, String password) {
- this.host = host;
- this.port = port;
- this.userName = userName;
- this.password = password;
- this.databaseName = databaseName;
- this.executorService = Executors.newSingleThreadScheduledExecutor();
- }
- public void connect(Consumer<Boolean> callback) {
- ServerAddress address = new ServerAddress(this.host, this.port);
- if (this.userName == null && this.password == null) {
- this.mongoClient = MongoClients.create(MongoClientSettings.builder()
- .applyToClusterSettings(builder -> builder.hosts(Collections.singletonList(address)))
- .build());
- System.out.println("NO AUTH!");
- } else {
- this.mongoClient = MongoClients.create(MongoClientSettings.builder()
- .applyToClusterSettings(builder -> builder.hosts(Collections.singletonList(address)))
- .credential(MongoCredential.createScramSha256Credential(this.userName, this.databaseName, this.password.toCharArray()))
- .build());
- System.out.println("AUTH!");
- }
- this.mongoDatabase = this.mongoClient.getDatabase(this.databaseName);
- callback.accept(this.testConnection());
- }
- public Document findDocumentSync(MongoCollection<Document> collection, Bson filter) {
- return collection.find(filter).first();
- }
- public void findDocumentAsync(MongoCollection<Document> collection, Bson filter, Consumer<Document> callback) {
- this.runAsync(() -> callback.accept(this.findDocumentSync(collection, filter)));
- }
- public void insertDocumentSync(MongoCollection<Document> collection, Document document) {
- collection.insertOne(document);
- }
- public void insertDocumentAsync(MongoCollection<Document> collection, Document document) {
- this.runAsync(() -> this.insertDocumentSync(collection, document));
- }
- public DeleteResult deleteDocumentSync(MongoCollection<Document> collection, Bson filter) {
- return collection.deleteOne(filter);
- }
- public void deleteDocumentAsync(MongoCollection<Document> collection, Bson filter, Consumer<DeleteResult> callback) {
- this.runAsync(() -> callback.accept(this.deleteDocumentSync(collection, filter)));
- }
- private boolean testConnection() {
- try {
- this.insertDocumentSync(this.mongoDatabase.getCollection("connectionTest"), new Document("connectionTest", "This is a connection test due to Mongo aint checking success of connection"));
- this.deleteDocumentSync(this.mongoDatabase.getCollection("connectionTest"), Filters.eq("connectionTest"));
- return true;
- } catch (MongoException exception) {
- exception.printStackTrace();
- return false;
- }
- }
- private void runAsync(Runnable runnable) {
- this.executorService.execute(runnable);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement