/**
   * Method invoked when a {@link MongoSession} needs to be created.
   *
   * @param username the username to use for authentication. NOTE: Please use a dummy user if you
   *     have disabled Mongo authentication
   * @param password the password to use for authentication. NOTE: Please use a dummy password if
   *     you have disabled Mongo authentication
   * @param database Name of the database
   * @return the newly created {@link MongoSession}
   * @throws org.mule.api.ConnectionException
   */
  @Connect
  public void connect(
      @ConnectionKey String username,
      @Password String password,
      @Optional @Default("test") String database)
      throws ConnectionException {
    DB db = null;
    try {
      MongoOptions options = new MongoOptions();

      if (connectionsPerHost != null) {
        options.connectionsPerHost = connectionsPerHost;
      }
      if (threadsAllowedToBlockForConnectionMultiplier != null) {
        options.threadsAllowedToBlockForConnectionMultiplier =
            threadsAllowedToBlockForConnectionMultiplier;
      }
      if (maxWaitTime != null) {
        options.maxWaitTime = maxWaitTime;
      }
      if (connectTimeout != null) {
        options.connectTimeout = connectTimeout;
      }
      if (socketTimeout != null) {
        options.socketTimeout = socketTimeout;
      }
      if (autoConnectRetry != null) {
        options.autoConnectRetry = autoConnectRetry;
      }
      if (slaveOk != null) {
        options.slaveOk = slaveOk;
      }
      if (safe != null) {
        options.safe = safe;
      }
      if (w != null) {
        options.w = w;
      }
      if (wtimeout != null) {
        options.wtimeout = wtimeout;
      }
      if (fsync != null) {
        options.fsync = fsync;
      }
      if (database != null) {
        this.database = database;
      }

      mongo = getOrCreateMongoInstance(host, port, options);
      db = getDatabase(mongo, username, password, database);
    } catch (MongoException me) {
      throw new ConnectionException(ConnectionExceptionCode.UNKNOWN, null, me.getMessage());
    } catch (UnknownHostException e) {
      throw new ConnectionException(ConnectionExceptionCode.UNKNOWN_HOST, null, e.getMessage());
    }
    this.client = new MongoClientImpl(db);
  }
  private void init(String dbPrefix) {
    try {
      mongoReplicaProps = new ArrayList<ServerAddress>();
      // mongoReplicaProps.add(new ServerAddress("mongodb1.qa.sg1.hike.in", 27017));
      mongoReplicaProps.add(new ServerAddress("10.0.1.141", 27017));
      mongoReplicaProps.add(new ServerAddress("10.0.1.141", 27017));
      MongoOptions options = new MongoOptions();
      options.autoConnectRetry = true;
      options.readPreference = ReadPreference.primaryPreferred();
      options.setThreadsAllowedToBlockForConnectionMultiplier(50);
      options.setConnectionsPerHost(100);
      options.slaveOk = true;
      mongo = new Mongo(mongoReplicaProps, options);
      userDB = mongo.getDB("userdb");

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  @SuppressWarnings("deprecation")
  @Test
  public void testApplyOptions() throws UnknownHostException {
    MongoOptions options = new MongoOptions();

    // test defaults
    Mongo m = new Mongo("localhost", options);
    assertEquals(ReadPreference.primary(), m.getReadPreference());
    assertEquals(WriteConcern.NORMAL, m.getWriteConcern());
    assertEquals(0, m.getOptions() & Bytes.QUERYOPTION_SLAVEOK);
    m.close();

    // test setting options
    options.setReadPreference(ReadPreference.nearest());
    options.slaveOk = true;
    options.safe = true;

    m = new Mongo("localhost", options);
    assertEquals(ReadPreference.nearest(), m.getReadPreference());
    assertEquals(WriteConcern.SAFE, m.getWriteConcern());
    assertEquals(Bytes.QUERYOPTION_SLAVEOK, m.getOptions() & Bytes.QUERYOPTION_SLAVEOK);
    m.close();
  }
  @Test
  @SuppressWarnings("deprecation")
  public void testTurnOffSlaveOk() throws MongoException, UnknownHostException {
    MongoOptions mongoOptions = new MongoOptions();

    mongoOptions.slaveOk = true;

    Mongo mongo = new Mongo("localhost", mongoOptions);
    try {
      mongo.addOption(Bytes.QUERYOPTION_PARTIAL);
      mongo.addOption(Bytes.QUERYOPTION_AWAITDATA);

      int isSlaveOk = mongo.getOptions() & Bytes.QUERYOPTION_SLAVEOK;

      assertEquals(Bytes.QUERYOPTION_SLAVEOK, isSlaveOk);

      mongo.setOptions(mongo.getOptions() & (~Bytes.QUERYOPTION_SLAVEOK));

      assertEquals(Bytes.QUERYOPTION_AWAITDATA | Bytes.QUERYOPTION_PARTIAL, mongo.getOptions());
    } finally {
      mongo.close();
    }
  }