Example usage for com.mongodb Mongo Mongo

List of usage examples for com.mongodb Mongo Mongo

Introduction

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

Prototype

Mongo(final MongoClientURI mongoURI) 

Source Link

Usage

From source file:org.ardverk.gibson.dashboard.MongoModule.java

License:Apache License

@Provides
@Singleton// w  ww  .j  a  v  a  2s .c o  m
Datastore getDatastore(Configuration configuration) throws IOException {

    MongoURI uri = parseURI(configuration.getString(URI_KEY), Gibson.URI);

    String database = uri.getDatabase();
    if (database == null) {
        throw new IOException("Database missing: " + uri);
    }

    if (LOG.isInfoEnabled()) {
        LOG.info("Connecting: uri=" + uri);
    }

    Mongo mongo = new Mongo(uri);
    DatastoreFactory factory = new DatastoreFactory(mongo);
    return factory.createDatastore(database);
}

From source file:org.ardverk.gibson.transport.MongoTransport.java

License:Apache License

@Override
public synchronized void connect() throws IOException {
    if (!open) {//w  w  w.  j a  v  a 2  s . com
        throw new IOException("closed");
    }

    if (connected) {
        throw new IOException("connected");
    }

    String database = uri.getDatabase();
    if (database == null) {
        throw new IOException("Database missing: " + uri);
    }

    if (LOG.isInfoEnabled()) {
        LOG.info(Gibson.MARKER, "Connecting to: " + uri);
    }

    this.mongo = new Mongo(uri);

    // [SIX-3989]: The Mongo driver and Morphia have really bad Exception handling. 
    // Things will not bubble up if it fails to connect. Attempt to connect manually
    // and let it fail fast if it does.
    DBTCPConnector connector = mongo.getConnector();
    ServerAddress address = connector.getAddress();
    DBPortPool pool = connector.getDBPortPool(address);
    DBPort port = pool.get();
    port.ensureOpen(); // Throws an IOException!

    DatastoreFactory factory = new DatastoreFactory(mongo);
    final Datastore datastore = factory.createDatastore(database);

    Runnable task = new Runnable() {
        @Override
        public void run() {
            try {
                process(datastore);
            } catch (InterruptedException err) {
                LOG.error(Gibson.MARKER, "InterruptedException", err);
            } finally {
                destroy(mongo);
            }
        }
    };

    this.future = EXECUTOR.submit(task);
    this.connected = true;
}

From source file:org.atlasapi.AtlasModule.java

License:Apache License

public @Bean Mongo mongo() {
    Mongo mongo = new Mongo(mongoHosts());
    mongo.setReadPreference(readPreference());
    if (isProcessing() && processingWriteConcern != null
            && !Strings.isNullOrEmpty(processingWriteConcern.get())) {

        WriteConcern writeConcern = WriteConcern.valueOf(processingWriteConcern.get());
        if (writeConcern == null) {
            throw new IllegalArgumentException(
                    "Could not parse write concern: " + processingWriteConcern.get());
        }//from  w  w  w.j av  a 2  s  . com
        mongo.setWriteConcern(writeConcern);
    }
    return mongo;
}

From source file:org.benjp.services.mongodb.MongoBootstrap.java

License:Open Source License

private static Mongo setupEmbedMongo() throws IOException {
    MongodStarter runtime = MongodStarter.getDefaultInstance();
    int port = Integer.parseInt(PropertyManager.getProperty(PropertyManager.PROPERTY_SERVER_PORT));
    mongodExe = runtime.prepare(new MongodConfig(Version.V2_3_0, port, Network.localhostIsIPv6()));
    mongod = mongodExe.start();/*w w  w .  j a va  2 s  .  c  om*/
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        log.info(e.getMessage());
    }
    String host = PropertyManager.getProperty(PropertyManager.PROPERTY_SERVER_HOST);

    return new Mongo(new ServerAddress(host, port));
}

From source file:org.bigmouth.nvwa.log4mongo.MongoDbAppender.java

License:Apache License

protected Mongo getMongo(List<ServerAddress> addresses) {
    if (addresses.size() < 2) {
        return new Mongo(addresses.get(0));
    } else {/*from  ww  w  .ja va  2  s .c  o  m*/
        // Replica set
        return new Mongo(addresses);
    }
}

From source file:org.clevermore.monitor.server.mongodb.MongoSessionFactory.java

License:Apache License

