|
12 | 12 | import org.csource.common.MyException;
|
13 | 13 |
|
14 | 14 | import java.io.IOException;
|
| 15 | +import java.io.InputStream; |
15 | 16 | import java.net.InetSocketAddress;
|
16 | 17 | import java.net.Socket;
|
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Properties; |
17 | 21 |
|
18 | 22 | /**
|
19 | 23 | * Global variables
|
|
22 | 26 | * @version Version 1.11
|
23 | 27 | */
|
24 | 28 | public class ClientGlobal {
|
25 |
| - public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second |
| 29 | + |
| 30 | + public static final String CONF_KEY_CONNECT_TIMEOUT = "connect_timeout"; |
| 31 | + public static final String CONF_KEY_NETWORK_TIMEOUT = "network_timeout"; |
| 32 | + public static final String CONF_KEY_CHARSET = "charset"; |
| 33 | + public static final String CONF_KEY_HTTP_ANTI_STEAL_TOKEN = "http.anti_steal_token"; |
| 34 | + public static final String CONF_KEY_HTTP_SECRET_KEY = "http.secret_key"; |
| 35 | + public static final String CONF_KEY_HTTP_TRACKER_HTTP_PORT = "http.tracker_http_port"; |
| 36 | + public static final String CONF_KEY_TRACKER_SERVER = "tracker_server"; |
| 37 | + |
| 38 | + public static final String PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS = "fastdfs.connect_timeout_in_seconds"; |
| 39 | + public static final String PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS = "fastdfs.network_timeout_in_seconds"; |
| 40 | + public static final String PROP_KEY_CHARSET = "fastdfs.charset"; |
| 41 | + public static final String PROP_KEY_HTTP_ANTI_STEAL_TOKEN = "fastdfs.http_anti_steal_token"; |
| 42 | + public static final String PROP_KEY_HTTP_SECRET_KEY = "fastdfs.http_secret_key"; |
| 43 | + public static final String PROP_KEY_HTTP_TRACKER_HTTP_PORT = "fastdfs.http_tracker_http_port"; |
| 44 | + public static final String PROP_KEY_TRACKER_SERVERS = "fastdfs.tracker_servers"; |
| 45 | + |
| 46 | + public static final int DEFAULT_CONNECT_TIMEOUT = 5; //second |
26 | 47 | public static final int DEFAULT_NETWORK_TIMEOUT = 30; //second
|
27 |
| - public static int g_connect_timeout; //millisecond |
28 |
| - public static int g_network_timeout; //millisecond |
29 |
| - public static String g_charset; |
30 |
| - public static int g_tracker_http_port; |
31 |
| - public static boolean g_anti_steal_token; //if anti-steal token |
32 |
| - public static String g_secret_key; //generage token secret key |
| 48 | + public static final String DEFAULT_CHARSET = "UTF-8"; |
| 49 | + public static final boolean DEFAULT_HTTP_ANTI_STEAL_TOKEN = false; |
| 50 | + public static final String DEFAULT_HTTP_SECRET_KEY = "FastDFS1234567890"; |
| 51 | + public static final int DEFAULT_HTTP_TRACKER_HTTP_PORT = 80; |
| 52 | + |
| 53 | + public static int g_connect_timeout = DEFAULT_CONNECT_TIMEOUT * 1000; //millisecond |
| 54 | + public static int g_network_timeout = DEFAULT_NETWORK_TIMEOUT * 1000; //millisecond |
| 55 | + public static String g_charset = DEFAULT_CHARSET; |
| 56 | + public static boolean g_anti_steal_token = DEFAULT_HTTP_ANTI_STEAL_TOKEN; //if anti-steal token |
| 57 | + public static String g_secret_key = DEFAULT_HTTP_SECRET_KEY; //generage token secret key |
| 58 | + public static int g_tracker_http_port = DEFAULT_HTTP_TRACKER_HTTP_PORT; |
| 59 | + |
33 | 60 | public static TrackerGroup g_tracker_group;
|
34 | 61 |
|
35 | 62 | private ClientGlobal() {
|
@@ -87,6 +114,87 @@ public static void init(String conf_filename) throws IOException, MyException {
|
87 | 114 | }
|
88 | 115 | }
|
89 | 116 |
|
| 117 | + /** |
| 118 | + * load from properties file |
| 119 | + * |
| 120 | + * @param propsFilePath properties file path, eg: |
| 121 | + * "fastdfs-client.properties" |
| 122 | + * "config/fastdfs-client.properties" |
| 123 | + * "/opt/fastdfs-client.properties" |
| 124 | + * "C:\\Users\\James\\config\\fastdfs-client.properties" |
| 125 | + * properties文件至少包含一个配置项 fastdfs.tracker_servers 例如: |
| 126 | + * fastdfs.tracker_servers = 10.0.11.245:22122,10.0.11.246:22122 |
| 127 | + * server的IP和端口用冒号':'分隔 |
| 128 | + * server之间用逗号','分隔 |
| 129 | + */ |
| 130 | + public static void initByProperties(String propsFilePath) throws IOException, MyException { |
| 131 | + Properties props = new Properties(); |
| 132 | + InputStream in = IniFileReader.loadFromOsFileSystemOrClasspathAsStream(propsFilePath); |
| 133 | + if (in != null) { |
| 134 | + props.load(in); |
| 135 | + } |
| 136 | + initByProperties(props); |
| 137 | + } |
| 138 | + |
| 139 | + public static void initByProperties(Properties props) throws IOException, MyException { |
| 140 | + String trackerServersConf = props.getProperty(PROP_KEY_TRACKER_SERVERS); |
| 141 | + if (trackerServersConf == null || trackerServersConf.trim().length() == 0) { |
| 142 | + throw new MyException(String.format("configure item %s is required", PROP_KEY_TRACKER_SERVERS)); |
| 143 | + } |
| 144 | + initByTrackers(trackerServersConf.trim()); |
| 145 | + |
| 146 | + String connectTimeoutInSecondsConf = props.getProperty(PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS); |
| 147 | + String networkTimeoutInSecondsConf = props.getProperty(PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS); |
| 148 | + String charsetConf = props.getProperty(PROP_KEY_CHARSET); |
| 149 | + String httpAntiStealTokenConf = props.getProperty(PROP_KEY_HTTP_ANTI_STEAL_TOKEN); |
| 150 | + String httpSecretKeyConf = props.getProperty(PROP_KEY_HTTP_SECRET_KEY); |
| 151 | + String httpTrackerHttpPortConf = props.getProperty(PROP_KEY_HTTP_TRACKER_HTTP_PORT); |
| 152 | + if (connectTimeoutInSecondsConf != null && connectTimeoutInSecondsConf.trim().length() != 0) { |
| 153 | + g_connect_timeout = Integer.parseInt(connectTimeoutInSecondsConf.trim()) * 1000; |
| 154 | + } |
| 155 | + if (networkTimeoutInSecondsConf != null && networkTimeoutInSecondsConf.trim().length() != 0) { |
| 156 | + g_network_timeout = Integer.parseInt(networkTimeoutInSecondsConf.trim()) * 1000; |
| 157 | + } |
| 158 | + if (charsetConf != null && charsetConf.trim().length() != 0) { |
| 159 | + g_charset = charsetConf.trim(); |
| 160 | + } |
| 161 | + if (httpAntiStealTokenConf != null && httpAntiStealTokenConf.trim().length() != 0) { |
| 162 | + g_anti_steal_token = Boolean.parseBoolean(httpAntiStealTokenConf); |
| 163 | + } |
| 164 | + if (httpSecretKeyConf != null && httpSecretKeyConf.trim().length() != 0) { |
| 165 | + g_secret_key = httpSecretKeyConf.trim(); |
| 166 | + } |
| 167 | + if (httpTrackerHttpPortConf != null && httpTrackerHttpPortConf.trim().length() != 0) { |
| 168 | + g_tracker_http_port = Integer.parseInt(httpTrackerHttpPortConf); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * load from properties file |
| 174 | + * |
| 175 | + * @param trackerServers 例如:"10.0.11.245:22122,10.0.11.246:22122" |
| 176 | + * server的IP和端口用冒号':'分隔 |
| 177 | + * server之间用逗号','分隔 |
| 178 | + */ |
| 179 | + public static void initByTrackers(String trackerServers) throws IOException, MyException { |
| 180 | + List<InetSocketAddress> list = new ArrayList(); |
| 181 | + String spr1 = ","; |
| 182 | + String spr2 = ":"; |
| 183 | + String[] arr1 = trackerServers.trim().split(spr1); |
| 184 | + for (String addrStr : arr1) { |
| 185 | + String[] arr2 = addrStr.trim().split(spr2); |
| 186 | + String host = arr2[0].trim(); |
| 187 | + int port = Integer.parseInt(arr2[1].trim()); |
| 188 | + list.add(new InetSocketAddress(host, port)); |
| 189 | + } |
| 190 | + InetSocketAddress[] trackerAddresses = list.toArray(new InetSocketAddress[list.size()]); |
| 191 | + initByTrackers(trackerAddresses); |
| 192 | + } |
| 193 | + |
| 194 | + public static void initByTrackers(InetSocketAddress[] trackerAddresses) throws IOException, MyException { |
| 195 | + g_tracker_group = new TrackerGroup(trackerAddresses); |
| 196 | + } |
| 197 | + |
90 | 198 | /**
|
91 | 199 | * construct Socket object
|
92 | 200 | *
|
@@ -173,4 +281,25 @@ public static TrackerGroup getG_tracker_group() {
|
173 | 281 | public static void setG_tracker_group(TrackerGroup tracker_group) {
|
174 | 282 | ClientGlobal.g_tracker_group = tracker_group;
|
175 | 283 | }
|
| 284 | + |
| 285 | + public static String configInfo() { |
| 286 | + String trackerServers = ""; |
| 287 | + if (g_tracker_group != null) { |
| 288 | + InetSocketAddress[] trackerAddresses = g_tracker_group.tracker_servers; |
| 289 | + for (InetSocketAddress inetSocketAddress : trackerAddresses) { |
| 290 | + if(trackerServers.length() > 0) trackerServers += ","; |
| 291 | + trackerServers += inetSocketAddress.toString().substring(1); |
| 292 | + } |
| 293 | + } |
| 294 | + return "{" |
| 295 | + + "\n g_connect_timeout(ms) = " + g_connect_timeout |
| 296 | + + "\n g_network_timeout(ms) = " + g_network_timeout |
| 297 | + + "\n g_charset = " + g_charset |
| 298 | + + "\n g_anti_steal_token = " + g_anti_steal_token |
| 299 | + + "\n g_secret_key = " + g_secret_key |
| 300 | + + "\n g_tracker_http_port = " + g_tracker_http_port |
| 301 | + + "\n trackerServers = " + trackerServers |
| 302 | + + "\n}"; |
| 303 | + } |
| 304 | + |
176 | 305 | }
|
0 commit comments