Make the choice of AMQP serializer for primitive types configurable.

This commit is contained in:
Chris Rankin 2019-08-22 18:02:55 +01:00
parent 99074b5a49
commit b2d335c518
4 changed files with 12 additions and 6 deletions

View File

@ -12,6 +12,7 @@ import org.apache.qpid.proton.amqp.Symbol
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.util.*
import java.util.function.Function
import javax.annotation.concurrent.ThreadSafe
/**
@ -89,6 +90,7 @@ class DefaultLocalSerializerFactory(
private val fingerPrinter: FingerPrinter,
override val classloader: ClassLoader,
private val descriptorBasedSerializerRegistry: DescriptorBasedSerializerRegistry,
private val primitiveSerializerFactory: Function<Class<*>, AMQPSerializer<Any>>,
private val customSerializerRegistry: CustomSerializerRegistry,
private val onlyCustomSerializers: Boolean)
: LocalSerializerFactory {
@ -237,7 +239,7 @@ class DefaultLocalSerializerFactory(
throw AMQPNotSerializableException(
type,
"Serializer does not support synthetic classes")
AMQPTypeIdentifiers.isPrimitive(typeInformation.typeIdentifier) -> AMQPPrimitiveSerializer(clazz)
AMQPTypeIdentifiers.isPrimitive(typeInformation.typeIdentifier) -> primitiveSerializerFactory.apply(clazz)
else -> makeNonCustomSerializer(type, typeInformation, clazz)
}
}

View File

@ -3,7 +3,6 @@ package net.corda.serialization.internal.amqp
import net.corda.core.serialization.SerializationContext
import net.corda.core.utilities.contextLogger
import net.corda.serialization.internal.model.*
import org.hibernate.type.descriptor.java.ByteTypeDescriptor
import java.io.NotSerializableException
/**

View File

@ -4,6 +4,7 @@ import net.corda.core.KeepForDJVM
import net.corda.core.internal.uncheckedCast
import net.corda.serialization.internal.CordaSerializationMagic
import net.corda.serialization.internal.amqp.AMQPTypeIdentifiers.isPrimitive
import net.corda.serialization.internal.model.TypeIdentifier
import net.corda.serialization.internal.model.TypeIdentifier.TopType
import net.corda.serialization.internal.model.TypeIdentifier.Companion.forGenericType
import org.apache.qpid.proton.amqp.*
@ -14,7 +15,8 @@ import java.lang.reflect.Type
const val DESCRIPTOR_DOMAIN: String = "net.corda"
val amqpMagic = CordaSerializationMagic("corda".toByteArray() + byteArrayOf(1, 0))
fun typeDescriptorFor(type: Type): Symbol = Symbol.valueOf("$DESCRIPTOR_DOMAIN:${AMQPTypeIdentifiers.nameForType(type)}")
fun typeDescriptorFor(typeId: TypeIdentifier): Symbol = Symbol.valueOf("$DESCRIPTOR_DOMAIN:${AMQPTypeIdentifiers.nameForType(typeId)}")
fun typeDescriptorFor(type: Type): Symbol = typeDescriptorFor(forGenericType(type))
fun redescribe(obj: Any?, type: Type): Any? {
return if (obj == null || obj is DescribedType || obj is Binary || forGenericType(type).run { isPrimitive(this) || this == TopType }) {

View File

@ -8,6 +8,7 @@ import net.corda.serialization.internal.carpenter.ClassCarpenterImpl
import net.corda.serialization.internal.model.*
import java.io.NotSerializableException
import java.util.Collections.unmodifiableMap
import java.util.function.Function
@KeepForDJVM
object SerializerFactoryBuilder {
@ -15,6 +16,7 @@ object SerializerFactoryBuilder {
* The standard mapping of Java object types to Java primitive types.
* The DJVM will need to override these, but probably not anyone else.
*/
@Suppress("unchecked_cast")
private val javaPrimitiveTypes: Map<Class<*>, Class<*>> = unmodifiableMap(mapOf<Class<out Any>?, Class<out Any>?>(
Boolean::class.javaObjectType to Boolean::class.javaPrimitiveType,
Byte::class.javaObjectType to Byte::class.javaPrimitiveType,
@ -104,6 +106,7 @@ object SerializerFactoryBuilder {
fingerPrinter,
classCarpenter.classloader,
descriptorBasedSerializerRegistry,
Function { clazz -> AMQPPrimitiveSerializer(clazz) },
customSerializerRegistry,
onlyCustomSerializers)
@ -132,15 +135,15 @@ object SerializerFactoryBuilder {
}
object NoEvolutionSerializerFactory : EvolutionSerializerFactory {
override fun getEvolutionSerializer(remoteTypeInformation: RemoteTypeInformation, localTypeInformation: LocalTypeInformation): AMQPSerializer<Any> {
override fun getEvolutionSerializer(remote: RemoteTypeInformation, local: LocalTypeInformation): AMQPSerializer<Any> {
throw NotSerializableException("""
Evolution not permitted.
Remote:
${remoteTypeInformation.prettyPrint(false)}
${remote.prettyPrint(false)}
Local:
${localTypeInformation.prettyPrint(false)}
${local.prettyPrint(false)}
""")
}