Permalink
Browse files

Merge pull request #1 from dashorst/patch-1

Simplify Mongo connection URL parsing by utilizing MongoURI
  • Loading branch information...
jamesward committed Nov 28, 2011
2 parents 39655b5 + 151a60d commit da53ed736aa9f0a97557d25b82a0ae252a9f6301
Showing with 7 additions and 21 deletions.
  1. +7 −21 src/main/java/com/heroku/test/Main.java
@@ -12,7 +12,7 @@
import org.eclipse.jetty.webapp.WebAppContext;
import com.mongodb.DB;
-import com.mongodb.Mongo;
+import com.mongodb.MongoURI;
/**
*
@@ -40,27 +40,13 @@ public static void main(String[] args) throws Exception{
Server server = new Server(Integer.valueOf(webPort));
WebAppContext root = new WebAppContext();
-
-
-
- //Parse mango db URL
- String mongoDbURL = System.getenv("MONGOHQ_URL");
-
- System.out.println("mongo url: " + mongoDbURL);
-
- Matcher matcher = Pattern.compile("mongodb://(.*):(.*)@(.*):(.*)/(.*)").matcher(mongoDbURL);
- matcher.find();
-
- String mongoUser = matcher.group(1);
- String mongoPassword = matcher.group(2);
- String mongoHost = matcher.group(3);
- String mongoPort = matcher.group(4);
- String mongoDatabase = matcher.group(5);
+ MongoURI mongoDbURI = new MongoURI(System.getenv("MONGOHQ_URL"));
+ DB connectedDB = mongoURI.connectDB();
-
- Mongo mongo = new Mongo(mongoHost, Integer.valueOf(mongoPort));
- DB db = mongo.getDB(mongoDatabase);
- db.authenticate(mongoUser, mongoPassword.toCharArray());
+ // allow for localhost, non-authenticated Mongo use
+ if (mongoURI.getUsername() != null) {
+ connectedDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
+ }
//Set up session handling through MongoDb
MongoSessionIdManager idMgr = new MongoSessionIdManager(server, db.getCollection("sessions"));

0 comments on commit da53ed7

Please sign in to comment.