Example usage for com.mongodb MongoURI MongoURI

List of usage examples for com.mongodb MongoURI MongoURI

Introduction

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

Prototype

@SuppressWarnings("deprecation")
@Deprecated
public MongoURI(final MongoClientURI proxied) 

Source Link

Document

Create a new MongoURI from a MongoClientURI.

Usage

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

License:Open Source License

public IStore createStore(String repoName) {
    MongoURI mongoURI = new MongoURI("mongodb://localhost");
    if (!isRestarting()) {
        dropDatabase(mongoURI, repoName);
    }/*ww  w . j av  a  2 s .c  om*/

    return CDOMongoDBUtil.createStore(mongoURI.toString(), repoName);
}

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

License:Open Source License

/**
 * @throws MongoException/*w w w  .  j  a va  2 s  . c o m*/
 * @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;
}

From source file:org.eclipselabs.restlet.mongo.MongoResource.java

License:Open Source License

private DBCollection getCollection() throws UnknownHostException {
    // TODO consider using dependency injection instead of a service tracker for the IMongoDB
    // service/*from  ww w.  j a  v  a 2s.com*/

    IMongoDB mongoDB = Activator.getInstance().getMongoDB();
    Mongo mongo = mongoDB.getMongo(new MongoURI("mongodb://localhost"));
    DB db = mongo.getDB((String) getRequestAttributes().get("database"));
    DBCollection collection = db.getCollection((String) getRequestAttributes().get("collection"));
    return collection;
}

From source file:org.icgc.dcc.release.job.imports.hadoop.MongoAdminInputFormat.java

License:Open Source License

@SuppressWarnings("deprecation")
private static MongoURI mongoUri(String collectionUri) {
    // Need to use the deprecated version until mongo-hadoop upgrades
    return new MongoURI(collectionUri);
}

From source file:org.ingini.monogo.testbed.MongoManager.java

License:Apache License

/**
 * Tries to connect to a given running MongoDB instance at {@code uri}
 * <p>Example URIs:</p>/*from  w  ww.ja  v a2s  .com*/
 *      *) mongodb://127.0.0.1:27017 - connecting to 127.0.0.1 @ port 27017 without authenticating
 *      *) mongodb://john:doe@127.0.0.1:27017 - connecting to 127.0.0.1 @ port 27017 with username: 'john' and password: 'doe'
 *      *) mongodb://john:@127.0.0.1:27017 - connecting to 127.0.0.1 @ port 27017 with username: 'john' and empty password
 * @param uri
 * @throws IllegalStateException in case of difficulties while connecting
 */
private MongoManager(String uri) {
    MongoURI mongoURI = new MongoURI(uri);
    try {
        this.mongo = mongoURI.connect();
    } catch (UnknownHostException e) {
        logger.error("Could not connect to {} due to an exception!", uri, e);
        throw new IllegalStateException(e);
    }
    this.mongoDB = mongo.getDB(MONOGO_TESTBED_DB);
    if (mongoURI.getUsername() != null) {
        this.mongoDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
    }
}

From source file:org.jimmyray.mongo.events.TailableCursorExample.java

License:Apache License

public static void main(final String[] pArgs) throws Exception {
    final Mongo mongo = new Mongo(new MongoURI("mongodb://127.0.0.1:29009"));

    mongo.getDB("testTailableCursor").dropDatabase();

    // Create the capped collection
    final BasicDBObject conf = new BasicDBObject("capped", true);
    conf.put("size", 20971520); // 20 MB
    mongo.getDB("testTailableCursor").createCollection("test", conf);

    final AtomicBoolean readRunning = new AtomicBoolean(true);
    final AtomicBoolean writeRunning = new AtomicBoolean(true);

    final AtomicLong writeCounter = new AtomicLong(0);
    final AtomicLong readCounter = new AtomicLong(0);

    final ArrayList<Thread> writeThreads = new ArrayList<Thread>();
    final ArrayList<Thread> readThreads = new ArrayList<Thread>();

    for (int idx = 0; idx < 10; idx++) {
        final Thread writeThread = new Thread(new Writer(mongo, writeRunning, writeCounter));
        final Thread readThread = new Thread(new Reader(mongo, readRunning, readCounter));
        writeThread.start();//from w ww.j av a  2  s . com
        readThread.start();
        writeThreads.add(writeThread);
        readThreads.add(readThread);
    }

    // Run for five minutes
    //Thread.sleep(300000);
    Thread.sleep(20000);
    writeRunning.set(false);
    Thread.sleep(5000);
    readRunning.set(false);
    Thread.sleep(5000);

    for (final Thread readThread : readThreads)
        readThread.interrupt();
    for (final Thread writeThread : writeThreads)
        writeThread.interrupt();

    System.out.println("----- write count: " + writeCounter.get());
    System.out.println("----- read count: " + readCounter.get());
}

