// This code is derived from jcifs smb client library // Ported by J. Arturo // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA using System; using System.IO; using System.Net; using System.Security; using SharpCifs.Util; using SharpCifs.Util.Sharpen; namespace SharpCifs { /// /// This class uses a static /// Sharpen.Properties /// to act /// as a cental repository for all jCIFS configuration properties. It cannot be /// instantiated. Similar to System properties the namespace /// is global therefore property names should be unique. Before use, /// the load method should be called with the name of a /// Properties file (or null indicating no /// file) to initialize the Config. The System /// properties will then populate the Config as well potentially /// overwriting properties from the file. Thus properties provided on the /// commandline with the -Dproperty.name=value VM parameter /// will override properties from the configuration file. /// /// There are several ways to set jCIFS properties. See /// the overview page of the API /// documentation for details. /// public class Config { /// The static Properties. /// The static Properties. private static Properties _prp = new Properties(); private static LogStream _log; public static string DefaultOemEncoding = "UTF-8"; //"Cp850"; static Config() { int level; FileInputStream fis = null; _log = LogStream.GetInstance(); try { string filename = Runtime.GetProperty("jcifs.properties"); if (filename != null && filename.Length > 1) { fis = new FileInputStream(filename); } Load(fis); if (fis != null) { fis.Close(); } } catch (IOException ioe) { if (_log.Level > 0) { Runtime.PrintStackTrace(ioe, _log); } } if ((level = GetInt("jcifs.util.loglevel", -1)) != -1) { _log.SetLevel(level); } try { Runtime.GetBytesForString(string.Empty, DefaultOemEncoding); } catch (Exception ex) { if (_log.Level >= 2) { _log.WriteLine("WARNING: The default OEM encoding " + DefaultOemEncoding + " does not appear to be supported by this JRE. The default encoding will be US-ASCII." ); } //DEFAULT_OEM_ENCODING = "US-ASCII"; } if (_log.Level >= 4) { try { _prp.Store(_log); } catch (IOException) { } } } /// /// This static method registers the SMB URL protocol handler which is /// required to use SMB URLs with the java.net.URL class. /// /// /// This static method registers the SMB URL protocol handler which is /// required to use SMB URLs with the java.net.URL class. If this /// method is not called before attempting to create an SMB URL with the /// URL class the following exception will occur: ///
        /// Exception MalformedURLException: unknown protocol: smb
        /// at java.net.URL.(URL.java:480)
        /// at java.net.URL.(URL.java:376)
        /// at java.net.URL.(URL.java:330)
        /// at jcifs.smb.SmbFile.(SmbFile.java:355)
        /// ...
        /// 
