Example usage for com.mongodb CommandResult toString

List of usage examples for com.mongodb CommandResult toString

Introduction

In this page you can find the example usage for com.mongodb CommandResult toString.

Prototype

@SuppressWarnings("deprecation")
public String toString() 

Source Link

Document

Returns a JSON serialization of this object

The output will look like: {"a":1, "b":["x","y","z"]} }

Usage

From source file:org.apache.whirr.service.mongodb.MongoDBReplSetMemberClusterActionHandler.java

License:Apache License

@Override
protected void afterConfigure(ClusterActionEvent event) {
    ClusterSpec clusterSpec = event.getClusterSpec();
    Cluster cluster = event.getCluster();

    LOG.info("Configuring replica set members.");

    //Get all the instances that are marked as replica set members
    Set<String> replSetRoles = Sets.newHashSet(ROLE, MongoDBArbiterClusterActionHandler.ROLE);
    Set<Cluster.Instance> replSetInstances = cluster.getInstancesMatching(anyRoleIn(replSetRoles));
    //Just grab the first of these instances, use it to send the rs.initiate()
    Cluster.Instance setLeader = replSetInstances.iterator().next();

    try {// w  w  w  .  ja  v  a 2s  . com
        Configuration config = getConfiguration(clusterSpec);
        this.arbiterPort = config.getInt(MongoDBArbiterClusterActionHandler.CFG_KEY_PORT,
                MongoDBArbiterClusterActionHandler.PORT);
    } catch (IOException e) {
        this.arbiterPort = MongoDBArbiterClusterActionHandler.PORT;
    }

    Mongo mongo;
    DB db;

    try {
        // throws IOExc, UnknownHostExc:
        LOG.info(
                "Connecting to " + setLeader.getPublicAddress().getHostAddress() + " to initiate replica set.");
        mongo = new Mongo(setLeader.getPublicAddress().getHostAddress(), PORT);
        db = mongo.getDB("admin");
        if (this.authPassword != null && this.authUsername != null) {
            db.authenticate(this.authUsername, this.authPassword.toCharArray());
        }
    } catch (Exception e) {
        LOG.error("Unable to get public host address of replica set leader, " + e.getMessage());
        return;
    }

    try {
        BasicDBObject configObject = this.generateReplicaSetConfig(replSetInstances); // throws IOexc
        LOG.info("config object:" + configObject.toString());
        BasicDBObject commandInfo = new BasicDBObject("replSetInitiate", configObject);
        LOG.info("Sending rs.initiate() command");
        CommandResult initiateResult = db.command(commandInfo);
        LOG.info("Command Result: " + initiateResult.toString());
    } catch (IOException e) {
        LOG.error("Unable to get private host addresses of replica set members, " + e.getMessage());
    } finally {
        //TODO any cleanup?
    }

}

From source file:org.basex.modules.MongoDB.java

License:BSD License

/**
 * MongoDB runCommand() function with integer as parameter.
 * @param handler// ww  w  . j a  v a 2  s.  com
 * @param command command to execute in Map or  JSON or simply String
 * @param options integer options.
 * @return Item
 * @throws Exception
 */
public Item runCommand(final Str handler, final Item command, final Int options) throws Exception {
    final DB db = getDbHandler(handler);
    db.requestStart();
    try {
        CommandResult result = null;
        if (command instanceof Map) {
            DBObject cmd = mapToDBObject((Map) command);
            if (options != null) {
                result = db.command(cmd, (DBEncoder) options.toJava());
            } else {
                result = db.command(cmd);
            }
        } else {
            result = db.command(((Str) command).toJava());
        }
        return returnResult(handler, Str.get(result.toString()));
    } catch (MongoException e) {
        throw MongoDBErrors.generalExceptionError(e);
    } finally {
        db.requestDone();
    }
}

From source file:org.basex.modules.nosql.MongoDB.java

License:BSD License

/**
 * MongoDB runCommand() function with integer as parameter.
 * @param handler database handler//from   ww  w . j ava  2 s.  c  om
 * @param command command to execute in Map or  JSON or simply String
 * @param options integer options.
 * @return Item
 * @throws Exception exception
 */
public Item runCommand(final Str handler, final Item command, final Int options) throws Exception {
    final DB db = getDbHandler(handler);
    db.requestStart();
    try {
        CommandResult result = null;
        DBObject cmd = getDbObjectFromItem(command);
        if (options != null) {
            result = db.command(cmd, (DBEncoder) options.toJava());
        } else {
            result = db.command(cmd);
        }
        return returnResult(handler, Str.get(result.toString()));
    } catch (MongoException e) {
        throw MongoDBErrors.generalExceptionError(e);
    } finally {
        db.requestDone();
    }
}

From source file:org.vertx.mods.MongoPersistor.java

License:Apache License

private void getCollectionStats(Message<JsonObject> message) {
    String collection = getMandatoryString("collection", message);

    if (collection == null) {
        return;/*from w ww.j a v a2  s . c o  m*/
    }

    DBCollection coll = db.getCollection(collection);
    CommandResult stats = coll.getStats();

    JsonObject reply = new JsonObject();
    reply.putObject("stats", new JsonObject(stats.toString()));
    sendOK(message, reply);

}

From source file:org.vertx.mods.MongoPersistor.java

License:Apache License

private void runCommand(Message<JsonObject> message) {
    JsonObject reply = new JsonObject();

    String command = getMandatoryString("command", message);

    if (command == null) {
        return;/*from  ww w.j a  va  2  s .co m*/
    }

    CommandResult result = db.command(command);

    reply.putObject("result", new JsonObject(result.toString()));
    sendOK(message, reply);
}