Guest User

Untitled

a guest
Feb 7th, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. public class CommonModule extends AbstractModule {
  2.  
  3.     private final Config config;
  4.  
  5.     public CommonModule(Config config) {
  6.         this.config = config;
  7.     }
  8.  
  9.     @Override
  10.     protected void configure() {
  11.         this.bind(Morphia.class).toProvider(MorphiaProvider.class).asEagerSingleton();
  12.     }
  13.  
  14.     @Provides
  15.     public MongoClient provideMongoClient() {
  16.         ServerAddress serverAddress = new ServerAddress(this.config.databaseHost(), this.config.databasePort());
  17.  
  18.         MongoCredential credential = MongoCredential.createCredential(this.config.databaseUser(), this.config.databaseName(), this.config.databasePassword().toCharArray());
  19.  
  20.         return new MongoClient(serverAddress, Collections.singletonList(credential));
  21.     }
  22.  
  23.     @Provides
  24.     @Named(Constants.DATASTORE)
  25.     public Datastore provideDatastore(MongoClient mongoClient, Morphia morphia) {
  26.         Datastore datastore = morphia.createDatastore(mongoClient, this.config.databaseName());
  27.         datastore.ensureIndexes();
  28.  
  29.         return datastore;
  30.     }
  31.  
  32.     @Provides
  33.     public Config provideConfig() {
  34.         return this.config;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment