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