Changeset 47 for trunk/src/main/java/omq/common/util/Serializer.java
- Timestamp:
- 06/18/13 16:51:22 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/main/java/omq/common/util/Serializer.java
r44 r47 4 4 import java.util.Properties; 5 5 6 import omq.common.broker.Broker; 6 7 import omq.common.event.Event; 7 8 import omq.common.message.Request; … … 11 12 import omq.common.util.Serializers.JavaImp; 12 13 import omq.common.util.Serializers.KryoImp; 13 import omq.exception.EnvironmentException;14 14 import omq.exception.SerializerException; 15 15 import omq.server.RemoteObject; … … 21 21 */ 22 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();23 public static String kryo = "kryo"; 24 public static String java = "java"; 25 public static String gson = "gson"; 26 26 27 // Client serializer 27 28 public static ISerializer serializer; 28 29 30 // Server serializers 31 private static ISerializer kryoSerializer; 32 private static ISerializer javaSerializer; 33 private static ISerializer gsonSerializer; 34 29 35 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; 36 Properties env = Broker.getEnvironment(); 37 return Boolean.valueOf(env.getProperty(ParameterQueue.ENABLECOMPRESSION, "false")); 39 38 } 40 39 … … 42 41 if (serializer == null) { 43 42 try { 44 Properties env = Environment.getEnvironment();43 Properties env = Broker.getEnvironment(); 45 44 String className = env.getProperty(ParameterQueue.SERIALIZER_NAME, Serializer.java); 46 45 … … 49 48 } 50 49 51 serializer = (ISerializer) Class.forName(className).newInstance();50 serializer = getInstance(className); 52 51 } catch (Exception ex) { 53 52 throw new SerializerException(ex.getMessage(), ex); … … 58 57 } 59 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 60 95 public static byte[] serialize(Object obj) throws SerializerException { 61 96 ISerializer instance = getInstance(); … … 74 109 } 75 110 76 public static Request deserializeRequest( byte[] bytes, RemoteObject obj) throws SerializerException {77 ISerializer instance = getInstance( );111 public static Request deserializeRequest(String type, byte[] bytes, RemoteObject obj) throws SerializerException { 112 ISerializer instance = getInstance(type); 78 113 79 114 Boolean enableCompression = getEnableCompression(); … … 121 156 } 122 157 } 158 159 public static void removeSerializers() { 160 serializer = null; 161 kryoSerializer = null; 162 javaSerializer = null; 163 gsonSerializer = null; 164 } 123 165 }
Note: See TracChangeset
for help on using the changeset viewer.