From 9445f986bc5449019d392e32a5dd429e0a393d3b Mon Sep 17 00:00:00 2001 From: Robert Gruener Date: Mon, 23 Feb 2015 13:52:33 -0500 Subject: [PATCH] added transcoder for serializable objects --- pom.xml | 5 ++ .../spotify/folsom/MemcacheClientBuilder.java | 10 ++++ .../SerializableObjectTranscoder.java | 42 ++++++++++++++++ .../SerializableObjectTranscoderTest.java | 48 +++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 src/main/java/com/spotify/folsom/transcoder/SerializableObjectTranscoder.java create mode 100644 src/test/java/com/spotify/folsom/transcoder/SerializableObjectTranscoderTest.java diff --git a/pom.xml b/pom.xml index 811362ad..85ddc2c7 100644 --- a/pom.xml +++ b/pom.xml @@ -89,6 +89,11 @@ dns 2.2.0 + + commons-lang + commons-lang + 2.6 + diff --git a/src/main/java/com/spotify/folsom/MemcacheClientBuilder.java b/src/main/java/com/spotify/folsom/MemcacheClientBuilder.java index 3242b202..ebc82772 100644 --- a/src/main/java/com/spotify/folsom/MemcacheClientBuilder.java +++ b/src/main/java/com/spotify/folsom/MemcacheClientBuilder.java @@ -34,8 +34,10 @@ import com.spotify.folsom.retry.RetryingClient; import com.spotify.folsom.roundrobin.RoundRobinMemcacheClient; import com.spotify.folsom.transcoder.ByteArrayTranscoder; +import com.spotify.folsom.transcoder.SerializableObjectTranscoder; import com.spotify.folsom.transcoder.StringTranscoder; +import java.io.Serializable; import java.nio.charset.Charset; import java.util.List; import java.util.concurrent.Executor; @@ -137,6 +139,14 @@ public static MemcacheClientBuilder newStringClient(Charset charset) { return new MemcacheClientBuilder<>(new StringTranscoder(charset)); } + /** + * Create a client builder for serializable object values. + * @return The builder + */ + public static MemcacheClientBuilder newSerializableObjectClient() { + return new MemcacheClientBuilder<>(SerializableObjectTranscoder.INSTANCE); + } + /** * Create a client builder with the provided value transcoder. diff --git a/src/main/java/com/spotify/folsom/transcoder/SerializableObjectTranscoder.java b/src/main/java/com/spotify/folsom/transcoder/SerializableObjectTranscoder.java new file mode 100644 index 00000000..a0658715 --- /dev/null +++ b/src/main/java/com/spotify/folsom/transcoder/SerializableObjectTranscoder.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2015 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.spotify.folsom.transcoder; + +import com.spotify.folsom.Transcoder; +import org.apache.commons.lang.SerializationUtils; + +import java.io.Serializable; + + +public final class SerializableObjectTranscoder implements Transcoder { + + public static final SerializableObjectTranscoder INSTANCE = new SerializableObjectTranscoder(); + + private SerializableObjectTranscoder() { + } + + @Override + public byte[] encode(final Serializable t) { + return SerializationUtils.serialize(t); + } + + @Override + public Serializable decode(final byte[] b) { + return (Serializable) SerializationUtils.deserialize(b); + } + +} diff --git a/src/test/java/com/spotify/folsom/transcoder/SerializableObjectTranscoderTest.java b/src/test/java/com/spotify/folsom/transcoder/SerializableObjectTranscoderTest.java new file mode 100644 index 00000000..c301939b --- /dev/null +++ b/src/test/java/com/spotify/folsom/transcoder/SerializableObjectTranscoderTest.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2015 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.spotify.folsom.transcoder; + +import org.junit.Test; + +import java.io.Serializable; + +import static org.junit.Assert.assertEquals; + +public class SerializableObjectTranscoderTest { + + private static final class SerializableTestObject implements Serializable { + private static final long serialVersionUID = 6961370831727465915L; + private String value1; + private String value2; + private int value3; + } + + @Test + public void testEncodeDecode() { + SerializableTestObject b = new SerializableTestObject(); + b.value1 = "hello"; + b.value2 = "world"; + b.value3 = 5; + + final byte[] encoded = SerializableObjectTranscoder.INSTANCE.encode(b); + final SerializableTestObject testObject = + (SerializableTestObject) SerializableObjectTranscoder.INSTANCE.decode(encoded); + + assertEquals("hello", testObject.value1); + assertEquals("world", testObject.value2); + assertEquals(5, testObject.value3); + } +}