public MongoDataStore(String uri) { MongoURI mongoUri = new MongoURI(uri); try { this.mongo = new Mongo(mongoUri); } catch (UnknownHostException e) { throw new MongoException("Unknown host", e); } this.db = mongo.getDB(mongoUri.getDatabase()); }
public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException { final int port = System.getenv("PORT") != null ? Integer.valueOf(System.getenv("PORT")) : 8080; final URI baseUri = UriBuilder.fromUri("http://0.0.0.0/").port(port).build(); final Application application = Application.builder( ResourceConfig.builder().packages(BarServer.class.getPackage().getName()).build()) .build(); application.addModules(new JsonJacksonModule()); final HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(baseUri, application); httpServer .getServerConfiguration() .addHttpHandler(new StaticHttpHandler("src/main/webapp"), CONTENT_PATH); for (NetworkListener networkListener : httpServer.getListeners()) { if (System.getenv("FILE_CACHE_ENABLED") == null) { networkListener.getFileCache().setEnabled(false); } } Runtime.getRuntime() .addShutdownHook( new Thread() { @Override public void run() { httpServer.stop(); } }); MongoURI mongolabUri = new MongoURI( System.getenv("MONGOLAB_URI") != null ? System.getenv("MONGOLAB_URI") : "mongodb://127.0.0.1:27017/hello"); Mongo m = new Mongo(mongolabUri); mongoDB = m.getDB(mongolabUri.getDatabase()); if ((mongolabUri.getUsername() != null) && (mongolabUri.getPassword() != null)) { mongoDB.authenticate(mongolabUri.getUsername(), mongolabUri.getPassword()); } contentUrl = System.getenv("CONTENT_URL") != null ? System.getenv("CONTENT_URL") : CONTENT_PATH; Thread.currentThread().join(); }
public MongoTemplate fetchMongoTemplate() { try { 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; } }