Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.github.jknack.mwa.mongo;
  
  
  import org.slf4j.Logger;
 

Configure Mongo database resources:

This module let you take full control of a Mongo DB by using POJOs through the Spring Data org.springframework.data.mongodb.core.MongoTemplate.

Note: For Morphia Support use the MorphiaModule.

Author(s):
edgar.espina
See also:
0.1
MorphiaModule
 
 public class MongoModule {

  
The logging system.
 
   private static final Logger logger = LoggerFactory
       .getLogger(MongoModule.class);

  

Publish a com.mongodb.MongoURI by reading the "db" property from the application environment.

Mongo URI format: mongodb://[username:password@]host1[:port1][,host2[:port2],... [,hostN[:portN]]][/[database][?options]]

Parameters:
environment The application environment. Required.
Returns:
A uri.
Throws:
java.net.UnknownHostException If the host connection is rejected.
 
   @Bean
   public MongoURI mongoURI(final Environment environment)
       throws UnknownHostException {
     Validate.notNull(environment"The application environment is required.");
     MongoURI uri = new MongoURI(environment.getRequiredProperty("db"));
     String noUserUri = uri.toString();
     int atSign = noUserUri.indexOf("@");
     if (atSign > 0) {
       // Hide user and pass
       noUserUri = . + noUserUri.substring(atSign + 1);
     }
     .info("Starting {}"noUserUri);
     return uri;
   }

  
Connect to a Mongo database connection using the give uri.

Parameters:
uri The mongo db uri. Required.
Returns:
A Mongo database connection.
Throws:
java.net.UnknownHostException If the host connection is rejected.
 
   @Bean
   public Mongo mongo(final MongoURI uri)
       throws UnknownHostException {
     Validate.notNull(uri"The mongo database uri is required.");
     return uri.connect();
   }

  
Publish a org.springframework.data.mongodb.MongoDbFactory service.

Parameters:
uri The mongo db uri. Required.
mongo The mongo database connection. Required.
Returns:
A org.springframework.data.mongodb.MongoDbFactory service.
  @Bean
  public MongoDbFactory mongoDbFactory(final MongoURI urifinal Mongo mongo) {
    Validate.notNull(uri"The mongo database uri is required.");
    Validate.notNull(mongo"The mongo database connection is required.");
    String username = uri.getUsername();
    char[] password = uri.getPassword();
    if (Strings.isNullOrEmpty(username)) {
      return new SimpleMongoDbFactory(mongouri.getDatabase());
    }
    return new SimpleMongoDbFactory(mongouri.getDatabase(),
        new UserCredentials(usernamepassword == null ? null
            : String.valueOf(password)));
  }

  
Publish a org.springframework.data.mongodb.core.MongoTemplate for POJOs support.

Parameters:
mongoDbFactory The mongo database factory. Required.
Returns:
A org.springframework.data.mongodb.core.MongoTemplate for POJOs support.
  @Bean
  public MongoTemplate mongoTemplate(final MongoDbFactory mongoDbFactory) {
    Validate.notNull(mongoDbFactory"The mongo database factory is required.");
    return new MongoTemplate(mongoDbFactory);
  }

  
Translate com.mongodb.Mongo database exception as Spring database exception if possible.

Returns:
A Spring database exception translator.
  @Bean
    return new MongoExceptionTranslator();
  }
New to GrepCode? Check out our FAQ X