Example usage for com.mongodb ServerAddress ServerAddress

List of usage examples for com.mongodb ServerAddress ServerAddress

Introduction

In this page you can find the example usage for com.mongodb ServerAddress ServerAddress.

Prototype

public ServerAddress() 

Source Link

Document

Creates a ServerAddress with default host and port

Usage

From source file:br.inpe.lac.projetoalunoseorientadoresinpe2016.controller.Controller.java

public String getAllPersons() throws Exception {
    MongoClientOptions options = MongoClientOptions.builder().connectionsPerHost(100).build();
    MongoClient mongoClient = new MongoClient(new ServerAddress(), options);
    MongoDatabase my_db = mongoClient.getDatabase("alunos_e_orientadores_inpe");
    MongoCollection<Document> pessoas = my_db.getCollection("pessoas");
    List<Document> list = pessoas.find().into(new ArrayList<Document>());
    return new Gson().toJson(list);
}

From source file:br.inpe.lac.projetoalunoseorientadoresinpe2016.mongo.Mongo.java

public Mongo() {
    options = MongoClientOptions.builder().connectionsPerHost(100).build();
    mongoClient = new MongoClient(new ServerAddress(), options);//MongoClient mongoClient = new MongoClient(new MongoClientURI("mongod://localhost:27017"));
    my_db = mongoClient.getDatabase("alunos_orientadores_inpe");
    alunos = my_db.getCollection("alunos");
    orientadores = my_db.getCollection("orientadores");
}

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  ww  w.  ja  va  2 s .  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:de.anycook.db.drafts.mongo.Mongo.java

License:Open Source License

protected Mongo() {
    logger = LogManager.getLogger(getClass());

    final CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
            CodecRegistries.fromProviders(new DraftCodecProvider()), MongoClient.getDefaultCodecRegistry());
    final MongoClientOptions options = MongoClientOptions.builder().codecRegistry(codecRegistry).build();
    client = new MongoClient(new ServerAddress(), options);
}

From source file:DutyDatabase.DutyScheduleDB.java

License:Open Source License

/**
 * Creates a connection to a running MongoDB instance using the required codecs.
 *//*from  w  w w.  j  a v a2s . c o  m*/
public DutyScheduleDB() {
    //Create codec registry with LocalDateCodec.
    CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
            CodecRegistries.fromCodecs(new LocalDateCodec()),
            CodecRegistries.fromProviders(new RaCodecProvider(), new DutyBlockCodecProvider(),
                    new ScheduledDutyCodecProvider()),
            MongoClient.getDefaultCodecRegistry());
    MongoClientOptions options = MongoClientOptions.builder().codecRegistry(codecRegistry).build();
    mongoClient = new MongoClient(new ServerAddress(), options);
    db = mongoClient.getDatabase("DutySchedulerDB");
}

From source file:edu.stanford.epad.common.util.MongoDBOperations.java

License:Open Source License

/**
 * Connection to mongo database/*from ww w.ja  va 2  s.c  o  m*/
 * @return
 */
public static DB getMongoDB() {
    if (mongoDB == null) {
        try {
            String mongoHost = EPADConfig.mongoHostname;
            if (mongoHost == null)
                return null;
            MongoClient mongoClient = null;
            if (EPADConfig.mongoUser == null) {
                mongoClient = new MongoClient(mongoHost);
            } else {
                MongoCredential credential = MongoCredential.createCredential(EPADConfig.mongoUser,
                        EPADConfig.mongoDB, EPADConfig.mongoPassword.toCharArray());
                mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential));
            }
            mongoDB = mongoClient.getDB(EPADConfig.mongoDB);
        } catch (Exception e) {
            log.warning("Error connecting to MongoDB", e);
        }
    }
    return mongoDB;
}

From source file:io.opentracing.contrib.mongo.TracingMongoClient.java

License:Apache License

public TracingMongoClient(Tracer tracer) {
    this(tracer, new ServerAddress());
}

From source file:it.terrinoni.m101j.App.java

public static void main(String[] args) {
    MongoClientOptions options = MongoClientOptions.builder().connectionsPerHost(100).build();
    MongoClient client = new MongoClient(new ServerAddress(), options);

    MongoDatabase db = client.getDatabase("test").withReadPreference(ReadPreference.secondary());

    MongoCollection<BsonDocument> coll = db.getCollection("test", BsonDocument.class);
}

From source file:me.konglong.momei.mongodb.core.MongoClientFactory.java

License:Apache License

private ServerAddress createConfiguredOrDefaultServerAddress() throws UnknownHostException {
    ServerAddress defaultAddress = new ServerAddress();

    return new ServerAddress(StringUtils.hasText(host) ? host : defaultAddress.getHost(),
            port != null ? port.intValue() : defaultAddress.getPort());
}

From source file:nosqltools.DBConnection.java

/**
 * Passed parameters are used to connect to the MongoDB 3.0 server
 * @param username//from  w  ww. j a  v  a2  s  . c  o m
 * @param password
 * @param database
 * @param serveraddr The IP Address of the MongoDB Server
 * @param port The port on which the MongoDB Server is running
 * @return True if the connection to the MongoDB server is a success, else false
 */
public boolean connect(String username, String password, String database, String serveraddr, int port) {
    try {
        //If any of the parameters (username, password or DB name are empty, connection will not be attempted)
        if ("".equals(username.trim()) || "".equals(password.trim()) || "".equals(database.trim()))
            return false;

        //A mongocredential object is constructed using the username, password and DB name
        MongoCredential credential = MongoCredential.createCredential(username, database,
                password.toCharArray());

        //if no server address has been specified, use localhost
        if ("".equals(serveraddr) || serveraddr == null)
            mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential));
        else
            mongoClient = new MongoClient(new ServerAddress(serveraddr, port), Arrays.asList(credential));

        //The DB instance is stored
        db = mongoClient.getDB(database);

        //Authentication to the  DB is done below using the username and password supplied by the user
        boolean auth;
        try {
            auth = db.authenticate(username, password.toCharArray());
        } catch (Exception e) {
            auth = false;
        }
        return auth;

    } catch (UnknownHostException | MongoCommandException e) {
        return false;
    }
}