public KryoSerializerFactory (ConfigurableFactory<?> parent, List<String> path, TypeDescriptor type) { super(getName(type), KryoSerializerFactory.<T>getGenericSerializerClass(), parent, path, true); _type = type; addProperty(NEEDED_CLASSES); addProperty(CODEC); }
@Override protected TileSerializer<T> create () throws ConfigurationException { // Check class registrations List<String> classNames = getPropertyValue(NEEDED_CLASSES); Codec codec = getPropertyValue(CODEC); int nc = classNames.size(); Class<?>[] classes = new Class<?>[nc]; for (int n=0; n<nc; ++n) { try { classes[n] = Class.forName(classNames.get(n)); } catch (ClassNotFoundException e) { LOGGER.warn("Class {} not found", classNames.get(n)); } } return new KryoSerializer<>(_type, codec, classes); } }
private static String getName (TypeDescriptor type) { return getTypeName(type)+"-k"; }
@Test public void testKryoDefaultCodecSpecification () throws Exception { TypeDescriptor integerType = new TypeDescriptor(Integer.class); KryoSerializerFactory<Integer> factory = new KryoSerializerFactory<Integer>(null, Arrays.asList("factory"), integerType); factory.readConfiguration(new JSONObject("{}")); TileSerializer<Integer> product = SerializationTypeChecker.checkBinClass(factory.produce(TileSerializer.class), Integer.class, integerType); TileSerializer<Integer> expected = new KryoSerializer<Integer>(integerType, Codec.GZIP); assertSerializersEqual(expected, product, new IntegerSource()); }
@Test public void testKryoGZipSpecification () throws Exception { TypeDescriptor integerType = new TypeDescriptor(Integer.class); KryoSerializerFactory<Integer> factory = new KryoSerializerFactory<Integer>(null, Arrays.asList("factory"), integerType); factory.readConfiguration(new JSONObject("{\"factory\": { \"codec\": \"GZIP\"}}")); TileSerializer<Integer> product = SerializationTypeChecker.checkBinClass(factory.produce(TileSerializer.class), Integer.class, integerType); TileSerializer<Integer> expected = new KryoSerializer<Integer>(integerType, Codec.GZIP); assertSerializersEqual(expected, product, new IntegerSource()); }
@Test public void testKryoBZip2Specification () throws Exception { TypeDescriptor integerType = new TypeDescriptor(Integer.class); KryoSerializerFactory<Integer> factory = new KryoSerializerFactory<Integer>(null, Arrays.asList("factory"), integerType); factory.readConfiguration(new JSONObject("{\"factory\": { \"codec\": \"BZIP\"}}")); TileSerializer<Integer> product = SerializationTypeChecker.checkBinClass(factory.produce(TileSerializer.class), Integer.class, integerType); TileSerializer<Integer> expected = new KryoSerializer<Integer>(integerType, Codec.BZIP); assertSerializersEqual(expected, product, new IntegerSource()); }
for (int i=0; i<genericTypes.size(); ++i) { if (i > 0) name += ","; name += getTypeName(genericTypes.get(i));
@Test public void testKryoDeflateSpecification () throws Exception { TypeDescriptor integerType = new TypeDescriptor(Integer.class); KryoSerializerFactory<Integer> factory = new KryoSerializerFactory<Integer>(null, Arrays.asList("factory"), integerType); factory.readConfiguration(new JSONObject("{\"factory\": { \"codec\": \"DEFLATE\"}}")); TileSerializer<Integer> product = SerializationTypeChecker.checkBinClass(factory.produce(TileSerializer.class), Integer.class, integerType); TileSerializer<Integer> expected = new KryoSerializer<Integer>(integerType, Codec.DEFLATE); assertSerializersEqual(expected, product, new IntegerSource()); }
@Test public void testKryoCustomClassSpecification () throws Exception { TypeDescriptor testType = new TypeDescriptor(TestClass.class); KryoSerializerFactory<TestClass> factory = new KryoSerializerFactory<TestClass>(null, Arrays.asList("factory"), testType); factory.readConfiguration(new JSONObject( "{\"factory\": {\n"+ " \"codec\": \"DEFLATE\",\n"+ " \"classes\": [\""+TestClass.class.getName()+"\"]\n"+ "}}")); TileSerializer<TestClass> product = SerializationTypeChecker.checkBinClass(factory.produce(TileSerializer.class), TestClass.class, testType); TileSerializer<TestClass> expected = new KryoSerializer<TestClass>(testType, Codec.DEFLATE, TestClass.class); assertSerializersEqual(expected, product, new TestClassSource()); }