Java Code Examples for com.mongodb.DB.authenticate()

The following are Jave code examples for showing how to use authenticate() of the com.mongodb.DB class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: cloud-meter   File: MongoDB.java   View Source Code Vote up 6 votes
public DB getDB(String database, String username, String password) {

        if(log.isDebugEnabled()) {
            log.debug("username: " + username+", password: " + password+", database: " + database);
        }
        DB db = mongo.getDB(database);
        boolean authenticated = db.isAuthenticated();

        if(!authenticated) {
            if(username != null && password != null && username.length() > 0 && password.length() > 0) {
                authenticated = db.authenticate(username, password.toCharArray());
            }
        }
        if(log.isDebugEnabled()) {
            log.debug("authenticated: " + authenticated);
        }
        return db;
    }
 
Example 2
Project: PhET   File: MongoLog.java   View Source Code Vote up 5 votes
public MongoLog( String sessionID, String dbName ) throws UnknownHostException {
    mongo = new Mongo( HOST_IP_ADDRESS, PORT );

    // All sessions are stored in the same DB.  Having a separate DB for
    // each uses an excessive amount of disk space.
    DB database = mongo.getDB( dbName );

    // Authenticate.
    try {
        boolean auth = database.authenticate( "phetsimclient", ( MER + SimSharingManager.MONGO_PASSWORD + "" + ( 2 * 2 * 2 ) + "ss0O88723otbubaoue" ).toCharArray() );
        if ( !auth ) {
            new RuntimeException( "Authentication failed" ).printStackTrace();
        }
    }

    //A MongoException.Network indicates a failure to reach mongo for the authentication attempt, and hence there is probably no internet connection.  See #3304
    catch ( MongoException.Network exception ) {
        LOGGER.warning( "Failed to connect to mongo during authentication.  Perhaps there is no internet connection." );
    }

    //One collection per session, lets us easily iterate and add messages per session.
    collection = database.getCollection( sessionID );

    /*
     * Mongo logs entire stack traces when failure occur, which is incredibly annoying.
     * Turn off Mongo logging here by interrogating the LogManager.
     * Do this at the end of the constructor, so that Mongo loggers have been instantiated.
     */
    LOGGER.info( "turning off MongoDB loggers" );
    Enumeration<String> names = LogManager.getLogManager().getLoggerNames();
    while ( names.hasMoreElements() ) {
        String name = names.nextElement();
        if ( name.startsWith( "com.mongodb" ) ) {
            LogManager.getLogManager().getLogger( name ).setLevel( Level.OFF );
        }
    }
}
 
Example 3
Project: PhET   File: MongoLoadTesterReader.java   View Source Code Vote up 5 votes
public static void main( String[] args ) throws UnknownHostException {
    Mongo m = new Mongo( MongoLog.HOST_IP_ADDRESS, MongoLog.PORT );
    DB db = m.getDB( MongoLoadTester.LOAD_TESTING_DB_NAME );
    boolean authenticated = db.authenticate( MongoLoadTester.DB_USER_NAME,
                                             ( MongoLoadTester.MER + SimSharingManager.MONGO_PASSWORD + "" + ( 2 * 2 * 2 ) + "ss0O88723otbubaoue" ).toCharArray() );

    System.out.println( "authenticated = " + authenticated );
    if ( !authenticated ) {
        System.out.println( "Authentication failed, aborting test." );
        return;
    }
    DBCollection collection = db.getCollection( MongoLoadTester.DB_COLLECTION );
    long count = collection.getCount();
    System.out.println( "count = " + count );
}
 
Example 4
Project: LiveQA   File: MongoPojoCache.java   View Source Code Vote up 5 votes
public MongoPojoCache(String bucket) {
    MongoClient mongo;
    try {
        mongo = new MongoClient(mongodb_host, mongodb_port);
        DB db = mongo.getDB(dbname);
        db.authenticate("username", "password".toCharArray()); //FIXME: load from config file
        gridfs = new GridFS(db, bucket);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
}