Example usage for com.mongodb MongoURI getDatabase

List of usage examples for com.mongodb MongoURI getDatabase

Introduction

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

Prototype

@Nullable
public String getDatabase() 

Source Link

Document

Gets the database name.

Usage

From source file:com.appleframework.monitor.model.Project.java

License:Open Source License

public MongoTemplate fetchMongoTemplate() {

    try {//from www .java2 s. c  o m
        Mongo mongo;
        if (MONGO_MAP.containsKey(mongoUri)) {
            mongo = MONGO_MAP.get(mongoUri);

        } else {
            mongo = new Mongo(new MongoURI(mongoUri));
            MONGO_MAP.put(mongoUri, mongo);

        }

        MongoURI uri = new MongoURI(mongoUri);
        return new MongoTemplate(new SimpleMongoDbFactory(mongo, uri.getDatabase(),
                new UserCredentials(uri.getUsername(), parseChars(uri.getPassword()))));

    } catch (Exception e) {
        logger.error("mongo db error ,uri={}", mongoUri, e);
        return null;
    }

}

From source file:com.cloudbees.gasp.model.MongoConnection.java

License:Apache License

public void connect() throws Exception {
    try {//ww w . j  a  v  a2s .  c  om
        // Connect to Mongo and Authenticate
        MongoURI mongoURI = new MongoURI(mongoURL);
        mongo = new Mongo(mongoURI);
        mongoDB = mongo.getDB(mongoURI.getDatabase());
        mongoDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword());

        // Get Mongo collections and set WriteConcern
        String mongoLocations = "locations";
        locations = getMongoDB().getCollection(mongoLocations);
        mongoDB.setWriteConcern(WriteConcern.SAFE);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}

From source file:com.dianping.swallow.web.dao.SimMongoDbFactory.java

License:Apache License

/**
 * Creates a new {@link SimpleMongoDbFactory} instance from the given {@link MongoURI}.
 * //from w ww.j av  a2  s.com
 * @param uri must not be {@literal null}.
 * @throws MongoException
 * @throws UnknownHostException
 * @see MongoURI
 * @deprecated since 1.7. Please use {@link #SimpleMongoDbFactory(MongoClientURI)} instead.
 */
@Deprecated
public SimMongoDbFactory(MongoURI uri) throws MongoException, UnknownHostException {
    this(new Mongo(uri), uri.getDatabase(),
            new UserCredentials(uri.getUsername(), parseChars(uri.getPassword())), true, uri.getDatabase());
}

From source file:com.flyingdonut.implementation.persistence.MongoDBConnection.java

License:Apache License

private void mongoURIInit() {
    MongoURI uri = new MongoURI(getMongoURI());
    try {/*from  www.j  a  v a 2s . c o  m*/
        logger.info("Connecting to " + uri + "...");
        mongo = new Mongo(uri);
        logger.info("...success!");
        String uriDatabase = uri.getDatabase();
        if (StringUtils.hasText(uriDatabase)) {
            db = mongo.getDB(uriDatabase);
        } else {
            db = mongo.getDB(dbName);
        }
    } catch (UnknownHostException e) {
        logger.error("Could not init the database", e);
    }
}

From source file:de.belaso.mongolyn.ui.MongolynUtils.java

License:Open Source License

public static DB openNewDB(final TaskRepository repository) throws CoreException {
    try {/*from w w w . jav a  2 s .  c  om*/
        MongoURI mongoURI = new MongoURI(repository.getRepositoryUrl());
        DB db = new Mongo(mongoURI).getDB(mongoURI.getDatabase());
        AuthenticationCredentials credentials = repository.getCredentials(AuthenticationType.REPOSITORY);
        if (credentials != null) {
            db.authenticate(credentials.getUserName(), credentials.getPassword().toCharArray());
        }
        return db;
    } catch (UnknownHostException unknownHostException) {
        throw new CoreException(Activator.INSTANCE.getErrorStatus(unknownHostException));
    }
}

From source file:de.belaso.mongolyn.ui.RepositorySettingsPage.java

License:Open Source License

@Override
protected boolean isValidUrl(String url) {
    try {//from  w  ww . j a  v a2s . c o m
        MongoURI mongoURI = new MongoURI(url);
        if (mongoURI.getDatabase() == null)
            return false;
        else
            return true;
    } catch (RuntimeException runtimeException) {
        return false;
    }
}

From source file:fr.xebia.cocktail.CocktailRepository.java

License:Apache License

@Inject
public CocktailRepository(@Value("${mongodb_uri}") String mongoUri, @Value("${solr_url}") String solrUri)
        throws UnknownHostException, MalformedURLException {
    MongoURI databaseUri = new MongoURI(mongoUri);
    mongo = new Mongo(databaseUri);

    db = mongo.getDB(databaseUri.getDatabase());
    if (!Strings.isNullOrEmpty(databaseUri.getUsername())) {
        db.authenticate(databaseUri.getUsername(), databaseUri.getPassword());
    }/*from w  ww. j  a v  a 2 s . c o  m*/
    cocktails = db.getCollection("cocktails");

    solrServer = new CommonsHttpSolrServer(solrUri);

}

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

License:Apache License

@Provides
@Singleton//from  w  ww  .j  a  va2s . 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.eclipse.birt.data.oda.mongodb.impl.MongoDBDriver.java

License:Open Source License

static String getDatabaseName(Properties connProps) {
    MongoURI mongoURI = getMongoURI(connProps);
    if (mongoURI != null)
        return mongoURI.getDatabase();

    // no mongoURI specified, get from the individual property
    return getStringPropValue(connProps, DBNAME_PROP);
}

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

License:Open Source License

public void setPropertiesFile(String propertiesFileName) {
    try {/*  w w w . j  ava  2 s .  c om*/
        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);
    }
}