/// public static void RegisterSmbURLHandler() { throw new NotImplementedException(); } // supress javadoc constructor summary by removing 'protected' /// Set the default properties of the static Properties used by Config. /// /// /// Set the default properties of the static Properties used by Config. This permits /// a different Properties object/file to be used as the source of properties for /// use by the jCIFS library. The Properties must be set before jCIFS /// classes are accessed as most jCIFS classes load properties statically once. /// Using this method will also override properties loaded /// using the -Djcifs.properties= commandline parameter. /// public static void SetProperties(Properties prp) { Config._prp = new Properties(prp); try { Config._prp.PutAll(Runtime.GetProperties()); } catch (SecurityException) { if (_log.Level > 1) { _log.WriteLine("SecurityException: jcifs will ignore System properties"); } } } /// /// Load the Config with properties from the stream /// in from a Properties file. /// /// /// Load the Config with properties from the stream /// in from a Properties file. /// /// public static void Load(InputStream input) { if (input != null) { _prp.Load(input); } try { _prp.PutAll(Runtime.GetProperties()); } catch (SecurityException) { if (_log.Level > 1) { _log.WriteLine("SecurityException: jcifs will ignore System properties"); } } } /// public static void Store(OutputStream output, string header) { _prp.Store(output); } /// Add a property. /// Add a property. public static void SetProperty(string key, string value) { _prp.SetProperty(key, value); } /// Retrieve a property as an Object. /// Retrieve a property as an Object. public static object Get(string key) { return _prp.GetProperty(key); } /// Retrieve a String. /// /// Retrieve a String. If the key cannot be found, /// the provided def default parameter will be returned. /// public static string GetProperty(string key, string def) { return (string)_prp.GetProperty(key, def); } /// Retrieve a String. /// Retrieve a String. If the property is not found, null is returned. /// public static string GetProperty(string key) { return (string)_prp.GetProperty(key); } /// Retrieve an int. /// /// Retrieve an int. If the key does not exist or /// cannot be converted to an int, the provided default /// argument will be returned. /// public static int GetInt(string key, int def) { string s = (string)_prp.GetProperty(key); if (s != null) { try { def = Convert.ToInt32(s); } catch (FormatException nfe) { if (_log.Level > 0) { Runtime.PrintStackTrace(nfe, _log); } } } return def; } /// Retrieve an int. /// Retrieve an int. If the property is not found, -1 is returned. /// public static int GetInt(string key) { string s = (string)_prp.GetProperty(key); int result = -1; if (s != null) { try { result = Convert.ToInt32(s); } catch (FormatException nfe) { if (_log.Level > 0) { Runtime.PrintStackTrace(nfe, _log); } } } return result; } /// Retrieve a long. /// /// Retrieve a long. If the key does not exist or /// cannot be converted to a long, the provided default /// argument will be returned. /// public static long GetLong(string key, long def) { string s = (string)_prp.GetProperty(key); if (s != null) { try { def = long.Parse(s); } catch (FormatException nfe) { if (_log.Level > 0) { Runtime.PrintStackTrace(nfe, _log); } } } return def; } /// Retrieve an InetAddress. /// /// Retrieve an InetAddress. If the address is not /// an IP address and cannot be resolved null will /// be returned. /// public static IPAddress GetInetAddress(string key, IPAddress def) { string addr = (string)_prp.GetProperty(key); if (addr != null) { try { def = Extensions.GetAddressByName(addr); } catch (UnknownHostException uhe) { if (_log.Level > 0) { _log.WriteLine(addr); Runtime.PrintStackTrace(uhe, _log); } } } return def; } public static IPAddress GetLocalHost() { string addr = (string)_prp.GetProperty("jcifs.smb.client.laddr"); if (addr != null) { try { return Extensions.GetAddressByName(addr); } catch (UnknownHostException uhe) { if (_log.Level > 0) { _log.WriteLine("Ignoring jcifs.smb.client.laddr address: " + addr); Runtime.PrintStackTrace(uhe, _log); } } } return null; } /// Retrieve a boolean value. /// Retrieve a boolean value. If the property is not found, the value of def is returned. /// public static bool GetBoolean(string key, bool def) { string b = GetProperty(key); if (b != null) { def = b.ToLower().Equals("true"); } return def; } /// /// Retrieve an array of InetAddress created from a property /// value containting a delim separated list of hostnames and/or /// ipaddresses. /// /// /// Retrieve an array of InetAddress created from a property /// value containting a delim separated list of hostnames and/or /// ipaddresses. /// public static IPAddress[] GetInetAddressArray(string key, string delim, IPAddress [] def) { string p = GetProperty(key); if (p != null) { StringTokenizer tok = new StringTokenizer(p, delim); int len = tok.CountTokens(); IPAddress[] arr = new IPAddress[len]; for (int i = 0; i < len; i++) { string addr = tok.NextToken(); try { arr[i] = Extensions.GetAddressByName(addr); } catch (UnknownHostException uhe) { if (_log.Level > 0) { _log.WriteLine(addr); Runtime.PrintStackTrace(uhe, _log); } return def; } } return arr; } return def; } } }