Permalink
Browse files

JAVA-691: Generalized examples with command line args

  • Loading branch information...
1 parent 393bb8b commit 6354df53249720ee0f11c0e19cdd443aedf57faa @jyemin jyemin committed Dec 4, 2012
Showing with 31 additions and 40 deletions.
  1. +16 −20 examples/CramMd5CredentialsExample.java
  2. +15 −20 examples/GSSAPICredentialsExample.java
@@ -20,44 +20,40 @@
import com.mongodb.MongoClientAuthority;
import com.mongodb.MongoClientCredentials;
import com.mongodb.MongoClientOptions;
-import com.mongodb.MongoException;
import com.mongodb.ServerAddress;
import com.mongodb.WriteResult;
import java.net.UnknownHostException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
/**
* Example usage of CRAM-MD5 credentials.
+ * <p>
+ * Usage:
+ * </p>
+ * <pre>
+ * java CramMd5CredentialsExample server userName password databaseName
+ * </pre>
*/
public class CramMd5CredentialsExample {
public static void main(String[] args) throws UnknownHostException, InterruptedException {
+ String server = args[0];
+ String user = args[1];
+ String pwd = args[2];
+ String db = args[3];
MongoClient mongo = new MongoClient(
- new MongoClientAuthority(new ServerAddress("kdc.10gen.me"),
- new MongoClientCredentials("dev0", "a".toCharArray(),
- MongoClientCredentials.CRAM_MD5_MECHANISM, "test")),
+ new MongoClientAuthority(new ServerAddress(server),
+ new MongoClientCredentials(user, pwd.toCharArray(),
+ MongoClientCredentials.CRAM_MD5_MECHANISM, db)),
new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
- DB testDB = mongo.getDB("test");
+ DB testDB = mongo.getDB(db);
System.out.println("Find one: " + testDB.getCollection("test").findOne());
System.out.println("Count: " + testDB.getCollection("test").count());
WriteResult writeResult = testDB.getCollection("test").insert(new BasicDBObject());
System.out.println("Write result: " + writeResult);
System.out.println();
- System.out.println("Trying a query once every 15 seconds...");
- System.out.println();
- for (; ; ) {
- try {
- System.out.println(new SimpleDateFormat().format(new Date()));
- System.out.println("Count: " + testDB.getCollection("test").count());
- System.out.println();
- } catch (MongoException e) {
- e.printStackTrace();
- System.out.println();
- }
- Thread.sleep(15000);
- }
+ System.out.println("Count: " + testDB.getCollection("test").count());
+
}
}
@@ -20,17 +20,20 @@
import com.mongodb.MongoClientAuthority;
import com.mongodb.MongoClientCredentials;
import com.mongodb.MongoClientOptions;
-import com.mongodb.MongoException;
import com.mongodb.ServerAddress;
import com.mongodb.WriteResult;
import java.net.UnknownHostException;
import java.security.Security;
-import java.text.SimpleDateFormat;
-import java.util.Date;
/**
* Example usage of Kerberos (GSSAPI) credentials.
+ * <p>
+ * Usage:
+ * </p>
+ * <pre>
+ * java CramMd5CredentialsExample server userName databaseName
+ * </pre>
*/
public class GSSAPICredentialsExample {
@@ -47,30 +50,22 @@ public static void main(String[] args) throws UnknownHostException, InterruptedE
// for username/password
Security.setProperty("auth.login.defaultCallbackHandler", "DefaultSecurityCallbackHandler");
+ String server = args[0];
+ String user = args[1];
+ String dbName = args[2];
+
MongoClient mongo = new MongoClient(
- new MongoClientAuthority(new ServerAddress("kdc.10gen.me"),
- new MongoClientCredentials("dev1@10GEN.ME", MongoClientCredentials.GSSAPI_MECHANISM)),
+ new MongoClientAuthority(new ServerAddress(server),
+ new MongoClientCredentials(user, MongoClientCredentials.GSSAPI_MECHANISM)),
new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
- DB testDB = mongo.getDB("test");
+ DB testDB = mongo.getDB(dbName);
System.out.println("Find one: " + testDB.getCollection("test").findOne());
System.out.println("Count: " + testDB.getCollection("test").count());
WriteResult writeResult = testDB.getCollection("test").insert(new BasicDBObject());
System.out.println("Write result: " + writeResult);
System.out.println();
- System.out.println("Trying a query once every 15 seconds...");
- System.out.println();
- for (; ; ) {
- try {
- System.out.println(new SimpleDateFormat().format(new Date()));
- System.out.println("Count: " + testDB.getCollection("test").count());
- System.out.println();
- } catch (MongoException e) {
- e.printStackTrace();
- System.out.println();
- }
- Thread.sleep(15000);
- }
+ System.out.println("Count: " + testDB.getCollection("test").count());
}
-}
+}

0 comments on commit 6354df5

Please sign in to comment.