List of usage examples for com.mongodb MongoCredential createCredential
public static MongoCredential createCredential(final String userName, final String database, final char[] password)
From source file:com.handu.open.dubbo.monitor.config.MongoConfig.java
License:Apache License
@Override @Bean/* w ww . j a v a 2s . c o m*/ public Mongo mongo() throws Exception { final String url = Preconditions.checkNotNull(env.getProperty(DB_URL)); final int port = Integer.parseInt(env.getProperty(DB_PORT, "27017")); final String username = env.getProperty(DB_USERNAME); final String database = env.getProperty(DB_DATABASE); final String password = env.getProperty(DB_PASSWORD); return new MongoClient(singletonList(new ServerAddress(url, port)), singletonList(MongoCredential.createCredential(username, database, password.toCharArray()))); }
From source file:com.meltmedia.dropwizard.mongo.MongoBundle.java
License:Apache License
MongoClient buildClient(MongoConfiguration configuration) { try {//from w w w . j a va 2 s. c o m // build the seed server list. List<ServerAddress> servers = new ArrayList<>(); for (Server seed : configuration.getSeeds()) { servers.add(new ServerAddress(seed.getHost(), seed.getPort())); } log.info("Found {} mongo seed servers", servers.size()); for (ServerAddress server : servers) { log.info("Found mongo seed server {}:{}", server.getHost(), server.getPort()); } // build the credentials Credentials credentialConfig = configuration.getCredentials(); List<MongoCredential> credentials = credentialConfig == null ? Collections.<MongoCredential>emptyList() : Collections.singletonList(MongoCredential.createCredential(credentialConfig.getUserName(), configuration.getDatabase(), credentialConfig.getPassword().toCharArray())); if (credentials.isEmpty()) { log.info("Found {} mongo credentials.", credentials.size()); } else { for (MongoCredential credential : credentials) { log.info("Found mongo credential for {} on database {}.", credential.getUserName(), credential.getSource()); } } // build the options. MongoClientOptions options = new MongoClientOptions.Builder() .writeConcern(writeConcern(configuration.getWriteConcern())).build(); log.info("Mongo database is {}", configuration.getDatabase()); return new MongoClient(servers, credentials, options); } catch (Exception e) { throw new RuntimeException("Could not configure MongoDB client.", e); } }
From source file:com.redhat.jielicious.storage.mongo.ThermostatMongoStorage.java
License:Open Source License
public static void start(Map<String, String> mongoConfiguration) { String username = null;//www .j av a 2 s . c o m char[] password = null; String host = "127.0.0.1"; int port = 27518; int timeout = 1000; if (mongoConfiguration.containsKey(MongoConfiguration.MONGO_DB.toString())) { dbName = mongoConfiguration.get(MongoConfiguration.MONGO_DB.toString()); } if (mongoConfiguration.containsKey(MongoConfiguration.MONGO_USERNAME.toString())) { username = mongoConfiguration.get(MongoConfiguration.MONGO_USERNAME.toString()); } if (mongoConfiguration.containsKey(MongoConfiguration.MONGO_PASSWORD.toString())) { password = mongoConfiguration.get(MongoConfiguration.MONGO_PASSWORD.toString()).toCharArray(); } if (mongoConfiguration.containsKey(MongoConfiguration.MONGO_URL.toString())) { try { URI url = new URI(mongoConfiguration.get(MongoConfiguration.MONGO_URL.toString())); host = url.getHost(); port = url.getPort(); } catch (URISyntaxException e) { // Do nothing. Defaults will be used. } } if (mongoConfiguration.containsKey(MongoConfiguration.MONGO_SERVER_TIMEOUT.toString())) { timeout = Integer.valueOf(mongoConfiguration.get(MongoConfiguration.MONGO_SERVER_TIMEOUT.toString())); } ServerAddress address = new ServerAddress(host, port); if (username != null && password != null) { MongoCredential credential = MongoCredential.createCredential(username, dbName, password); mongoClient = new MongoClient(address, Collections.singletonList(credential), new MongoClientOptions.Builder().serverSelectionTimeout(timeout).connectTimeout(0) .socketTimeout(0).build()); } else { mongoClient = new MongoClient(address, new MongoClientOptions.Builder().serverSelectionTimeout(timeout) .connectTimeout(0).socketTimeout(0).build()); } Logger mongoLog = Logger.getLogger("org.mongodb.driver"); mongoLog.setLevel(Level.OFF); }
From source file:com.redhat.thermostat.gateway.common.mongodb.ThermostatMongoStorage.java
License:Open Source License
public ThermostatMongoStorage(Map<String, String> mongoConfiguration) { String username = null;//w w w . j a v a 2 s .c om char[] password = null; String host = "127.0.0.1"; int port = 27518; int timeout = 1000; String dbName = DEFAULT_DB_NAME; if (mongoConfiguration.containsKey(MongoConfiguration.MONGO_DB.toString())) { dbName = mongoConfiguration.get(MongoConfiguration.MONGO_DB.toString()); } if (mongoConfiguration.containsKey(MongoConfiguration.MONGO_USERNAME.toString())) { username = mongoConfiguration.get(MongoConfiguration.MONGO_USERNAME.toString()); } if (mongoConfiguration.containsKey(MongoConfiguration.MONGO_PASSWORD.toString())) { password = mongoConfiguration.get(MongoConfiguration.MONGO_PASSWORD.toString()).toCharArray(); } if (mongoConfiguration.containsKey(MongoConfiguration.MONGO_URL.toString())) { try { URI url = new URI(mongoConfiguration.get(MongoConfiguration.MONGO_URL.toString())); host = url.getHost(); port = url.getPort(); } catch (URISyntaxException e) { // Do nothing. Defaults will be used. } } if (mongoConfiguration.containsKey(MongoConfiguration.MONGO_SERVER_TIMEOUT.toString())) { timeout = Integer.valueOf(mongoConfiguration.get(MongoConfiguration.MONGO_SERVER_TIMEOUT.toString())); } ServerAddress address = new ServerAddress(host, port); if (username != null && password != null) { MongoCredential credential = MongoCredential.createCredential(username, dbName, password); mongoClient = new MongoClient(address, Collections.singletonList(credential), new MongoClientOptions.Builder().serverSelectionTimeout(timeout).connectTimeout(0) .socketTimeout(0).build()); } else { mongoClient = new MongoClient(address, new MongoClientOptions.Builder().serverSelectionTimeout(timeout) .connectTimeout(0).socketTimeout(0).build()); } Logger mongoLog = Logger.getLogger("org.mongodb.driver"); mongoLog.setLevel(Level.OFF); this.dbName = dbName; }
From source file:com.redhat.thermostat.web2.endpoint.command.MongoStorage.java
License:Open Source License
public static void start(final String dbName, int port) { MongoStorage.dbName = dbName;//w ww. j a va 2 s . c o m MongoCredential credential = MongoCredential.createCredential(username, dbName, password); ServerAddress address = new ServerAddress("127.0.0.1", port); mongoClient = new MongoClient(address, Collections.singletonList(credential), new MongoClientOptions.Builder().serverSelectionTimeout(0).build()); }
From source file:com.sag.tn.storm.stormmaven.bolts.CounterIncrementorBolt.java
License:Open Source License
@SuppressWarnings("rawtypes") public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) { this.collector = collector; MongoCredential credential = MongoCredential.createCredential((String) stormConf.get("MongoUser"), (String) stormConf.get("MongoDatabase"), ((String) stormConf.get("MongoPass")).toCharArray()); this.mClient = new MongoClient(new ServerAddress((String) stormConf.get("MongoHost"), Integer.parseInt((String) stormConf.get("MongoPort"))), Arrays.asList(credential)); this.db = this.mClient.getDatabase((String) stormConf.get("MongoDatabase")); this.coll = db.getCollection((String) stormConf.get("MongoColl")); }
From source file:com.sag.tn.storm.stormmaven.bolts.DocumentTypeIdentifierBolt.java
License:Open Source License
@SuppressWarnings("rawtypes") public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) { this.collector = collector; MongoCredential credential = MongoCredential.createCredential((String) stormConf.get("MongoUser"), (String) stormConf.get("MongoDatabase"), ((String) stormConf.get("MongoPass")).toCharArray()); this.mClient = new MongoClient(new ServerAddress((String) stormConf.get("MongoHost"), Integer.parseInt((String) stormConf.get("MongoPort"))), Arrays.asList(credential)); this.db = this.mClient.getDatabase((String) stormConf.get("MongoDatabase")); this.coll = db.getCollection((String) stormConf.get("MongoColl")); }
From source file:com.sitewhere.mongodb.MongoDbClient.java
License:Open Source License
@Override public void initialize(ILifecycleProgressMonitor monitor) throws SiteWhereException { try {//from w ww.jav a 2s . com MongoClientOptions.Builder builder = new MongoClientOptions.Builder(); builder.maxConnectionIdleTime(60 * 60 * 1000); // 1hour getLogger().info("MongoDB Connection: hosts=" + getHostname().getValue() + " ports=" + getConfiguration().getPort() + " replicaSet=" + getConfiguration().getReplicaSetName()); // Parse hostname(s) and port(s) into address list. List<ServerAddress> addresses = parseServerAddresses(); // Indicator for whether a replica set is being used. boolean isUsingReplicaSet = ((addresses.size() > 1) && (!StringUtils.isEmpty(getConfiguration().getReplicaSetName()))); if (isUsingReplicaSet) { getLogger().info("MongoDB using replicated mode."); } else { getLogger().info("MongoDB using standalone mode."); } // Handle authenticated access. if ((getConfiguration().getUsername() != null) && (getConfiguration().getPassword() != null)) { MongoCredential credential = MongoCredential.createCredential(getConfiguration().getUsername(), getConfiguration().getAuthDatabaseName(), getConfiguration().getPassword().toCharArray()); if (isUsingReplicaSet) { this.client = new MongoClient(addresses, Arrays.asList(credential), builder.build()); } else { this.client = new MongoClient(addresses.get(0), Arrays.asList(credential), builder.build()); } } // Handle unauthenticated access. else { if (isUsingReplicaSet) { this.client = new MongoClient(addresses, builder.build()); } else { this.client = new MongoClient(addresses.get(0), builder.build()); } } // Handle automatic configuration of replication. if ((isUsingReplicaSet) && (getConfiguration().isAutoConfigureReplication())) { doAutoConfigureReplication(addresses); } // Force interaction to test connectivity. getDatabase().listCollectionNames(); } catch (MongoTimeoutException e) { throw new SiteWhereException("Timed out connecting to MongoDB instance. " + "Verify that MongoDB is running on " + getHostname().getValue() + ":" + getConfiguration().getPort() + " and restart server.", e); } }
From source file:com.sitewhere.mongodb.MongoDbClient.java
License:Open Source License
/** * Create a connection to the primary in a replica set. * /*from w w w . j av a 2s . c o m*/ * @param addresses * @return */ protected MongoClient getPrimaryConnection(List<ServerAddress> addresses) { MongoClientOptions.Builder builder = new MongoClientOptions.Builder(); if ((getConfiguration().getUsername() != null) && (getConfiguration().getPassword() != null)) { MongoCredential credential = MongoCredential.createCredential(getConfiguration().getUsername(), getConfiguration().getAuthDatabaseName(), getConfiguration().getPassword().toCharArray()); return new MongoClient(addresses.get(0), Arrays.asList(credential), builder.build()); } else { return new MongoClient(addresses.get(0), builder.build()); } }
From source file:com.spleefleague.core.io.Config.java
public static List<MongoCredential> getCredentials() { List<MongoCredential> credentials = new ArrayList<>(); Iterator<String> i = ADDITIONAL_CONFIG.keySet().iterator(); while (i.hasNext()) { String key = i.next();// ww w . j a va2 s . co m if (key.startsWith("pw.")) { MongoCredential credential = MongoCredential.createCredential("plugin", key.substring(3), getString(key).toCharArray()); credentials.add(credential); } } return credentials; }