コード例 #1
0
ファイル: ConfigReader.cs プロジェクト: nonomal/NCache
 /// <summary>
 /// see if the client.ncconf is present in the executing assembly local folder. if yes
 /// then returns the local path. otherwise search for the client.ncconf in NCache install folder
 /// and if found returns the file global path.
 /// </summary>
 /// <returns>returns the client.ncconf local or global path if found. otherwise returns null.</returns>
 internal static string GetClientConfigurationPath()
 {
     return(DirectoryUtil.GetBaseFilePath("client.ncconf"));
 }
コード例 #2
0
        public void LoadConfiguration()
        {
            FileStream  fs = null;
            string      c_configFileName = null;
            XmlDocument configuration    = new XmlDocument();

            try
            {
                if (_cacheId == null)
                {
                    return;
                }

                c_configFileName = DirectoryUtil.GetBaseFilePath("client.ncconf", _search, out _result);

                if (c_configFileName == null)
                {
                    return;
                }

                FileInfo fileInfo = new FileInfo(c_configFileName);
                fs = fileInfo.OpenRead();
                configuration.Load(fs);
                fileInfo = null;

                bool serverPortFound = false;

                XmlNodeList clientInfoTag = configuration.GetElementsByTagName("ncache-server");

                CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
                try
                {
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                    if (clientInfoTag != null && clientInfoTag.Count > 0)
                    {
                        XmlNode portNode = clientInfoTag.Item(0);
                        if (portNode != null)
                        {
                            XmlAttributeCollection attributes = portNode.Attributes;
                            if (attributes != null)
                            {
                                string currentAttrib = string.Empty;

                                if (attributes["port"] != null && attributes["port"].Value != null)
                                {
                                    ConfigServerPort = Convert.ToInt32(attributes["port"].Value);
                                }

                                if (_cacheConnectionOptions != null && _cacheConnectionOptions.Port.HasValue)
                                {
                                    _serverPort = _cacheConnectionOptions.Port.Value;
                                }
                                else
                                {
                                    _serverPort = ConfigServerPort;
                                }

                                if (_cacheConnectionOptions != null && _cacheConnectionOptions.ClientRequestTimeOut.HasValue)
                                {
                                    _clientRequestTimeout = Convert.ToInt32(_cacheConnectionOptions.ClientRequestTimeOut.Value.TotalMilliseconds);
                                }
                                else
                                {
                                    if (attributes["client-request-timeout"] != null && attributes["client-request-timeout"].Value != null)
                                    {
                                        _clientRequestTimeout = Convert.ToInt32(attributes["client-request-timeout"].Value) * 1000;
                                    }
                                }


                                if (_cacheConnectionOptions != null && _cacheConnectionOptions.ConnectionTimeout.HasValue)
                                {
                                    _connectionTimeout = Convert.ToInt32(_cacheConnectionOptions.ConnectionTimeout.Value.TotalMilliseconds);
                                }
                                else
                                {
                                    if (attributes["connection-timeout"] != null && attributes["connection-timeout"].Value != null)
                                    {
                                        _connectionTimeout = Convert.ToInt32(attributes["connection-timeout"].Value) * 1000;
                                    }
                                }
                            }
                            serverPortFound = true;
                        }
                    }

                    if (!serverPortFound)
                    {
                        throw new Runtime.Exceptions.ConfigurationException("ncache-server missing in client confiugration");
                    }


                    XmlNodeList cacheList   = configuration.GetElementsByTagName("cache");
                    XmlNodeList cacheConfig = null;

                    for (int i = 0; i < cacheList.Count; i++)
                    {
                        XmlNode cache = cacheList.Item(i);
                        if (cache.Attributes.GetNamedItem("id").Value.ToLower().Equals(_cacheId.ToLower()))
                        {
                            if (cache.Attributes["load-balance"] != null)
                            {
                                _balanceNodes = Convert.ToBoolean(cache.Attributes["load-balance"].Value);
                            }
                            if (_cacheConnectionOptions != null && _cacheConnectionOptions.LoadBalance.HasValue)
                            {
                                _balanceNodes = _cacheConnectionOptions.LoadBalance.Value;
                            }


                            if (_cacheConnectionOptions != null && _cacheConnectionOptions.EnableClientLogs.HasValue)
                            {
                                EnableClientLogs = _cacheConnectionOptions.EnableClientLogs.Value;
                            }
                            else
                            {
                                if (cache.Attributes["enable-client-logs"] != null)
                                {
                                    EnableClientLogs = Convert.ToBoolean(cache.Attributes["enable-client-logs"].Value.ToString());
                                }
                            }


                            if (_cacheConnectionOptions != null && _cacheConnectionOptions.LogLevel.HasValue)
                            {
                                LogLevels = _cacheConnectionOptions.LogLevel.Value;
                                switch (LogLevels)
                                {
                                case LogLevel.Debug:
                                case LogLevel.Info:
                                    EnableDetailedClientLogs = true;
                                    break;

                                case LogLevel.Error:
                                    EnableDetailedClientLogs = false;
                                    break;
                                }
                            }
                            else
                            {
                                if (cache.Attributes["log-level"] != null)
                                {
                                    var logLevel = cache.Attributes["log-level"].Value.ToString().ToLower();
                                    switch (logLevel)
                                    {
                                    case "info":
                                        LogLevels = LogLevel.Info;
                                        EnableDetailedClientLogs = true;
                                        break;

                                    case "debug":
                                        LogLevels = LogLevel.Debug;
                                        EnableDetailedClientLogs = true;
                                        break;

                                    case "error":
                                        LogLevels = LogLevel.Error;
                                        EnableDetailedClientLogs = false;
                                        break;
                                    }
                                }
                            }

                            ImportHashmap = true;

                            cacheConfig = cache.ChildNodes;
                            break;
                        }
                    }

                    if (cacheConfig == null)
                    {
                        if (!string.IsNullOrEmpty(_cacheId))
                        {
                            if (_result != Search.GlobalSearch)
                            {
                                _search = _result + 1;
                                LoadConfiguration();
                            }
                        }
                        return;
                    }
                    _search = _result;

                    LoadRemoteServerMappingConfig(cacheConfig);
                    LoadRemoteServerConfig(cacheConfig);
                }
                finally
                {
                    Thread.CurrentThread.CurrentCulture = cultureInfo;
                }
            }

            catch (Runtime.Exceptions.ConfigurationException)
            {
                throw;
            }
            catch (IOException) { throw; }
            catch (Exception e)
            {
                throw new Runtime.Exceptions.ConfigurationException("An error occurred while reading client.ncconf. " + e.Message);
            }

            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }