List of usage examples for com.mongodb ServerAddress ServerAddress
public ServerAddress(@Nullable final String host, final int port)
From source file:com.jagornet.dhcp.db.MongoLeaseManager.java
License:Open Source License
protected ServerAddress getMongoServer() throws Exception { ServerAddress serverAddress = null;//from w ww.j a v a 2 s . c o m FileInputStream fis = null; try { fis = new FileInputStream(DhcpConstants.JAGORNET_DHCP_HOME + File.separator + "conf/mongo.properties"); Properties props = new Properties(); props.load(fis); String mongoPrimary = props.getProperty("mongo.primary"); if (mongoPrimary != null) { String[] server = mongoPrimary.split(":"); if (server != null) { if (server.length > 1) { int port = Integer.parseInt(server[1]); serverAddress = new ServerAddress(server[0], port); } else { serverAddress = new ServerAddress(server[0]); } } else { serverAddress = new ServerAddress(); // fallback to default } } else { serverAddress = new ServerAddress(); // fallback to default } } finally { if (fis != null) try { fis.close(); } catch (IOException e) { } } return serverAddress; }
From source file:com.jim.im.offline.config.OfflineMongoConfig.java
License:Open Source License
@Bean public MongoClient mongoClient() { ServerAddress serverAddress = null;/* www. j a v a 2 s . c om*/ try { serverAddress = new ServerAddress(properties.getHost(), properties.getPort()); } catch (UnknownHostException e) { LOGGER.error("Init mongo client failure, cause in UnknownHostException!", e); } MongoCredential credential = MongoCredential.createScramSha1Credential(properties.getUsername(), properties.getAuthenticationDatabase(), properties.getPassword()); return new MongoClient(serverAddress, Arrays.asList(credential)); }
From source file:com.korpacz.testproject.HelloWorldMongoDBSparkFreemarkerStyle.java
public static void main(String... args) throws UnknownHostException { //ssssss//from w w w . j av a 2 s . co m final Configuration config = new Configuration(); config.setClassForTemplateLoading(HelloWorldSparkFreemarkerStyle.class, "/"); MongoClient client = new MongoClient(new ServerAddress("localhost", 27017)); DB database = client.getDB("test"); final DBCollection collection = database.getCollection("names"); Spark.get("/", new Route() { @Override public Object handle(Request rqst, Response rspns) throws Exception { Template helloTemplete = config.getTemplate("hello.ftl"); StringWriter writter = new StringWriter(); DBObject object = collection.findOne(); helloTemplete.process(object, writter); System.out.println(writter); return writter; } }); }
From source file:com.liferay.mongodb.util.MongoDBUtil.java
License:Open Source License
private List<ServerAddress> _getServerAddresses() throws Exception { List<ServerAddress> serverAddresses = new ArrayList<ServerAddress>(); for (String hostname : PortletPropsValues.SERVER_HOSTNAMES) { ServerAddress serverAddress = new ServerAddress(hostname, PortletPropsValues.SERVER_PORT); serverAddresses.add(serverAddress); }//from w w w . ja v a2 s. c o m return serverAddresses; }
From source file:com.meltmedia.dropwizard.mongo.MongoBundle.java
License:Apache License
MongoClient buildClient(MongoConfiguration configuration) { try {/*from ww w. j a v a 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.nec.strudel.tkvs.store.mongodb.MongodbStore.java
License:Apache License
List<ServerAddress> getServerAddressList(Properties props) { String[] mongos = props.getProperty("mongodb.mongos", "localhost").split(","); List<ServerAddress> list = new ArrayList<ServerAddress>(); for (String m : mongos) { String[] parts = m.split(":"); try {/*from w w w . j a v a 2s . c o m*/ if (parts.length == 2) { list.add(new ServerAddress(parts[0], Integer.parseInt(parts[1]))); } else { list.add(new ServerAddress(m)); } } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return list; }
From source file:com.norconex.collector.core.data.store.impl.mongo.MongoCrawlDataStore.java
License:Apache License
protected static DB buildMongoDB(String crawlerId, MongoConnectionDetails connDetails) { String dbName = MongoUtil.getDbNameOrGenerate(connDetails.getDatabaseName(), crawlerId); int port = connDetails.getPort(); if (port <= 0) { port = ServerAddress.defaultPort(); }//w w w .j av a2s . c om try { ServerAddress server = new ServerAddress(connDetails.getHost(), port); List<MongoCredential> credentialsList = new ArrayList<MongoCredential>(); if (StringUtils.isNoneBlank(connDetails.getUsername())) { MongoCredential credential = MongoCredential.createMongoCRCredential(connDetails.getUsername(), dbName, connDetails.getPassword().toCharArray()); credentialsList.add(credential); } MongoClient client = new MongoClient(server, credentialsList); return client.getDB(dbName); } catch (UnknownHostException e) { throw new CrawlDataStoreException(e); } }
From source file:com.openbravo.data.loader.Session.java
License:Open Source License
public void connectMongoDB() { if (!m_sappuser.isEmpty() && !m_spassword.isEmpty() && !m_database.isEmpty()) { try {//from w w w . j ava 2s . com MongoCredential credential = MongoCredential.createMongoCRCredential(m_sappuser, m_database, m_spassword.toCharArray()); m_mongoClient = new MongoClient(new ServerAddress(m_host, m_port), Arrays.asList(credential)); } catch (UnknownHostException ex) { } } else { try { m_mongoClient = new MongoClient(new ServerAddress(m_host, m_port)); } catch (UnknownHostException ex) { } } }
From source file:com.ott.bookings.auth.form.impl.MongoTockenStore.java
License:Apache License
private List<ServerAddress> getServerAddresses() throws NumberFormatException, UnknownHostException { /* build up the host list */ List<ServerAddress> hosts = new ArrayList<ServerAddress>(); String[] dbHosts = this.hostsString.split(","); for (String dbHost : dbHosts) { String[] hostInfo = dbHost.split(":"); ServerAddress address = new ServerAddress(hostInfo[0], Integer.parseInt(hostInfo[1])); hosts.add(address);//from w ww. j a v a 2 s . com } return hosts; }
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;//w w w. ja v a2 s. co 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); }