Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Simplify Mongo connection URL parsing by utilizing MongoURI #1

Merged
merged 1 commit into from

3 participants

@dashorst

I've simplified the connection URL parsing by utilizing com.mongodb.MongoURI instead of pattern matching. Since MongoURI is provided out of the box by the Mongo driver this cuts down considerably on code. Also made testing locally with anonymous a possibility.

@jamesward jamesward merged commit da53ed7 into jamesward:master
@jamesward
Owner

Thanks Martijn! That is so much simpler.

@PrakashThapa

Hi is there someway to get MongoClient from the connection url like following? I need MongoClient object so that I can close it after complete the query.
MongoClient mongoClient = (MongoClient)mongoDbURI.connect();
above code get error.
thank you

ok I got solution. might be helpful for others

MongoClientURI uri = new MongoClientURI(url.toJava());
String handler = "Client" + connections.size();
try {
MongoClient mongoClient = new MongoClient(uri);
connections.put(handler, mongoClient);
//select database
final String dbh = "DB" + dbs.size();
try {
DB db = mongoClient.getDB(uri.getDatabase());
dbs.put(dbh, db);
return Str.get(dbh);

        } catch (final MongoException ex) {
            throw new QueryException(ex);
        }

    } catch (final MongoException ex) {
        throw new QueryException(ex);
    } catch (UnknownHostException ex) {
        throw new QueryException(ex);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 28, 2011
  1. @dashorst
This page is out of date. Refresh to see the latest.
Showing with 7 additions and 21 deletions.
  1. +7 −21 src/main/java/com/heroku/test/Main.java
View
28 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"));
Something went wrong with that request. Please try again.