1 package wrappers; 2 3 import java.io.BufferedReader; 4 import java.io.Console; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.util.Set; 8 9 public class Client2 { 10 static boolean DEBUG = true; 11 static final Console c = System.console(); 12 static Mongo2Interface jdi = null; 13 main(String[] args)14 public static void main(String[] args) { 15 int rc, m, i = 0; 16 boolean brc; 17 Set<String> columns; 18 String[] parms = new String[4]; 19 20 jdi = new Mongo2Interface(DEBUG); 21 22 parms[0] = getLine("URI: ", false); 23 parms[1] = getLine("DB: ", false); 24 parms[2] = null; 25 parms[3] = null; 26 27 if (parms[0] == null) 28 parms[0] = "mongodb://localhost:27017"; 29 30 if (parms[1] == null) 31 parms[1] = "test"; 32 33 rc = jdi.MongoConnect(parms); 34 35 if (rc == 0) { 36 String name, pipeline, query, fields; 37 System.out.println("Successfully connected to " + parms[1]); 38 39 while ((name = getLine("Collection: ", false)) != null) { 40 if (jdi.GetCollection(name)) 41 System.out.println("GetCollection failed"); 42 else 43 System.out.println("Collection size: " + jdi.GetCollSize()); 44 45 pipeline = getLine("Pipeline: ", false); 46 47 if (pipeline == null) { 48 query = getLine("Filter: ", false); 49 fields = getLine("Proj: ", false); 50 brc = jdi.FindColl(query, fields); 51 } else 52 brc = jdi.AggregateColl(pipeline); 53 54 System.out.println("Returned brc = " + brc); 55 56 if (!brc) { 57 for (i = 0; i < 10; i++) { 58 m = jdi.ReadNext(); 59 60 if (m > 0) { 61 columns = jdi.GetColumns(); 62 63 for (String col : columns) 64 System.out.println(col + "=" + jdi.GetField(col)); 65 66 if (pipeline == null) { 67 if (name.equalsIgnoreCase("gtst")) 68 System.out.println("gtst=" + jdi.GetField("*")); 69 70 if (name.equalsIgnoreCase("inventory")) { 71 System.out.println("warehouse=" + jdi.GetField("instock.0.warehouse")); 72 System.out.println("quantity=" + jdi.GetField("instock.1.qty")); 73 } // endif inventory 74 75 if (name.equalsIgnoreCase("restaurants")) { 76 System.out.println("score=" + jdi.GetField("grades.0.score")); 77 System.out.println("date=" + jdi.GetField("grades.0.date")); 78 } // endif restaurants 79 80 } // endif pipeline 81 82 } else if (m < 0) { 83 System.out.println("ReadNext: " + jdi.GetErrmsg()); 84 break; 85 } else 86 break; 87 88 } // endfor i 89 90 } // endif brc 91 92 } // endwhile name 93 94 rc = jdi.MongoDisconnect(); 95 System.out.println("Disconnect returned " + rc); 96 } else 97 System.out.println(jdi.GetErrmsg() + " rc=" + rc); 98 99 } // end of main 100 101 // ================================================================== getLine(String p, boolean b)102 private static String getLine(String p, boolean b) { 103 String response; 104 105 if (c != null) { 106 // Standard console mode 107 if (b) { 108 response = new String(c.readPassword(p)); 109 } else 110 response = c.readLine(p); 111 112 } else { 113 // For instance when testing from Eclipse 114 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 115 116 System.out.print(p); 117 118 try { 119 // Cannot suppress echo for password entry 120 response = in.readLine(); 121 } catch (IOException e) { 122 response = ""; 123 } // end of try/catch 124 125 } // endif c 126 127 return (response.isEmpty()) ? null : response; 128 } // end of getLine 129 130 } // end of class Client 131