YmlConfiguration.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package com.yiidata.intergration.proxy;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.google.common.base.Charsets;
  5. import com.google.common.collect.Lists;
  6. import com.google.common.io.Files;
  7. import com.yiidata.intergration.api.SwaggerController;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.commons.cli.CommandLine;
  10. import org.apache.commons.cli.GnuParser;
  11. import org.apache.commons.cli.HelpFormatter;
  12. import org.apache.commons.cli.Options;
  13. import org.apache.commons.cli.ParseException;
  14. import org.apache.commons.collections.CollectionUtils;
  15. import org.apache.commons.lang.StringUtils;
  16. import org.eclipse.jetty.server.CustomRequestLog;
  17. import org.eclipse.jetty.server.RequestLog;
  18. import org.eclipse.jetty.server.Server;
  19. import org.eclipse.jetty.server.Slf4jRequestLogWriter;
  20. import org.eclipse.jetty.server.handler.HandlerCollection;
  21. import org.eclipse.jetty.server.handler.RequestLogHandler;
  22. import org.eclipse.jetty.server.handler.gzip.GzipHandler;
  23. import org.eclipse.jetty.util.resource.PathResource;
  24. import org.eclipse.jetty.util.thread.QueuedThreadPool;
  25. import org.eclipse.jetty.util.thread.ThreadPool;
  26. import org.eclipse.jetty.xml.XmlConfiguration;
  27. import org.yaml.snakeyaml.Yaml;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import java.lang.reflect.Array;
  31. import java.net.InetSocketAddress;
  32. import java.net.URL;
  33. import java.util.ArrayList;
  34. import java.util.Collection;
  35. import java.util.HashSet;
  36. import java.util.List;
  37. import java.util.Map;
  38. import java.util.Optional;
  39. import java.util.Set;
  40. /**
  41. * <pre>
  42. *
  43. * Created by zhenqin.
  44. * User: zhenqin
  45. * Date: 2019/11/13
  46. * Time: 14:51
  47. * Vendor: yiidata.com
  48. * To change this template use File | Settings | File Templates.
  49. *
  50. * </pre>
  51. *
  52. * @author zhenqin
  53. */
  54. @Slf4j
  55. public class YmlConfiguration {
  56. /**
  57. * 获取 AMC 部署的目录
  58. * @return 返回 AMC 部署的目录
  59. */
  60. public static String getAppHome(){
  61. /*
  62. * modify By ZhenQin AT:2018-1-5
  63. * 增加了先从环境变量中读取该参数,再从 系统JVM 变量中读取
  64. */
  65. String spHome = System.getenv("APP_HOME");
  66. if(StringUtils.isBlank(spHome)) {
  67. spHome = System.getProperty("app.home");
  68. }
  69. // 返回当前目录
  70. if(StringUtils.isBlank(spHome)){
  71. spHome = ".";
  72. }
  73. return spHome;
  74. }
  75. /**
  76. * 返回 jvm 加載的 ClassLoader
  77. * @return
  78. */
  79. public static ClassLoader getClassLoader(){
  80. ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  81. if(classLoader == null){
  82. classLoader = ProxyAppServer.class.getClassLoader();
  83. }
  84. return classLoader;
  85. }
  86. /**
  87. * 獲取項目類路徑的 Classpath
  88. * @return
  89. */
  90. public static String getClasspath(){
  91. URL resource = getClassLoader().getResource("");
  92. if(resource != null){
  93. String classpath = resource.getPath();
  94. return classpath;
  95. }
  96. return getAppHome() + File.separator + "etc";
  97. }
  98. /**
  99. * 从 conf 中获得 整形 值
  100. * @param conf
  101. * @param key
  102. * @return
  103. */
  104. private Integer getInteger(Map<String, Object> conf, String key) {
  105. return getInteger(conf, key, null);
  106. }
  107. private Integer getInteger(Map<String, Object> conf, String key, Integer defaultValue) {
  108. String[] keySplits = key.split("\\.");
  109. if(keySplits.length >= 2) {
  110. Object value = conf.get(keySplits[0]);
  111. // 内层的值如果取到 null 了,说明配置中没有那么深的层次
  112. if(value == null) {
  113. return defaultValue;
  114. }
  115. if(!(value instanceof Map)) {
  116. return defaultValue;
  117. }
  118. String[] ckeys = new String[keySplits.length -1];
  119. System.arraycopy(keySplits, 1, ckeys, 0, ckeys.length);
  120. return getInteger((Map)value, StringUtils.join(ckeys, "."), defaultValue);
  121. }
  122. Object value = conf.get(key);
  123. if(value == null) {
  124. return defaultValue;
  125. }
  126. return value instanceof Number ? ((Number) value).intValue() : Integer.parseInt((String) value);
  127. }
  128. /**
  129. * 从 conf 中获取 字符串值
  130. * @param conf
  131. * @param key
  132. * @return
  133. */
  134. private String getString(Map<String, Object> conf, String key) {
  135. return getString(conf, key, null);
  136. }
  137. private String getString(Map<String, Object> conf, String key, String defaultValue) {
  138. String[] keySplits = key.split("\\.");
  139. if(keySplits.length >= 2) {
  140. Object value = conf.get(keySplits[0]);
  141. // 内层的值如果取到 null 了,说明配置中没有那么深的层次
  142. if(value == null) {
  143. return defaultValue;
  144. }
  145. if(!(value instanceof Map)) {
  146. return defaultValue;
  147. }
  148. String[] ckeys = new String[keySplits.length -1];
  149. System.arraycopy(keySplits, 1, ckeys, 0, ckeys.length);
  150. return getString((Map)value, StringUtils.join(ckeys, "."), defaultValue);
  151. }
  152. Object value = conf.get(key);
  153. if(value == null) {
  154. return defaultValue;
  155. }
  156. return value instanceof String ? (String) value : String.valueOf(value);
  157. }
  158. /**
  159. * 获取 数组 配置
  160. * @param conf
  161. * @param key
  162. * @param <T>
  163. * @return
  164. */
  165. private <T> Collection<T> getArray(Map<String, Object> conf, String key) {
  166. return getArray(conf, key, null);
  167. }
  168. private <T> Collection<T> getArray(Map<String, Object> conf, String key, Collection<T> defaultValue) {
  169. String[] keySplits = key.split("\\.");
  170. if(keySplits.length >= 2) {
  171. Object value = conf.get(keySplits[0]);
  172. // 内层的值如果取到 null 了,说明配置中没有那么深的层次
  173. if(value == null) {
  174. return defaultValue;
  175. }
  176. if(!(value instanceof Map)) {
  177. return defaultValue;
  178. }
  179. String[] ckeys = new String[keySplits.length -1];
  180. System.arraycopy(keySplits, 1, ckeys, 0, ckeys.length);
  181. return getArray((Map)value, StringUtils.join(ckeys, "."), defaultValue);
  182. }
  183. Object value = conf.get(key);
  184. if(value == null) {
  185. return defaultValue;
  186. }
  187. if(value instanceof Collection) {
  188. return (Collection) value;
  189. } else if(value instanceof Array) {
  190. ArrayList objects = Lists.newArrayList();
  191. CollectionUtils.addAll(objects, (Object[])value);
  192. return (Collection) objects;
  193. }
  194. return (Collection) Lists.newArrayList(value);
  195. }
  196. /**
  197. * 支持 JSON 和 Yml 两种配置
  198. * @param configFilePath
  199. * @return
  200. * @throws IOException
  201. */
  202. public Map<String, Object> parseConfig(String configFilePath) throws IOException {
  203. String configContent = Files.asCharSource(new File(configFilePath), Charsets.UTF_8).read();
  204. if(configFilePath.endsWith(".json")) {
  205. JSONObject json = JSON.parseObject(configContent);
  206. return json;
  207. } else if(configFilePath.endsWith(".yml") || configFilePath.endsWith(".yaml")) {
  208. Yaml yaml = new Yaml();
  209. Object ret = yaml.load(configContent);
  210. return (Map)ret;
  211. }
  212. throw new IllegalStateException("unknown type config file: " + configFilePath);
  213. }
  214. }