Example usage for com.mongodb MongoClient MongoClient

List of usage examples for com.mongodb MongoClient MongoClient

Introduction

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

Prototype

public MongoClient(final MongoClientURI uri, final MongoDriverInformation mongoDriverInformation) 

Source Link

Document

Creates a Mongo described by a URI.

Usage

From source file:com.erudika.para.persistence.MongoDBUtils.java

License:Apache License

/**
 * Returns a client instance for MongoDB
 * @return a client that talks to MongoDB
 *//*from w w  w  . j av a2  s  . c om*/
public static MongoDatabase getClient() {
    if (mongodb != null) {
        return mongodb;
    }

    logger.info("MongoDB host: " + DBHOST + ":" + DBPORT + ", database: " + DBNAME);
    ServerAddress s = new ServerAddress(DBHOST, DBPORT);
    if (!StringUtils.isBlank(DBUSER) && !StringUtils.isBlank(DBPASS)) {
        MongoCredential credential = MongoCredential.createCredential(DBUSER, DBNAME, DBPASS.toCharArray());
        mongodbClient = new MongoClient(s, Arrays.asList(credential));
    } else {
        mongodbClient = new MongoClient(s);
    }

    mongodb = mongodbClient.getDatabase(DBNAME);

    if (!existsTable(Config.APP_NAME_NS)) {
        createTable(Config.APP_NAME_NS);
    }

    // We don't have access to Para.addDestroyListener() here.
    // Users will be responsible for calling shutDownClient().

    return mongodb;
}

From source file:com.esri.geoevent.transport.mongodb.MongoDBOutboundTransport.java

License:Apache License

private void applyProperties() throws IOException {
    mongoClient = new MongoClient(host, port);
    mongoClient.setWriteConcern(writeConcern);
    db = mongoClient.getDB(databaseName);

    boolean auth = true;
    if (userName != null && userName.length() > 0 && password != null && password.length() > 0) {
        auth = db.authenticate(userName, password.toCharArray());
    }/*from   w  w w.  j a  v  a 2s . c om*/
    if (!auth) {
        throw new IOException(LOGGER.translate("AUTHENTICATION_ERROR", host, userName));
    }

    collection = db.getCollection(collectionName);
}

From source file:com.eventshero.api.dao.impl.PMF.java

License:Apache License

