blob: 4f756f616c6938198e2fa88210b560ee373b454f [file] [log] [blame]
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -07001package org.onlab.onos.store.serializers;
2
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -07003import org.apache.felix.scr.annotations.Activate;
4import org.apache.felix.scr.annotations.Component;
5import org.apache.felix.scr.annotations.Deactivate;
6import org.apache.felix.scr.annotations.Service;
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -07007import org.onlab.util.KryoPool;
8import org.slf4j.Logger;
9import org.slf4j.LoggerFactory;
10
tomf5d85d42014-10-02 05:27:56 -070011import java.nio.ByteBuffer;
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070012
13/**
14 * Serialization service using Kryo.
15 */
16@Component(immediate = true)
17@Service
18public class KryoSerializationManager implements KryoSerializationService {
19
20 private final Logger log = LoggerFactory.getLogger(getClass());
21 private KryoPool serializerPool;
22
23
24 @Activate
25 public void activate() {
26 setupKryoPool();
27 log.info("Started");
28 }
29
30 @Deactivate
31 public void deactivate() {
32 log.info("Stopped");
33 }
34
35 /**
36 * Sets up the common serialzers pool.
37 */
38 protected void setupKryoPool() {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070039 serializerPool = KryoPool.newBuilder()
Yuta HIGUCHI03fec1f2014-10-03 09:13:50 -070040 .register(KryoPoolUtil.API)
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070041 .build()
42 .populate(1);
43 }
44
45 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070046 public byte[] encode(final Object obj) {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070047 return serializerPool.serialize(obj);
48 }
49
50 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070051 public <T> T decode(final byte[] bytes) {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070052 if (bytes == null) {
53 return null;
54 }
55 return serializerPool.deserialize(bytes);
56 }
57
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070058 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070059 public void encode(Object obj, ByteBuffer buffer) {
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070060 serializerPool.serialize(obj, buffer);
61 }
62
63 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070064 public <T> T decode(ByteBuffer buffer) {
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070065 return serializerPool.deserialize(buffer);
66 }
67
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070068}