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:cc.acs.mongofs.gridfs.GridFSDBFile.java

License:Apache License

byte[] getChunk(int i) {
    if (_fs == null)
        throw new RuntimeException("no gridfs!");

    DBObject chunk = _fs._chunkCollection
            .findOne(BasicDBObjectBuilder.start("files_id", _id).add("n", i).get());
    if (chunk == null)
        throw new MongoException("can't find a chunk!  file id: " + _id + " chunk: " + i);

    return (byte[]) chunk.get("data");
}

From source file:cc.acs.mongofs.gridfs.GridFSFile.java

License:Apache License

public void save() {
    if (_fs == null)
        throw new MongoException("need _fs");
    _fs._filesCollection.save(this);
}

From source file:cn.vlabs.clb.server.storage.mongo.extend.MyGridFSDBFile.java

License:Apache License

byte[] getChunk(int i) {
    if (_fs == null) {
        throw new RuntimeException("no gridfs!");
    }//from w  w  w .jav  a2s .c o  m

    DBObject chunk = _fs._chunkCollection
            .findOne(BasicDBObjectBuilder.start("files_id", _id).add("n", i).get());
    if (chunk == null)
        throw new MongoException("can't find a chunk!  file id: " + _id + " chunk: " + i);

    return (byte[]) chunk.get("data");
}

From source file:cn.vlabs.clb.server.storage.mongo.extend.MyGridFSFile.java

License:Apache License

public void save() {
    if (_fs == null)
        throw new MongoException("need _fs");
    _fs.getFilesCollection().save(this);
}

From source file:cn.vlabs.clb.server.storage.mongo.extend.MyGridFSFile.java

License:Apache License

public void validate() {
    if (_fs == null)
        throw new MongoException("no _fs");
    if (_md5 == null)
        throw new MongoException("no _md5 stored");

    DBObject cmd = new BasicDBObject("filemd5", _id);
    cmd.put("root", _fs._bucketName);
    DBObject res = _fs._db.command(cmd);
    if (res != null && res.containsField("md5")) {
        String m = res.get("md5").toString();
        if (m.equals(_md5))
            return;
        throw new MongoException("md5 differ.  mine [" + _md5 + "] theirs [" + m + "]");
    }/*from w w w. j av a 2s .  com*/

    // no md5 from the server
    throw new MongoException("no md5 returned from server: " + res);

}

From source file:cn.vlabs.clb.server.storage.mongo.extend.MyGridFSInputFile.java

License:Apache License

public void save(long chunkSize) {
    if (_outputStream != null)
        throw new MongoException("cannot mix OutputStream and regular save()");

    if (!_savedChunks) {
        try {/*from   ww  w.j a v  a 2 s .  c  om*/
            saveChunks(chunkSize);
        } catch (IOException ioe) {
            throw new MongoException("couldn't save chunks", ioe);
        }
    }

    super.save();
}

From source file:cn.vlabs.clb.server.storage.mongo.extend.MyGridFSInputFile.java

License:Apache License

public int saveChunks(long chunkSize) throws IOException {
    if (_outputStream != null)
        throw new MongoException("cannot mix OutputStream and regular save()");

    if (chunkSize <= 0) {
        throw new MongoException("chunkSize must be greater than zero");
    }/*  ww w  .ja v  a  2  s  . c  o m*/

    if (_chunkSize != chunkSize) {
        _chunkSize = chunkSize;
        _buffer = new byte[(int) _chunkSize];
    }

    int bytesRead = 0;
    while (bytesRead >= 0) {
        _currentBufferPosition = 0;
        bytesRead = _readStream2Buffer();
        _dumpBuffer(_currentChunkNumber, true);
    }

    _finishChunkData();
    return _currentChunkNumber + 1;
}

From source file:com.almende.eve.state.mongo.MongoState.java

License:Apache License

/**
 * updating the entire properties object at the same time, with force flag
 * to allow overwriting of updates/*  w w  w.j a v a2s.  c om*/
 * from other instances of the state
 * 
 * @param force
 * @throws UpdateConflictException
 *             | will not throw anything when $force flag is true
 */
private synchronized boolean updateProperties(final boolean force) throws UpdateConflictException {
    final Long now = System.nanoTime();
    /* write to database */
    final WriteResult result = (force)
            ? collection.update("{_id: #}", getId()).with("{$set: {properties: #, timestamp: #}}", properties,
                    now)
            : collection.update("{_id: #, timestamp: #}", getId(), timestamp)
                    .with("{$set: {properties: #, timestamp: #}}", properties, now);
    /* check results */
    final Boolean updatedExisting = (Boolean) result.getField("updatedExisting");
    if (result.getN() == 0 && result.getError() == null) {
        throw new UpdateConflictException(timestamp);
    } else if (result.getN() != 1) {
        throw new MongoException(result.getError() + " <--- " + properties);
    }
    timestamp = now;
    return updatedExisting;
}

From source file:com.charmyin.shiro.realm.jdbc.JMongodbRealm.java

License:Apache License

private String[] getPasswordForUser(String username) throws MongoException {

    String[] result;//from   ww w  . j  a  va 2 s .  co m
    switch (saltStyle) {
    case NO_SALT:
    case CRYPT:
    case EXTERNAL:
        result = new String[1];
        break;
    default:
        result = new String[2];
    }

    try {
        // "select passphrase, CONCAT('${shiro.applicationSalt}', ':', salt) as salt from shiro_user where login_id = ?"
        User user = mongoOperations.findOne(new Query(Criteria.where("loginId").is(username)), User.class,
                "shiro_user");
        result[0] = user.getPassphrase();
        result[1] = shiroSalt + ":" + user.getSalt();
    } catch (Exception e) {
        e.printStackTrace();
        log.error(e.getMessage());
        throw new MongoException("Mongo query exception when get PasswordForUser");
    }

    return result;
}

From source file:com.daprota.m2.manager.MongoManager.java

License:Apache License

/**
 * Retrieves user via spceified userName.
 *
 * @param userName A user's userName.//from  ww  w .j  ava 2 s  .  c  o  m
* @return user if exists. Otherwise returns null.
 */
public User findUserByUserName(String userName) throws MongoException {
    DBCursor dbCur = null;

    try {
        // get a user collection
        DBCollection coll = _db.getCollection("user");
        DBObject query = new BasicDBObject("userName", userName);
        dbCur = coll.find(query);
        boolean hasNext;
        try {
            logger.debug("Call dbCur.hasNext().");
            hasNext = dbCur.hasNext();
        } catch (MongoException ex) {
            logger.error("Search for user {} failed.\n" + ex.toString(), userName);
            throw new MongoException("Search for user " + userName + " failed.");
        }
        if (hasNext) {
            DBObject user = dbCur.next();
            ArrayList<BasicDBObject> roles = (ArrayList<BasicDBObject>) user.get("roles");
            ArrayList<Role> roles2 = new ArrayList<Role>();
            if (roles != null) {
                for (BasicDBObject role : roles) {
                    Role role3 = new Role(role.get("_id").toString(), (String) role.get("name"),
                            (String) role.get("description"));
                    roles2.add(role3);
                }
                logger.debug("User {} roles added.", userName);
            }
            return (new User(user.get("_id").toString(), (String) user.get("userName"),
                    (String) user.get("password"), roles2));
        } else {
            logger.warn("User {} could not be found", userName);
            return null;
        }
    } catch (MongoException ex) {
        logger.error("Search for user {} failed.\n" + ex.toString(), userName);
        throw new MongoException("Search for user " + userName + " failed.");
    } finally {
        dbCur.close();
    }
}