@Inject
public MongoSessionFactory() {
    mongoConfig = ConfigurationService.getInstance().getMongoConfig();

    if (!mongoConfig.getEnabled()) {
        logger.info("Mongo DB is disabled.Skipping initialization");
        return;/*from  ww  w .j  ava2s.c  o  m*/
    }

    List<HostAddress> hosts = mongoConfig.getHosts();
    if (hosts == null || hosts.isEmpty()) {
        throw new RuntimeException("Mongo DB  Initialization Error: hosts not configured");
    }

    if (mongoConfig.getDatabaseName() == null || mongoConfig.getDatabaseName().isEmpty()) {
        throw new RuntimeException("Mongo DB Initialization Error : database name not specified");
    }

    List<ServerAddress> mongoServers = new ArrayList<ServerAddress>();
    for (HostAddress host : hosts) {
        try {
            mongoServers.add(new ServerAddress(host.getHostName(), host.getPort()));
        } catch (UnknownHostException e) {
            throw new RuntimeException("Mongo DB  Initialization Error ", e);
        }
    }

    mongo = new Mongo(mongoServers);
    morphia = new Morphia();
}

From source file:org.eclipse.emf.cdo.server.internal.mongodb.MongoDBStore.java

License:Open Source License

@Override
protected void doActivate() throws Exception {
    InternalRepository repository = getRepository();
    branching = repository.isSupportingBranches();
    if (branching) {
        throw new IllegalStateException("Branching is not supported");
    }/*w  ww.  j  a v  a 2 s  .c o m*/

    REPOS.put(repository.getName(), repository);

    super.doActivate();

    Mongo mongo = new Mongo(mongoURI);
    db = mongo.getDB(dbName);

    Set<String> collectionNames = db.getCollectionNames();
    firstStart = !collectionNames.contains(Props.NAME);

    props = new Props(this);
    commits = new Commits(this);
    classes = new Classes(this);

    LifecycleUtil.activate(idHandler);
    setObjectIDTypes(idHandler.getObjectIDTypes());

    Arrays.fill(valueHandlers, new ValueHandler());
    initValueHandlers();

    if (firstStart) {
        firstStart();
    } else {
        reStart();
    }
}

From source file:org.eclipse.emf.cdo.server.internal.mongodb.MongoDBStoreFactory.java

License:Open Source License

protected void dropDatabase(MongoURI mongoURI, String dbName) {
    Mongo mongo = null;/*w  ww . j  av  a2  s  .  c om*/

    try {
        mongo = new Mongo(mongoURI);
        DB db = mongo.getDB(dbName);
        db.dropDatabase();
    } catch (Exception ex) {
        throw WrappedException.wrap(ex);
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.eclipse.emf.cdo.tests.mongodb.MongoDBConfig.java

License:Open Source License

protected void dropDatabase(MongoURI mongoURI, String repoName) {
    Mongo mongo = null;//from ww w .java  2  s. c o  m

    try {
        mongo = new Mongo(mongoURI);
        DB db = mongo.getDB(repoName);
        if (!db.getCollectionNames().isEmpty()) {
            db.dropDatabase();
        }
    } catch (Exception ex) {
        throw WrappedException.wrap(ex);
    } finally {
        if (mongo != null) {
            mongo.close();
        }
    }
}

From source file:org.eclipse.jetty.nosql.mongodb.MongoSessionDataStoreFactory.java

License:Open Source License

/**
 * @throws MongoException/*from ww w.  jav  a  2 s .  c  om*/
 * @throws UnknownHostException
 * @see org.eclipse.jetty.server.session.SessionDataStoreFactory#getSessionDataStore(org.eclipse.jetty.server.session.SessionHandler)
 */
@Override
public SessionDataStore getSessionDataStore(SessionHandler handler) throws Exception {
    MongoSessionDataStore store = new MongoSessionDataStore();
    store.setGracePeriodSec(getGracePeriodSec());
    store.setSavePeriodSec(getSavePeriodSec());
    Mongo mongo;

    if (!StringUtil.isBlank(getConnectionString()))
        mongo = new Mongo(new MongoURI(getConnectionString()));
    else if (!StringUtil.isBlank(getHost()) && getPort() != -1)
        mongo = new Mongo(getHost(), getPort());
    else if (!StringUtil.isBlank(getHost()))
        mongo = new Mongo(getHost());
    else
        mongo = new Mongo();
    store.setDBCollection(mongo.getDB(getDbName()).getCollection(getCollectionName()));
    return store;
}