List of usage examples for com.mongodb MongoURI MongoURI
@SuppressWarnings("deprecation") @Deprecated public MongoURI(final MongoClientURI proxied)
From source file:com.appleframework.monitor.model.Project.java
License:Open Source License
public MongoTemplate fetchMongoTemplate() { try {/*from w ww.j ava2s . 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.arquivolivre.mongocom.management.CollectionManagerFactory.java
License:Apache License
/** * Create an instance of <code>Mongo</code> based on the information * provided in the configuration files located into <code>WEB-INF/conf</code>, if the instance has already been * created using the same information, so it uses the same instance. * * @param context <code>ServletContext</code> of a web application. * @return an instance of a <code>CollectionManager</code>. *//* w w w .j a v a 2 s . c o m*/ public static CollectionManager setup(ServletContext context) { try { File props = getPropertiesFile(context); if (props == null) { throw new FileNotFoundException("application or database configuration file not found."); } InputStream in = new FileInputStream(props); Properties properties = new Properties(); properties.load(in); StringBuilder builder = new StringBuilder(); builder.append(MongoURI.MONGODB_PREFIX); String user, password, host, port, dbName; user = properties.containsKey("mongocom.user") ? properties.getProperty("mongocom.user") : ""; password = properties.containsKey("mongocom.password") ? properties.getProperty("mongocom.password") : ""; host = properties.containsKey("mongocom.host") ? properties.getProperty("mongocom.host") : ""; port = properties.containsKey("mongocom.port") ? properties.getProperty("mongocom.port") : ""; dbName = properties.containsKey("mongocom.database") ? properties.getProperty("mongocom.database") : ""; if (!user.equals("")) { builder.append(user).append(":").append(password).append("@"); } if (host.equals("")) { builder.append("localhost"); } else { builder.append(host); } if (!port.equals("")) { builder.append(":"); builder.append(port); } builder.append("/"); if (!dbName.equals("")) { builder.append(dbName); } LOG.log(Level.INFO, "Mongo URI: {0}", builder.toString()); MongoURI uri = new MongoURI(builder.toString()); client = MongoClient.Holder.singleton().connect(uri); return new CollectionManager(client, dbName); } catch (IOException ex) { LOG.log(Level.SEVERE, null, ex); } return null; }
From source file:com.cloudbees.gasp.model.MongoConnection.java
License:Apache License
public void connect() throws Exception { try {//from w w w. ja va 2 s . c o m // 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.deftlabs.examples.mongo.MorphiaExample.java
License:Apache License
/** * A simple insert write concern./*from w ww.j a v a 2s.c o m*/ */ @Test public void morphiaExample() throws Exception { final Morphia morphia = new Morphia(); morphia.mapPackage("com.deftlabs.examples.mongo"); final Datastore datastore = morphia.createDatastore(new Mongo(new MongoURI("mongodb://127.0.0.1:27017")), "mongo-java-driver-intro"); // Create the object(s) final ObjectId docId = ObjectId.get(); final TestEntity test = new TestEntity(); test.setId(docId); test.setName("NameValueTest"); final Child child = new Child(); child.setChildName("NameValueTestChild"); test.setChild(child); // Save datastore.save(test); // Query for the entity. final TestEntity findTest = datastore.find(TestEntity.class, "_id", docId).get(); assertNotNull(findTest); assertNotNull(findTest.getChild()); }
From source file:com.deftlabs.examples.mongo.TailableCursorExample.java
License:Apache License
public static void main(final String[] pArgs) throws Exception { final Mongo mongo = new Mongo(new MongoURI("mongodb://127.0.0.1:27017")); mongo.getDB("testTailableCursor").dropDatabase(); // Create the capped collection final BasicDBObject conf = new BasicDBObject("capped", true); conf.put("size", 20971520); // 20 MB mongo.getDB("testTailableCursor").createCollection("test", conf); final AtomicBoolean readRunning = new AtomicBoolean(true); final AtomicBoolean writeRunning = new AtomicBoolean(true); final AtomicLong writeCounter = new AtomicLong(0); final AtomicLong readCounter = new AtomicLong(0); final ArrayList<Thread> writeThreads = new ArrayList<Thread>(); final ArrayList<Thread> readThreads = new ArrayList<Thread>(); for (int idx = 0; idx < 10; idx++) { final Thread writeThread = new Thread(new Writer(mongo, writeRunning, writeCounter)); final Thread readThread = new Thread(new Reader(mongo, readRunning, readCounter)); writeThread.start();//from w w w .j ava 2s . c om readThread.start(); writeThreads.add(writeThread); readThreads.add(readThread); } // Run for five minutes //Thread.sleep(300000); Thread.sleep(20000); writeRunning.set(false); Thread.sleep(5000); readRunning.set(false); Thread.sleep(5000); for (final Thread readThread : readThreads) readThread.interrupt(); for (final Thread writeThread : writeThreads) writeThread.interrupt(); System.out.println("----- write count: " + writeCounter.get()); System.out.println("----- read count: " + readCounter.get()); }
From source file:com.deftlabs.logging.mongo.MongoHandler.java
License:Apache License
/** * Returns the configured collection./*from www . j a v a 2 s. c om*/ */ private DBCollection getCollection() throws UnknownHostException { if (_collection != null) return _collection; synchronized (sMutex) { if (_collection != null) return _collection; _mongo = new Mongo(new MongoURI(_mongoUri)); final DB db = _mongo.getDB(_databaseName); _collection = _mongo.getDB(_databaseName).getCollection(_collectionName); return _collection; } }
From source file:com.flyingdonut.implementation.persistence.MongoDBConnection.java
License:Apache License
private void mongoURIInit() { MongoURI uri = new MongoURI(getMongoURI()); try {//from w w w .ja v a2 s. 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:com.github.trask.sandbox.mongodb.MongoDatastoreProvider.java
License:Apache License
public AdvancedDatastore get() { if (initMorphiaLogging) { MorphiaLoggerFactory.registerLogger(SLF4JLogrImplFactory.class); initMorphiaLogging = false;/* w w w . j av a 2 s . c o m*/ } Mongo mongo; try { mongo = new Mongo(new MongoURI(uri)); } catch (MongoException e) { throw new IllegalStateException(e); } catch (UnknownHostException e) { throw new IllegalStateException(e); } Morphia morphia = new Morphia(); AdvancedDatastore datastore = (AdvancedDatastore) morphia.createDatastore(mongo, dbName); datastore.ensureIndexes(); datastore.ensureCaps(); return datastore; }
From source file:com.ijuru.ijambo.Context.java
License:Open Source License
/** * Connects to the database// w w w. j a va2 s . co m * @return the Mongo DB * @throws ParseException * @throws UnknownHostException */ public static DB connectDatabase() throws ParseException, UnknownHostException { // Check for AppFog environment String appFogEnv = System.getenv("VCAP_SERVICES"); if (appFogEnv != null) { // Connect to MongoDB as AppFog service JSONParser parser = new JSONParser(); JSONObject svcs = (JSONObject) parser.parse(appFogEnv); JSONArray mongoSettings = (JSONArray) svcs.get("mongodb-1.8"); JSONObject mongoSettings0 = (JSONObject) mongoSettings.get(0); JSONObject mongoCreds0 = (JSONObject) mongoSettings0.get("credentials"); //String db = (String)mongoCreds0.get("db"); String connectionURL = (String) mongoCreds0.get("url"); log.info("Running as AppFog instance with connection URL: " + connectionURL); DB db = new MongoURI(connectionURL).connectDB(); // https://jira.mongodb.org/browse/JAVA-436 db.authenticate((String) mongoCreds0.get("username"), ((String) mongoCreds0.get("password")).toCharArray()); return db; } // Create default MongoDB instance m = new Mongo(); return m.getDB("ijambo"); }
From source file:com.jaspersoft.mongodb.connection.MongoDbConnection.java
License:Open Source License
private void create(String mongoURI) throws JRException { close();/* w ww. ja v a 2 s . com*/ try { client = new Mongo(mongoURIObject = new MongoURI(mongoURI)); } catch (Exception e) { logger.error(e); throw new JRException(e.getMessage()); } }