Custom Logger serializer and its related unit-test.

Custom Logger serialiser using only its name when serialising and deserialising using LoggerFactory.getLogger(name).
This commit is contained in:
Konstantinos Chalkias 2017-03-16 15:38:37 +00:00 committed by GitHub
parent 525607441b
commit 6844ab32f8
3 changed files with 29 additions and 3 deletions

View File

@ -17,6 +17,7 @@ import net.corda.core.utilities.NonEmptySetSerializer
import net.i2p.crypto.eddsa.EdDSAPrivateKey
import net.i2p.crypto.eddsa.EdDSAPublicKey
import org.objenesis.strategy.StdInstantiatorStrategy
import org.slf4j.Logger
import java.io.BufferedInputStream
import java.util.*
@ -34,7 +35,7 @@ object DefaultKryoCustomizer {
// for change to a class.
setDefaultSerializer(CompatibleFieldSerializer::class.java)
// Take the safest route here and allow subclasses to have fields named the same as super classes.
fieldSerializerConfig.setCachedFieldNameStrategy(FieldSerializer.CachedFieldNameStrategy.EXTENDED)
fieldSerializerConfig.cachedFieldNameStrategy = FieldSerializer.CachedFieldNameStrategy.EXTENDED
// Allow construction of objects using a JVM backdoor that skips invoking the constructors, if there is no
// no-arg constructor available.
@ -78,8 +79,10 @@ object DefaultKryoCustomizer {
register(MetaData::class.java, MetaDataSerializer)
register(BitSet::class.java, ReferencesAwareJavaSerializer)
addDefaultSerializer(Logger::class.java, LoggerSerializer)
val customization = KryoSerializationCustomization(this)
pluginRegistries.forEach { it.customizeSerialization(customization) }
}
}
}
}

View File

@ -5,7 +5,6 @@ import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import com.esotericsoftware.kryo.pool.KryoPool
import com.esotericsoftware.kryo.serializers.JavaSerializer
import com.esotericsoftware.kryo.serializers.MapSerializer
import com.esotericsoftware.kryo.util.MapReferenceResolver
import com.google.common.annotations.VisibleForTesting
import net.corda.core.contracts.*
@ -17,6 +16,8 @@ import net.i2p.crypto.eddsa.EdDSAPrivateKey
import net.i2p.crypto.eddsa.EdDSAPublicKey
import net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec
import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.*
import java.lang.reflect.InvocationTargetException
import java.nio.file.Files
@ -579,3 +580,15 @@ object MetaDataSerializer : Serializer<MetaData>() {
return MetaData(schemeCodeName, versionID, signatureType, timestamp, visibleInputs, signedInputs, merkleRoot, publicKey)
}
}
/** For serialising a Logger. */
@ThreadSafe
object LoggerSerializer : Serializer<Logger>() {
override fun write(kryo: Kryo, output: Output, obj: Logger) {
output.writeString(obj.name)
}
override fun read(kryo: Kryo, input: Input, type: Class<Logger>): Logger {
return LoggerFactory.getLogger(input.readString())
}
}

View File

@ -11,11 +11,13 @@ import org.bouncycastle.pqc.jcajce.provider.BouncyCastlePQCProvider
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.slf4j.LoggerFactory
import java.io.InputStream
import java.security.Security
import java.time.Instant
import java.util.*
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class KryoTests {
@ -125,6 +127,14 @@ class KryoTests {
assertEquals(meta2, meta)
}
@Test
fun `serialize - deserialize Logger`() {
val logger = LoggerFactory.getLogger("aName")
val logger2 = logger.serialize(storageKryo()).deserialize(storageKryo())
assertEquals(logger.name, logger2.name)
assertTrue(logger === logger2)
}
@CordaSerializable
private data class Person(val name: String, val birthday: Instant?)