Advertisement
Guest User

Untitled

a guest
May 13th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. @Getter private final String host, databaseName;
  2. @Getter private final int port;
  3. @Getter private String userName, password;
  4.  
  5. private MongoClient mongoClient;
  6. private MongoDatabase mongoDatabase;
  7.  
  8. private ExecutorService executorService;
  9.  
  10. public MongoDBManager(String host, String databaseName, int port) {
  11. this.host = host;
  12. this.port = port;
  13. this.databaseName = databaseName;
  14. this.executorService = Executors.newSingleThreadScheduledExecutor();
  15. }
  16.  
  17. public MongoDBManager(String host, String databaseName, int port, String userName, String password) {
  18. this.host = host;
  19. this.port = port;
  20. this.userName = userName;
  21. this.password = password;
  22. this.databaseName = databaseName;
  23. this.executorService = Executors.newSingleThreadScheduledExecutor();
  24. }
  25.  
  26. public void connect(Consumer<Boolean> callback) {
  27. ServerAddress address = new ServerAddress(this.host, this.port);
  28.  
  29. if (this.userName == null && this.password == null) {
  30. this.mongoClient = MongoClients.create(MongoClientSettings.builder()
  31. .applyToClusterSettings(builder -> builder.hosts(Collections.singletonList(address)))
  32. .build());
  33. System.out.println("NO AUTH!");
  34. } else {
  35. this.mongoClient = MongoClients.create(MongoClientSettings.builder()
  36. .applyToClusterSettings(builder -> builder.hosts(Collections.singletonList(address)))
  37. .credential(MongoCredential.createScramSha256Credential(this.userName, this.databaseName, this.password.toCharArray()))
  38. .build());
  39. System.out.println("AUTH!");
  40. }
  41.  
  42. this.mongoDatabase = this.mongoClient.getDatabase(this.databaseName);
  43. callback.accept(this.testConnection());
  44. }
  45.  
  46. public Document findDocumentSync(MongoCollection<Document> collection, Bson filter) {
  47. return collection.find(filter).first();
  48. }
  49.  
  50. public void findDocumentAsync(MongoCollection<Document> collection, Bson filter, Consumer<Document> callback) {
  51. this.runAsync(() -> callback.accept(this.findDocumentSync(collection, filter)));
  52. }
  53.  
  54. public void insertDocumentSync(MongoCollection<Document> collection, Document document) {
  55. collection.insertOne(document);
  56. }
  57.  
  58. public void insertDocumentAsync(MongoCollection<Document> collection, Document document) {
  59. this.runAsync(() -> this.insertDocumentSync(collection, document));
  60. }
  61.  
  62. public DeleteResult deleteDocumentSync(MongoCollection<Document> collection, Bson filter) {
  63. return collection.deleteOne(filter);
  64. }
  65.  
  66. public void deleteDocumentAsync(MongoCollection<Document> collection, Bson filter, Consumer<DeleteResult> callback) {
  67. this.runAsync(() -> callback.accept(this.deleteDocumentSync(collection, filter)));
  68. }
  69.  
  70. private boolean testConnection() {
  71. try {
  72. this.insertDocumentSync(this.mongoDatabase.getCollection("connectionTest"), new Document("connectionTest", "This is a connection test due to Mongo aint checking success of connection"));
  73. this.deleteDocumentSync(this.mongoDatabase.getCollection("connectionTest"), Filters.eq("connectionTest"));
  74. return true;
  75. } catch (MongoException exception) {
  76. exception.printStackTrace();
  77. return false;
  78. }
  79. }
  80.  
  81. private void runAsync(Runnable runnable) {
  82. this.executorService.execute(runnable);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement