Example usage for com.mongodb MongoURI connectDB

List of usage examples for com.mongodb MongoURI connectDB

Introduction

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

Prototype

public DB connectDB() 

Source Link

Document

Returns the DB object from a newly created Mongo instance based on this URI.

Usage

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

License:Open Source License

public void setPropertiesFile(String propertiesFileName) {
    try {//from   ww w.j  ava2 s  . co  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.wrml.contrib.runtime.service.mongo.MongoService.java

License:Apache License

@Override
protected void initFromConfiguration(final ServiceConfiguration config) {

    if (config == null) {
        final ServiceException e = new ServiceException("The config cannot be null.", null, this);
        LOG.error(e.getMessage(), e);/*www.  j ava2s.c o  m*/
        throw e;
    }

    final Map<String, String> settings = config.getSettings();
    String mongoUriString = DEFAULT_URI_STRING;

    if (settings != null) {
        if (settings.containsKey(MONGO_URI_SETTING_NAME)) {
            mongoUriString = settings.get(MONGO_URI_SETTING_NAME);
        }

        if (settings.containsKey(MONGO_COLLECTION_PREFIX_SETTING_NAME)) {
            _CollectionPrefix = settings.get(MONGO_COLLECTION_PREFIX_SETTING_NAME);
        }
    }

    // TODO: Look into MongoClientURI replacement
    final MongoURI mongoUri = new MongoURI(mongoUriString);
    try {
        _Mongo = mongoUri.connectDB();

        if (!_Mongo.isAuthenticated() && mongoUri.getPassword() != null) {
            _Mongo.authenticate(mongoUri.getUsername(), mongoUri.getPassword());
        }

    } catch (MongoException | UnknownHostException ex) {
        final String logMessage = "Error creating connection to Mongo: " + _Mongo;
        LOG.error(logMessage);
        throw new ServiceException(logMessage, ex, this);
    }
}