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, final Throwable t) 

Source Link

Usage

From source file:org.mongojack.JacksonDBCollection.java

License:Apache License

DBObject convertToBasicDbObject(T object) throws MongoException {
    if (object == null) {
        return null;
    }/*w  w w .  jav  a 2  s.  c  om*/
    BsonObjectGenerator generator = new BsonObjectGenerator();
    try {
        objectMapper.writeValue(generator, object);
    } catch (JsonMappingException e) {
        throw new MongoJsonMappingException(e);
    } catch (IOException e) {
        // This shouldn't happen
        throw new MongoException("Unknown error occurred converting BSON to object", e);
    }
    return generator.getDBObject();
}

From source file:org.mongojack.JacksonDBCollection.java

License:Apache License

DBObject convertToDbObject(T object) throws MongoException {
    if (object == null) {
        return null;
    }/*from  w  w w .j  a v a  2  s  .com*/
    if (isEnabled(Feature.USE_STREAM_SERIALIZATION)) {
        return new JacksonDBObject<T>(object, view);
    } else {
        BsonObjectGenerator generator = new BsonObjectGenerator();
        try {
            objectMapper.writerWithView(view).writeValue(generator, object);
        } catch (JsonMappingException e) {
            throw new MongoJsonMappingException(e);
        } catch (IOException e) {
            // This shouldn't happen
            throw new MongoException("Unknown error occurred converting BSON to object", e);
        }
        return generator.getDBObject();
    }
}

From source file:org.mongojack.JacksonDBCollection.java

License:Apache License

T convertFromDbObject(DBObject dbObject) throws MongoException {
    if (dbObject == null) {
        return null;
    }//from   ww w . j  a  va  2 s  .c  o  m
    if (dbObject instanceof JacksonDBObject) {
        return (T) ((JacksonDBObject) dbObject).getObject();
    }
    try {
        return (T) objectMapper.readValue(new BsonObjectTraversingParser(this, dbObject), type);
    } catch (JsonMappingException e) {
        throw new MongoJsonMappingException(e);
    } catch (IOException e) {
        // This shouldn't happen
        throw new MongoException("Unknown error occurred converting BSON to object", e);
    }
}

From source file:org.mongojack.JacksonDBCollection.java

License:Apache License

<S> S convertFromDbObject(DBObject dbObject, Class<S> clazz) throws MongoException {
    if (dbObject == null) {
        return null;
    }/*  w ww.  ja va  2 s. c  o m*/
    if (dbObject instanceof JacksonDBObject) {
        return (S) ((JacksonDBObject) dbObject).getObject();
    }
    try {
        return objectMapper.readValue(new BsonObjectTraversingParser(this, dbObject), clazz);
    } catch (JsonMappingException e) {
        throw new MongoJsonMappingException(e);
    } catch (IOException e) {
        // This shouldn't happen
        throw new MongoException("Unknown error occurred converting BSON to object", e);
    }
}

From source file:org.mongojack.JacksonMongoCollection.java

License:Apache License

/**
 * This method provides a static way to convert an object into a Document. Defaults will be used for all parameters
 * left null./*from   w  w  w. ja  va 2 s .c  om*/
 * 
 * @param object The object to convert
 * @param objectMapper The specific Jackson ObjectMapper to use. (Default MongoJack ObjectMapper)
 * @param view The Jackson View to use in serialization. (Default null)
 * @return
 */
public static <T> Document convertToDocument(T object, ObjectMapper objectMapper, Class<?> view) {
    if (object == null) {
        return null;
    }
    if (objectMapper == null) {
        objectMapper = DEFAULT_OBJECT_MAPPER;
    }
    DocumentObjectGenerator generator = new DocumentObjectGenerator();
    try {
        objectMapper.writerWithView(view).writeValue(generator, object);
    } catch (JsonMappingException e) {
        throw new MongoJsonMappingException(e);
    } catch (IOException e) {
        // This shouldn't happen
        throw new MongoException("Unknown error occurred converting BSON to object", e);
    }
    return generator.getDocument();
}

From source file:org.mongojack.JacksonMongoCollection.java

License:Apache License

/**
 * This method provides a static method to convert a DBObject into a given class. If the ObjectMapper is null, use a
 * default ObjectMapper/*w  w w.  j ava 2  s .  c  o m*/
 * 
 * @param document
 * @param clazz
 * @param objectMapper
 * @param view
 * @return
 * @throws MongoException
 */
public static <S> S convertFromDocument(Document document, Class<S> clazz, ObjectMapper objectMapper,
        Class<?> view) throws MongoException {
    if (document == null) {
        return null;
    }
    if (objectMapper == null)
        objectMapper = DEFAULT_OBJECT_MAPPER;
    try {
        return objectMapper.readerWithView(view)
                .readValue(new DocumentObjectTraversingParser(document, objectMapper), clazz);
    } catch (JsonMappingException e) {
        throw new MongoJsonMappingException(e);
    } catch (IOException e) {
        // This shouldn't happen
        throw new MongoException("Unknown error occurred converting BSON to object", e);
    }
}

From source file:org.pentaho.di.trans.steps.mongodboutput.MongoDbOutput.java

License:Open Source License

protected void commitUpdate(DBObject updateQuery, DBObject insertUpdate, Object[] row) throws KettleException {

    int retrys = 0;
    MongoException lastEx = null;/*from w w  w . ja va 2  s . c  o  m*/

    while (retrys <= m_writeRetries && !isStopped()) {
        WriteResult result = null;
        CommandResult cmd = null;
        try {
            // TODO It seems that doing an update() via a secondary node does not
            // generate any sort of exception or error result! (at least via
            // driver version 2.11.1). Transformation completes successfully
            // but no updates are made to the collection.
            // This is unlike doing an insert(), which generates
            // a MongoException if you are not talking to the primary. So we need
            // some logic to check whether or not the connection configuration
            // contains the primary in the replica set and give feedback if it
            // doesnt
            try {
                result = m_data.getCollection().update(updateQuery, insertUpdate, m_meta.getUpsert(),
                        m_meta.getMulti());
            } catch (MongoDbException e) {
                throw new MongoException(e.getMessage(), e);
            }

            cmd = result.getLastError();
            if (cmd != null && !cmd.ok()) {
                String message = cmd.getErrorMessage();
                logError(BaseMessages.getString(PKG, "MongoDbOutput.Messages.Error.MongoReported", message)); //$NON-NLS-1$

                cmd.throwOnError();
            }
        } catch (MongoException me) {
            lastEx = me;
            retrys++;
            if (retrys <= m_writeRetries) {
                logError(BaseMessages.getString(PKG, "MongoDbOutput.Messages.Error.ErrorWritingToMongo", //$NON-NLS-1$
                        me.toString()));
                logBasic(
                        BaseMessages.getString(PKG, "MongoDbOutput.Messages.Message.Retry", m_writeRetryDelay)); //$NON-NLS-1$
                try {
                    Thread.sleep(m_writeRetryDelay * 1000);
                    // CHECKSTYLE:OFF
                } catch (InterruptedException e) {
                    // CHECKSTYLE:ON
                }
            }
        }

        if (cmd != null && cmd.ok()) {
            break;
        }
    }

    if ((retrys > m_writeRetries || isStopped()) && lastEx != null) {

        // Send this one to the error stream if doing error handling
        if (getStepMeta().isDoingErrorHandling()) {
            putError(getInputRowMeta(), row, 1, lastEx.getMessage(), "", "MongoDbOutput");
        } else {
            throw new KettleException(lastEx);
        }
    }
}