AbstractMessage.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.dianping.cat.message.internal;
  2. import io.netty.buffer.ByteBuf;
  3. import io.netty.buffer.ByteBufAllocator;
  4. import java.nio.charset.Charset;
  5. import com.dianping.cat.message.Message;
  6. import com.dianping.cat.message.spi.codec.PlainTextMessageCodec;
  7. public abstract class AbstractMessage implements Message {
  8. private String m_type;
  9. private String m_name;
  10. private String m_status = "unset";
  11. private long m_timestampInMillis;
  12. private CharSequence m_data;
  13. private boolean m_completed;
  14. public AbstractMessage(String type, String name) {
  15. m_type = String.valueOf(type);
  16. m_name = String.valueOf(name);
  17. m_timestampInMillis = MilliSecondTimer.currentTimeMillis();
  18. }
  19. @Override
  20. public void addData(String keyValuePairs) {
  21. if (m_data == null) {
  22. m_data = keyValuePairs;
  23. } else if (m_data instanceof StringBuilder) {
  24. ((StringBuilder) m_data).append('&').append(keyValuePairs);
  25. } else {
  26. StringBuilder sb = new StringBuilder(m_data.length() + keyValuePairs.length() + 16);
  27. sb.append(m_data).append('&');
  28. sb.append(keyValuePairs);
  29. m_data = sb;
  30. }
  31. }
  32. @Override
  33. public void addData(String key, Object value) {
  34. if (m_data instanceof StringBuilder) {
  35. ((StringBuilder) m_data).append('&').append(key).append('=').append(value);
  36. } else {
  37. String str = String.valueOf(value);
  38. int old = m_data == null ? 0 : m_data.length();
  39. StringBuilder sb = new StringBuilder(old + key.length() + str.length() + 16);
  40. if (m_data != null) {
  41. sb.append(m_data).append('&');
  42. }
  43. sb.append(key).append('=').append(str);
  44. m_data = sb;
  45. }
  46. }
  47. @Override
  48. public CharSequence getData() {
  49. if (m_data == null) {
  50. return "";
  51. } else {
  52. return m_data;
  53. }
  54. }
  55. @Override
  56. public String getName() {
  57. return m_name;
  58. }
  59. @Override
  60. public String getStatus() {
  61. return m_status;
  62. }
  63. @Override
  64. public long getTimestamp() {
  65. return m_timestampInMillis;
  66. }
  67. @Override
  68. public String getType() {
  69. return m_type;
  70. }
  71. @Override
  72. public boolean isCompleted() {
  73. return m_completed;
  74. }
  75. @Override
  76. public boolean isSuccess() {
  77. return Message.SUCCESS.equals(m_status);
  78. }
  79. public void setCompleted(boolean completed) {
  80. m_completed = completed;
  81. }
  82. public void setName(String name) {
  83. m_name = name;
  84. }
  85. @Override
  86. public void setStatus(String status) {
  87. m_status = status;
  88. }
  89. @Override
  90. public void setStatus(Throwable e) {
  91. m_status = e.getClass().getName();
  92. }
  93. public void setTimestamp(long timestamp) {
  94. m_timestampInMillis = timestamp;
  95. }
  96. public void setType(String type) {
  97. m_type = type;
  98. }
  99. @Override
  100. public String toString() {
  101. PlainTextMessageCodec codec = new PlainTextMessageCodec();
  102. ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
  103. codec.encodeMessage(this, buf);
  104. codec.reset();
  105. return buf.toString(Charset.forName("utf-8"));
  106. }
  107. }