Search in sources :

Example 6 with Nullable

use of com.mongodb.lang.Nullable in project morphia by mongodb.

the class Mapper method getId.

/**
 * Gets the ID value for an entity
 *
 * @param entity the entity to process
 * @return the ID value
 */
@Nullable
public Object getId(@Nullable Object entity) {
    if (entity == null) {
        return null;
    }
    try {
        final EntityModel model = getEntityModel(entity.getClass());
        final PropertyModel idField = model.getIdProperty();
        if (idField != null) {
            return idField.getValue(entity);
        }
    } catch (NotMappableException ignored) {
    }
    return null;
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Nullable(com.mongodb.lang.Nullable)

Example 7 with Nullable

use of com.mongodb.lang.Nullable in project morphia by mongodb.

the class ReferenceCodec method encodeId.

/**
 * Encodes a value
 *
 * @param mapper
 * @param value  the value to encode
 * @param model  the mapped class of the field type
 * @return the encoded value
 * @morphia.internal
 */
@Nullable
public static Object encodeId(Mapper mapper, Object value, EntityModel model) {
    Object idValue;
    Class<?> type;
    if (value instanceof Key) {
        idValue = ((Key) value).getId();
        String collectionName = ((Key<?>) value).getCollection();
        type = collectionName != null ? mapper.getClassFromCollection(collectionName) : ((Key<?>) value).getType();
        if (type == null) {
            throw new MappingException("The type for the reference could not be determined for the key " + value);
        }
    } else {
        idValue = mapper.getId(value);
        if (idValue == null) {
            return !mapper.isMappable(value.getClass()) ? value : null;
        }
        type = value.getClass();
    }
    String valueCollectionName = mapper.getEntityModel(type).getCollectionName();
    String fieldCollectionName = model.getCollectionName();
    Reference annotation = model.getAnnotation(Reference.class);
    if (annotation != null && !annotation.idOnly() || valueCollectionName != null && !valueCollectionName.equals(fieldCollectionName)) {
        idValue = new DBRef(valueCollectionName, idValue);
    }
    return idValue;
}
Also used : SetReference(dev.morphia.mapping.experimental.SetReference) ListReference(dev.morphia.mapping.experimental.ListReference) MapReference(dev.morphia.mapping.experimental.MapReference) SingleReference(dev.morphia.mapping.experimental.SingleReference) MorphiaReference(dev.morphia.mapping.experimental.MorphiaReference) Reference(dev.morphia.annotations.Reference) DBRef(com.mongodb.DBRef) Key(dev.morphia.Key) MappingException(dev.morphia.mapping.MappingException) Nullable(com.mongodb.lang.Nullable)

Example 8 with Nullable

use of com.mongodb.lang.Nullable in project mongo-java-driver by mongodb.

the class ClusterFixture method getOverriddenStreamFactoryFactory.

@Nullable
public static StreamFactoryFactory getOverriddenStreamFactoryFactory() {
    String streamType = System.getProperty("org.mongodb.test.async.type", "nio2");
    if (nettyStreamFactoryFactory == null && streamType.equals("netty")) {
        NettyStreamFactoryFactory.Builder builder = NettyStreamFactoryFactory.builder();
        String sslProvider = System.getProperty("org.mongodb.test.netty.ssl.provider");
        if (sslProvider != null) {
            SslContext sslContext;
            try {
                sslContext = SslContextBuilder.forClient().sslProvider(SslProvider.valueOf(sslProvider)).build();
            } catch (SSLException e) {
                throw new MongoClientException("Unable to create Netty SslContext", e);
            }
            builder.sslContext(sslContext);
        }
        nettyStreamFactoryFactory = builder.build();
    }
    return nettyStreamFactoryFactory;
}
Also used : BsonString(org.bson.BsonString) NettyStreamFactoryFactory(com.mongodb.connection.netty.NettyStreamFactoryFactory) SSLException(javax.net.ssl.SSLException) SslContext(io.netty.handler.ssl.SslContext) Nullable(com.mongodb.lang.Nullable)

Example 9 with Nullable

use of com.mongodb.lang.Nullable in project mongo-java-driver by mongodb.

the class MongoClientDelegate method createClientSession.

@Nullable
public ClientSession createClientSession(final ClientSessionOptions options, final ReadConcern readConcern, final WriteConcern writeConcern, final ReadPreference readPreference) {
    notNull("readConcern", readConcern);
    notNull("writeConcern", writeConcern);
    notNull("readPreference", readPreference);
    ClusterDescription connectedClusterDescription = getConnectedClusterDescription();
    if (connectedClusterDescription.getLogicalSessionTimeoutMinutes() == null && connectedClusterDescription.getConnectionMode() != ClusterConnectionMode.LOAD_BALANCED) {
        return null;
    } else {
        ClientSessionOptions mergedOptions = ClientSessionOptions.builder(options).defaultTransactionOptions(TransactionOptions.merge(options.getDefaultTransactionOptions(), TransactionOptions.builder().readConcern(readConcern).writeConcern(writeConcern).readPreference(readPreference).build())).build();
        return new ClientSessionImpl(serverSessionPool, originator, mergedOptions, this);
    }
}
Also used : ClusterDescription(com.mongodb.connection.ClusterDescription) ClientSessionOptions(com.mongodb.ClientSessionOptions) Nullable(com.mongodb.lang.Nullable)

Example 10 with Nullable

use of com.mongodb.lang.Nullable in project mongo-java-driver by mongodb.

the class Crypt method fetchKeys.

private void fetchKeys(final MongoCryptContext cryptContext, @Nullable final String databaseName, final MonoSink<RawBsonDocument> sink) {
    keyRetriever.find(cryptContext.getMongoOperation()).doOnSuccess(results -> {
        for (BsonDocument result : results) {
            cryptContext.addMongoOperationResult(result);
        }
        cryptContext.completeMongoOperation();
        executeStateMachineWithSink(cryptContext, databaseName, sink);
    }).doOnError(t -> sink.error(MongoException.fromThrowableNonNull(t))).subscribe();
}
Also used : MongoInternalException(com.mongodb.MongoInternalException) MongoClientException(com.mongodb.MongoClientException) EncryptOptions(com.mongodb.client.model.vault.EncryptOptions) MonoSink(reactor.core.publisher.MonoSink) MongoCryptException(com.mongodb.crypt.capi.MongoCryptException) Supplier(java.util.function.Supplier) DataKeyOptions(com.mongodb.client.model.vault.DataKeyOptions) MongoDataKeyOptions(com.mongodb.crypt.capi.MongoDataKeyOptions) MongoClient(com.mongodb.reactivestreams.client.MongoClient) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) MongoExplicitEncryptOptions(com.mongodb.crypt.capi.MongoExplicitEncryptOptions) MongoCryptContext(com.mongodb.crypt.capi.MongoCryptContext) MongoCrypt(com.mongodb.crypt.capi.MongoCrypt) Logger(com.mongodb.diagnostics.logging.Logger) MongoException(com.mongodb.MongoException) RawBsonDocument(org.bson.RawBsonDocument) MongoKeyDecryptor(com.mongodb.crypt.capi.MongoKeyDecryptor) State(com.mongodb.crypt.capi.MongoCryptContext.State) Mono(reactor.core.publisher.Mono) String.format(java.lang.String.format) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) Loggers(com.mongodb.diagnostics.logging.Loggers) Closeable(java.io.Closeable) Nullable(com.mongodb.lang.Nullable) BsonBinary(org.bson.BsonBinary) BsonDocument(org.bson.BsonDocument) RawBsonDocument(org.bson.RawBsonDocument)

Aggregations

Nullable (com.mongodb.lang.Nullable)19 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)4 BsonDocument (org.bson.BsonDocument)4 Mono (reactor.core.publisher.Mono)4 MongoClientException (com.mongodb.MongoClientException)3 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)3 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)3 BsonString (org.bson.BsonString)3 RawBsonDocument (org.bson.RawBsonDocument)3 MongoException (com.mongodb.MongoException)2 MongoInternalException (com.mongodb.MongoInternalException)2 MongoNamespace (com.mongodb.MongoNamespace)2 ReadPreference (com.mongodb.ReadPreference)2 RequestContext (com.mongodb.RequestContext)2 ClientSession (com.mongodb.reactivestreams.client.ClientSession)2 MongoOperationPublisher.sinkToCallback (com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.sinkToCallback)2 String.format (java.lang.String.format)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2