[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.SerializerException; |
---|
| 14 | import omq.server.RemoteObject; |
---|
| 15 | |
---|
| 16 | /** |
---|
| 17 | * |
---|
| 18 | * @author Sergi Toda <sergi.toda@estudiants.urv.cat> |
---|
| 19 | * |
---|
| 20 | */ |
---|
| 21 | public class Serializer { |
---|
[53] | 22 | // private static final Logger logger = |
---|
| 23 | // Logger.getLogger(Serializer.class.getName()); |
---|
[62] | 24 | public static final String KRYO = "kryo"; |
---|
| 25 | public static final String JAVA = "java"; |
---|
| 26 | public static final String GSON = "gson"; |
---|
[44] | 27 | |
---|
[47] | 28 | // Client serializer |
---|
[53] | 29 | public ISerializer serializer; |
---|
[44] | 30 | |
---|
[47] | 31 | // Server serializers |
---|
[53] | 32 | private ISerializer kryoSerializer; |
---|
| 33 | private ISerializer javaSerializer; |
---|
| 34 | private ISerializer gsonSerializer; |
---|
[47] | 35 | |
---|
[53] | 36 | private Properties env; |
---|
| 37 | |
---|
| 38 | public Serializer(Properties env) { |
---|
| 39 | this.env = env; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | private Boolean getEnableCompression() { |
---|
[47] | 43 | return Boolean.valueOf(env.getProperty(ParameterQueue.ENABLECOMPRESSION, "false")); |
---|
[44] | 44 | } |
---|
| 45 | |
---|
[53] | 46 | public ISerializer getInstance() throws SerializerException { |
---|
[44] | 47 | if (serializer == null) { |
---|
| 48 | try { |
---|
[62] | 49 | String className = env.getProperty(ParameterQueue.SERIALIZER_NAME, Serializer.JAVA); |
---|
[87] | 50 | |
---|
[44] | 51 | if (className == null || className.isEmpty()) { |
---|
| 52 | throw new ClassNotFoundException("Class name is null or empty."); |
---|
| 53 | } |
---|
[87] | 54 | |
---|
[47] | 55 | serializer = getInstance(className); |
---|
[44] | 56 | } catch (Exception ex) { |
---|
| 57 | throw new SerializerException(ex.getMessage(), ex); |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | return serializer; |
---|
| 62 | } |
---|
| 63 | |
---|
[87] | 64 | public ISerializer getInstance(String type) throws SerializerException { |
---|
[62] | 65 | if (KRYO.equals(type)) { |
---|
[47] | 66 | if (kryoSerializer == null) { |
---|
| 67 | kryoSerializer = new KryoImp(); |
---|
| 68 | } |
---|
| 69 | return kryoSerializer; |
---|
[62] | 70 | } else if (GSON.equals(type)) { |
---|
[47] | 71 | if (gsonSerializer == null) { |
---|
| 72 | gsonSerializer = new GsonImp(); |
---|
| 73 | } |
---|
| 74 | return gsonSerializer; |
---|
[62] | 75 | } else if (JAVA.equals(type)) { |
---|
[47] | 76 | if (javaSerializer == null) { |
---|
| 77 | javaSerializer = new JavaImp(); |
---|
| 78 | } |
---|
| 79 | return javaSerializer; |
---|
| 80 | } |
---|
[87] | 81 | |
---|
| 82 | throw new SerializerException("Serializer '" + type + "' not found"); |
---|
[47] | 83 | } |
---|
| 84 | |
---|
[53] | 85 | public byte[] serialize(String type, Object obj) throws SerializerException { |
---|
[47] | 86 | ISerializer instance = getInstance(type); |
---|
| 87 | |
---|
| 88 | Boolean enableCompression = getEnableCompression(); |
---|
| 89 | if (enableCompression) { |
---|
| 90 | byte[] objSerialized = instance.serialize(obj); |
---|
| 91 | try { |
---|
| 92 | return Zipper.zip(objSerialized); |
---|
| 93 | } catch (IOException e) { |
---|
| 94 | throw new SerializerException(e.getMessage(), e); |
---|
| 95 | } |
---|
| 96 | } else { |
---|
| 97 | return instance.serialize(obj); |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | // TODO: remove this function and think about the event serialization |
---|
[53] | 102 | public byte[] serialize(Object obj) throws SerializerException { |
---|
[44] | 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 | |
---|
[53] | 118 | public Request deserializeRequest(String type, byte[] bytes, RemoteObject obj) throws SerializerException { |
---|
[47] | 119 | ISerializer instance = getInstance(type); |
---|
[44] | 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 | |
---|
[53] | 134 | public Response deserializeResponse(byte[] bytes, Class<?> type) throws SerializerException { |
---|
[44] | 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 | |
---|
[53] | 150 | public Event deserializeEvent(byte[] bytes) throws SerializerException { |
---|
[44] | 151 | ISerializer instance = getInstance(); |
---|
| 152 | |
---|
| 153 | Boolean enableCompression = getEnableCompression(); |
---|
| 154 | if (enableCompression) { |
---|
| 155 | try { |
---|
| 156 | byte[] unZippedBytes = Zipper.unzip(bytes); |
---|
| 157 | return instance.deserializeEvent(unZippedBytes); |
---|
| 158 | } catch (IOException e) { |
---|
| 159 | throw new SerializerException(e.getMessage(), e); |
---|
| 160 | } |
---|
| 161 | } else { |
---|
| 162 | return instance.deserializeEvent(bytes); |
---|
| 163 | } |
---|
| 164 | } |
---|
[47] | 165 | |
---|
[53] | 166 | // public static void removeSerializers() { |
---|
| 167 | // logger.warn("Removing serializers"); |
---|
| 168 | // serializer = null; |
---|
| 169 | // kryoSerializer = null; |
---|
| 170 | // javaSerializer = null; |
---|
| 171 | // gsonSerializer = null; |
---|
| 172 | // } |
---|
[44] | 173 | } |
---|