public static DB getDB() {
    if (dbInstance == null) {
        MongoClient mongo;// ww w.  j  ava 2 s. c o  m
        try {
            mongo = new MongoClient("localhost", 27017);

            //Get database. If the database doesnt exist, MongoDB will create it
            dbInstance = mongo.getDB("eventshero");
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return dbInstance;
}

From source file:com.example.rest.DbConnection.java

public DbConnection() {
    mangoClient = new MongoClient("localhost", 27017);
    db = mangoClient.getDB("testdb");
    table = db.getCollection("Customer");
    orderTable = db.getCollection("Order");
}

From source file:com.exorath.exodata.impl.IDataAPI.java

License:Apache License

public IDataAPI(String host, int port) {
    this.client = new MongoClient(host, port);
}

From source file:com.ff.reportgenerator.mongodb.DynamicDatabase.java

private static void init() {
    try {//  w  w w. j a va 2 s.  co  m
        if (mongoClient == null) {
            mongoClient = new MongoClient("localhost", 27017);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.focusit.log4jmongo.appender.SimpleMongoDbAppender.java

License:Apache License

protected MongoClient getMongo(final List<ServerAddress> addresses) {

    List<MongoCredential> credential = null;

    if (userName != null && password != null && sourceDb != null && userName.trim().length() > 0
            && password.trim().length() > 0 && sourceDb.trim().length() > 0) {
        credential = new ArrayList<MongoCredential>();
        credential.add(MongoCredential.createCredential(userName, sourceDb, password.toCharArray()));
    }/*from w  w  w .j av  a  2s.  c  o  m*/

    if (credential != null) {
        return new MongoClient(addresses, credential);
    }
    return new MongoClient(addresses);
}

From source file:com.foodtruckdata.mongodb.UsersInput.java

public UsersInput(String host, int port, String db, String userName, String passwd)
        throws UnknownHostException {
    MongoCredential credential = MongoCredential.createMongoCRCredential(userName, db, passwd.toCharArray());
    MongoClient mongoClient = new MongoClient(new ServerAddress(host, port), Arrays.asList(credential));
    mongoDB = mongoClient.getDB(db);/*from w ww . j a  v a 2  s .c  o m*/
}

From source file:com.fpt.xml.hth.db.lib.DAO.MovieDAO.java

private void connection() {
    try {//from   www  .j  av a  2  s  . c  o  m
        MongoCredential credential = MongoCredential.createMongoCRCredential(Config.USER_NAME,
                Config.DATABASE_NAME, Config.PASS_WORD.toCharArray());
        ServerAddress address = new ServerAddress(Config.getHost(), Config.getPort());
        List<MongoCredential> lst = new ArrayList<MongoCredential>();
        lst.add(credential);
        this.mongoClient = new MongoClient(address, lst);
        this.cinemaDB = mongoClient.getDB(Config.DATABASE_NAME);
        this.movieCollection = cinemaDB.getCollection(Config.MOVIE_COLLECTION);
        this.converter = new MovieTheaterSessionConverter();
    } catch (UnknownHostException ex) {
        Logger.getLogger(MovieDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.garyclayburg.persistence.config.EmbeddedMongoConfig.java

License:Open Source License

/**
 * This is for a relset of db's/*from  www  .j a v a  2  s  .com*/
 */

//        return new MongoClient(new ArrayList<ServerAddress>() {{
//            add(new ServerAddress("127.0.0.1",27017));
//            add(new ServerAddress("127.0.0.1",27027));
//            add(new ServerAddress("127.0.0.1",27037));
//        }});

//    }
@Bean
@Override
public Mongo mongo() throws Exception {

    log.info("configuring embedded mongo");
    //Files that could be left over after a previous execution was (rudely) killed with kill -9

    try {
        DeletionFileVisitor.deletePath(Paths.get(System.getProperty("java.io.tmpdir")), "embedmongo-db-*");
        DeletionFileVisitor.deletePath(Paths.get(System.getProperty("java.io.tmpdir")), "extract-*-mongod*");
    } catch (IOException e) {
        log.warn(
                "could not delete temporary files from embedded mongod process.  Try manually stopping or killing mongod.exe process first.");
    }

    //        RuntimeConfig config = new RuntimeConfig();
    //        config.setExecutableNaming(new UserTempNaming());

    //        MongodStarter starter = MongodStarter.getInstance(config);

    //        MongodExecutable mongoExecutable = starter.prepare(new MongodConfig(Version.V2_2_0,MONGO_TEST_PORT,false));
    //        mongoExecutable.start();

    File storeFile = new File(System.getProperty("user.home"));
    IDirectory artifactStorePath;
    if (storeFile.exists() && storeFile.isDirectory() && storeFile.canWrite()) {
        artifactStorePath = new FixedPath(System.getProperty("user.home") + "/.embeddedmongo");
    } else {
        //use java tmp dir instead of the default user.home - cloudbees cannot write to user.home
        artifactStorePath = new FixedPath(System.getProperty("java.io.tmpdir") + "/.embeddedmongo");
    }
    ITempNaming executableNaming = new UUIDTempNaming();
    Command command = Command.MongoD;
    IRuntimeConfig runtimeConfig;

    if (!mongoDownloadServer.equals("none")) {
        log.debug("using custom download server: " + mongoDownloadServer);
        runtimeConfig = new RuntimeConfigBuilder().defaults(command)
                .artifactStore(new ArtifactStoreBuilder().defaults(command)
                        .download(new DownloadConfigBuilder().defaultsForCommand(command)
                                .downloadPath(mongoDownloadServer).artifactStorePath(artifactStorePath))
                        .executableNaming(executableNaming))
                .build();
    } else {
        log.debug("using standard download server: " + mongoDownloadServer);
        runtimeConfig = new RuntimeConfigBuilder().defaults(command)
                .artifactStore(new ArtifactStoreBuilder().defaults(command)
                        .download(new DownloadConfigBuilder().defaultsForCommand(command)
                                .artifactStorePath(artifactStorePath))
                        .executableNaming(executableNaming))
                .build();
    }
    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
    MongodExecutable mongodExe = runtime.prepare(new MongodConfigBuilder().version(Version.Main.PRODUCTION)
            .timeout(new Timeout(60000)).net(new Net(MONGO_TEST_PORT, Network.localhostIsIPv6())).build());
    mongodExe.start();

    mongo = new MongoClient(LOCALHOST, MONGO_TEST_PORT);
    mongo.getDB(DB_NAME);

    return new MongoClient(new ArrayList<ServerAddress>() {
        {
            add(new ServerAddress(LOCALHOST, MONGO_TEST_PORT));
        }
    });
}