List of usage examples for com.mongodb Mongo getDB
@Deprecated public DB getDB(final String dbName)
From source file:com.tomtom.services.notifications.dao.DaoModule.java
License:Apache License
/** * Get the MongoDB database instance./* w w w. j a v a2s . c om*/ * * @param mongo MongoDB handle. * @param databaseName Database name. * @param subDatabaseName Sub-database name (for unit tests). If the subdatabase name is empty, the database name is * used, otherwise the subdatabase name is appended to. Example, database="TEST", subdatabase * name="xyz" would produce "TEST_xyz". * @param userName Username for authentication. * @param password Password for authentication. * @return MongoDB instance. * @throws MongoDBConnectionException If something went wrong. */ @Nonnull protected static MongoDB getDB(@Nonnull final Mongo mongo, @Nonnull final String databaseName, @Nonnull final String subDatabaseName, @Nonnull final String userName, @Nonnull final String password) { assert mongo != null; assert databaseName != null; assert subDatabaseName != null; assert userName != null; assert password != null; final MongoDB mongoDb = new MongoDB(mongo.getDB(databaseName), subDatabaseName); return mongoDb; }
From source file:com.tomtom.speedtools.tracer.mongo.MongoDBTraceHandler.java
License:Apache License
@Nonnull static DBCollection getDBCollection(@Nonnull final String servers, @Nonnull final String database, @Nonnull final String userName, @Nonnull final String password, final int sizeMB, final int connectTimeoutMsecs) throws UnknownHostException { assert servers != null; assert database != null; assert userName != null; assert password != null; assert connectTimeoutMsecs >= 0; LOG.info("getDBCollection: Creating MongoDB connection for traces: {}", servers); final Mongo mongo = MongoConnectionCache.getMongoDB(servers, connectTimeoutMsecs, userName, database, password);/* w w w. ja va 2 s . c om*/ // If this is a replica set, set read preference to secondary for traces. final List<ServerAddress> serverAddressList = mongo.getServerAddressList(); if (serverAddressList.size() > 1) { mongo.setReadPreference(ReadPreference.secondary()); } // Should writes fail, then don't throw exceptions, just ignore. // We care more about not disturbing the primary system then our own (traces) integrity. mongo.setWriteConcern(WriteConcern.UNACKNOWLEDGED); // The connection point may actually be null... Not an error. final String connectPoint = mongo.getConnectPoint(); LOG.info("getDBCollection: MongoDB connection for traces established: '{}' at {}", database, connectPoint); final DB db = mongo.getDB(database); final DBObject options = new BasicDBObject(); options.put("capped", true); options.put("size", sizeMB * MEGABYTE); final DBCollection collection; if (!db.getCollectionNames().contains(Constants.COLLECTION_NAME)) { collection = db.createCollection(Constants.COLLECTION_NAME, options); } else { collection = db.getCollection(Constants.COLLECTION_NAME); } return collection; }
From source file:com.uquetignyadminapp.connection.ConnectionMongoDB.java
public ConnectionMongoDB() throws UnknownHostException { Mongo mongoClient = new Mongo("localhost"); db = mongoClient.getDB("test"); //boolean auth = db.authenticate(myUserName, myPassword); }
From source file:com.uquetignywebapp.connection.ConnectionMongoDB.java
public ConnectionMongoDB() throws UnknownHostException { Mongo mongoClient = new Mongo("localhost"); db = mongoClient.getDB("uqb2"); //boolean auth = db.authenticate(myUserName, myPassword); }
From source file:com.wenzani.maven.mongodb.StopMongoDb.java
License:Apache License
public void execute() throws MojoExecutionException { port = null == port ? "27017" : port; try {/*from w w w.jav a 2 s.com*/ Logger.getLogger("com.mongodb").setLevel(Level.OFF); getLog().info("Shutting down mongodb..."); Mongo mongo = new Mongo("127.0.0.1", Integer.valueOf(port)); mongo.getDB("admin").command(new BasicDBObject("shutdown", 1)); getLog().info("MongoDB shutdown. Ignore the EOFException..."); } catch (UnknownHostException e) { getLog().error(ExceptionUtils.getFullStackTrace(e)); } catch (Throwable ex) { //ignore } }
From source file:com.zjy.mongo.splitter.MongoCollectionSplitter.java
License:Apache License
protected DB getConfigDB() { Mongo mongo; MongoClientURI inputURI = MongoConfigUtil.getInputURI(getConfiguration()); MongoClientURI authURI = MongoConfigUtil.getAuthURI(getConfiguration()); final DBCollection inputCollection; if (authURI != null && authURI.getUsername() != null && authURI.getPassword() != null) { inputCollection = MongoConfigUtil.getCollectionWithAuth(inputURI, authURI); } else {/* www .j av a 2 s.co m*/ inputCollection = MongoConfigUtil.getCollection(inputURI); } DB db = inputCollection.getDB(); mongo = db.getMongo(); if (authURI != null) { if (authURI.getUsername() != null && authURI.getPassword() != null) { authDB = mongo.getDB(authURI.getDatabase()); } } return mongo.getDB("config"); }
From source file:com.zjy.mongo.util.MongoConfigUtil.java
License:Apache License
/** * Get an authenticated DBCollection from a MongodB URI. * @param authURI the URI with which to authenticate * @param uri the MongoDB URI//from w w w . j av a 2s .c o m * @return the authenticated DBCollection */ public static DBCollection getCollectionWithAuth(final MongoClientURI uri, final MongoClientURI authURI) { //Make sure auth uri is valid and actually has a username/pw to use if (authURI == null || authURI.getUsername() == null || authURI.getPassword() == null) { throw new IllegalArgumentException( "auth URI is empty or does not contain a valid username/password combination."); } DBCollection coll; try { Mongo mongo = getMongoClient(authURI); coll = mongo.getDB(uri.getDatabase()).getCollection(uri.getCollection()); return coll; } catch (Exception e) { throw new IllegalArgumentException("Couldn't connect and authenticate to get collection", e); } }
From source file:conexionmongodb.frmPersona.java
/** * Creates new form frmPersona//from www . ja va 2 s. co m */ public frmPersona() { initComponents(); try { Mongo mongodb = new Mongo("localhost", 27017); db = mongodb.getDB("dasedb"); personas = db.getCollection("personas"); } catch (UnknownHostException ex) { Logger.getLogger(frmPersona.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:DataAccess.DAO.LoginDAO.java
public String validate(String username, String password) { String returnText = LOGIN_FAILURE; Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("Restaurant"); try {//from w w w. ja v a 2s. co m //boolean auth = db.authenticate(username, password.toCharArray()); MongoCredential credential2 = MongoCredential.createCredential(username, "Restaurant", password.toCharArray()); MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential2)); DB db2 = mongoClient.getDB("Restaurant"); System.out.println("Connect to database successfully"); DBCollection coll = db2.getCollection("Users"); System.out.println("Collection users selected successfully"); BasicDBObject document2 = new BasicDBObject(); document2.put("UserName", username); document2.put("Password", password); //coll.insert(document2); DBCursor cur2 = coll.find(document2); System.out.println(cur2.toString()); while (cur2.hasNext()) { System.out.println(cur2.next()); } System.out.println("Login is successful!"); if (credential2 != null) { DBCollection table = db.getCollection("Users"); BasicDBObject document = new BasicDBObject(); document.put("UserName", username); table.insert(document); DBCursor cur = table.find(document); while (cur.hasNext()) { System.out.println(cur.next()); } HttpSession session = SessionBean.getSession(); session.setAttribute("UserName", user); System.out.println("Login is successful!"); returnText = LOGIN_SUCCESS; return "admins"; } else { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Incorrect Username and Passowrd", "Please enter correct username and Password")); System.out.println("Login is failed!"); return "login"; } //System.out.println("Done"); //return returnText; } catch (MongoException e) { e.printStackTrace(); } finally { mongo.close(); } return returnText; }
From source file:Datos.ServerMongo.java
public void conectar() { try {//from w ww . j a v a 2s. co m Mongo mongodb = new Mongo(this.address, this.port); db = mongodb.getDB(this.nameDB); //collection = db.getCollection(nameTable); this.estate = true; } catch (UnknownHostException ex) { System.out.println(ex); } }