Skip to content

Commit

Permalink
Improve Kryo Codec for registrations
Browse files Browse the repository at this point in the history
# Conflicts:
#	spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/KryoCodecTests.java

# Conflicts:
#	spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/AbstractKryoCodec.java
#	spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/CompositeCodecTests.java
#	spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/KryoCodecTests.java
  • Loading branch information
artembilan committed Jul 22, 2020
1 parent c95e6dc commit 8468edc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,6 +46,7 @@ protected AbstractKryoCodec() {
KryoFactory factory = new KryoFactory() {
public Kryo create() {
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(true);
// configure Kryo instance, customize settings
configureKryoInstance(kryo);
return kryo;
Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.springframework.integration.codec.kryo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.util.Assert;
Expand All @@ -39,6 +40,13 @@ public class KryoClassListRegistrar extends AbstractKryoRegistrar {

private int initialValue = 50;

/**
* @param classes the vararg of classes to validateRegistration
*/
public KryoClassListRegistrar(Class<?>... classes) {
this(Arrays.asList(classes));
}

/**
* @param classes the list of classes to validateRegistration
*/
Expand All @@ -52,8 +60,7 @@ public KryoClassListRegistrar(List<Class<?>> classes) {
* @param initialValue the initial value
*/
public void setInitialValue(int initialValue) {
Assert.isTrue(initialValue >= MIN_REGISTRATION_VALUE,
"'initialValue' must be >= " + MIN_REGISTRATION_VALUE);
Assert.isTrue(initialValue >= MIN_REGISTRATION_VALUE, "'initialValue' must be >= " + MIN_REGISTRATION_VALUE);
this.initialValue = initialValue;
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,8 +39,9 @@ public class CompositeCodecTests {

@Before
public void setup() {
Map<Class<?>, Codec> codecs = new HashMap<Class<?>, Codec>();
this.codec = new CompositeCodec(codecs, new PojoCodec());
Map<Class<?>, Codec> codecs = new HashMap<>();
this.codec = new CompositeCodec(codecs, new PojoCodec(
new KryoClassListRegistrar(SomeClassWithNoDefaultConstructors.class)));
}

@Test
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -63,7 +63,7 @@ public void testSerializationWithStreams() throws IOException {

@Test
public void testPojoSerialization() throws IOException {
PojoCodec codec = new PojoCodec();
PojoCodec codec = new PojoCodec(new KryoClassListRegistrar(SomeClassWithNoDefaultConstructors.class));
SomeClassWithNoDefaultConstructors foo = new SomeClassWithNoDefaultConstructors("foo", 123);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
codec.encode(foo, bos);
Expand Down Expand Up @@ -98,12 +98,12 @@ public void testPrimitiveSerialization() throws IOException {

@Test
public void testMapSerialization() throws IOException {
PojoCodec codec = new PojoCodec();
PojoCodec codec = new PojoCodec(new KryoClassListRegistrar(HashMap.class));
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("one", 1);
map.put("two", 2);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
codec.encode(map, bos);
codec.encode(map, bos);4
Map<?, ?> m2 = (Map<?, ?>) codec.decode(bos.toByteArray(), HashMap.class);
assertEquals(2, m2.size());
assertEquals(1, m2.get("one"));
Expand All @@ -112,7 +112,7 @@ public void testMapSerialization() throws IOException {

@Test
public void testComplexObjectSerialization() throws IOException {
PojoCodec codec = new PojoCodec();
PojoCodec codec = new PojoCodec(new KryoClassListRegistrar(Foo.class));
Foo foo = new Foo();
foo.put("one", 1);
foo.put("two", 2);
Expand Down

0 comments on commit 8468edc

Please sign in to comment.