use of org.bson.codecs.configuration.CodecConfigurationException in project morphia by mongodb.
the class LifecycleDecoder method decode.
@Override
@SuppressWarnings("unchecked")
public T decode(BsonReader reader, DecoderContext decoderContext) {
Document document = getMorphiaCodec().getRegistry().get(Document.class).decode(reader, decoderContext);
EntityModel model = getMorphiaCodec().getEntityModel();
if (model.useDiscriminator()) {
String discriminator = document.getString(model.getDiscriminatorKey());
if (discriminator != null) {
Class<?> discriminatorClass = getMorphiaCodec().getDiscriminatorLookup().lookup(discriminator);
// need to load the codec to initialize cachedCodecs in field models
Codec<?> codec = getMorphiaCodec().getRegistry().get(discriminatorClass);
if (codec instanceof MorphiaCodec) {
model = ((MorphiaCodec<?>) codec).getEntityModel();
} else {
throw new CodecConfigurationException(format("Non-entity class used as discriminator: '%s'.", discriminator));
}
}
}
final MorphiaInstanceCreator instanceCreator = model.getInstanceCreator();
T entity = (T) instanceCreator.getInstance();
model.callLifecycleMethods(PreLoad.class, entity, document, getMorphiaCodec().getDatastore());
decodeProperties(new DocumentReader(document), decoderContext, instanceCreator, model);
model.callLifecycleMethods(PostLoad.class, entity, document, getMorphiaCodec().getDatastore());
return entity;
}
Aggregations