Example usage for com.mongodb WriteConcern valueOf

List of usage examples for com.mongodb WriteConcern valueOf

Introduction

In this page you can find the example usage for com.mongodb WriteConcern valueOf.

Prototype

public static WriteConcern valueOf(final String name) 

Source Link

Document

Gets the WriteConcern constants by name (matching is done case insensitively).

Usage

From source file:com.esri.geoevent.transport.mongodb.MongoDBOutboundTransport.java

License:Apache License

protected void readProperties() throws ConfigurationException {
    if (hasProperty(HOST_NAME_PROPERTY))
        host = getProperty(HOST_NAME_PROPERTY).getValueAsString();
    else/*from   w  w w . j  a  v  a  2  s  . c o m*/
        host = "localhost";

    if (hasProperty(PORT_PROPERTY))
        port = ((Integer) getProperty(PORT_PROPERTY).getValue());
    else
        port = 27017;

    if (hasProperty(DATABASE_NAME_PROPERTY))
        databaseName = getProperty(DATABASE_NAME_PROPERTY).getValueAsString();
    else
        databaseName = "db";

    if (hasProperty(USER_NAME_PROPERTY))
        userName = getProperty(USER_NAME_PROPERTY).getValueAsString();
    else
        userName = "";

    if (hasProperty(PASSWORD_PROPERTY))
        password = getProperty(PASSWORD_PROPERTY).getValueAsString();
    else
        password = "";

    if (hasProperty(COLLECTION_NAME_PROPERTY))
        collectionName = getProperty(COLLECTION_NAME_PROPERTY).getValueAsString();
    else
        collectionName = "GeoEvents";

    if (hasProperty(WRITE_CONCERN_PROPERTY)) {
        String writeConcernString = getProperty(WRITE_CONCERN_PROPERTY).getValueAsString();
        writeConcern = WriteConcern.valueOf(writeConcernString);
    }
}

From source file:com.focusit.log4jmongo.appender.SimpleMongoDbAppender.java

License:Apache License

/**
 * @param writeConcern/*  w  w w  . ja v a2  s .c  o m*/
 *             The WriteConcern setting for Mongo.<i>(may be null). If null, set to default of dbCollection's writeConcern.</i>
 */
public void setWriteConcern(final String writeConcern) {
    this.writeConcern = writeConcern;
    concern = WriteConcern.valueOf(writeConcern);
}

From source file:com.focusit.log4jmongo.appender.SimpleMongoDbAppender.java

License:Apache License

protected void initialize() {
    final List<ServerAddress> addresses = getServerAddresses(hostname, port);
    mongo = getMongo(addresses);/* w  ww .  j a v a2s.c  o m*/
    mongo.setWriteConcern(WriteConcern.valueOf(getWriteConcern()));

    final MongoDatabase database = getDatabase(mongo, databaseName);

    if (userName != null && userName.trim().length() > 0) {
        // Allow password to be GCed
        password = null;
    }

    setCollection(database.getCollection(collectionName));
    initialized = true;
}

From source file:com.meltmedia.dropwizard.mongo.MongoBundle.java

License:Apache License

static WriteConcern writeConcern(String writeConcernString) {
    WriteConcern writeConcern = WriteConcern.valueOf(writeConcernString);
    if (writeConcern == null) {
        throw new IllegalArgumentException(String.format("Unknown mongo write concern %s", writeConcernString));
    }//from w ww  .  ja  va2s .  c  o m
    return writeConcern;
}

From source file:com.redhat.lightblue.mongo.crud.MongoExecutionOptions.java

License:Open Source License

public static WriteConcern getWriteConcern(ExecutionOptions options) {
    if (options != null) {
        String value = options.getOptions().get(OPT_WRITE_CONCERN);
        if (value != null) {
            value = value.trim();/*from w  w w.  j av  a 2s . c  o  m*/
            if (value.length() > 0) {
                return WriteConcern.valueOf(value);
            }
        }
    }
    return null;
}

From source file:com.socialsky.mods.MongoPersistor.java

License:Apache License

private void doSave(Message<JsonObject> message) {
    String collection = getMandatoryString("collection", message);
    if (collection == null) {
        return;//from  ww w  .  j a  va2s  .co  m
    }
    JsonObject doc = getMandatoryObject("document", message);
    if (doc == null) {
        return;
    }
    String genID;
    if (doc.getField("_id") == null) {
        genID = UUID.randomUUID().toString();
        doc.putString("_id", genID);
    } else {
        genID = null;
    }
    DBCollection coll = db.getCollection(collection);
    DBObject obj = jsonToDBObject(doc);
    WriteConcern writeConcern = WriteConcern.valueOf(getOptionalStringConfig("writeConcern", ""));
    // Backwards compatibility
    if (writeConcern == null) {
        writeConcern = WriteConcern.valueOf(getOptionalStringConfig("write_concern", ""));
    }
    if (writeConcern == null) {
        writeConcern = db.getWriteConcern();
    }
    WriteResult res = coll.save(obj, writeConcern);
    if (res.getError() == null) {
        if (genID != null) {
            JsonObject reply = new JsonObject();
            reply.putString("_id", genID);
            sendOK(message, reply);
        } else {
            sendOK(message);
        }
    } else {
        sendError(message, res.getError());
    }
}

