PageRenderTime 128ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/jvm/backtype/storm/serialization/SerializationFactory.java

https://github.com/Ang3lFir3/storm
Java | 207 lines | 171 code | 28 blank | 8 comment | 14 complexity | 804137e016002c2a4b4036ea92bf829f MD5 | raw file
  1. package backtype.storm.serialization;
  2. import backtype.storm.Config;
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import org.apache.log4j.Logger;
  9. public class SerializationFactory {
  10. public static Logger LOG = Logger.getLogger(SerializationFactory.class);
  11. private static byte[] EMPTY_BYTE_ARRAY = new byte[0];
  12. private Map<Integer, ISerialization> _serializations = new HashMap<Integer, ISerialization>() {{
  13. put(1, new ISerialization<Integer>() {
  14. public boolean accept(Class c) {
  15. return Integer.class.equals(c);
  16. }
  17. public void serialize(Integer object, DataOutputStream stream) throws IOException {
  18. stream.writeInt(object);
  19. // WritableUtils.writeVInt(stream, object);
  20. }
  21. public Integer deserialize(DataInputStream stream) throws IOException {
  22. // return WritableUtils.readVInt(stream);
  23. return stream.readInt();
  24. }
  25. });
  26. put(2, new ISerialization<Long>() {
  27. public boolean accept(Class c) {
  28. return Long.class.equals(c);
  29. }
  30. public void serialize(Long object, DataOutputStream stream) throws IOException {
  31. stream.writeLong(object);
  32. // WritableUtils.writeVLong(stream, object);
  33. }
  34. public Long deserialize(DataInputStream stream) throws IOException {
  35. // return WritableUtils.readVLong(stream);
  36. return stream.readLong();
  37. }
  38. });
  39. put(3, new ISerialization<Float>() {
  40. public boolean accept(Class c) {
  41. return Float.class.equals(c);
  42. }
  43. public void serialize(Float object, DataOutputStream stream) throws IOException {
  44. stream.writeFloat(object);
  45. }
  46. public Float deserialize(DataInputStream stream) throws IOException {
  47. return stream.readFloat();
  48. }
  49. });
  50. put(4, new ISerialization<Double>() {
  51. public boolean accept(Class c) {
  52. return Double.class.equals(c);
  53. }
  54. public void serialize(Double object, DataOutputStream stream) throws IOException {
  55. stream.writeDouble(object);
  56. }
  57. public Double deserialize(DataInputStream stream) throws IOException {
  58. return stream.readDouble();
  59. }
  60. });
  61. put(5, new ISerialization<Byte>() {
  62. public boolean accept(Class c) {
  63. return Byte.class.equals(c);
  64. }
  65. public void serialize(Byte object, DataOutputStream stream) throws IOException {
  66. stream.writeByte(object);
  67. }
  68. public Byte deserialize(DataInputStream stream) throws IOException {
  69. return stream.readByte();
  70. }
  71. });
  72. put(6, new ISerialization<Short>() {
  73. public boolean accept(Class c) {
  74. return Short.class.equals(c);
  75. }
  76. public void serialize(Short object, DataOutputStream stream) throws IOException {
  77. stream.writeShort(object);
  78. }
  79. public Short deserialize(DataInputStream stream) throws IOException {
  80. return stream.readShort();
  81. }
  82. });
  83. put(7, new ISerialization<String>() {
  84. public boolean accept(Class c) {
  85. return String.class.equals(c);
  86. }
  87. public void serialize(String object, DataOutputStream stream) throws IOException {
  88. stream.writeUTF(object);
  89. }
  90. public String deserialize(DataInputStream stream) throws IOException {
  91. return stream.readUTF();
  92. }
  93. });
  94. put(8, new ISerialization<Boolean>() {
  95. public boolean accept(Class c) {
  96. return Boolean.class.equals(c);
  97. }
  98. public void serialize(Boolean object, DataOutputStream stream) throws IOException {
  99. stream.writeBoolean(object);
  100. }
  101. public Boolean deserialize(DataInputStream stream) throws IOException {
  102. return stream.readBoolean();
  103. }
  104. });
  105. put(9, new ISerialization<byte[]>() {
  106. public boolean accept(Class c) {
  107. return EMPTY_BYTE_ARRAY.getClass().equals(c);
  108. }
  109. public void serialize(byte[] object, DataOutputStream stream) throws IOException {
  110. // WritableUtils.writeVInt(stream, object.length);
  111. stream.writeInt(object.length);
  112. stream.write(object, 0, object.length);
  113. }
  114. public byte[] deserialize(DataInputStream stream) throws IOException {
  115. // int size = WritableUtils.readVInt(stream);
  116. int size = stream.readInt();
  117. byte[] ret = new byte[size];
  118. stream.readFully(ret);
  119. return ret;
  120. }
  121. });
  122. }};
  123. public SerializationFactory(Map conf) {
  124. boolean skipMissing = (Boolean) conf.get(Config.TOPOLOGY_SKIP_MISSING_SERIALIZATIONS);
  125. Map<Object, String> customSerializations = (Map<Object, String>) conf.get(Config.TOPOLOGY_SERIALIZATIONS);
  126. if(customSerializations==null) customSerializations = new HashMap<Object, String>();
  127. for(Object tokenObj: customSerializations.keySet()) {
  128. String serializationClassName = customSerializations.get(tokenObj);
  129. int token = toToken(tokenObj);
  130. if(token<=32) {
  131. throw new RuntimeException("Illegal token " + token + " for " + serializationClassName);
  132. }
  133. try {
  134. LOG.info("Loading custom serialization " + serializationClassName + " for token " + token);
  135. Class serClass = Class.forName(serializationClassName);
  136. _serializations.put(token, (ISerialization) serClass.newInstance());
  137. } catch(ClassNotFoundException e) {
  138. if(skipMissing) {
  139. LOG.info("Could not find serialization for " + serializationClassName + ". Skipping...");
  140. } else {
  141. throw new RuntimeException(e);
  142. }
  143. } catch(InstantiationException e) {
  144. throw new RuntimeException(e);
  145. } catch(IllegalAccessException e) {
  146. throw new RuntimeException(e);
  147. }
  148. }
  149. }
  150. private int toToken(Object tokenObj) {
  151. if(tokenObj instanceof Long) {
  152. return ((Long) tokenObj).intValue();
  153. } else if(tokenObj instanceof Integer) {
  154. return (Integer) tokenObj;
  155. } else if (tokenObj instanceof String) {
  156. // this special case exists because user's storm conf gets serialized to json, which will convert numbered keys into strings
  157. // TODO: replace use of json in nimbus storm submit with yaml
  158. return Integer.parseInt((String) tokenObj);
  159. } else {
  160. throw new RuntimeException("Unexpected token class " + tokenObj + " " + tokenObj.getClass().toString());
  161. }
  162. }
  163. public FieldSerialization getSerializationForToken(int token) {
  164. ISerialization ser = _serializations.get(token);
  165. if(ser==null) {
  166. throw new RuntimeException("Could not find serialization for token " + token);
  167. }
  168. return new FieldSerialization(token, ser);
  169. }
  170. public FieldSerialization getSerializationForClass(Class klass) {
  171. for(int token: _serializations.keySet()) {
  172. ISerialization ser = _serializations.get(token);
  173. if(ser.accept(klass)) {
  174. return getSerializationForToken(token);
  175. }
  176. }
  177. throw new RuntimeException("Could not find serialization for class " + klass.toString());
  178. }
  179. }