/lofka-persistence/src/main/java/com/github/tsingjyujing/lofka/persistence/util/ConfigLoader.java

https://github.com/TsingJyujing/lofka · Java · 232 lines · 134 code · 34 blank · 64 comment · 6 complexity · d5e6853c59f5b1faa753ee83c8c1db93 MD5 · raw file

  1. package com.github.tsingjyujing.lofka.persistence.util;
  2. import com.github.tsingjyujing.lofka.persistence.basic.IBatchLoggerProcessor;
  3. import com.github.tsingjyujing.lofka.persistence.basic.ILogReceiver;
  4. import com.github.tsingjyujing.lofka.persistence.source.KafkaMultiPersistence;
  5. import com.github.tsingjyujing.lofka.persistence.source.LocalWriterQueue;
  6. import com.github.tsingjyujing.lofka.persistence.writers.ElasticSearchWriter;
  7. import com.github.tsingjyujing.lofka.persistence.writers.LocalFileWriter;
  8. import com.github.tsingjyujing.lofka.persistence.writers.MongoDBWriter;
  9. import com.github.tsingjyujing.lofka.util.FileUtil;
  10. import com.google.common.collect.Lists;
  11. import com.google.gson.Gson;
  12. import com.google.gson.JsonElement;
  13. import com.google.gson.JsonParser;
  14. import com.google.gson.stream.JsonReader;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import java.io.IOException;
  18. import java.io.StringReader;
  19. import java.util.*;
  20. /**
  21. * 配置文件加载器
  22. *
  23. * 通过加载格式化的JSON文件内容
  24. *
  25. * @author yuanyifan
  26. */
  27. public class ConfigLoader {
  28. private static final Gson GSON = new Gson();
  29. protected static final Logger LOGGER = LoggerFactory.getLogger(ConfigLoader.class);
  30. /**
  31. * 消息处理器的工厂方法
  32. *
  33. * @param processorInfo 需要初始化的处理器信息
  34. * @return
  35. * @throws ClassNotFoundException 无法找到该初始化方法
  36. * @throws IOException 无法读取相应的配置文件
  37. */
  38. public static IBatchLoggerProcessor processorFactory(ProcessorInfo processorInfo) throws Exception {
  39. switch (processorInfo.getProcessorType().toLowerCase()) {
  40. case "mongodb":
  41. return new MongoDBWriter(processorInfo.getProperties());
  42. case "elasticsearch":
  43. return new ElasticSearchWriter(processorInfo.getProperties());
  44. case "file":
  45. return new LocalFileWriter(processorInfo.getProperties());
  46. // 可以在这里继续增加相应的持久化工厂方法
  47. default:
  48. throw new ClassNotFoundException("Can't initialize by name:" + processorInfo.getProcessorType());
  49. }
  50. }
  51. /**
  52. * 数据源的工厂方法
  53. *
  54. * @param srcInfo
  55. * @param processors
  56. * @return
  57. * @throws IOException
  58. * @throws ClassNotFoundException
  59. */
  60. public static ILogReceiver sourceFactory(SourceInfo srcInfo, Collection<IBatchLoggerProcessor> processors) throws Exception {
  61. switch (srcInfo.getSourceType()) {
  62. case "kafka":
  63. return new KafkaMultiPersistence(
  64. srcInfo.getProperties(),
  65. processors
  66. );
  67. case "local":
  68. return new LocalWriterQueue(
  69. srcInfo.getProperties(),
  70. processors
  71. );
  72. default:
  73. throw new ClassNotFoundException("Can't initialize by name:" + srcInfo.getSourceType());
  74. }
  75. }
  76. /**
  77. * 方便解析配置文件的实体类
  78. */
  79. public static class ProcessorInfo {
  80. /**
  81. * 日志处理器的类型
  82. */
  83. private String processorType;
  84. /**
  85. * Map类型的配置信息
  86. */
  87. private Map<String, String> config;
  88. public String getProcessorType() {
  89. return processorType;
  90. }
  91. public void setProcessorType(String processorType) {
  92. this.processorType = processorType;
  93. }
  94. public Map<String, String> getConfig() {
  95. return config;
  96. }
  97. public void setConfig(Map<String, String> config) {
  98. this.config = config;
  99. }
  100. public IBatchLoggerProcessor getProcessor() throws Exception {
  101. return processorFactory(this);
  102. }
  103. public Properties getProperties() {
  104. final Properties prop = new Properties();
  105. prop.putAll(getConfig());
  106. return prop;
  107. }
  108. }
  109. /**
  110. * 解析Source配置文件的实体类
  111. */
  112. public static class SourceInfo {
  113. /**
  114. * 数据源类型
  115. */
  116. private String sourceType;
  117. /**
  118. *
  119. */
  120. private Map<String, String> config;
  121. public String getSourceType() {
  122. return sourceType;
  123. }
  124. public void setSourceType(String sourceType) {
  125. this.sourceType = sourceType;
  126. }
  127. public Map<String, String> getConfig() {
  128. return config;
  129. }
  130. public void setConfig(Map<String, String> config) {
  131. this.config = config;
  132. }
  133. public List<ProcessorInfo> getProcessors() {
  134. final ArrayList<ProcessorInfo> processorList = Lists.newArrayList();
  135. for (Object processor : processors.values()) {
  136. processorList.add(GSON.fromJson(GSON.toJson(processor), ProcessorInfo.class));
  137. }
  138. return processorList;
  139. }
  140. public void setProcessors(Map<String, Object> processors) {
  141. this.processors = processors;
  142. }
  143. private Map<String, Object> processors;
  144. public ILogReceiver getSource() throws Exception {
  145. ArrayList<IBatchLoggerProcessor> result = Lists.newArrayList();
  146. for (ProcessorInfo processorInfo : getProcessors()) {
  147. result.add(processorInfo.getProcessor());
  148. }
  149. return sourceFactory(this, result);
  150. }
  151. public Properties getProperties() {
  152. final Properties prop = new Properties();
  153. prop.putAll(getConfig());
  154. return prop;
  155. }
  156. }
  157. /**
  158. * 从配置文件中加载数据源
  159. *
  160. * @param jsonConfigFile JSON配置文件
  161. * @return
  162. * @throws IOException
  163. */
  164. public static ArrayList<ILogReceiver> loadSource(String jsonConfigFile) throws Exception {
  165. final ArrayList<ILogReceiver> result = Lists.newArrayList();
  166. for (SourceInfo sourceInfo : stringToList(FileUtil.getFileText(jsonConfigFile), SourceInfo.class)) {
  167. result.add(sourceInfo.getSource());
  168. }
  169. return result;
  170. }
  171. /**
  172. * 从默认配置文件中加载处理器
  173. *
  174. * @return
  175. * @throws IOException
  176. */
  177. public static ArrayList<ILogReceiver> loadSource() throws Exception {
  178. return loadSource("source.json");
  179. }
  180. /**
  181. * JSON String 转成 bean list
  182. *
  183. * @param gsonString
  184. * @param cls
  185. * @param <T>
  186. * @return
  187. */
  188. public static <T> ArrayList<T> stringToList(String gsonString, Class<T> cls) {
  189. final ArrayList<T> result = new ArrayList<>();
  190. for (final JsonElement elem : new JsonParser().parse(gsonString).getAsJsonArray()) {
  191. Gson gson = new Gson();
  192. final JsonReader reader = new JsonReader(new StringReader(gsonString));
  193. reader.setLenient(true);
  194. result.add(gson.fromJson(elem, cls));
  195. }
  196. return result;
  197. }
  198. }