Example usage for com.mongodb MongoException MongoException

List of usage examples for com.mongodb MongoException MongoException

Introduction

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

Prototype

public MongoException(final String msg) 

Source Link

Usage

From source file:org.mule.module.mongo.api.MongoClientImpl.java

License:Open Source License

public DBObject findOneFile(final DBObject query) {
    Validate.notNull(query);/*from w  ww.ja  v  a2 s.  com*/
    final GridFSDBFile file = getGridFs().findOne(query);
    if (file == null) {
        throw new MongoException("No file found for query " + query);
    }
    return file;
}

From source file:org.mule.modules.morphia.MorphiaConnector.java

License:Open Source License

/**
 * Add a new user to the database//w  w  w .j  a  v  a2s  .c  o m
 * <p/>
 * {@sample.xml ../../../doc/mule-module-morphia.xml.sample morphia:add-user}
 *
 * @param newUsername    Username to be created
 * @param newPassword    Password that will be used for authentication
 * @param targetDatabase Database at which this user will be created. It defaults to the current one if not specified.
 * @param username the username to use in case authentication is required
 * @param password the password to use in case authentication is required, null
 *                 if no authentication is desired
 * @param host     The host of the Mongo server. If the host is part of a replica set then you can specify all the hosts
 *                 separated by comma.
 * @param port     The port of the Mongo server
 * @param database The database name of the Mongo server
 * @throws ExecutionException if there is a connection exception
 */
@Processor
public void addUser(String newUsername, String newPassword, @Optional String targetDatabase,
        @Optional String username, @Optional @Password String password, @Optional String host,
        @Optional Integer port, @Optional String database) throws ExecutionException {
    Datastore datastore = getDatastore(username, password, database, host, port);
    WriteResult writeResult = null;
    if (targetDatabase == null) {
        writeResult = datastore.getDB().addUser(newUsername, newPassword.toCharArray());
    } else {
        writeResult = datastore.getDB().getMongo().getDB(targetDatabase).addUser(newUsername,
                newPassword.toCharArray());
    }
    if (!writeResult.getLastError().ok()) {
        throw new MongoException(writeResult.getLastError().getErrorMessage());
    }
}

From source file:org.mule.transport.mongodb.MongoDBMessageDispatcher.java

License:Open Source License

protected BasicDBObject update(BasicDBObject object, DB db, String collection, MuleEvent event)
        throws Exception {
    logger.debug(String.format("Updating collection %s in DB %s: %s", collection, db, object));

    boolean upsert = false;
    boolean multi = false;

    MuleMessage message = event.getMessage();

    if (message.getOutboundPropertyNames().contains(MongoDBConnector.MULE_MONGO_UPDATE_UPSERT)) {
        if (message.getOutboundProperty(MongoDBConnector.MULE_MONGO_UPDATE_UPSERT).equals("true"))
            upsert = true;/*  w  ww.  j  a v a 2s  .c o  m*/
    }

    if (event.getEndpoint().getProperties().containsKey("upsert")) {
        if (event.getEndpoint().getProperty("upsert").equals("true"))
            upsert = true;
    }

    if (message.getOutboundPropertyNames().contains(MongoDBConnector.MULE_MONGO_UPDATE_MULTI)) {
        if (message.getOutboundProperty(MongoDBConnector.MULE_MONGO_UPDATE_MULTI).equals("true"))
            multi = true;
    }

    if (event.getEndpoint().getProperties().containsKey("multi")) {
        if (event.getEndpoint().getProperty("multi").equals("true"))
            multi = true;
    }

    DBObject objectToUpdate;

    if (!message.getOutboundPropertyNames().contains(MongoDBConnector.MULE_MONGO_UPDATE_QUERY)
            && !event.getEndpoint().getProperties().containsKey("updateQuery")) {
        logger.debug(String.format("%s property is  not set, updating object using _id value of payload",
                MongoDBConnector.MULE_MONGO_UPDATE_QUERY));

        objectToUpdate = db.getCollection(collection)
                .findOne(new BasicDBObject("_id", new ObjectId(object.get("_id").toString())));

        if (!upsert) {
            throw new MongoException("Object not found from update query and upsert is false");
        }
    } else {
        String updateQuery = message.getOutboundProperty(MongoDBConnector.MULE_MONGO_UPDATE_QUERY);

        if (updateQuery == null) {
            updateQuery = (String) event.getEndpoint().getProperty("updateQuery");
        }

        String evaluatedQuery = message.getMuleContext().getExpressionManager().parse(updateQuery, message);

        logger.debug(String.format("%s property is set, building query from %s",
                MongoDBConnector.MULE_MONGO_UPDATE_QUERY, evaluatedQuery));

        objectToUpdate = (DBObject) JSON.parse(evaluatedQuery);

        if (objectToUpdate == null)
            throw new MongoException("Could not find create update query from: " + updateQuery);
    }

    WriteConcern writeConcern = WriteConcernFactory.getWriteConcern(event);

    if (writeConcern == null) {
        db.getCollection(collection).update(objectToUpdate, object, upsert, multi);
    } else {
        logger.debug("Using WriteConcern value " + writeConcern);
        db.getCollection(collection).update(objectToUpdate, object, upsert, multi, writeConcern);
    }

    return object;
}

From source file:org.springframework.data.document.mongodb.monitor.AbstractMonitor.java

License:Apache License

public CommandResult getServerStatus() {
    CommandResult result = getDb("admin").command("serverStatus");
    if (!result.ok()) {
        logger.error("Could not query for server status.  Command Result = " + result);
        throw new MongoException("could not query for server status.  Command Result = " + result);
    }/*from  www.java  2  s.com*/
    return result;
}

From source file:org.unitedid.shibboleth.attribute.resolver.provider.dataConnector.MongoDbDataConnector.java

License:Apache License

/**
 * Creates the mongo database connection
 *//*from   w  w  w  . j  a va  2  s .c  o m*/
protected void initializeMongoDbConnection() {
    if (initialized) {
        log.debug("MongoDB connector initializing!");
        Mongo mongoCon = new Mongo(mongoHost);
        mongoCon.setReadPreference(getPreferredRead());
        db = mongoCon.getDB(mongoDbName);

        if (getMongoUser() != null && getMongoPassword() != null) {
            boolean dbAuth = db.authenticate(getMongoUser(), getMongoPassword().toCharArray());
            if (!dbAuth) {
                log.error(
                        "MongoDB data connector {} authentication failed for database {}, username or password!",
                        getId(), mongoDbName);
                throw new MongoException("MongoDB data connector " + getId() + " authentication failed!");
            } else {
                log.debug("MongoDB data connector {} authentication successful!", getId());
            }
        }
    }
}

From source file:org.venice.piazza.servicecontroller.data.mongodb.accessors.MongoAccessor.java

License:Apache License

/**
 * Gets the Service Job for the specified service with the specified Job ID
 * /*  www. j  a va2s .  c  o  m*/
 * @param serviceId
 *            The ID of the Service
 * @param jobId
 *            The ID of the Job
 * @return
 */
public ServiceJob getServiceJob(String serviceId, String jobId) throws MongoException {
    BasicDBObject query = new BasicDBObject("jobId", jobId);
    ServiceJob serviceJob;

    try {
        serviceJob = getServiceJobCollection(serviceId).findOne(query);
    } catch (MongoTimeoutException mte) {
        String error = "Mongo Instance Not Available.";
        LOGGER.error(error, mte);
        throw new MongoException(error);
    }

    return serviceJob;
}