PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/c4/src/org/snova/c4/client/config/C4ClientConfiguration.java

http://snova.googlecode.com/
Java | 584 lines | 492 code | 80 blank | 12 comment | 35 complexity | f0e2595b945c0b6d9cfdd70db578ec9a MD5 | raw file
Possible License(s): GPL-3.0
  1. /**
  2. * This file is part of the hyk-proxy project.
  3. * Copyright (c) 2010 Yin QiWen <yinqiwen@gmail.com>
  4. *
  5. * Description: Config.java
  6. *
  7. * @author yinqiwen [ 2010-5-14 | 08:49:33 PM]
  8. *
  9. */
  10. package org.snova.c4.client.config;
  11. import java.io.File;
  12. import java.io.FileInputStream;
  13. import java.io.FileOutputStream;
  14. import java.io.UnsupportedEncodingException;
  15. import java.net.URL;
  16. import java.net.URLDecoder;
  17. import java.util.LinkedList;
  18. import java.util.List;
  19. import java.util.Properties;
  20. import org.arch.config.IniProperties;
  21. import org.arch.event.misc.CompressorType;
  22. import org.arch.event.misc.EncryptType;
  23. import org.arch.util.StringHelper;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import org.snova.c4.common.C4PluginVersion;
  27. import org.snova.framework.config.DesktopFrameworkConfiguration;
  28. import org.snova.framework.config.ReloadableConfiguration;
  29. import org.snova.framework.config.ReloadableConfigurationMonitor;
  30. import org.snova.framework.util.proxy.ProxyInfo;
  31. /**
  32. *
  33. */
  34. public class C4ClientConfiguration implements ReloadableConfiguration
  35. {
  36. protected static Logger logger = LoggerFactory
  37. .getLogger(C4ClientConfiguration.class);
  38. private static C4ClientConfiguration instance = new C4ClientConfiguration();
  39. private static File getConfigFile()
  40. {
  41. URL url = C4ClientConfiguration.class.getResource("/"
  42. + "c4-client.conf");
  43. String conf;
  44. try
  45. {
  46. conf = URLDecoder.decode(url.getFile(), "UTF-8");
  47. }
  48. catch (UnsupportedEncodingException e)
  49. {
  50. return null;
  51. }
  52. return new File(conf);
  53. }
  54. private C4ClientConfiguration()
  55. {
  56. loadConfig();
  57. ReloadableConfigurationMonitor.getInstance().registerConfigFile(this);
  58. }
  59. private static final String C4_TAG = "C4";
  60. private static final String WORKER_NODE_NAME = "WorkerNode";
  61. private static final String CLIENT_TAG = "Client";
  62. private static final String RSERVER_TAG = "RServer";
  63. private static final String CONNECTION_MODE_NAME = "ConnectionMode";
  64. private static final String SESSION_IDLE_TIMEOUT_NAME = "SessionIdleTimeout";
  65. private static final String SIMPLE_URL_ENABLE_NAME = "SimpleURLEnable";
  66. private static final String COMPRESSOR_NAME = "Compressor";
  67. private static final String ENCRYPTER_NAME = "Encrypter";
  68. private static final String HEARTBEAT_PERIOD_NAME = "HeartBeatPeriod";
  69. private static final String HTTP_REQUEST_TIMEOUT_NAME = "HTTPRequestTimeout";
  70. private static final String PULL_TRANSACTION_NAME = "PullTransactionTime";
  71. private static final String CLIENT_PULL_ENABLE_NAME = "ClientPullEnable";
  72. private static final String SERVER_PULL_ENABLE_NAME = "ServerPullEnable";
  73. private static final String USER_AGENT_NAME = "UserAgent";
  74. private static final String DUAL_CONN_ENABLE_NAME = "DualConnectionEnable";
  75. private static final String MIN_WRITE_PERIOD = "MinWritePeriod";
  76. private static final String CONN_POOL_SIZE_NAME = "ConnectionPoolSize";
  77. private static final String RSERVER_PORT_NAME = "Port";
  78. private static final String RSERVER_EXTERNAL_IP_NAME = "ExternalIP";
  79. private static final String APPID_BINDING_TAG = "DomainBinding";
  80. private void loadConfig()
  81. {
  82. try
  83. {
  84. FileInputStream fis = new FileInputStream(getConfigFile());
  85. IniProperties props = new IniProperties();
  86. props.load(fis);
  87. Properties ps = props.getProperties(C4_TAG);
  88. if (null != ps)
  89. {
  90. for (Object key : ps.keySet())
  91. {
  92. String k = ((String) key).trim();
  93. if (k.startsWith(WORKER_NODE_NAME))
  94. {
  95. C4ServerAuth auth = new C4ServerAuth();
  96. if (!auth.parse(ps.getProperty(k)))
  97. {
  98. throw new Exception("Failed to parse line:" + k
  99. + "=" + ps.getProperty(k));
  100. }
  101. if (!auth.domain.isEmpty())
  102. {
  103. serverAuths.add(auth);
  104. }
  105. }
  106. }
  107. }
  108. connectionMode = ConnectionMode.valueOf(props.getProperty(
  109. CLIENT_TAG, CONNECTION_MODE_NAME, "HTTP"));
  110. sessionTimeout = props.getIntProperty(CLIENT_TAG,
  111. SESSION_IDLE_TIMEOUT_NAME, sessionTimeout);
  112. simpleURLEnable = props.getBoolProperty(CLIENT_TAG,
  113. SIMPLE_URL_ENABLE_NAME, false);
  114. compressor = CompressorType.valueOf(props.getProperty(CLIENT_TAG,
  115. COMPRESSOR_NAME, "Snappy").toUpperCase());
  116. encrypter = EncryptType.valueOf(props.getProperty(CLIENT_TAG,
  117. ENCRYPTER_NAME, "SE1"));
  118. sessionTimeout = props.getIntProperty(CLIENT_TAG,
  119. SESSION_IDLE_TIMEOUT_NAME, sessionTimeout);
  120. pullTransactionTime = props.getIntProperty(CLIENT_TAG,
  121. PULL_TRANSACTION_NAME, pullTransactionTime);
  122. heartBeatPeriod = props.getIntProperty(CLIENT_TAG,
  123. HEARTBEAT_PERIOD_NAME, 2000);
  124. httpRequestTimeout = props.getIntProperty(CLIENT_TAG,
  125. HTTP_REQUEST_TIMEOUT_NAME, 30000);
  126. clientPullEnable = props.getBoolProperty(CLIENT_TAG,
  127. CLIENT_PULL_ENABLE_NAME, true);
  128. serverPullEnable = props.getBoolProperty(CLIENT_TAG,
  129. SERVER_PULL_ENABLE_NAME, true);
  130. dualConnectionEnable = props.getBoolProperty(CLIENT_TAG,
  131. DUAL_CONN_ENABLE_NAME, true);
  132. minWritePeriod = props.getIntProperty(CLIENT_TAG, MIN_WRITE_PERIOD,
  133. 500);
  134. connectionPoolSize = props.getIntProperty(CLIENT_TAG,
  135. CONN_POOL_SIZE_NAME, 2);
  136. rServerPort = props.getIntProperty(RSERVER_TAG, RSERVER_PORT_NAME, rServerPort);
  137. httpProxyUserAgent = props.getProperty(CLIENT_TAG, USER_AGENT_NAME,
  138. "Snova-C4 V" + C4PluginVersion.value);
  139. externalIP = props.getProperty(RSERVER_TAG, RSERVER_EXTERNAL_IP_NAME);
  140. ps = props.getProperties(APPID_BINDING_TAG);
  141. if (null != ps)
  142. {
  143. appIdBindings.clear();
  144. for (Object key : ps.keySet())
  145. {
  146. String k = ((String) key).trim();
  147. String v = ps.getProperty(k);
  148. AppIdBinding binding = new AppIdBinding();
  149. binding.parse(k, v);
  150. appIdBindings.add(binding);
  151. }
  152. }
  153. }
  154. catch (Exception e)
  155. {
  156. logger.error("Failed to load gae-client config file!", e);
  157. }
  158. }
  159. public static enum ConnectionMode
  160. {
  161. HTTP, RSOCKET;
  162. }
  163. public static class C4ServerAuth
  164. {
  165. public String domain;
  166. public int port = 80;
  167. public void init()
  168. {
  169. }
  170. @Override
  171. public int hashCode()
  172. {
  173. return domain.hashCode() + port;
  174. }
  175. @Override
  176. public boolean equals(Object anObject)
  177. {
  178. if (this == anObject)
  179. {
  180. return true;
  181. }
  182. if (anObject instanceof C4ServerAuth)
  183. {
  184. C4ServerAuth anotherString = (C4ServerAuth) anObject;
  185. if (anotherString.domain.equals(domain)
  186. && anotherString.port == port)
  187. {
  188. return true;
  189. }
  190. }
  191. return false;
  192. }
  193. public boolean parse(String line)
  194. {
  195. if (null == line || line.trim().isEmpty())
  196. {
  197. return false;
  198. }
  199. line = line.trim();
  200. String[] ss = StringHelper.split(line, ':');
  201. if (ss.length == 1)
  202. {
  203. domain = line;
  204. port = 80;
  205. }
  206. else
  207. {
  208. domain = ss[0];
  209. String portstr = ss[1];
  210. port = Integer.parseInt(portstr);
  211. }
  212. init();
  213. return true;
  214. }
  215. public String toString()
  216. {
  217. return domain + (port == 80 ? "" : ":" + port);
  218. }
  219. }
  220. private List<C4ServerAuth> serverAuths = new LinkedList<C4ServerAuth>();
  221. public void setC4ServerAuths(List<C4ServerAuth> serverAuths)
  222. {
  223. this.serverAuths = serverAuths;
  224. }
  225. public List<C4ServerAuth> getC4ServerAuths()
  226. {
  227. return serverAuths;
  228. }
  229. private ConnectionMode connectionMode = ConnectionMode.HTTP;
  230. public ConnectionMode getConnectionMode()
  231. {
  232. return connectionMode;
  233. }
  234. public void setConnectionMode(ConnectionMode mode)
  235. {
  236. connectionMode = mode;
  237. }
  238. private int connectionPoolSize = 1;
  239. public int getConnectionPoolSize()
  240. {
  241. return connectionPoolSize;
  242. }
  243. public void setConnectionPoolSize(int size)
  244. {
  245. connectionPoolSize = size;
  246. }
  247. private int sessionTimeout = 50000;
  248. public void setSessionIdleTimeout(int sessionTimeout)
  249. {
  250. this.sessionTimeout = sessionTimeout;
  251. }
  252. public int getSessionIdleTimeout()
  253. {
  254. return sessionTimeout;
  255. }
  256. private int minWritePeriod = 500;
  257. public int getMinWritePeriod()
  258. {
  259. return minWritePeriod;
  260. }
  261. public void setMinWritePeriod(int v)
  262. {
  263. minWritePeriod = v;
  264. }
  265. private boolean clientPullEnable = true;
  266. public void setClientPullEnable(boolean v)
  267. {
  268. this.clientPullEnable = v;
  269. }
  270. public boolean isClientPullEnable()
  271. {
  272. return clientPullEnable;
  273. }
  274. private boolean serverPullEnable = true;
  275. public void setServerPullEnable(boolean v)
  276. {
  277. this.serverPullEnable = v;
  278. }
  279. public boolean isServerPullEnable()
  280. {
  281. return serverPullEnable;
  282. }
  283. private boolean dualConnectionEnable = true;
  284. public void setDualConnectionEnable(boolean v)
  285. {
  286. this.dualConnectionEnable = v;
  287. }
  288. public boolean isDualConnectionEnable()
  289. {
  290. return dualConnectionEnable;
  291. }
  292. private int pullTransactionTime = 25000;
  293. public void setPullTransactionTime(int v)
  294. {
  295. this.pullTransactionTime = v;
  296. }
  297. public int getPullTransactionTime()
  298. {
  299. return pullTransactionTime;
  300. }
  301. private CompressorType compressor;
  302. public CompressorType getCompressor()
  303. {
  304. return compressor;
  305. }
  306. public void setCompressor(CompressorType type)
  307. {
  308. compressor = type;
  309. }
  310. private EncryptType encrypter;
  311. public EncryptType getEncrypter()
  312. {
  313. return encrypter;
  314. }
  315. public void setEncrypter(EncryptType type)
  316. {
  317. this.encrypter = type;
  318. }
  319. private boolean simpleURLEnable;
  320. public void setSimpleURLEnable(boolean simpleURLEnable)
  321. {
  322. this.simpleURLEnable = simpleURLEnable;
  323. }
  324. public boolean isSimpleURLEnable()
  325. {
  326. return simpleURLEnable;
  327. }
  328. private int heartBeatPeriod = 2000;
  329. public void setheartBeatPeriod(int heartBeatPeriod)
  330. {
  331. this.heartBeatPeriod = heartBeatPeriod;
  332. }
  333. public int getHeartBeatPeriod()
  334. {
  335. return heartBeatPeriod;
  336. }
  337. private int httpRequestTimeout = 30000;
  338. public void setHTTPRequestTimeout(int timeout)
  339. {
  340. this.httpRequestTimeout = timeout;
  341. }
  342. public int getHTTPRequestTimeout()
  343. {
  344. return httpRequestTimeout;
  345. }
  346. public void init() throws Exception
  347. {
  348. }
  349. private List<AppIdBinding> appIdBindings = new LinkedList<C4ClientConfiguration.AppIdBinding>();
  350. static class AppIdBinding
  351. {
  352. String appid;
  353. List<String> sites = new LinkedList<String>();
  354. void parse(String appid, String line)
  355. {
  356. this.appid = appid;
  357. String[] ss = line.split("[,|;|\\|]");
  358. for (String k : ss)
  359. {
  360. if (!k.trim().isEmpty())
  361. {
  362. sites.add(k);
  363. }
  364. }
  365. }
  366. void putToIniProperties(IniProperties props)
  367. {
  368. StringBuilder buffer = new StringBuilder();
  369. for (String s : sites)
  370. {
  371. buffer.append(s).append("|");
  372. }
  373. props.setProperty(APPID_BINDING_TAG, appid, buffer.toString());
  374. }
  375. }
  376. public C4ServerAuth getC4ServerAuth(String domain)
  377. {
  378. for (C4ServerAuth auth : serverAuths)
  379. {
  380. if (auth.domain.equals(domain))
  381. {
  382. return auth;
  383. }
  384. }
  385. return null;
  386. }
  387. public String getBindingDomain(String host)
  388. {
  389. if (null != appIdBindings)
  390. {
  391. for (AppIdBinding binding : appIdBindings)
  392. {
  393. for (String site : binding.sites)
  394. {
  395. if (host.contains(site))
  396. {
  397. return binding.appid.trim();
  398. }
  399. }
  400. }
  401. }
  402. return null;
  403. }
  404. private String httpProxyUserAgent;
  405. public String getUserAgent()
  406. {
  407. return httpProxyUserAgent;
  408. }
  409. public void setUserAgent(String v)
  410. {
  411. httpProxyUserAgent = v;
  412. }
  413. private String externalIP;
  414. public String getExternalIP()
  415. {
  416. return externalIP;
  417. }
  418. public void setExternalIP(String ip)
  419. {
  420. externalIP = ip;
  421. }
  422. private int rServerPort = 48101;
  423. public int getRServerPort()
  424. {
  425. return rServerPort;
  426. }
  427. public void setRServerPort(int port)
  428. {
  429. rServerPort = port;
  430. }
  431. public static C4ClientConfiguration getInstance()
  432. {
  433. return instance;
  434. }
  435. public void save() throws Exception
  436. {
  437. try
  438. {
  439. init();
  440. FileOutputStream fos = new FileOutputStream(getConfigFile());
  441. IniProperties props = new IniProperties();
  442. int i = 0;
  443. for (C4ServerAuth auth : serverAuths)
  444. {
  445. props.setProperty(C4_TAG, WORKER_NODE_NAME + "[" + i + "]",
  446. auth.toString());
  447. i++;
  448. }
  449. props.setProperty(CLIENT_TAG, CONNECTION_MODE_NAME,
  450. connectionMode.toString());
  451. props.setIntProperty(CLIENT_TAG, SESSION_IDLE_TIMEOUT_NAME,
  452. sessionTimeout);
  453. props.setIntProperty(CLIENT_TAG, HTTP_REQUEST_TIMEOUT_NAME,
  454. httpRequestTimeout);
  455. props.setIntProperty(CLIENT_TAG, PULL_TRANSACTION_NAME,
  456. pullTransactionTime);
  457. props.setIntProperty(CLIENT_TAG, HEARTBEAT_PERIOD_NAME,
  458. heartBeatPeriod);
  459. props.setIntProperty(CLIENT_TAG, CONN_POOL_SIZE_NAME,
  460. connectionPoolSize);
  461. props.setBoolProperty(CLIENT_TAG, CLIENT_PULL_ENABLE_NAME,
  462. clientPullEnable);
  463. props.setBoolProperty(CLIENT_TAG, SERVER_PULL_ENABLE_NAME,
  464. serverPullEnable);
  465. props.setBoolProperty(CLIENT_TAG, DUAL_CONN_ENABLE_NAME,
  466. dualConnectionEnable);
  467. props.setBoolProperty(CLIENT_TAG, SIMPLE_URL_ENABLE_NAME,
  468. simpleURLEnable);
  469. props.setProperty(CLIENT_TAG, COMPRESSOR_NAME,
  470. compressor.toString());
  471. props.setProperty(CLIENT_TAG, ENCRYPTER_NAME, encrypter.toString());
  472. props.setProperty(CLIENT_TAG, USER_AGENT_NAME, httpProxyUserAgent);
  473. props.setIntProperty(CLIENT_TAG, MIN_WRITE_PERIOD, minWritePeriod);
  474. props.setIntProperty(RSERVER_TAG, RSERVER_PORT_NAME, rServerPort);
  475. if(null != externalIP)
  476. {
  477. props.setProperty(RSERVER_TAG, RSERVER_EXTERNAL_IP_NAME, externalIP);
  478. }
  479. for (AppIdBinding binding : appIdBindings)
  480. {
  481. binding.putToIniProperties(props);
  482. }
  483. props.store(fos);
  484. }
  485. catch (Exception e)
  486. {
  487. throw e;
  488. }
  489. }
  490. public ProxyInfo getLocalProxy()
  491. {
  492. return DesktopFrameworkConfiguration.getInstance().getLocalProxy();
  493. }
  494. @Override
  495. public void reload()
  496. {
  497. loadConfig();
  498. }
  499. @Override
  500. public File getConfigurationFile()
  501. {
  502. return getConfigFile();
  503. }
  504. }