Skip to content

Commit

Permalink
Merge pull request #1 from dashorst/patch-1
Browse files Browse the repository at this point in the history
Simplify Mongo connection URL parsing by utilizing MongoURI
  • Loading branch information
jamesward committed Nov 28, 2011
2 parents 39655b5 + 151a60d commit da53ed7
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions src/main/java/com/heroku/test/Main.java
Expand Up @@ -12,7 +12,7 @@
import org.eclipse.jetty.webapp.WebAppContext;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoURI;

/**
*
Expand Down Expand Up @@ -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"));
Expand Down

0 comments on commit da53ed7

Please sign in to comment.