1 | package omq.common.util; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.util.Properties; |
---|
5 | |
---|
6 | import omq.common.event.Event; |
---|
7 | import omq.common.message.Request; |
---|
8 | import omq.common.message.Response; |
---|
9 | import omq.common.util.Serializers.ISerializer; |
---|
10 | import omq.exception.EnvironmentException; |
---|
11 | import omq.exception.SerializerException; |
---|
12 | import omq.server.remote.request.RemoteObject; |
---|
13 | |
---|
14 | /** |
---|
15 | * |
---|
16 | * @author Sergi Toda <sergi.toda@estudiants.urv.cat> |
---|
17 | * |
---|
18 | */ |
---|
19 | public class Serializer { |
---|
20 | public static ISerializer serializer; |
---|
21 | |
---|
22 | private static Boolean getEnableCompression() { |
---|
23 | Boolean enableCompression = false; |
---|
24 | try { |
---|
25 | Properties env = Environment.getEnvironment(); |
---|
26 | enableCompression = Boolean.valueOf(env.getProperty(ParameterQueue.ENABLECOMPRESSION, "false")); |
---|
27 | } catch (EnvironmentException e) { |
---|
28 | e.printStackTrace(); |
---|
29 | } |
---|
30 | |
---|
31 | return enableCompression; |
---|
32 | } |
---|
33 | |
---|
34 | public static ISerializer getInstance() throws SerializerException { |
---|
35 | if (serializer == null) { |
---|
36 | try { |
---|
37 | Properties env = Environment.getEnvironment(); |
---|
38 | String className = env.getProperty(ParameterQueue.SERIALIZERNAME, "omq.common.util.Serializers.JavaImp"); |
---|
39 | |
---|
40 | if (className == null || className.isEmpty()) { |
---|
41 | throw new ClassNotFoundException("Class name is null or empty."); |
---|
42 | } |
---|
43 | |
---|
44 | serializer = (ISerializer) Class.forName(className).newInstance(); |
---|
45 | } catch (Exception ex) { |
---|
46 | throw new SerializerException(ex.getMessage(), ex); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | return serializer; |
---|
51 | } |
---|
52 | |
---|
53 | public static byte[] serialize(Object obj) throws SerializerException { |
---|
54 | ISerializer instance = getInstance(); |
---|
55 | |
---|
56 | Boolean enableCompression = getEnableCompression(); |
---|
57 | if (enableCompression) { |
---|
58 | byte[] objSerialized = instance.serialize(obj); |
---|
59 | try { |
---|
60 | return Zipper.zip(objSerialized); |
---|
61 | } catch (IOException e) { |
---|
62 | throw new SerializerException(e.getMessage(), e); |
---|
63 | } |
---|
64 | } else { |
---|
65 | return instance.serialize(obj); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | public static Request deserializeRequest(byte[] bytes, RemoteObject obj) throws SerializerException { |
---|
70 | ISerializer instance = getInstance(); |
---|
71 | |
---|
72 | Boolean enableCompression = getEnableCompression(); |
---|
73 | if (enableCompression) { |
---|
74 | try { |
---|
75 | byte[] unZippedBytes = Zipper.unzip(bytes); |
---|
76 | return instance.deserializeRequest(unZippedBytes, obj); |
---|
77 | } catch (IOException e) { |
---|
78 | throw new SerializerException(e.getMessage(), e); |
---|
79 | } |
---|
80 | } else { |
---|
81 | return instance.deserializeRequest(bytes, obj); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | public static Response deserializeResponse(byte[] bytes, Class<?> type) throws SerializerException { |
---|
86 | ISerializer instance = getInstance(); |
---|
87 | |
---|
88 | Boolean enableCompression = getEnableCompression(); |
---|
89 | if (enableCompression) { |
---|
90 | try { |
---|
91 | byte[] unZippedBytes = Zipper.unzip(bytes); |
---|
92 | return instance.deserializeResponse(unZippedBytes, type); |
---|
93 | } catch (IOException e) { |
---|
94 | throw new SerializerException(e.getMessage(), e); |
---|
95 | } |
---|
96 | } else { |
---|
97 | return instance.deserializeResponse(bytes, type); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | public static Event deserializeEvent(byte[] bytes) throws SerializerException { |
---|
102 | ISerializer instance = getInstance(); |
---|
103 | |
---|
104 | Boolean enableCompression = getEnableCompression(); |
---|
105 | if (enableCompression) { |
---|
106 | try { |
---|
107 | byte[] unZippedBytes = Zipper.unzip(bytes); |
---|
108 | return instance.deserializeEvent(unZippedBytes); |
---|
109 | } catch (IOException e) { |
---|
110 | throw new SerializerException(e.getMessage(), e); |
---|
111 | } |
---|
112 | } else { |
---|
113 | return instance.deserializeEvent(bytes); |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|