Created
          April 19, 2011 15:00 
        
      - 
      
- 
        Save mattupstate/928270 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package net.nobien.cloudfoundry.example.config.database | |
| import java.net.UnknownHostException; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import org.springframework.beans.factory.annotation.Value; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.Configuration; | |
| import com.google.code.morphia.AdvancedDatastore; | |
| import com.google.code.morphia.Morphia; | |
| import com.mongodb.Mongo; | |
| import com.mongodb.MongoException; | |
| @Configuration | |
| public class DataAccessConfig { | |
| private static final Logger logger = LoggerFactory.getLogger(DataAccessConfig.class); | |
| @Value("#{environment['nobien-example-mongodb.db']}") | |
| private String db; | |
| @Value("#{environment['nobien-example-mongodb.hostname']}") | |
| private String host; | |
| @Value("#{environment['nobien-example-mongodb.port']}") | |
| private int port; | |
| @Value("#{environment['nobien-example-mongodb.username']}") | |
| private String username; | |
| @Value("#{environment['nobien-example-mongodb.password']}") | |
| private String password; | |
| @Bean | |
| public Mongo mongo() throws UnknownHostException, MongoException { | |
| logger.debug("Creating Mongo Bean"); | |
| return new Mongo(host, port); | |
| } | |
| @Bean | |
| public Morphia morphia() { | |
| logger.debug("Creating Morphia Bean"); | |
| Morphia m = new Morphia(); | |
| return m; | |
| } | |
| @Bean | |
| public AdvancedDatastore datastore(Morphia morphia, Mongo mongo) { | |
| logger.debug("Creating Datastore Bean"); | |
| String u = (username.length() > 0) ? username : null; | |
| char[] p = (password.length() > 0) ? password.toCharArray() : null; | |
| return (AdvancedDatastore)morphia.createDatastore(mongo, db, u, p); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment