Package com.mongodb

Examples of com.mongodb.MongoURI


  @Test
  public void mongoDbFactoryWithCredentials() throws Exception {
    String user = "user";
    char[] pass = {'p', 'a', 's', 's' };
    String database = "mydb";
    MongoURI uri = createMock(MongoURI.class);
    expect(uri.getUsername()).andReturn(user);
    expect(uri.getPassword()).andReturn(pass);
    expect(uri.getDatabase()).andReturn(database);

    Mongo mongo = createMock(Mongo.class);

    UserCredentials credentials =
        new UserCredentials(user, String.valueOf(pass));
View Full Code Here


   */
  @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 = MongoURI.MONGODB_PREFIX + noUserUri.substring(atSign + 1);
    }
View Full Code Here

        properties = new Properties();
        InputStream is = MongoDbConversionsTest.class.getResourceAsStream("/mongodb.test.properties");
        properties.load(is);
        // ping Mongo and populate db and collection
        try {
            mongo = new Mongo(new MongoURI(properties.getProperty("mongodb.connectionURI")));
            mongo.getDatabaseNames();
            dbName = properties.getProperty("mongodb.testDb");
            db = mongo.getDB(dbName);
        } catch (Exception e) {
            Assume.assumeNoException(e);
View Full Code Here

* @date 2012-12-28
*/
public class MongoURITest {
  @Test
  public void testMongoURI(){
    MongoURI uri = new MongoURI("mongodb://172.16.3.82:27017,172.16.3.37:27017/monitor_test?slaveOk=true");
    Assert.assertTrue(uri.getOptions().slaveOk);
   
  }
View Full Code Here

            Mongo mongo;
            if (MONGO_MAP.containsKey(mongoUri)) {
                mongo = MONGO_MAP.get(mongoUri);

            } else {
                mongo = new Mongo(new MongoURI(mongoUri));
                MONGO_MAP.put(mongoUri, mongo);

            }

            MongoURI uri = new MongoURI(mongoUri);
            return new MongoTemplate(new SimpleMongoDbFactory(mongo, uri.getDatabase(),
                    new UserCredentials(uri.getUsername(), parseChars(uri.getPassword()))));

        } catch (Exception e) {
            logger.error("mongo db error ,uri={}", mongoUri, e);
            return null;
        }
View Full Code Here

            JvmMonitor.getInstance();
    }
    /** mongodb初始化*/
    private void initMongodb() throws MongoException, UnknownHostException{
       if (mongo != nullclose();
         MongoURI uri = new MongoURI(mongoURI);
         mongo = new Mongo(uri);
         setCollection(mongo.getDB(uri.getDatabase()).getCollection(collectionName));
    }
View Full Code Here

        properties = new Properties();
        InputStream is = MongoDbConversionsTest.class.getResourceAsStream("/mongodb.test.properties");
        properties.load(is);
        // ping Mongo and populate db and collection
        try {
            mongo = new Mongo(new MongoURI(properties.getProperty("mongodb.connectionURI")));
            mongo.getDatabaseNames();
            dbName = properties.getProperty("mongodb.testDb");
            db = mongo.getDB(dbName);
        } catch (Exception e) {
            Assume.assumeNoException(e);
View Full Code Here

 
  /**
   * 使用MongoURI创建复杂的单个连接,以支持RepSet
   */
  public MongoConnector(String mongoURI) throws UnknownHostException, MongoException {
    this.mongo = new Mongo(new MongoURI(mongoURI));
  }
View Full Code Here

  /**
   * 使用MongoURI创建复杂的单个连接,以支持RepSet
   */
  public MongoConnector(String mongoURI, boolean slaveOK) throws UnknownHostException, MongoException {
    this.mongo = new Mongo(new MongoURI(mongoURI));
    //@see http://api.mongodb.org/java/current/com/mongodb/Mongo.html#slaveOk()
    //slaveOk已经被废弃,但名字很好听
    if (slaveOK)
      this.mongo.setReadPreference(ReadPreference.SECONDARY);
  }
View Full Code Here

TOP

Related Classes of com.mongodb.MongoURI

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.