[44] | 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.GsonImp; |
---|
| 10 | import omq.common.util.Serializers.ISerializer; |
---|
| 11 | import omq.common.util.Serializers.JavaImp; |
---|
| 12 | import omq.common.util.Serializers.KryoImp; |
---|
| 13 | import omq.exception.EnvironmentException; |
---|
| 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 = KryoImp.class.getCanonicalName(); |
---|
| 24 | public static String java = JavaImp.class.getCanonicalName(); |
---|
| 25 | public static String gson = GsonImp.class.getCanonicalName(); |
---|
| 26 | |
---|
| 27 | public static ISerializer serializer; |
---|
| 28 | |
---|
| 29 | private static Boolean getEnableCompression() { |
---|
| 30 | Boolean enableCompression = false; |
---|
| 31 | try { |
---|
| 32 | Properties env = Environment.getEnvironment(); |
---|
| 33 | enableCompression = Boolean.valueOf(env.getProperty(ParameterQueue.ENABLECOMPRESSION, "false")); |
---|
| 34 | } catch (EnvironmentException e) { |
---|
| 35 | e.printStackTrace(); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | return enableCompression; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | public static ISerializer getInstance() throws SerializerException { |
---|
| 42 | if (serializer == null) { |
---|
| 43 | try { |
---|
| 44 | Properties env = Environment.getEnvironment(); |
---|
| 45 | String className = env.getProperty(ParameterQueue.SERIALIZER_NAME, Serializer.java); |
---|
| 46 | |
---|
| 47 | if (className == null || className.isEmpty()) { |
---|
| 48 | throw new ClassNotFoundException("Class name is null or empty."); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | serializer = (ISerializer) Class.forName(className).newInstance(); |
---|
| 52 | } catch (Exception ex) { |
---|
| 53 | throw new SerializerException(ex.getMessage(), ex); |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | return serializer; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | public static byte[] serialize(Object obj) throws SerializerException { |
---|
| 61 | ISerializer instance = getInstance(); |
---|
| 62 | |
---|
| 63 | Boolean enableCompression = getEnableCompression(); |
---|
| 64 | if (enableCompression) { |
---|
| 65 | byte[] objSerialized = instance.serialize(obj); |
---|
| 66 | try { |
---|
| 67 | return Zipper.zip(objSerialized); |
---|
| 68 | } catch (IOException e) { |
---|
| 69 | throw new SerializerException(e.getMessage(), e); |
---|
| 70 | } |
---|
| 71 | } else { |
---|
| 72 | return instance.serialize(obj); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | public static Request deserializeRequest(byte[] bytes, RemoteObject obj) throws SerializerException { |
---|
| 77 | ISerializer instance = getInstance(); |
---|
| 78 | |
---|
| 79 | Boolean enableCompression = getEnableCompression(); |
---|
| 80 | if (enableCompression) { |
---|
| 81 | try { |
---|
| 82 | byte[] unZippedBytes = Zipper.unzip(bytes); |
---|
| 83 | return instance.deserializeRequest(unZippedBytes, obj); |
---|
| 84 | } catch (IOException e) { |
---|
| 85 | throw new SerializerException(e.getMessage(), e); |
---|
| 86 | } |
---|
| 87 | } else { |
---|
| 88 | return instance.deserializeRequest(bytes, obj); |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | public static Response deserializeResponse(byte[] bytes, Class<?> type) throws SerializerException { |
---|
| 93 | ISerializer instance = getInstance(); |
---|
| 94 | |
---|
| 95 | Boolean enableCompression = getEnableCompression(); |
---|
| 96 | if (enableCompression) { |
---|
| 97 | try { |
---|
| 98 | byte[] unZippedBytes = Zipper.unzip(bytes); |
---|
| 99 | return instance.deserializeResponse(unZippedBytes, type); |
---|
| 100 | } catch (IOException e) { |
---|
| 101 | throw new SerializerException(e.getMessage(), e); |
---|
| 102 | } |
---|
| 103 | } else { |
---|
| 104 | return instance.deserializeResponse(bytes, type); |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | public static Event deserializeEvent(byte[] bytes) throws SerializerException { |
---|
| 109 | ISerializer instance = getInstance(); |
---|
| 110 | |
---|
| 111 | Boolean enableCompression = getEnableCompression(); |
---|
| 112 | if (enableCompression) { |
---|
| 113 | try { |
---|
| 114 | byte[] unZippedBytes = Zipper.unzip(bytes); |
---|
| 115 | return instance.deserializeEvent(unZippedBytes); |
---|
| 116 | } catch (IOException e) { |
---|
| 117 | throw new SerializerException(e.getMessage(), e); |
---|
| 118 | } |
---|
| 119 | } else { |
---|
| 120 | return instance.deserializeEvent(bytes); |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | } |
---|