List of usage examples for com.mongodb ServerAddress ServerAddress
public ServerAddress(@Nullable final String host, final int port)
From source file:piecework.config.MongoConfiguration.java
License:Educational Community License
@Bean @Primary/*from ww w .j av a 2s .co m*/ public Mongo mongo() throws Exception { if (environment.acceptsProfiles("embedded-mongo")) { mongoInstance = embeddedMongo(); mongoInstance.startEmbeddedMongo(); mongoInstance.importData(); return new MongoClient(Collections.singletonList(new ServerAddress("127.0.0.1", 37017))); } MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder(); if (environment.getProperty("mongo.use.ssl", Boolean.class, Boolean.FALSE)) optionsBuilder.socketFactory(SSLSocketFactory.getDefault()); return new MongoClient(getServerAddresses(), optionsBuilder.build()); }
From source file:piecework.config.MongoConfiguration.java
License:Educational Community License
private List<ServerAddress> getServerAddresses() throws UnknownHostException { List<ServerAddress> serverAddresses = new LinkedList<ServerAddress>(); String mongoServerAddresses = environment.getProperty("mongo.server.addresses", "127.0.0.1:27017"); String[] addresses = mongoServerAddresses.split(","); for (String address : addresses) { String ip = null;//from w w w . j ava 2s . c om int port = 27017; String[] tokens = address.split(":"); if (tokens.length > 0) { ip = tokens[0]; if (tokens.length > 1) { port = Integer.parseInt(tokens[1]); } } serverAddresses.add(new ServerAddress(ip, port)); } return serverAddresses; }
From source file:redmart.crm.product.dao.ProductDAO.java
public ProductDAO() { this.host = ApplicationConstants.MONGO_DB_HOST; this.port = ApplicationConstants.MONGO_DB_PORT; this.databaseName = ApplicationConstants.MONGO_DB_NAME; this.userName = ApplicationConstants.MONGO_DB_USER; this.password = ApplicationConstants.MONGO_DB_PWD; List<MongoCredential> credentialsList = new ArrayList<MongoCredential>(); MongoCredential credentia = MongoCredential.createCredential(userName, databaseName, password.toCharArray());//from w w w . j a v a2 s. c o m credentialsList.add(credentia); ServerAddress addr = new ServerAddress(host, port); MongoClient mongoClient = new MongoClient(addr, credentialsList); Morphia morphia = new Morphia(); morphia.mapPackage(ApplicationConstants.MONGO_PACKAGE); datastore = morphia.createDatastore(mongoClient, databaseName); datastore.ensureIndexes(); }
From source file:rmteles.learning.mongodb.examples.week2.spark.HelloWorldMongoDBSparkFreemarkerStyle.java
License:Apache License
public static void main(String[] args) { final Configuration configuration = new Configuration(); configuration.setClassForTemplateLoading(HelloWorldMongoDBSparkFreemarkerStyle.class, "/freemarker"); MongoClient client = new MongoClient(new ServerAddress("localhost", 27017)); MongoDatabase database = client.getDatabase("course"); final MongoCollection<Document> collection = database.getCollection("hello"); collection.drop();// ww w .j a v a 2s . com collection.insertOne(new Document("name", "MongoDB")); Spark.get("/", (request, response) -> { StringWriter writer = new StringWriter(); try { Template helloTemplate = configuration.getTemplate("hello.ftl"); Document document = collection.find().first(); helloTemplate.process(document, writer); } catch (Exception e) { Spark.halt(500); e.printStackTrace(); } return writer; }); }
From source file:ro.springsoft.mongoadapter.jndi.MongoClientTomcatFactory.java
License:Open Source License
private List<ServerAddress> getMongoSeeds(String[] hosts, String[] ports) { List<ServerAddress> seeds = new LinkedList<ServerAddress>(); if (hosts == null || ports == null || hosts.length != ports.length) { LOG.severe("Different nr. of hosts and ports or parsed hosts|ports = null !!!"); } else {/*from ww w. j a v a 2 s .co m*/ for (int i = 0; i < hosts.length; i++) { try { seeds.add(new ServerAddress(hosts[i], Integer.parseInt(ports[i]))); } catch (UnknownHostException ex) { LOG.log(Level.SEVERE, "Cannot parse mongo port: {0}", ports[i]); } } } return seeds; }
From source file:ru.frostman.web.mongo.config.MongoServerAddress.java
License:Apache License
public ServerAddress toServerAddress() throws UnknownHostException { return new ServerAddress(host, port); }
From source file:se.inera.axel.riv.impl.MongoDBTestContextConfig.java
License:Open Source License
public @Bean(destroyMethod = "close") Mongo mongo() throws Exception { MongodProcess mongodProcess = mongodProcess(); return new Mongo(new ServerAddress(mongodProcess.getConfig().net().getServerAddress(), mongodProcess.getConfig().net().getPort())); }
From source file:Server.Service.java
private void DBConnect() { try {//w ww .ja v a 2 s .c o m mongo = new Mongo(new ServerAddress("", 27017)); db = mongo.getDB("bank"); } catch (UnknownHostException | MongoException e) { this.DBShutdown(); } }
From source file:shionn.jug.database.MongoClientHolder.java
License:GNU General Public License
private ServerAddress buildServerAdress(Properties conf) throws UnknownHostException { String host = conf.getProperty("host"); int port = Integer.parseInt(conf.getProperty("port")); return new ServerAddress(host, port); }
From source file:Singletons.SharedMongoMorphiaInstance.java
License:Open Source License
public static void loadConfiguration() { try {// w w w . j ava2 s .c om MongoCredential credential = MongoCredential.createMongoCRCredential("admin", "admin", "$$PASdefumee1984".toCharArray()); ServerAddress server = new ServerAddress(Admin.ipMongo(), 27017); mongoClient = new MongoClient(server, Arrays.asList(credential)); morphia = new Morphia(); dsAccessToken = morphia.createDatastore(mongoClient, "AccessToken"); dsJobsInfos = morphia.createDatastore(mongoClient, "JobInfo"); dsSessions = morphia.createDatastore(mongoClient, "Session"); dsTweets = morphia.createDatastore(mongoClient, "Tweet"); dsUsers = morphia.createDatastore(mongoClient, "User"); morphia.map(AccessTokenPlus.class); morphia.map(JobInfo.class); morphia.map(Session.class); morphia.map(Tweet.class); morphia.map(User.class); } catch (UnknownHostException ex) { Logger.getLogger(SharedMongoMorphiaInstance.class.getName()).log(Level.SEVERE, null, ex); } }