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:net.vz.mongodb.jackson.internal.stream.JacksonDBEncoder.java

License:Apache License

public int writeObject(OutputBuffer buf, BSONObject object) {
    if (object instanceof JacksonDBObject) {
        Object actualObject = ((JacksonDBObject) object).getObject();
        OutputBufferOutputStream stream = new OutputBufferOutputStream(buf);
        BsonGenerator generator = new DBEncoderBsonGenerator(JsonGenerator.Feature.collectDefaults(), stream);
        try {//from w ww.ja  va2  s  . c o m
            objectMapper.writeValue(generator, actualObject);
            // The generator buffers everything so that it can write the number of bytes to the stream
            generator.close();
        } catch (JsonMappingException e) {
            throw new MongoJsonMappingException(e);
        } catch (IOException e) {
            throw new MongoException("Error writing object out", e);
        }
        return stream.getCount();
    } else {
        return defaultDBEncoder.writeObject(buf, object);
    }
}

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

License:Apache License

DBObject convertToDbObject(T object) throws MongoException {
    if (object == null) {
        return null;
    }//from w ww  . j  a  va 2s  . 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:net.vz.mongodb.jackson.JacksonDBCollection.java

License:Apache License

T convertFromDbObject(DBObject dbObject) throws MongoException {
    if (dbObject == null) {
        return null;
    }//from   w ww .  j av  a  2 s.  c  o m
    if (dbObject instanceof JacksonDBObject) {
        return (T) ((JacksonDBObject) dbObject).getObject();
    }
    try {
        return objectMapper.readValue(new BsonObjectTraversingParser(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.graylog2.database.MongoConnectionImpl.java

License:Open Source License

/**
 * Connect the instance./*from w ww  .  j  av a2 s  . c o m*/
 */
@Override
public synchronized Mongo connect() {
    if (m == null) {
        try {
            m = new MongoClient(mongoClientURI);
            db = m.getDB(mongoClientURI.getDatabase());
            db.setWriteConcern(WriteConcern.SAFE);
        } catch (UnknownHostException e) {
            throw new RuntimeException("Cannot resolve host name for MongoDB", e);
        }
    }

    try {
        db.command("{ ping: 1 }");
    } catch (CommandFailureException e) {
        if (e.getCode() == 18) {
            throw new MongoException(
                    "Couldn't connect to MongoDB. Please check the authentication credentials.", e);
        } else {
            throw new MongoException("Couldn't connect to MongoDB: " + e.getMessage(), e);
        }
    }

    return m;
}

From source file:org.ingini.spring.boot.mongodb.InginiMain.java

License:Apache License

@Bean
public Jongo jongo() {
    DB db;// w ww  .  ja va 2 s.  com
    try {
        db = new MongoClient("127.0.0.1", 27017).getDB("test");
    } catch (UnknownHostException e) {
        throw new MongoException("Connection error : ", e);
    }
    return new Jongo(db);
}

From source file:org.jongo.bson.BsonDBEncoder.java

License:Apache License

public int writeObject(final OutputBuffer buf, BSONObject o) {

    if (!(o instanceof LazyDBObject)) {
        return DefaultDBEncoder.FACTORY.create().writeObject(buf, o);
    }/*  w w w  . j a  va2s. c  o m*/

    try {
        return ((LazyDBObject) o).pipe(buf);
    } catch (IOException e) {
        throw new MongoException("Exception serializing a LazyDBObject", e);
    }
}

From source file:org.jongo.stream.JacksonDBEncoder.java

License:Apache License

public int writeObject(OutputBuffer buf, BSONObject object) {
    if (object instanceof JacksonDBObject) {
        JacksonDBObject<?> jacksonDbObject = (JacksonDBObject<?>) object;
        OutputBufferOutputStream stream = new OutputBufferOutputStream(buf);
        BsonGenerator generator = new DBEncoderBsonGenerator(JsonGenerator.Feature.collectDefaults(), stream);
        try {//from w w w. jav a  2s . com
            objectMapper.writerWithView(jacksonDbObject.getView()).writeValue(generator,
                    jacksonDbObject.getObject());
            // The generator buffers everything so that it can write the number of bytes to the stream
            generator.close();
        } catch (JsonMappingException e) {
            throw new MongoException("Error Mapping JSON", e);
        } catch (IOException e) {
            throw new MongoException("Error writing object out", e);
        }
        return stream.getCount();
    } else {
        return defaultDBEncoder.writeObject(buf, object);
    }
}

From source file:org.mongeez.dao.impl.MongeezDaoImpl.java

License:Apache License

@Override
public void runScript(String code) {
    try {//w  w  w  .  j a  v a 2s .co  m
        MongoRuntime.call(new MongoScriptAction(mongoScope, "script", code));
        //db.eval(code);
    } catch (RhinoException e) {
        throw new MongoException("Failure in script", e);
    }
}

From source file:org.mongojack.internal.stream.JacksonDBEncoder.java

License:Apache License

public int writeObject(OutputBuffer buf, BSONObject object) {
    if (object instanceof JacksonDBObject) {
        JacksonDBObject<?> jacksonDbObject = (JacksonDBObject<?>) object;
        OutputBufferOutputStream stream = new OutputBufferOutputStream(buf);
        BsonGenerator generator = new DBEncoderBsonGenerator(JsonGenerator.Feature.collectDefaults(), stream);
        try {/*  w ww . j a v a  2 s .co m*/
            objectMapper.writerWithView(jacksonDbObject.getView()).writeValue(generator,
                    jacksonDbObject.getObject());
            // The generator buffers everything so that it can write the number of bytes to the stream
            generator.close();
        } catch (JsonMappingException e) {
            throw new MongoJsonMappingException(e);
        } catch (IOException e) {
            throw new MongoException("Error writing object out", e);
        }
        return stream.getCount();
    } else {
        return defaultDBEncoder.writeObject(buf, object);
    }
}

From source file:org.mongojack.internal.util.SerializationUtils.java

License:Apache License

/**
 * Serialize the given field/*from   ww w . j a  v  a2 s.c  om*/
 *
 * @param objectMapper The object mapper to serialize it with
 * @param value        The value to serialize
 * @return The serialized field.  May return the same object if no serialization was necessary.
 */
public static Object serializeField(ObjectMapper objectMapper, JavaType parentType, String name, Object value) {
    if (value == null) {
        // return as is
        return value;
    } else if (value instanceof DBObject) {
        // todo Maybe should be passing the parent type and name and stuff....
        return serializeFields(objectMapper, (DBObject) value);
    } else if (value instanceof Collection) {
        Collection<?> coll = (Collection<?>) value;
        List<Object> copy = null;
        int position = 0;
        for (Object item : coll) {
            Object returned = serializeField(objectMapper, parentType, name, item);
            if (returned != item) {
                if (copy == null) {
                    copy = new ArrayList<Object>(coll);
                }
                copy.set(position, returned);
            }
            position++;
        }
        if (copy != null) {
            return copy;
        } else {
            return coll;
        }
    } else if (value.getClass().isArray()) {
        if (BASIC_TYPES.contains(value.getClass().getComponentType())) {
            return value;
        }
        Object[] array = (Object[]) value;
        Object[] copy = null;
        for (int i = 0; i < array.length; i++) {
            Object returned = serializeField(objectMapper, parentType, name, array[i]);
            if (returned != array[i]) {
                if (copy == null) {
                    copy = new Object[array.length];
                    System.arraycopy(array, 0, copy, 0, array.length);
                }
                copy[i] = returned;
            }
        }
        if (copy != null) {
            return copy;
        } else {
            return array;
        }
    }
    // It's not a collection, try finding a serializer
    JsonSerializer serializer = findSerializer(objectMapper, parentType, name);
    if (serializer != null) {
        BsonObjectGenerator gen = new BsonObjectGenerator();
        try {
            serializer.serialize(value, gen, JacksonAccessor.getSerializerProvider(objectMapper));
        } catch (JsonMappingException e) {
            throw new MongoJsonMappingException("Error serialising query field " + name, e);
        } catch (IOException e) {
            throw new MongoException("Unknown IOException while serialising query field " + name, e);
        }
        return gen.getValue();
    } else if (BASIC_TYPES.contains(value.getClass())) {
        // Return as is
        return value;
    } else {
        // We don't know what it is, serialise it
        BsonObjectGenerator generator = new BsonObjectGenerator();
        try {
            objectMapper.writeValue(generator, value);
        } catch (JsonMappingException e) {
            throw new MongoJsonMappingException(e);
        } catch (IOException e) {
            throw new RuntimeException("Somehow got an IOException writing to memory", e);
        }
        return generator.getValue();
    }
}