|
20 | 20 |
|
21 | 21 | import java.util.*; |
22 | 22 |
|
| 23 | +import com.mongodb.util.*; |
| 24 | + |
23 | 25 | public abstract class DB { |
24 | 26 |
|
25 | 27 | static enum WriteConcern { NONE, NORMAL, STRICT }; |
@@ -191,7 +193,92 @@ public WriteConcern getWriteConcern(){ |
191 | 193 | return _concern; |
192 | 194 | } |
193 | 195 |
|
194 | | - |
| 196 | + /** |
| 197 | + * Drops this database. Removes all data on disk. Use with caution. |
| 198 | + */ |
| 199 | + public void dropDatabase() |
| 200 | + throws MongoException { |
| 201 | + |
| 202 | + BasicDBObject res = (BasicDBObject) command(new BasicDBObject("dropDatabase", 1)); |
| 203 | + |
| 204 | + if (res.getInt("ok") != 1) { |
| 205 | + throw new RuntimeException("Error - unable to drop database : " + res.toString()); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + |
| 210 | + /** |
| 211 | + * Authenticates connection/db with given name and password |
| 212 | + * |
| 213 | + * @param username name of user for this database |
| 214 | + * @param passwd password of user for this database |
| 215 | + * @return true if authenticated, false otherwise |
| 216 | + */ |
| 217 | + public boolean authenticate(String username, String passwd) |
| 218 | + throws MongoException { |
| 219 | + |
| 220 | + BasicDBObject res = (BasicDBObject) command(new BasicDBObject("getnonce", 1)); |
| 221 | + |
| 222 | + if (res.getInt("ok") != 1) { |
| 223 | + throw new MongoException("Error - unable to get nonce value for authentication."); |
| 224 | + } |
| 225 | + |
| 226 | + String nonce = res.getString("nonce"); |
| 227 | + |
| 228 | + String auth = username + ":mongo:" + passwd; |
| 229 | + String key = nonce + username + Util.hexMD5(auth.getBytes()); |
| 230 | + |
| 231 | + BasicDBObject cmd = new BasicDBObject(); |
| 232 | + |
| 233 | + cmd.put("authenticate", 1); |
| 234 | + cmd.put("user", username); |
| 235 | + cmd.put("nonce", nonce); |
| 236 | + cmd.put("key", Util.hexMD5(key.getBytes())); |
| 237 | + |
| 238 | + res = (BasicDBObject) command(cmd); |
| 239 | + |
| 240 | + return res.getInt("ok") == 1; |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Returns the last error that occurred since start of database or a call to <code>resetError()</code> |
| 245 | + * |
| 246 | + * The return object will look like |
| 247 | + * |
| 248 | + * <pre> |
| 249 | + * { err : errorMessage, nPrev : countOpsBack, ok : 1 } |
| 250 | + * </pre> |
| 251 | + * |
| 252 | + * The value for errormMessage will be null of no error has ocurred, or the message. The value of |
| 253 | + * countOpsBack will be the number of operations since the error occurred. |
| 254 | + * |
| 255 | + * Care must be taken to ensure that calls to getPreviousError go to the same connection as that |
| 256 | + * of the previous operation. See com.mongodb.Mongo.requestStart for more information. |
| 257 | + * |
| 258 | + * @return DBObject with error and status information |
| 259 | + */ |
| 260 | + public DBObject getPreviousError() |
| 261 | + throws MongoException { |
| 262 | + return command(new BasicDBObject("getpreverror", 1)); |
| 263 | + } |
| 264 | + |
| 265 | + /** |
| 266 | + * Resets the error memory for this database. Used to clear all errors such that getPreviousError() |
| 267 | + * will return no error. |
| 268 | + */ |
| 269 | + public void resetError() |
| 270 | + throws MongoException { |
| 271 | + command(new BasicDBObject("reseterror", 1)); |
| 272 | + } |
| 273 | + |
| 274 | + /** |
| 275 | + * For testing purposes only - this method forces an error to help test error handling |
| 276 | + */ |
| 277 | + public void forceError() |
| 278 | + throws MongoException { |
| 279 | + command(new BasicDBObject("forceerror", 1)); |
| 280 | + } |
| 281 | + |
195 | 282 | final String _name; |
196 | 283 | final Set<DBCollection> _seenCollections = new HashSet<DBCollection>(); |
197 | 284 |
|
|
0 commit comments