|
| 1 | +package com.arhs.spring.cache.mongo.serializer; |
| 2 | + |
| 3 | +import com.caucho.hessian.io.Hessian2Input; |
| 4 | +import com.caucho.hessian.io.Hessian2Output; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | + |
| 8 | +import java.io.ByteArrayInputStream; |
| 9 | +import java.io.ByteArrayOutputStream; |
| 10 | + |
| 11 | +/** |
| 12 | + * Adapts HessianSerializer so it can be used in Redis. User: msellers Date: |
| 13 | + * 9/3/14 Time: 10:17 AM |
| 14 | + */ |
| 15 | +public class HessianSerializer implements Serializer { |
| 16 | + |
| 17 | + private static final Logger m_logger = LoggerFactory.getLogger(HessianSerializer.class); |
| 18 | + |
| 19 | + @Override |
| 20 | + public byte[] serialize(Object obj) { |
| 21 | + try { |
| 22 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 23 | + Hessian2Output out = new Hessian2Output(bos); |
| 24 | + out.startMessage(); |
| 25 | + out.writeObject(obj); |
| 26 | + out.completeMessage(); |
| 27 | + out.close(); |
| 28 | + byte[] array = bos.toByteArray(); |
| 29 | + |
| 30 | + return array; |
| 31 | + } catch (Exception e) { |
| 32 | + m_logger.error("Error Serializing a value to be put into the Redis cache", e); |
| 33 | + throw new RuntimeException(e.getMessage()); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public Object deserialize(byte[] bytes) { |
| 39 | + try { |
| 40 | + Object toReturn = null; |
| 41 | + |
| 42 | + if (bytes != null) { |
| 43 | + ByteArrayInputStream bin = new ByteArrayInputStream(bytes); |
| 44 | + Hessian2Input in = new Hessian2Input(bin); |
| 45 | + in.startMessage(); |
| 46 | + |
| 47 | + toReturn = in.readObject(); |
| 48 | + } |
| 49 | + |
| 50 | + return toReturn; |
| 51 | + } catch (Exception e) { |
| 52 | + m_logger.error("Error De-Serializing a value from the Redis cache", e); |
| 53 | + throw new RuntimeException(e.getMessage()); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | +} |
0 commit comments