ClientGlobal.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * Copyright (C) 2008 Happy Fish / YuQing
  3. * <p>
  4. * FastDFS Java Client may be copied only under the terms of the GNU Lesser
  5. * General Public License (LGPL).
  6. * Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
  7. **/
  8. package com.kmall.common.fileserver.fastdfs;
  9. import com.kmall.common.fileserver.common.IniFileReader;
  10. import com.kmall.common.fileserver.common.MyException;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.net.InetSocketAddress;
  14. import java.net.Socket;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.Properties;
  18. /**
  19. * Global variables
  20. *
  21. * @author Happy Fish / YuQing
  22. * @version Version 1.11
  23. */
  24. public class ClientGlobal {
  25. public static final String CONF_KEY_CONNECT_TIMEOUT = "connect_timeout";
  26. public static final String CONF_KEY_NETWORK_TIMEOUT = "network_timeout";
  27. public static final String CONF_KEY_CHARSET = "charset";
  28. public static final String CONF_KEY_HTTP_ANTI_STEAL_TOKEN = "http.anti_steal_token";
  29. public static final String CONF_KEY_HTTP_SECRET_KEY = "http.secret_key";
  30. public static final String CONF_KEY_HTTP_TRACKER_HTTP_PORT = "http.tracker_http_port";
  31. public static final String CONF_KEY_TRACKER_SERVER = "tracker_server";
  32. public static final String PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS = "fastdfs.connect_timeout_in_seconds";
  33. public static final String PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS = "fastdfs.network_timeout_in_seconds";
  34. public static final String PROP_KEY_CHARSET = "fastdfs.charset";
  35. public static final String PROP_KEY_HTTP_ANTI_STEAL_TOKEN = "fastdfs.http_anti_steal_token";
  36. public static final String PROP_KEY_HTTP_SECRET_KEY = "fastdfs.http_secret_key";
  37. public static final String PROP_KEY_HTTP_TRACKER_HTTP_PORT = "fastdfs.http_tracker_http_port";
  38. public static final String PROP_KEY_TRACKER_SERVERS = "fastdfs.tracker_servers";
  39. public static final int DEFAULT_CONNECT_TIMEOUT = 5; // second
  40. public static final int DEFAULT_NETWORK_TIMEOUT = 30; // second
  41. public static final String DEFAULT_CHARSET = "UTF-8";
  42. public static final boolean DEFAULT_HTTP_ANTI_STEAL_TOKEN = false;
  43. public static final String DEFAULT_HTTP_SECRET_KEY = "FastDFS1234567890";
  44. public static final int DEFAULT_HTTP_TRACKER_HTTP_PORT = 80;
  45. public static int g_connect_timeout = DEFAULT_CONNECT_TIMEOUT * 1000; // millisecond
  46. public static int g_network_timeout = DEFAULT_NETWORK_TIMEOUT * 1000; // millisecond
  47. public static String g_charset = DEFAULT_CHARSET;
  48. public static boolean g_anti_steal_token = DEFAULT_HTTP_ANTI_STEAL_TOKEN; // if anti-steal token
  49. public static String g_secret_key = DEFAULT_HTTP_SECRET_KEY; // generage token secret key
  50. public static int g_tracker_http_port = DEFAULT_HTTP_TRACKER_HTTP_PORT;
  51. public static String http_tracket_nginx_addr = null;
  52. public static String http_tracket_server_port = null;
  53. public static String file_author = null;
  54. public static TrackerGroup g_tracker_group;
  55. private ClientGlobal() {
  56. }
  57. /**
  58. * load global variables
  59. *
  60. * @param conf_filename
  61. * config filename
  62. */
  63. public static void init(String conf_filename) throws IOException, MyException {
  64. IniFileReader iniReader;
  65. String[] szTrackerServers;
  66. String[] parts;
  67. iniReader = new IniFileReader(conf_filename);
  68. g_connect_timeout = iniReader.getIntValue("connect_timeout", DEFAULT_CONNECT_TIMEOUT);
  69. if (g_connect_timeout < 0) {
  70. g_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
  71. }
  72. g_connect_timeout *= 1000; // millisecond
  73. g_network_timeout = iniReader.getIntValue("network_timeout", DEFAULT_NETWORK_TIMEOUT);
  74. if (g_network_timeout < 0) {
  75. g_network_timeout = DEFAULT_NETWORK_TIMEOUT;
  76. }
  77. g_network_timeout *= 1000; // millisecond
  78. g_charset = iniReader.getStrValue("charset");
  79. if (g_charset == null || g_charset.length() == 0) {
  80. g_charset = "ISO8859-1";
  81. }
  82. szTrackerServers = iniReader.getValues("tracker_server");
  83. if (szTrackerServers == null) {
  84. throw new MyException("item \"tracker_server\" in " + conf_filename + " not found");
  85. }
  86. InetSocketAddress[] tracker_servers = new InetSocketAddress[szTrackerServers.length];
  87. for (int i = 0; i < szTrackerServers.length; i++) {
  88. parts = szTrackerServers[i].split("\\:", 2);
  89. if (parts.length != 2) {
  90. throw new MyException("the value of item \"tracker_server\" is invalid, the correct format is host:port");
  91. }
  92. tracker_servers[i] = new InetSocketAddress(parts[0].trim(), Integer.parseInt(parts[1].trim()));
  93. }
  94. g_tracker_group = new TrackerGroup(tracker_servers);
  95. g_tracker_http_port = iniReader.getIntValue("http.tracker_http_port", 80);
  96. g_anti_steal_token = iniReader.getBoolValue("http.anti_steal_token", false);
  97. if (g_anti_steal_token) {
  98. g_secret_key = iniReader.getStrValue("http.secret_key");
  99. }
  100. http_tracket_nginx_addr = iniReader.getStrValue("http.tracket_nginx_addr");
  101. http_tracket_server_port = iniReader.getStrValue("http.tracker_server_port");
  102. file_author = iniReader.getStrValue("file.author");
  103. }
  104. /**
  105. * load from properties file
  106. *
  107. * @param propsFilePath
  108. */
  109. public static void initByProperties(String propsFilePath) throws IOException, MyException {
  110. Properties props = new Properties();
  111. InputStream in = IniFileReader.loadFromOsFileSystemOrClasspathAsStream(propsFilePath);
  112. if (in != null) {
  113. props.load(in);
  114. }
  115. initByProperties(props);
  116. }
  117. public static void initByProperties(Properties props) throws IOException, MyException {
  118. String trackerServersConf = props.getProperty(PROP_KEY_TRACKER_SERVERS);
  119. if (trackerServersConf == null || trackerServersConf.trim().length() == 0) {
  120. throw new MyException(String.format("configure item %s is required", PROP_KEY_TRACKER_SERVERS));
  121. }
  122. initByTrackers(trackerServersConf.trim());
  123. String connectTimeoutInSecondsConf = props.getProperty(PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS);
  124. String networkTimeoutInSecondsConf = props.getProperty(PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS);
  125. String charsetConf = props.getProperty(PROP_KEY_CHARSET);
  126. String httpAntiStealTokenConf = props.getProperty(PROP_KEY_HTTP_ANTI_STEAL_TOKEN);
  127. String httpSecretKeyConf = props.getProperty(PROP_KEY_HTTP_SECRET_KEY);
  128. String httpTrackerHttpPortConf = props.getProperty(PROP_KEY_HTTP_TRACKER_HTTP_PORT);
  129. if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) {
  130. g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000;
  131. }
  132. if (networkTimeoutInSecondsConf != null && networkTimeoutInSecondsConf.trim().length() != 0) {
  133. g_network_timeout = Integer.parseInt(networkTimeoutInSecondsConf.trim()) * 1000;
  134. }
  135. if (charsetConf != null && charsetConf.trim().length() != 0) {
  136. g_charset = charsetConf.trim();
  137. }
  138. if (httpAntiStealTokenConf != null && httpAntiStealTokenConf.trim().length() != 0) {
  139. g_anti_steal_token = Boolean.parseBoolean(httpAntiStealTokenConf);
  140. }
  141. if (httpSecretKeyConf != null && httpSecretKeyConf.trim().length() != 0) {
  142. g_secret_key = httpSecretKeyConf.trim();
  143. }
  144. if (httpTrackerHttpPortConf != null && httpTrackerHttpPortConf.trim().length() != 0) {
  145. g_tracker_http_port = Integer.parseInt(httpTrackerHttpPortConf);
  146. }
  147. }
  148. /**
  149. * load from properties file
  150. *
  151. * @param trackerServers
  152. * 例如:"10.0.11.245:22122,10.0.11.246:22122" server的IP和端口用冒号':'分隔 server之间用逗号','分隔
  153. */
  154. public static void initByTrackers(String trackerServers) throws IOException, MyException {
  155. List<InetSocketAddress> list = new ArrayList();
  156. String spr1 = ",";
  157. String spr2 = ":";
  158. String[] arr1 = trackerServers.trim().split(spr1);
  159. for (String addrStr : arr1) {
  160. String[] arr2 = addrStr.trim().split(spr2);
  161. String host = arr2[0].trim();
  162. int port = Integer.parseInt(arr2[1].trim());
  163. list.add(new InetSocketAddress(host, port));
  164. }
  165. InetSocketAddress[] trackerAddresses = list.toArray(new InetSocketAddress[list.size()]);
  166. initByTrackers(trackerAddresses);
  167. }
  168. public static void initByTrackers(InetSocketAddress[] trackerAddresses) throws IOException, MyException {
  169. g_tracker_group = new TrackerGroup(trackerAddresses);
  170. }
  171. /**
  172. * construct Socket object
  173. *
  174. * @param ip_addr
  175. * ip address or hostname
  176. * @param port
  177. * port number
  178. * @return connected Socket object
  179. */
  180. public static Socket getSocket(String ip_addr, int port) throws IOException {
  181. Socket sock = new Socket();
  182. sock.setSoTimeout(ClientGlobal.g_network_timeout);
  183. sock.connect(new InetSocketAddress(ip_addr, port), ClientGlobal.g_connect_timeout);
  184. return sock;
  185. }
  186. /**
  187. * construct Socket object
  188. *
  189. * @param addr
  190. * InetSocketAddress object, including ip address and port
  191. * @return connected Socket object
  192. */
  193. public static Socket getSocket(InetSocketAddress addr) throws IOException {
  194. Socket sock = new Socket();
  195. sock.setSoTimeout(ClientGlobal.g_network_timeout);
  196. sock.connect(addr, ClientGlobal.g_connect_timeout);
  197. return sock;
  198. }
  199. public static int getG_connect_timeout() {
  200. return g_connect_timeout;
  201. }
  202. public static void setG_connect_timeout(int connect_timeout) {
  203. ClientGlobal.g_connect_timeout = connect_timeout;
  204. }
  205. public static int getG_network_timeout() {
  206. return g_network_timeout;
  207. }
  208. public static void setG_network_timeout(int network_timeout) {
  209. ClientGlobal.g_network_timeout = network_timeout;
  210. }
  211. public static String getG_charset() {
  212. return g_charset;
  213. }
  214. public static void setG_charset(String charset) {
  215. ClientGlobal.g_charset = charset;
  216. }
  217. public static int getG_tracker_http_port() {
  218. return g_tracker_http_port;
  219. }
  220. public static void setG_tracker_http_port(int tracker_http_port) {
  221. ClientGlobal.g_tracker_http_port = tracker_http_port;
  222. }
  223. public static boolean getG_anti_steal_token() {
  224. return g_anti_steal_token;
  225. }
  226. public static boolean isG_anti_steal_token() {
  227. return g_anti_steal_token;
  228. }
  229. public static void setG_anti_steal_token(boolean anti_steal_token) {
  230. ClientGlobal.g_anti_steal_token = anti_steal_token;
  231. }
  232. public static String getG_secret_key() {
  233. return g_secret_key;
  234. }
  235. public static void setG_secret_key(String secret_key) {
  236. ClientGlobal.g_secret_key = secret_key;
  237. }
  238. public static TrackerGroup getG_tracker_group() {
  239. return g_tracker_group;
  240. }
  241. public static void setG_tracker_group(TrackerGroup tracker_group) {
  242. ClientGlobal.g_tracker_group = tracker_group;
  243. }
  244. public static String configInfo() {
  245. String trackerServers = "";
  246. if (g_tracker_group != null) {
  247. InetSocketAddress[] trackerAddresses = g_tracker_group.tracker_servers;
  248. for (InetSocketAddress inetSocketAddress : trackerAddresses) {
  249. if (trackerServers.length() > 0)
  250. trackerServers += ",";
  251. trackerServers += inetSocketAddress.toString().substring(1);
  252. }
  253. }
  254. return "{" + "\n g_connect_timeout(ms) = " + g_connect_timeout + "\n g_network_timeout(ms) = " + g_network_timeout + "\n g_charset = "
  255. + g_charset + "\n g_anti_steal_token = " + g_anti_steal_token + "\n g_secret_key = " + g_secret_key + "\n g_tracker_http_port = "
  256. + g_tracker_http_port + "\n trackerServers = " + trackerServers + "\n}";
  257. }
  258. }