ResourceFactory.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package com.x.server.console;
  2. import java.io.PrintStream;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7. import java.util.Properties;
  8. import java.util.TreeMap;
  9. import java.util.concurrent.ExecutorService;
  10. import java.util.concurrent.Executors;
  11. import java.util.concurrent.LinkedBlockingQueue;
  12. import org.apache.commons.collections.MapUtils;
  13. import org.apache.commons.collections4.ListUtils;
  14. import org.apache.commons.lang3.BooleanUtils;
  15. import org.eclipse.jetty.plus.jndi.Resource;
  16. import org.eclipse.jetty.util.RolloverFileOutputStream;
  17. import com.alibaba.druid.pool.DruidDataSourceC3P0Adapter;
  18. import com.google.gson.JsonElement;
  19. import com.x.base.core.container.factory.SlicePropertiesBuilder;
  20. import com.x.base.core.entity.Storage;
  21. import com.x.base.core.entity.annotation.ContainerEntity;
  22. import com.x.base.core.project.annotation.Module;
  23. import com.x.base.core.project.config.CenterServer;
  24. import com.x.base.core.project.config.Config;
  25. import com.x.base.core.project.config.DataServer;
  26. import com.x.base.core.project.config.ExternalDataSource;
  27. import com.x.base.core.project.logger.Logger;
  28. import com.x.base.core.project.logger.LoggerFactory;
  29. import com.x.base.core.project.tools.ClassLoaderTools;
  30. import com.x.base.core.project.tools.DefaultCharset;
  31. import com.x.base.core.project.tools.ListTools;
  32. import com.x.server.console.node.EventQueueExecutor;
  33. import io.github.classgraph.ClassGraph;
  34. import io.github.classgraph.ClassInfo;
  35. import io.github.classgraph.ScanResult;
  36. public class ResourceFactory {
  37. private static Logger logger = LoggerFactory.getLogger(ResourceFactory.class);
  38. private ResourceFactory() {
  39. // nothing
  40. }
  41. public static void bind() throws Exception {
  42. try (ScanResult sr = new ClassGraph()
  43. .addClassLoader(ClassLoaderTools.urlClassLoader(true, false, true, true, true)).enableAnnotationInfo()
  44. .scan()) {
  45. node(sr);
  46. containerEntities(sr);
  47. containerEntityNames(sr);
  48. stroageContainerEntityNames(sr);
  49. }
  50. if (BooleanUtils.isTrue(Config.logLevel().audit().enable())) {
  51. auditLog();
  52. }
  53. if (BooleanUtils.isTrue(Config.externalDataSources().enable())) {
  54. external();
  55. } else {
  56. internal();
  57. }
  58. processPlatformExecutors();
  59. }
  60. private static void node(ScanResult sr) throws Exception {
  61. LinkedBlockingQueue<JsonElement> eventQueue = new LinkedBlockingQueue<>();
  62. EventQueueExecutor eventQueueExecutor = new EventQueueExecutor(eventQueue);
  63. eventQueueExecutor.start();
  64. new Resource(Config.RESOURCE_NODE_EVENTQUEUE, eventQueue);
  65. new Resource(Config.RESOURCE_NODE_EVENTQUEUEEXECUTOR, eventQueueExecutor);
  66. new Resource(Config.RESOURCE_NODE_APPLICATIONS, null);
  67. new Resource(Config.RESOURCE_NODE_APPLICATIONSTIMESTAMP, null);
  68. Entry<String, CenterServer> entry = Config.nodes().centerServers().first();
  69. new Resource(Config.RESOURCE_NODE_CENTERSPRIMARYNODE, entry.getKey());
  70. new Resource(Config.RESOURCE_NODE_CENTERSPRIMARYPORT, entry.getValue().getPort());
  71. new Resource(Config.RESOURCE_NODE_CENTERSPRIMARYSSLENABLE, entry.getValue().getSslEnable());
  72. }
  73. private static void external() throws Exception {
  74. external_druid_c3p0();
  75. }
  76. private static void external_druid_c3p0() throws Exception {
  77. for (ExternalDataSource ds : Config.externalDataSources()) {
  78. if (BooleanUtils.isNotTrue(ds.getEnable())) {
  79. continue;
  80. }
  81. DruidDataSourceC3P0Adapter dataSource = new DruidDataSourceC3P0Adapter();
  82. dataSource.setJdbcUrl(ds.getUrl());
  83. dataSource.setDriverClass(ds.getDriverClassName());
  84. dataSource.setPreferredTestQuery(SlicePropertiesBuilder.validationQueryOfUrl(ds.getUrl()));
  85. dataSource.setUser(ds.getUsername());
  86. dataSource.setPassword(ds.getPassword());
  87. dataSource.setMaxPoolSize(ds.getMaxTotal());
  88. dataSource.setMinPoolSize(ds.getMaxIdle());
  89. // 增加校验
  90. dataSource.setTestConnectionOnCheckin(true);
  91. dataSource.setTestConnectionOnCheckout(true);
  92. dataSource.setAcquireIncrement(2);
  93. if (BooleanUtils.isTrue(ds.getStatEnable())) {
  94. dataSource.setFilters(ds.getStatFilter());
  95. Properties properties = new Properties();
  96. properties.setProperty("druid.stat.slowSqlMillis", ds.getSlowSqlMillis().toString());
  97. dataSource.setProperties(properties);
  98. }
  99. String name = Config.externalDataSources().name(ds);
  100. new Resource(Config.RESOURCE_JDBC_PREFIX + name, dataSource);
  101. }
  102. }
  103. private static void internal() throws Exception {
  104. internal_driud_c3p0();
  105. }
  106. private static void internal_driud_c3p0() throws Exception {
  107. for (Entry<String, DataServer> entry : Config.nodes().dataServers().entrySet()) {
  108. DruidDataSourceC3P0Adapter dataSource = new DruidDataSourceC3P0Adapter();
  109. String url = "jdbc:h2:tcp://" + entry.getKey() + ":" + entry.getValue().getTcpPort()
  110. + "/X;LOCK_MODE=0;DEFAULT_LOCK_TIMEOUT=" + entry.getValue().getLockTimeout() + ";JMX="
  111. + (BooleanUtils.isTrue(entry.getValue().getJmxEnable()) ? "TRUE" : "FALSE") + ";CACHE_SIZE="
  112. + (entry.getValue().getCacheSize() * 1024);
  113. dataSource.setJdbcUrl(url);
  114. dataSource.setDriverClass(SlicePropertiesBuilder.driver_h2);
  115. dataSource.setPreferredTestQuery(SlicePropertiesBuilder.validationQueryOfUrl(url));
  116. dataSource.setUser("sa");
  117. dataSource.setPassword(Config.token().getPassword());
  118. dataSource.setMaxPoolSize(entry.getValue().getMaxTotal());
  119. dataSource.setMinPoolSize(entry.getValue().getMaxIdle());
  120. dataSource.setAcquireIncrement(2);
  121. if (BooleanUtils.isTrue(entry.getValue().getStatEnable())) {
  122. dataSource.setFilters(entry.getValue().getStatFilter());
  123. Properties properties = new Properties();
  124. properties.setProperty("druid.stat.slowSqlMillis", entry.getValue().getSlowSqlMillis().toString());
  125. dataSource.setProperties(properties);
  126. }
  127. String name = Config.nodes().dataServers().name(entry.getValue());
  128. new Resource(Config.RESOURCE_JDBC_PREFIX + name, dataSource);
  129. }
  130. }
  131. private static void containerEntityNames(ScanResult sr) throws Exception {
  132. List<String> list = new ArrayList<>();
  133. for (ClassInfo info : sr.getClassesWithAnnotation(ContainerEntity.class.getName())) {
  134. list.add(info.getName());
  135. }
  136. list = ListTools.trim(list, true, true);
  137. new Resource(Config.RESOURCE_CONTAINERENTITYNAMES, ListUtils.unmodifiableList(list));
  138. }
  139. private static void stroageContainerEntityNames(ScanResult sr) throws Exception {
  140. List<String> list = new ArrayList<>();
  141. for (ClassInfo info : sr.getClassesWithAnnotation(Storage.class.getName())) {
  142. list.add(info.getName());
  143. }
  144. list = ListTools.trim(list, true, true);
  145. new Resource(Config.RESOURCE_STORAGECONTAINERENTITYNAMES, ListUtils.unmodifiableList(list));
  146. }
  147. private static void containerEntities(ScanResult sr) throws Exception {
  148. Map<String, List<String>> map = new TreeMap<>();
  149. for (ClassInfo info : sr.getClassesWithAnnotation(Module.class.getName())) {
  150. Class<?> cls = Class.forName(info.getName());
  151. List<String> os = ListTools.toList(cls.getAnnotation(Module.class).containerEntities());
  152. map.put(info.getName(), ListUtils.unmodifiableList(os));
  153. }
  154. new Resource(Config.RESOURCE_CONTAINERENTITIES, MapUtils.unmodifiableMap(map));
  155. }
  156. private static void auditLog() throws Exception {
  157. RolloverFileOutputStream rolloverFileOutputStream = new RolloverFileOutputStream(
  158. Config.dir_logs(true).getAbsolutePath() + "/yyyy_mm_dd.audit.log", true,
  159. Config.logLevel().audit().logSize());
  160. new Resource(Config.RESOURCE_AUDITLOGPRINTSTREAM,
  161. new PrintStream(rolloverFileOutputStream, true, DefaultCharset.name_iso_utf_8));
  162. }
  163. private static void processPlatformExecutors() throws Exception {
  164. ExecutorService[] services = new ExecutorService[Config.processPlatform().getExecutorCount()];
  165. for (int i = 0; i < Config.processPlatform().getExecutorCount(); i++) {
  166. services[i] = Executors.newFixedThreadPool(1);
  167. }
  168. new Resource(Config.RESOURCE_NODE_PROCESSPLATFORMEXECUTORS, services);
  169. }
  170. }