1 | package omq.common.util; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.util.Properties; |
---|
5 | |
---|
6 | import omq.common.message.Request; |
---|
7 | import omq.common.message.Response; |
---|
8 | import omq.common.util.Serializers.GsonImp; |
---|
9 | import omq.common.util.Serializers.ISerializer; |
---|
10 | import omq.common.util.Serializers.JavaImp; |
---|
11 | import omq.common.util.Serializers.KryoImp; |
---|
12 | import omq.exception.SerializerException; |
---|
13 | import omq.server.RemoteObject; |
---|
14 | |
---|
15 | /** |
---|
16 | * |
---|
17 | * Serializer enables to serialize the requests and the responses of the |
---|
18 | * remoteObjects. This class is used to have the same serializer object a not to |
---|
19 | * create new instances every time they are needed. |
---|
20 | * |
---|
21 | * @author Sergi Toda <sergi.toda@estudiants.urv.cat> |
---|
22 | * |
---|
23 | */ |
---|
24 | public class Serializer { |
---|
25 | public static final String KRYO = "kryo"; |
---|
26 | public static final String JAVA = "java"; |
---|
27 | public static final String GSON = "gson"; |
---|
28 | |
---|
29 | // Client serializer |
---|
30 | public ISerializer serializer; |
---|
31 | |
---|
32 | // Server serializers |
---|
33 | private ISerializer kryoSerializer; |
---|
34 | private ISerializer javaSerializer; |
---|
35 | private ISerializer gsonSerializer; |
---|
36 | |
---|
37 | private Properties env; |
---|
38 | |
---|
39 | public Serializer(Properties env) { |
---|
40 | this.env = env; |
---|
41 | } |
---|
42 | |
---|
43 | private Boolean getEnableCompression() { |
---|
44 | return Boolean.valueOf(env.getProperty(ParameterQueue.ENABLE_COMPRESSION, "false")); |
---|
45 | } |
---|
46 | |
---|
47 | public ISerializer getInstance() throws SerializerException { |
---|
48 | if (serializer == null) { |
---|
49 | try { |
---|
50 | String className = env.getProperty(ParameterQueue.PROXY_SERIALIZER, Serializer.JAVA); |
---|
51 | |
---|
52 | if (className == null || className.isEmpty()) { |
---|
53 | throw new ClassNotFoundException("Class name is null or empty."); |
---|
54 | } |
---|
55 | |
---|
56 | serializer = getInstance(className); |
---|
57 | } catch (Exception ex) { |
---|
58 | throw new SerializerException(ex.getMessage(), ex); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | return serializer; |
---|
63 | } |
---|
64 | |
---|
65 | public ISerializer getInstance(String type) throws SerializerException { |
---|
66 | if (KRYO.equals(type)) { |
---|
67 | if (kryoSerializer == null) { |
---|
68 | kryoSerializer = new KryoImp(); |
---|
69 | } |
---|
70 | return kryoSerializer; |
---|
71 | } else if (GSON.equals(type)) { |
---|
72 | if (gsonSerializer == null) { |
---|
73 | gsonSerializer = new GsonImp(); |
---|
74 | } |
---|
75 | return gsonSerializer; |
---|
76 | } else if (JAVA.equals(type)) { |
---|
77 | if (javaSerializer == null) { |
---|
78 | javaSerializer = new JavaImp(); |
---|
79 | } |
---|
80 | return javaSerializer; |
---|
81 | } |
---|
82 | |
---|
83 | throw new SerializerException("Serializer not found."); |
---|
84 | } |
---|
85 | |
---|
86 | public byte[] serialize(String type, Object obj) throws SerializerException { |
---|
87 | ISerializer instance = getInstance(type); |
---|
88 | |
---|
89 | Boolean enableCompression = getEnableCompression(); |
---|
90 | if (enableCompression) { |
---|
91 | byte[] objSerialized = instance.serialize(obj); |
---|
92 | try { |
---|
93 | return Zipper.zip(objSerialized); |
---|
94 | } catch (IOException e) { |
---|
95 | throw new SerializerException(e.getMessage(), e); |
---|
96 | } |
---|
97 | } else { |
---|
98 | return instance.serialize(obj); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | public byte[] serialize(Object obj) throws SerializerException { |
---|
103 | ISerializer instance = getInstance(); |
---|
104 | |
---|
105 | Boolean enableCompression = getEnableCompression(); |
---|
106 | if (enableCompression) { |
---|
107 | byte[] objSerialized = instance.serialize(obj); |
---|
108 | try { |
---|
109 | return Zipper.zip(objSerialized); |
---|
110 | } catch (IOException e) { |
---|
111 | throw new SerializerException(e.getMessage(), e); |
---|
112 | } |
---|
113 | } else { |
---|
114 | return instance.serialize(obj); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | public Request deserializeRequest(String type, byte[] bytes, RemoteObject obj) throws SerializerException { |
---|
119 | ISerializer instance = getInstance(type); |
---|
120 | |
---|
121 | Boolean enableCompression = getEnableCompression(); |
---|
122 | if (enableCompression) { |
---|
123 | try { |
---|
124 | byte[] unZippedBytes = Zipper.unzip(bytes); |
---|
125 | return instance.deserializeRequest(unZippedBytes, obj); |
---|
126 | } catch (IOException e) { |
---|
127 | throw new SerializerException(e.getMessage(), e); |
---|
128 | } |
---|
129 | } else { |
---|
130 | return instance.deserializeRequest(bytes, obj); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | public Response deserializeResponse(byte[] bytes, Class<?> type) throws SerializerException { |
---|
135 | ISerializer instance = getInstance(); |
---|
136 | |
---|
137 | Boolean enableCompression = getEnableCompression(); |
---|
138 | if (enableCompression) { |
---|
139 | try { |
---|
140 | byte[] unZippedBytes = Zipper.unzip(bytes); |
---|
141 | return instance.deserializeResponse(unZippedBytes, type); |
---|
142 | } catch (IOException e) { |
---|
143 | throw new SerializerException(e.getMessage(), e); |
---|
144 | } |
---|
145 | } else { |
---|
146 | return instance.deserializeResponse(bytes, type); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | } |
---|