Example usage for com.mongodb CommandResult getException

List of usage examples for com.mongodb CommandResult getException

Introduction

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

Prototype

@Nullable
public MongoException getException() 

Source Link

Document

Utility method to create an exception from a failed command.

Usage

From source file:org.apache.camel.component.mongodb.MongoDbProducer.java

License:Apache License

private void processAndTransferWriteResult(WriteResult result, Exchange exchange) {
    // if invokeGetLastError is set, or a WriteConcern is set which implicitly calls getLastError, then we have the chance to populate 
    // the MONGODB_LAST_ERROR header, as well as setting an exception on the Exchange if one occurred at the MongoDB server
    if (endpoint.isInvokeGetLastError()
            || (endpoint.getWriteConcern() != null ? endpoint.getWriteConcern().callGetLastError() : false)) {
        CommandResult cr = result.getCachedLastError() == null ? result.getLastError()
                : result.getCachedLastError();
        exchange.getOut().setHeader(MongoDbConstants.LAST_ERROR, cr);
        if (!cr.ok()) {
            exchange.setException(MongoDbComponent.wrapInCamelMongoDbException(cr.getException()));
        }//from  ww  w.j  a  va  2 s .  c o  m
    }

    // determine where to set the WriteResult: as the OUT body or as an IN message header
    if (endpoint.isWriteResultAsHeader()) {
        exchange.getOut().setHeader(MongoDbConstants.WRITERESULT, result);
    } else {
        exchange.getOut().setBody(result);
    }
}

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.java

License:Apache License

@Override
public long determineServerTimeDifferenceMillis() {
    // the assumption is that the network delay from this instance
    // to the server, and from the server back to this instance
    // are (more or less) equal.
    // taking this assumption into account allows to remove
    // the network delays from the picture: the difference
    // between end and start time is exactly this network
    // delay (plus some server time, but that's neglected).
    // so if the clocks are in perfect sync and the above
    // mentioned assumption holds, then the server time should
    // be exactly at the midPoint between start and end.
    // this should allow a more accurate picture of the diff.
    final long start = System.currentTimeMillis();
    // assumption here: server returns UTC - ie the returned
    // date object is correctly taking care of time zones.
    final CommandResult serverStatus = db.command("serverStatus");
    if (serverStatus == null) {
        // OAK-4107 / OAK-4515 : extra safety
        LOG.warn(//from   w  w w  . j a  va  2s.co m
                "determineServerTimeDifferenceMillis: db.serverStatus returned null - cannot determine time difference - assuming 0ms.");
        return 0;
    }
    final Date serverLocalTime = serverStatus.getDate("localTime");
    if (serverLocalTime == null) {
        // OAK-4107 / OAK-4515 : looks like this can happen - at least
        // has been seen once on mongo 3.0.9
        // let's handle this gently and issue a log.warn
        // instead of throwing a NPE
        LOG.warn(
                "determineServerTimeDifferenceMillis: db.serverStatus.localTime returned null - cannot determine time difference - assuming 0ms. "
                        + "(Result details: server exception=" + serverStatus.getException()
                        + ", server error message=" + serverStatus.getErrorMessage() + ")",
                serverStatus.getException());
        return 0;
    }
    final long end = System.currentTimeMillis();

    final long midPoint = (start + end) / 2;
    final long serverLocalTimeMillis = serverLocalTime.getTime();

    // the difference should be
    // * positive when local instance is ahead
    // * and negative when the local instance is behind
    final long diff = midPoint - serverLocalTimeMillis;

    return diff;
}

From source file:org.craftercms.commons.mongo.MongoScriptRunner.java

License:Open Source License

private void runScript(DB db, Resource scriptPath) throws MongoDataException {
    String script;//from ww w  . jav  a2 s.  c  o m
    try {
        if (scriptPath.getFile().isDirectory()) {
            final File[] files = scriptPath.getFile().listFiles(new FilenameFilter() {
                @Override
                public boolean accept(final File dir, final String name) {
                    return name.toLowerCase().endsWith(".js");
                }
            });
            List<File> orderFiles = Arrays.asList(files);
            Collections.sort(orderFiles, new Comparator<File>() {
                @Override
                public int compare(final File o1, final File o2) {
                    return o1.getName().compareTo(o2.getName());
                }
            });
            logger.debug("Directory {} files to exec {}", scriptPath.getFile(), orderFiles);
            for (File file : orderFiles) {
                runScript(db, new FileSystemResource(file.getPath()));
            }
        } else {
            logger.debug("Running Script {}", scriptPath.getURI());
            try {
                script = IOUtils.toString(scriptPath.getInputStream(), "UTF-8");
            } catch (IOException e) {
                throw new MongoDataException("Unable to read script at " + scriptPath.getURI().toString());
            }

            CommandResult result = db.doEval(script);
            if (!result.ok()) {
                Exception ex = result.getException();

                throw new MongoDataException(
                        "An error occurred while running script at " + scriptPath.getURI().toString(), ex);

            }
            logger.info("Mongo script at {} executed successfully", scriptPath.getDescription());
        }
    } catch (IOException ex) {
        logger.error("Unable to read files from {}", ex);
    }
}

From source file:org.greenmongoquery.db.service.impl.MongoServiceImpl.java

License:Open Source License

@Override
public void updatetDb(String db, String updateDb, Mongo mongo) {
    DB dbs = mongo.getDB("admin");
    DBObject cmd = new BasicDBObject();
    DBObject key = new BasicDBObject();
    logger.info("host " + mongo.getAddress().getHost() + " from db " + db + " updte db " + updateDb);

    cmd.put("copydb", 1);

    cmd.put("fromhost", mongo.getAddress().getHost());
    cmd.put("todb", updateDb);
    cmd.put("fromdb", db);

    CommandResult result = dbs.command(cmd);

    DB dbsDop = mongo.getDB(db);/*  w  ww.  jav a2  s . c  om*/
    dbsDop.dropDatabase();
    logger.info(result.getErrorMessage() + result.getException());
}