/** * A {@code CodecRegistry} that combines the given {@code CodecRegistry} instances into a single registry. * * <p>The registries are checked in order until one returns a {@code Codec} for the requested {@code Class}.</p> * * <p>The created instance can handle cycles of {@code Codec} dependencies, i.e when the construction of a {@code Codec} for class A * requires the construction of a {@code Codec} for class B, and vice versa.</p> * <p>Any of the given registries that also implement {@code CodecProvider} will be treated as a {@code CodecProvider} instead of a * {@code CodecRegistry}, which will ensure proper resolution of any dependencies between registries.</p> * * @param registries the preferred registry for {@code Codec} lookups * * @return a {@code CodecRegistry} that combines the list of {@code CodecRegistry} instances into a single one */ public static CodecRegistry fromRegistries(final CodecRegistry... registries) { return fromRegistries(asList(registries)); }
PojoCodecImpl(final ClassModel<T> classModel, final CodecRegistry registry, final PropertyCodecRegistry propertyCodecRegistry, final DiscriminatorLookup discriminatorLookup, final ConcurrentMap<ClassModel<?>, Codec<?>> codecCache, final boolean specialized) { this.classModel = classModel; this.registry = fromRegistries(fromCodecs(this), registry); this.discriminatorLookup = discriminatorLookup; this.codecCache = codecCache; this.propertyCodecRegistry = propertyCodecRegistry; this.specialized = specialized; specialize(); }
/** * Creates a {@code CodecRegistry} from the provided list of {@code CodecProvider} instances. * * <p>The created instance can handle cycles of {@code Codec} dependencies, i.e when the construction of a {@code Codec} for class A * requires the construction of a {@code Codec} for class B, and vice versa.</p> * * @param providers the codec provider * @return a {@code CodecRegistry} with the ordered list of {@code CodecProvider} instances. The registry is also guaranteed to be an * instance of {code CodecProvider}, so that when one is passed to {@link #fromRegistries(CodecRegistry...)} or {@link * #fromRegistries(java.util.List)} it will be treated as a {@code CodecProvider} and properly resolve any dependencies between * registries. */ public static CodecRegistry fromProviders(final CodecProvider... providers) { return fromProviders(asList(providers)); }
protected I configure() { I datastore = getDatastore(); // codecs List<CodecRegistry> registries = new LinkedList<>(); if (!codecs.isEmpty()) { registries.add(CodecRegistries.fromCodecs(codecs)); } if (!codecProviders.isEmpty()) { registries.add(CodecRegistries.fromProviders(codecProviders)); } if (!registries.isEmpty()) { datastore.setAdditionalCodecRegistry(CodecRegistries.fromRegistries(registries)); } // init datastore.initialize(); return datastore; }
ChangeStreamDocumentCodec(final Class<TResult> fullDocumentClass, final CodecRegistry codecRegistry) { ClassModelBuilder<ChangeStreamDocument> classModelBuilder = ClassModel.builder(ChangeStreamDocument.class); ((PropertyModelBuilder<TResult>) classModelBuilder.getProperty("fullDocument")).codec(codecRegistry.get(fullDocumentClass)); ((PropertyModelBuilder<OperationType>) classModelBuilder.getProperty("operationType")).codec(OPERATION_TYPE_CODEC); ClassModel<ChangeStreamDocument> changeStreamDocumentClassModel = classModelBuilder.build(); PojoCodecProvider provider = PojoCodecProvider.builder() .register(MongoNamespace.class) .register(UpdateDescription.class) .register(changeStreamDocumentClassModel) .build(); CodecRegistry registry = fromRegistries(fromProviders(provider, new BsonValueCodecProvider()), codecRegistry); this.codec = (Codec<ChangeStreamDocument<TResult>>) (Codec<? extends ChangeStreamDocument>) registry.get(ChangeStreamDocument.class); }
/** * Creates a {@code CodecRegistry} from the provided list of {@code Codec} instances. * * <p>This registry can then be used alongside other registries. Typically used when adding extra codecs to existing codecs with the * {@link #fromRegistries(CodecRegistry...)} )} helper.</p> * * @param codecs the {@code Codec} to create a registry for * @return a {@code CodecRegistry} for the given list of {@code Codec} instances. */ public static CodecRegistry fromCodecs(final Codec<?>... codecs) { return fromCodecs(asList(codecs)); }
/** * A {@code CodecRegistry} that combines the given {@code CodecRegistry} instances into a single registry. * * <p>The registries are checked in order until one returns a {@code Codec} for the requested {@code Class}.</p> * * <p>The created instance can handle cycles of {@code Codec} dependencies, i.e when the construction of a {@code Codec} for class A * requires the construction of a {@code Codec} for class B, and vice versa.</p> * <p>Any of the given registries that also implement {@code CodecProvider} will be treated as a {@code CodecProvider} instead of a * {@code CodecRegistry}, which will ensure proper resolution of any dependencies between registries.</p> * * @param registries the preferred registry for {@code Codec} lookups * * @return a {@code CodecRegistry} that combines the list of {@code CodecRegistry} instances into a single one */ public static CodecRegistry fromRegistries(final List<? extends CodecRegistry> registries) { List<CodecProvider> providers = new ArrayList<CodecProvider>(); for (CodecRegistry registry : registries) { providers.add(providerFromRegistry(registry)); } return new ProvidersCodecRegistry(providers); }
public static CodecRegistry codecRegistry() { if (codecRegistry == null) { synchronized (MongoCodecs.class) { if (codecRegistry == null) { Map<BsonType, Class<?>> replacements = new HashMap<BsonType, Class<?>>(); replacements.put(BsonType.TIMESTAMP, Timestamp.class); // make it use ByteArrayCodec instead of BinaryCodec // see also https://jira.mongodb.org/browse/JAVA-2025 replacements.put(BsonType.BINARY, byte[].class); // TODO: replacement for java.sql.Date and java.sql.Time // need a way to add two distinct replacements // can't use DATE_TIME, used for java.util.Date BsonTypeClassMap bsonTypeClassMap = new BsonTypeClassMap(replacements); DocumentCodecProvider documentCodecProvider = new DocumentCodecProvider(bsonTypeClassMap); codecRegistry = CodecRegistries.fromRegistries( CodecRegistries.fromCodecs( new TimestampCodec(), new DateCodec(), new TimeCodec()), CodecRegistries.fromProviders(documentCodecProvider), MongoClient.getDefaultCodecRegistry()); } } } return codecRegistry; }
/** * Retrieves the database with the specified {@code name} on the {@code client}. * The database will be created if it does not already exist. * * @param client the MongoDB client, connected on the server * @param name the name of the database * * @return the database */ @Nonnull private MongoDatabase createDatabase(MongoClient client, String name) { final CodecRegistry registry = fromRegistries( client.getMongoClientOptions().getCodecRegistry(), fromProviders(PojoCodecProvider.builder().automatic(true).build())); return client.getDatabase(name) .withCodecRegistry(registry) .withWriteConcern(WriteConcern.MAJORITY) .withReadConcern(ReadConcern.MAJORITY); } }
@Inject public MongoClientWrapper(MongoClientConfiguration configuration) throws UnreachableMongoServerException { try { MongoClientOptions options = toMongoClientOptions(configuration); ImmutableList<MongoCredential> credentials = toMongoCredentials(configuration); testAddress(configuration.getHostAndPort(), options); this.configuration = configuration; this.driverClient = new com.mongodb.MongoClient( new ServerAddress( configuration.getHostAndPort().getHostText(), configuration.getHostAndPort().getPort()), credentials, options ); version = calculateVersion(); codecRegistry = CodecRegistries.fromCodecs(new DocumentCodec()); closed = false; } catch (com.mongodb.MongoException ex) { throw new UnreachableMongoServerException(configuration.getHostAndPort(), ex); } }
private static MongoCollection<GridFSFile> getFilesCollection(final MongoDatabase database, final String bucketName) { return database.getCollection(bucketName + ".files", GridFSFile.class).withCodecRegistry( fromRegistries(database.getCodecRegistry(), MongoClientSettings.getDefaultCodecRegistry()) ); }
@Bean CodecRegistry codecRegistry() { PojoCodecProvider pojoCodecProvider = PojoCodecProvider .builder() .conventions(ImmutableList.of(CLASS_AND_PROPERTY_CONVENTION)) .register(Context.class) .register(Job.class) .register(TaskExecution.class) .register(Error.class) .register(Accessor.class) .build(); return fromRegistries( fromProviders( new PiperCodecProvider(), pojoCodecProvider, new EnumPropertyCodecProvider()), getDefaultCodecRegistry(), fromCodecs(new StringArrayCodec()) ); }
PojoCodecImpl(final ClassModel<T> classModel, final CodecRegistry codecRegistry, final List<PropertyCodecProvider> propertyCodecProviders, final DiscriminatorLookup discriminatorLookup) { this.classModel = classModel; this.registry = fromRegistries(fromCodecs(this), codecRegistry); this.discriminatorLookup = discriminatorLookup; this.codecCache = new ConcurrentHashMap<ClassModel<?>, Codec<?>>(); this.propertyCodecRegistry = new PropertyCodecRegistryImpl(this, registry, propertyCodecProviders); this.specialized = shouldSpecialize(classModel); specialize(); }
ChangeStreamDocumentCodec(final Class<TResult> fullDocumentClass, final CodecRegistry codecRegistry) { ClassModelBuilder<ChangeStreamDocument> classModelBuilder = ClassModel.builder(ChangeStreamDocument.class); ((PropertyModelBuilder<TResult>) classModelBuilder.getProperty("fullDocument")).codec(codecRegistry.get(fullDocumentClass)); ((PropertyModelBuilder<OperationType>) classModelBuilder.getProperty("operationType")).codec(OPERATION_TYPE_CODEC); ClassModel<ChangeStreamDocument> changeStreamDocumentClassModel = classModelBuilder.build(); PojoCodecProvider provider = PojoCodecProvider.builder() .register(MongoNamespace.class) .register(UpdateDescription.class) .register(changeStreamDocumentClassModel) .build(); CodecRegistry registry = fromRegistries(fromProviders(provider, new BsonValueCodecProvider()), codecRegistry); this.codec = (Codec<ChangeStreamDocument<TResult>>) (Codec<? extends ChangeStreamDocument>) registry.get(ChangeStreamDocument.class); }
/** * Creates a new instance with a default codec registry that uses the {@link BsonValueCodecProvider}. */ public BsonValueCodec() { this(fromProviders(new BsonValueCodecProvider())); }
@Inject public MongoClientWrapper(MongoClientConfiguration configuration) throws UnreachableMongoServerException { try { MongoClientOptions options = toMongoClientOptions(configuration); ImmutableList<MongoCredential> credentials = toMongoCredentials(configuration); testAddress(configuration.getHostAndPort(), options); this.configuration = configuration; this.driverClient = new com.mongodb.MongoClient( new ServerAddress( configuration.getHostAndPort().getHostText(), configuration.getHostAndPort().getPort()), credentials, options ); version = calculateVersion(); codecRegistry = CodecRegistries.fromCodecs(new DocumentCodec()); closed = false; } catch (com.mongodb.MongoException ex) { throw new UnreachableMongoServerException(configuration.getHostAndPort(), ex); } }
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(GsonCodecs.codecRegistryFromGson(newGson));
@Bean public CodecRegistry registry(Codec[] codecs) { return CodecRegistries.fromRegistries(MongoClient.getDefaultCodecRegistry(), CodecRegistries.fromCodecs(codecs)); }
CodecRegistry pojoCodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry(), fromProviders(PojoCodecProvider.builder().automatic(true).build())); builder.codecRegistry(pojoCodecRegistry);
/** * Creates a {@code CodecRegistry} from the provided list of {@code Codec} instances. * * <p>This registry can then be used alongside other registries. Typically used when adding extra codecs to existing codecs with the * {@link #fromRegistries(CodecRegistry...)} )} helper.</p> * * @param codecs the {@code Codec} to create a registry for * @return a {@code CodecRegistry} for the given list of {@code Codec} instances. */ public static CodecRegistry fromCodecs(final List<? extends Codec<?>> codecs) { return fromProviders(new MapOfCodecsProvider(codecs)); }