2
0

Config.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System;
  18. using System.IO;
  19. using System.Net;
  20. using System.Security;
  21. using SharpCifs.Util;
  22. using SharpCifs.Util.Sharpen;
  23. namespace SharpCifs
  24. {
  25. /// <summary>
  26. /// This class uses a static
  27. /// <see cref="Properties">Sharpen.Properties</see>
  28. /// to act
  29. /// as a cental repository for all jCIFS configuration properties. It cannot be
  30. /// instantiated. Similar to <code>System</code> properties the namespace
  31. /// is global therefore property names should be unique. Before use,
  32. /// the <code>load</code> method should be called with the name of a
  33. /// <code>Properties</code> file (or <code>null</code> indicating no
  34. /// file) to initialize the <code>Config</code>. The <code>System</code>
  35. /// properties will then populate the <code>Config</code> as well potentially
  36. /// overwriting properties from the file. Thus properties provided on the
  37. /// commandline with the <code>-Dproperty.name=value</code> VM parameter
  38. /// will override properties from the configuration file.
  39. ///
  40. /// There are several ways to set jCIFS properties. See
  41. /// the <a href="../overview-summary.html#scp">overview page of the API
  42. /// documentation</a> for details.
  43. /// </summary>
  44. public class Config
  45. {
  46. /// <summary>The static <code>Properties</code>.</summary>
  47. /// <remarks>The static <code>Properties</code>.</remarks>
  48. private static Properties _prp = new Properties();
  49. private static LogStream _log;
  50. public static string DefaultOemEncoding = "UTF-8"; //"Cp850";
  51. static Config()
  52. {
  53. int level;
  54. FileInputStream fis = null;
  55. _log = LogStream.GetInstance();
  56. try
  57. {
  58. string filename = Runtime.GetProperty("jcifs.properties");
  59. if (filename != null && filename.Length > 1)
  60. {
  61. fis = new FileInputStream(filename);
  62. }
  63. Load(fis);
  64. if (fis != null)
  65. {
  66. fis.Close();
  67. }
  68. }
  69. catch (IOException ioe)
  70. {
  71. if (_log.Level > 0)
  72. {
  73. Runtime.PrintStackTrace(ioe, _log);
  74. }
  75. }
  76. if ((level = GetInt("jcifs.util.loglevel", -1)) != -1)
  77. {
  78. _log.SetLevel(level);
  79. }
  80. try
  81. {
  82. Runtime.GetBytesForString(string.Empty, DefaultOemEncoding);
  83. }
  84. catch (Exception ex)
  85. {
  86. if (_log.Level >= 2)
  87. {
  88. _log.WriteLine("WARNING: The default OEM encoding " + DefaultOemEncoding + " does not appear to be supported by this JRE. The default encoding will be US-ASCII."
  89. );
  90. }
  91. //DEFAULT_OEM_ENCODING = "US-ASCII";
  92. }
  93. if (_log.Level >= 4)
  94. {
  95. try
  96. {
  97. _prp.Store(_log);
  98. }
  99. catch (IOException)
  100. {
  101. }
  102. }
  103. }
  104. /// <summary>
  105. /// This static method registers the SMB URL protocol handler which is
  106. /// required to use SMB URLs with the <tt>java.net.URL</tt> class.
  107. /// </summary>
  108. /// <remarks>
  109. /// This static method registers the SMB URL protocol handler which is
  110. /// required to use SMB URLs with the <tt>java.net.URL</tt> class. If this
  111. /// method is not called before attempting to create an SMB URL with the
  112. /// URL class the following exception will occur:
  113. /// <blockquote><pre>
  114. /// Exception MalformedURLException: unknown protocol: smb
  115. /// at java.net.URL.<init>(URL.java:480)
  116. /// at java.net.URL.<init>(URL.java:376)
  117. /// at java.net.URL.<init>(URL.java:330)
  118. /// at jcifs.smb.SmbFile.<init>(SmbFile.java:355)
  119. /// ...
  120. /// </pre><blockquote>
  121. /// </remarks>
  122. public static void RegisterSmbURLHandler()
  123. {
  124. throw new NotImplementedException();
  125. }
  126. // supress javadoc constructor summary by removing 'protected'
  127. /// <summary>Set the default properties of the static Properties used by <tt>Config</tt>.
  128. /// </summary>
  129. /// <remarks>
  130. /// Set the default properties of the static Properties used by <tt>Config</tt>. This permits
  131. /// a different Properties object/file to be used as the source of properties for
  132. /// use by the jCIFS library. The Properties must be set <i>before jCIFS
  133. /// classes are accessed</i> as most jCIFS classes load properties statically once.
  134. /// Using this method will also override properties loaded
  135. /// using the <tt>-Djcifs.properties=</tt> commandline parameter.
  136. /// </remarks>
  137. public static void SetProperties(Properties prp)
  138. {
  139. Config._prp = new Properties(prp);
  140. try
  141. {
  142. Config._prp.PutAll(Runtime.GetProperties());
  143. }
  144. catch (SecurityException)
  145. {
  146. if (_log.Level > 1)
  147. {
  148. _log.WriteLine("SecurityException: jcifs will ignore System properties");
  149. }
  150. }
  151. }
  152. /// <summary>
  153. /// Load the <code>Config</code> with properties from the stream
  154. /// <code>in</code> from a <code>Properties</code> file.
  155. /// </summary>
  156. /// <remarks>
  157. /// Load the <code>Config</code> with properties from the stream
  158. /// <code>in</code> from a <code>Properties</code> file.
  159. /// </remarks>
  160. /// <exception cref="System.IO.IOException"></exception>
  161. public static void Load(InputStream input)
  162. {
  163. if (input != null)
  164. {
  165. _prp.Load(input);
  166. }
  167. try
  168. {
  169. _prp.PutAll(Runtime.GetProperties());
  170. }
  171. catch (SecurityException)
  172. {
  173. if (_log.Level > 1)
  174. {
  175. _log.WriteLine("SecurityException: jcifs will ignore System properties");
  176. }
  177. }
  178. }
  179. /// <exception cref="System.IO.IOException"></exception>
  180. public static void Store(OutputStream output, string header)
  181. {
  182. _prp.Store(output);
  183. }
  184. /// <summary>Add a property.</summary>
  185. /// <remarks>Add a property.</remarks>
  186. public static void SetProperty(string key, string value)
  187. {
  188. _prp.SetProperty(key, value);
  189. }
  190. /// <summary>Retrieve a property as an <code>Object</code>.</summary>
  191. /// <remarks>Retrieve a property as an <code>Object</code>.</remarks>
  192. public static object Get(string key)
  193. {
  194. return _prp.GetProperty(key);
  195. }
  196. /// <summary>Retrieve a <code>String</code>.</summary>
  197. /// <remarks>
  198. /// Retrieve a <code>String</code>. If the key cannot be found,
  199. /// the provided <code>def</code> default parameter will be returned.
  200. /// </remarks>
  201. public static string GetProperty(string key, string def)
  202. {
  203. return (string)_prp.GetProperty(key, def);
  204. }
  205. /// <summary>Retrieve a <code>String</code>.</summary>
  206. /// <remarks>Retrieve a <code>String</code>. If the property is not found, <code>null</code> is returned.
  207. /// </remarks>
  208. public static string GetProperty(string key)
  209. {
  210. return (string)_prp.GetProperty(key);
  211. }
  212. /// <summary>Retrieve an <code>int</code>.</summary>
  213. /// <remarks>
  214. /// Retrieve an <code>int</code>. If the key does not exist or
  215. /// cannot be converted to an <code>int</code>, the provided default
  216. /// argument will be returned.
  217. /// </remarks>
  218. public static int GetInt(string key, int def)
  219. {
  220. string s = (string)_prp.GetProperty(key);
  221. if (s != null)
  222. {
  223. try
  224. {
  225. def = Convert.ToInt32(s);
  226. }
  227. catch (FormatException nfe)
  228. {
  229. if (_log.Level > 0)
  230. {
  231. Runtime.PrintStackTrace(nfe, _log);
  232. }
  233. }
  234. }
  235. return def;
  236. }
  237. /// <summary>Retrieve an <code>int</code>.</summary>
  238. /// <remarks>Retrieve an <code>int</code>. If the property is not found, <code>-1</code> is returned.
  239. /// </remarks>
  240. public static int GetInt(string key)
  241. {
  242. string s = (string)_prp.GetProperty(key);
  243. int result = -1;
  244. if (s != null)
  245. {
  246. try
  247. {
  248. result = Convert.ToInt32(s);
  249. }
  250. catch (FormatException nfe)
  251. {
  252. if (_log.Level > 0)
  253. {
  254. Runtime.PrintStackTrace(nfe, _log);
  255. }
  256. }
  257. }
  258. return result;
  259. }
  260. /// <summary>Retrieve a <code>long</code>.</summary>
  261. /// <remarks>
  262. /// Retrieve a <code>long</code>. If the key does not exist or
  263. /// cannot be converted to a <code>long</code>, the provided default
  264. /// argument will be returned.
  265. /// </remarks>
  266. public static long GetLong(string key, long def)
  267. {
  268. string s = (string)_prp.GetProperty(key);
  269. if (s != null)
  270. {
  271. try
  272. {
  273. def = long.Parse(s);
  274. }
  275. catch (FormatException nfe)
  276. {
  277. if (_log.Level > 0)
  278. {
  279. Runtime.PrintStackTrace(nfe, _log);
  280. }
  281. }
  282. }
  283. return def;
  284. }
  285. /// <summary>Retrieve an <code>InetAddress</code>.</summary>
  286. /// <remarks>
  287. /// Retrieve an <code>InetAddress</code>. If the address is not
  288. /// an IP address and cannot be resolved <code>null</code> will
  289. /// be returned.
  290. /// </remarks>
  291. public static IPAddress GetInetAddress(string key, IPAddress def)
  292. {
  293. string addr = (string)_prp.GetProperty(key);
  294. if (addr != null)
  295. {
  296. try
  297. {
  298. def = Extensions.GetAddressByName(addr);
  299. }
  300. catch (UnknownHostException uhe)
  301. {
  302. if (_log.Level > 0)
  303. {
  304. _log.WriteLine(addr);
  305. Runtime.PrintStackTrace(uhe, _log);
  306. }
  307. }
  308. }
  309. return def;
  310. }
  311. public static IPAddress GetLocalHost()
  312. {
  313. string addr = (string)_prp.GetProperty("jcifs.smb.client.laddr");
  314. if (addr != null)
  315. {
  316. try
  317. {
  318. return Extensions.GetAddressByName(addr);
  319. }
  320. catch (UnknownHostException uhe)
  321. {
  322. if (_log.Level > 0)
  323. {
  324. _log.WriteLine("Ignoring jcifs.smb.client.laddr address: " + addr);
  325. Runtime.PrintStackTrace(uhe, _log);
  326. }
  327. }
  328. }
  329. return null;
  330. }
  331. /// <summary>Retrieve a boolean value.</summary>
  332. /// <remarks>Retrieve a boolean value. If the property is not found, the value of <code>def</code> is returned.
  333. /// </remarks>
  334. public static bool GetBoolean(string key, bool def)
  335. {
  336. string b = GetProperty(key);
  337. if (b != null)
  338. {
  339. def = b.ToLower().Equals("true");
  340. }
  341. return def;
  342. }
  343. /// <summary>
  344. /// Retrieve an array of <tt>InetAddress</tt> created from a property
  345. /// value containting a <tt>delim</tt> separated list of hostnames and/or
  346. /// ipaddresses.
  347. /// </summary>
  348. /// <remarks>
  349. /// Retrieve an array of <tt>InetAddress</tt> created from a property
  350. /// value containting a <tt>delim</tt> separated list of hostnames and/or
  351. /// ipaddresses.
  352. /// </remarks>
  353. public static IPAddress[] GetInetAddressArray(string key, string delim, IPAddress
  354. [] def)
  355. {
  356. string p = GetProperty(key);
  357. if (p != null)
  358. {
  359. StringTokenizer tok = new StringTokenizer(p, delim);
  360. int len = tok.CountTokens();
  361. IPAddress[] arr = new IPAddress[len];
  362. for (int i = 0; i < len; i++)
  363. {
  364. string addr = tok.NextToken();
  365. try
  366. {
  367. arr[i] = Extensions.GetAddressByName(addr);
  368. }
  369. catch (UnknownHostException uhe)
  370. {
  371. if (_log.Level > 0)
  372. {
  373. _log.WriteLine(addr);
  374. Runtime.PrintStackTrace(uhe, _log);
  375. }
  376. return def;
  377. }
  378. }
  379. return arr;
  380. }
  381. return def;
  382. }
  383. }
  384. }