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:net.luminis.useradmin.mongodb.MongoSerializerHelper.java

License:Apache License

/**
 * Finds an existing member by its name.
 * /*from   ww  w  . j  a v  a2  s .c  o m*/
 * @param name the name of the member to return, cannot be <code>null</code>.
 * @return a member instance, never <code>null</code>.
 * @throws MongoException in case the requested member was not found (or any other MongoDB exception).
 */
private Role findExistingMember(String name) {
    Role result = m_roleProvider.getRole(name);
    if (result == null) {
        throw new MongoException("No such role: " + name);
    }
    return result;
}

From source file:net.vz.mongodb.jackson.WriteResult.java

License:Apache License

/**
 * Get the object that was saved.  This will contain the updated ID if the ID was generated.
 * <p/>/*  w ww.java2 s  . c  o  m*/
 * Note, this operation is a little expensive because it has to deserialise the object.  If you just want the ID,
 * call getSavedId() instead.
 *
 * @return The saved object
 * @throws MongoException If no objects were saved
 */
public T getSavedObject() {
    if (dbObjects.length == 0) {
        throw new MongoException("No objects to return");
    }
    return getSavedObjects()[0];
}

From source file:net.vz.mongodb.jackson.WriteResult.java

License:Apache License

/**
 * Get the saved ID.  This may be useful for finding out the ID that was generated by MongoDB if no ID was supplied.
 *
 * @return The saved ID/*ww w. j a  v  a 2s  .  co m*/
 * @throws MongoException If no objects were saved
 */
public K getSavedId() {
    if (dbObjects.length == 0) {
        throw new MongoException("No objects to return");
    }
    return jacksonDBCollection.convertFromDbId(dbObjects[0].get("_id"));
}

From source file:net.vz.mongodb.jackson.WriteResult.java

License:Apache License

/**
 * Get the underlying DBObject that was serialised before it was saved.  This will contain the updated ID if an
 * ID was generated.//ww  w . j ava2 s  . c o m
 *
 * @return The underlying DBObject
 * @throws MongoException If no objects were saved
 */
public DBObject getDbObject() {
    if (dbObjects.length == 0) {
        throw new MongoException("No objects to return");
    }
    return dbObjects[0];
}

From source file:NexT.db.mongo.MongoWrapper.java

License:GNU General Public License

public MongoWrapper(String user, String pass, String database, String host, int port) throws DBException {
    super(user, pass, database, host, port);
    try {/*from  w  w w.jav a 2s  .  c o  m*/
        LOG.log(Level.INFO, "[MongoDB] Connecting.");
        client = new Mongo(host, port);
        db = client.getDB(database);

        if (user != null && pass != null) {
            if (!db.authenticate(user, pass.toCharArray())) {
                LOG.log(Level.SEVERE, "[MongoDB] Authentication with user " + user + " failed!");
                throw new MongoException("Failed to authenticate");
            } else {
                LOG.log(Level.INFO, "[MongoDB] Authenticaton with user " + user + " successful.");
            }
        }

        MongoWrapper.INSTANCE = this;
    } catch (UnknownHostException ex) {
        LOG.log(Level.SEVERE, "[MongoDB] Failed to start connection.", ex);
        throw new MongoException("Failed to start connection", ex);
    }
}

From source file:org.apache.chemistry.opencmis.mongodb.MongodbUtils.java

License:Apache License

public BasicDBObject addNode(BasicDBObject node, BasicDBObject parent) {
    // Update all affected right and left values which are greater or equal
    // to the parents right value - we are incrementing to 'make room' for the 
    // new node/* w w  w.j  a va  2 s  . c o m*/
    db.getCollection(COLLECTION_CONTENT).update(
            new BasicDBObject().append("right", new BasicDBObject().append("$gte", parent.getLong("right"))),
            new BasicDBObject().append("$inc", new BasicDBObject().append("right", 2)), false, true);

    db.getCollection(COLLECTION_CONTENT).update(
            new BasicDBObject().append("left", new BasicDBObject().append("$gte", parent.getLong("right"))),
            new BasicDBObject().append("$inc", new BasicDBObject().append("left", 2)), false, true);

    // Finally insert the node into the created space in the tree, under the parent
    node.append("left", parent.getLong("right")).append("right", parent.getLong("right") + 1).append("level",
            parent.getLong("level") + 1);

    WriteResult result = db.getCollection(COLLECTION_CONTENT).insert(node);
    if (result.getN() != 1) {
        throw new MongoException("Error while inserting the node into the database.");
    } else {
        return node.append("_id", result.getUpsertedId());
    }
}

From source file:org.apache.chemistry.opencmis.mongodb.MongodbUtils.java

License:Apache License

public void removeNode(BasicDBObject node) {
    // Update all affected right and left values which are greater or equal
    // to the parents right value - we are incrementing to compress the void 
    // left by the removal of the node
    db.getCollection(COLLECTION_CONTENT).update(
            new BasicDBObject().append("right", new BasicDBObject().append("$gt", node.getLong("right"))),
            new BasicDBObject().append("$inc", new BasicDBObject().append("right", -2)), false, true);

    db.getCollection(COLLECTION_CONTENT).update(
            new BasicDBObject().append("left", new BasicDBObject().append("$gt", node.getLong("right"))),
            new BasicDBObject().append("$inc", new BasicDBObject().append("left", -2)), false, true);

    // Finally remove the node 

    WriteResult result = db.getCollection(COLLECTION_CONTENT).remove(node);
    if (result.getN() != 1) {
        throw new MongoException("Error while removing the node from the database.");
    }/*  ww  w . j a  va2  s .  c o m*/
}

From source file:org.apache.felix.useradmin.mongodb.MongoDB.java

License:Apache License

/**
 * Returns the database collection to work in.
 * /*from  www. j a v a 2s.  co m*/
 * @return the {@link DBCollection}, never <code>null</code>.
 * @throws MongoException in case no connection to Mongo exists.
 */
public DBCollection getCollection() {
    Mongo mongo = m_mongoRef.get();
    if (mongo == null) {
        throw new MongoException("Not connected to MongoDB!");
    }
    DB db = mongo.getDB(m_dbName);
    return db.getCollection(m_collectionName);
}

From source file:org.apache.felix.useradmin.mongodb.MongoDBStore.java

License:Apache License

/**
 * Creates a connection to MongoDB using the given credentials.
 * /*w  w  w  .java2 s  . c  o m*/
 * @param mongoDB the {@link MongoDB} facade to connect to;
 * @param userName the (optional) user name to use;
 * @param password the (optional) password to use.
 * @throws MongoException in case the connection or authentication failed.
 */
private void connectToDB(MongoDB mongoDB, String userName, String password) throws MongoException {
    if (!mongoDB.connect(userName, password)) {
        throw new MongoException("Failed to connect to MongoDB! Authentication failed!");
    }

    DBCollection collection = mongoDB.getCollection();
    if (collection == null) {
        throw new MongoException("Failed to connect to MongoDB! No collection returned!");
    }

    collection.ensureIndex(new BasicDBObject(NAME, 1).append("unique", true));
}

From source file:org.apache.felix.useradmin.mongodb.MongoDBStore.java

License:Apache License

/**
 * Returns the current database collection.
 * //from  w  w  w. j  a  va 2 s .  co  m
 * @return the database collection to work with, cannot be <code>null</code>.
 * @throws MongoException in case no connection to MongoDB exists.
 */
private DBCollection getCollection() {
    MongoDB mongoDB = m_mongoDbRef.get();
    if (mongoDB == null) {
        throw new MongoException("No connection to MongoDB?!");
    }
    return mongoDB.getCollection();
}