From source file:org.mongoj.db.DBFactoryImpl.java

License:Open Source License

public void setPropertiesFile(String propertiesFileName) {
    try {//from   w  w w .j a v a  2 s  .c  o m
        InputStream inStream = this.getClass().getClassLoader().getResourceAsStream(MONGOJ_PROPERTIES_FILE);

        _properties.load(inStream);

        //override the base/default properties
        if (propertiesFileName != null && propertiesFileName.length() > 0) {
            try {
                inStream = this.getClass().getResourceAsStream(propertiesFileName);

                if (inStream == null) {
                    inStream = Thread.currentThread().getContextClassLoader()
                            .getResourceAsStream(propertiesFileName);
                }

                if (inStream != null) {
                    _properties.load(inStream);
                } else {
                    _log.error("Unable to load {}", propertiesFileName);
                }
            } catch (IOException e) {
                _log.error("Unable to override default mogoj properties", e);
            }
        }

        String uri = _properties.getProperty(MONGOJ_URI).trim();

        if (uri == null || uri.length() == 0) {
            _log.error("Invalid {}", MONGOJ_URI);

            return;
        }

        MongoURI mongoURI = new MongoURI(uri);

        _db = mongoURI.connectDB();

        _log.info("Successfully connected to {}", mongoURI.getDatabase());

        if (Boolean.valueOf(_properties.getProperty(MONGOJ_INDEX))) {
            inStream = this.getClass().getClassLoader()
                    .getResourceAsStream(_properties.getProperty(MONGOJ_INDEX_PROPERTIES));

            if (inStream == null) {
                _log.error("Indexes not found in {} file...skipping indexing.",
                        _properties.getProperty(MONGOJ_INDEX_PROPERTIES));

                return;
            }

            Properties indexes = new Properties();

            try {

                indexes.load(inStream);

                new Indexer(indexes).run();
            } catch (IOException e) {
                _log.error("Error loading indexes...skipping indexing.", e);
            }
        }
    } catch (MongoException e) {
        _log.error("Error connecting to DB", e);
    } catch (UnknownHostException e) {
        _log.error("Error connecting to DB", e);
    } catch (IOException e) {
        _log.error("Error loading properties", e);
    }
}

From source file:org.mongoj.db.MongoUtil.java

License:Open Source License

private MongoUtil() {
    try {//ww  w.  ja v  a 2 s . c om
        //TODO: read uri from config
        _mongoURI = new MongoURI("mongodb://127.0.0.1:27017/test");

        _db = _mongoURI.connectDB();
    } catch (MongoException e) {
        _log.error(e.getMessage());

        _mongoEnabled = false;
    } catch (UnknownHostException e) {
        _log.error(e.getMessage());

        _mongoEnabled = false;
    }
}

From source file:org.mule.transport.mongodb.MongoDBConnector.java

License:Open Source License

public void doConnect() throws Exception {

    mongoURI = new MongoURI(uri);
    mongo = new Mongo.Holder().connect(mongoURI);
    setConnected(true);/*from w w  w.j av a2 s.  c o  m*/
}

From source file:org.sipfoundry.commons.mongo.MongoSpringFactory.java

License:Open Source License

private MongoDbFactory getDelegate() {
    if (m_delegate == null) {
        if (m_connectionUrl == null) {
            m_connectionUrl = UnfortunateLackOfSpringSupportFactory.getConnectionURL();
        }/*from  w w w.jav  a  2 s  . c  om*/
        MongoURI uri = new MongoURI(m_connectionUrl);
        try {
            m_delegate = new SimpleMongoDbFactory(new Mongo(uri), "notused");
        } catch (MongoException e) {
            throw new MongoConfigException(e);
        } catch (UnknownHostException e) {
            throw new MongoConfigException(e);
        }
    }
    return m_delegate;
}