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