From source file:com.socialsky.mods.MongoPersistor.java

License:Apache License

private void doUpdate(Message<JsonObject> message) {
    String collection = getMandatoryString("collection", message);
    if (collection == null) {
        return;/*w  w  w .  j a  va  2  s. c o m*/
    }
    JsonObject criteriaJson = getMandatoryObject("criteria", message);
    if (criteriaJson == null) {
        return;
    }
    DBObject criteria = jsonToDBObject(criteriaJson);

    JsonObject objNewJson = getMandatoryObject("objNew", message);
    if (objNewJson == null) {
        return;
    }
    DBObject objNew = jsonToDBObject(objNewJson);
    Boolean upsert = message.body().getBoolean("upsert", false);
    Boolean multi = message.body().getBoolean("multi", false);
    DBCollection coll = db.getCollection(collection);
    WriteConcern writeConcern = WriteConcern.valueOf(getOptionalStringConfig("writeConcern", ""));
    // Backwards compatibility
    if (writeConcern == null) {
        writeConcern = WriteConcern.valueOf(getOptionalStringConfig("write_concern", ""));
    }

    if (writeConcern == null) {
        writeConcern = db.getWriteConcern();
    }
    WriteResult res = coll.update(criteria, objNew, upsert, multi, writeConcern);
    if (res.getError() == null) {
        JsonObject reply = new JsonObject();
        reply.putNumber("number", res.getN());
        sendOK(message, reply);
    } else {
        sendError(message, res.getError());
    }
}

From source file:com.socialsky.mods.MongoPersistor.java

License:Apache License

private void doDelete(Message<JsonObject> message) {
    String collection = getMandatoryString("collection", message);
    if (collection == null) {
        return;//from  ww w . jav a  2 s  . com
    }
    JsonObject matcher = getMandatoryObject("matcher", message);
    if (matcher == null) {
        return;
    }
    DBCollection coll = db.getCollection(collection);
    DBObject obj = jsonToDBObject(matcher);
    WriteConcern writeConcern = WriteConcern.valueOf(getOptionalStringConfig("writeConcern", ""));
    // Backwards compatibility
    if (writeConcern == null) {
        writeConcern = WriteConcern.valueOf(getOptionalStringConfig("write_concern", ""));
    }

    if (writeConcern == null) {
        writeConcern = db.getWriteConcern();
    }
    WriteResult res = coll.remove(obj, writeConcern);
    int deleted = res.getN();
    JsonObject reply = new JsonObject().putNumber("number", deleted);
    sendOK(message, reply);
}

From source file:com.threecrickets.sincerity.logging.MongoDbManager.java

License:LGPL

@Override
protected void startupInternal() throws Exception {
    if (collection != null)
        return;/*from   ww  w. j  ava2  s .  c  om*/

    client = null;
    database = null;
    collection = null;

    MongoClientURI uri;
    try {
        if (this.uri == null)
            uri = new MongoClientURI(this.uri);
        else
            uri = new MongoClientURI("mongodb://localhost:27017/");
    } catch (IllegalArgumentException x) {
        throw new AppenderLoggingException("Can't parse MongoDB uri: " + this.uri);
    }

    try {
        client = new MongoClient(uri);
    } catch (MongoException x) {
        throw new AppenderLoggingException("Can't create MongoDB client: " + uri);
    }

    try {
        database = client.getDatabase(databaseName);
    } catch (MongoException x) {
        client.close();
        client = null;
        throw new AppenderLoggingException("Can't access MongoDB database: " + databaseName);
    }

    try {
        collection = database.getCollection(collectionName);
    } catch (MongoException x) {
        client.close();
        client = null;
        database = null;
        throw new AppenderLoggingException("Can't access MongoDB collection: " + collectionName);
    }

    if (writeConcernName != null) {
        WriteConcern writeConcern = WriteConcern.valueOf(writeConcernName);
        if (writeConcern == null)
            throw new AppenderLoggingException("Unsupported MongoDB write concern: " + writeConcernName);
        collection = collection.withWriteConcern(writeConcern);
    }
}

From source file:io.vertx.ext.mongo.impl.MongoClientImpl.java

License:Open Source License

private MongoCollection<JsonObject> getCollection(String name, WriteOption writeOption) {
    MongoCollection<JsonObject> coll = holder.db.getCollection(name, JsonObject.class);
    if (coll != null && writeOption != null) {
        coll = coll.withWriteConcern(WriteConcern.valueOf(writeOption.name()));
    }/* w w  w  .  j  a  v  a  2  s.  c o m*/
    return coll;
}