public void IncludeTests()
        {
            const string mainIniFile = "OpenSimDefaults.ini";
            m_config = new IniConfigSource();

            // Create ini files in a directory structure
            IniConfigSource ini;
            IConfig config;

            ini = new IniConfigSource();
            config = ini.AddConfig("IncludeTest");
            config.Set("Include-absolute", "absolute/*/config/*.ini");
            config.Set("Include-relative", "../" + m_testSubdirectory + "/relative/*/config/*.ini");
            CreateIni(mainIniFile, ini);

            ini = new IniConfigSource();
            ini.AddConfig("Absolute1").Set("name1", "value1");
            CreateIni("absolute/one/config/setting.ini", ini);

            ini = new IniConfigSource();
            ini.AddConfig("Absolute2").Set("name2", 2.3);
            CreateIni("absolute/two/config/setting1.ini", ini);

            ini = new IniConfigSource();
            ini.AddConfig("Absolute2").Set("name3", "value3");
            CreateIni("absolute/two/config/setting2.ini", ini);

            ini = new IniConfigSource();
            ini.AddConfig("Relative1").Set("name4", "value4");
            CreateIni("relative/one/config/setting.ini", ini);

            ini = new IniConfigSource();
            ini.AddConfig("Relative2").Set("name5", true);
            CreateIni("relative/two/config/setting1.ini", ini);

            ini = new IniConfigSource();
            ini.AddConfig("Relative2").Set("name6", 6);
            CreateIni("relative/two/config/setting2.ini", ini);

            // Prepare call to ConfigurationLoader.LoadConfigSettings()
            ConfigurationLoader cl = new ConfigurationLoader();
            IConfigSource argvSource = new IniConfigSource();
            EnvConfigSource envConfigSource = new EnvConfigSource();
            argvSource.AddConfig("Startup").Set("inifile", mainIniFile);
            ConfigSettings configSettings;
            NetworkServersInfo networkInfo;

            OpenSimConfigSource source = cl.LoadConfigSettings(argvSource, envConfigSource,
                out configSettings, out networkInfo);

            // Remove default config
            config = source.Source.Configs["Startup"];
            source.Source.Configs.Remove(config);
            config = source.Source.Configs["Network"];
            source.Source.Configs.Remove(config);

            // Finally, we are able to check the result
            Assert.AreEqual(m_config.ToString(), source.Source.ToString(),
                "Configuration with includes does not contain all settings.");
            // The following would be preferable but fails due to a type mismatch which I am not able to resolve
            //CollectionAssert.AreEquivalent(m_config.Configs, source.Source.Configs,
            //    String.Format("Configuration with includes does not contain all settings.\nAll settings:\n{0}\nSettings read:\n{1}", m_config, source.Source));
        }
示例#2
0
        /// <summary>
        /// Loads the region configuration
        /// </summary>
        /// <param name="argvSource">Parameters passed into the process when started</param>
        /// <param name="configSettings"></param>
        /// <param name="networkInfo"></param>
        /// <returns>A configuration that gets passed to modules</returns>
        public OpenSimConfigSource LoadConfigSettings(
                IConfigSource argvSource, EnvConfigSource envConfigSource, out ConfigSettings configSettings,
                out NetworkServersInfo networkInfo)
        {
            m_configSettings = configSettings = new ConfigSettings();
            m_networkServersInfo = networkInfo = new NetworkServersInfo();

            bool iniFileExists = false;

            IConfig startupConfig = argvSource.Configs["Startup"];

            List<string> sources = new List<string>();

            string masterFileName =
                    startupConfig.GetString("inimaster", "OpenSimDefaults.ini");

            if (masterFileName == "none")
                masterFileName = String.Empty;

            if (IsUri(masterFileName))
            {
                if (!sources.Contains(masterFileName))
                    sources.Add(masterFileName);
            }
            else
            {
                string masterFilePath = Path.GetFullPath(
                        Path.Combine(Util.configDir(), masterFileName));

                if (masterFileName != String.Empty)
                {
                    if (File.Exists(masterFilePath))
                    {
                        if (!sources.Contains(masterFilePath))
                            sources.Add(masterFilePath);
                    }
                    else
                    {
                        m_log.ErrorFormat("Master ini file {0} not found", masterFilePath);
                        Environment.Exit(1);
                    }
                }
            }


            string iniFileName =
                    startupConfig.GetString("inifile", "OpenSim.ini");

            if (IsUri(iniFileName))
            {
                if (!sources.Contains(iniFileName))
                    sources.Add(iniFileName);
                Application.iniFilePath = iniFileName;
            }
            else
            {
                Application.iniFilePath = Path.GetFullPath(
                        Path.Combine(Util.configDir(), iniFileName));

                if (!File.Exists(Application.iniFilePath))
                {
                    iniFileName = "OpenSim.xml";
                    Application.iniFilePath = Path.GetFullPath(
                            Path.Combine(Util.configDir(), iniFileName));
                }

                if (File.Exists(Application.iniFilePath))
                {
                    if (!sources.Contains(Application.iniFilePath))
                        sources.Add(Application.iniFilePath);
                }
            }

            string iniDirName =
                    startupConfig.GetString("inidirectory", "config");
            string iniDirPath =
                    Path.Combine(Util.configDir(), iniDirName);

            if (Directory.Exists(iniDirPath))
            {
                m_log.InfoFormat("Searching folder {0} for config ini files",
                        iniDirPath);

                string[] fileEntries = Directory.GetFiles(iniDirName);
                foreach (string filePath in fileEntries)
                {
                    if (Path.GetExtension(filePath).ToLower() == ".ini")
                    {
                        if (!sources.Contains(Path.GetFullPath(filePath)))
                            sources.Add(Path.GetFullPath(filePath));
                    }
                }
            }

            m_config = new OpenSimConfigSource();
            m_config.Source = new IniConfigSource();
            m_config.Source.Merge(DefaultConfig());

            m_log.Info("[CONFIG]: Reading configuration settings");

            if (sources.Count == 0)
            {
                m_log.FatalFormat("[CONFIG]: Could not load any configuration");
                m_log.FatalFormat("[CONFIG]: Did you copy the OpenSimDefaults.ini.example file to OpenSimDefaults.ini?");
                Environment.Exit(1);
            }

            for (int i = 0 ; i < sources.Count ; i++)
            {
                if (ReadConfig(sources[i]))
                {
                    iniFileExists = true;
                    AddIncludes(sources);
                }
            }

            if (!iniFileExists)
            {
                m_log.FatalFormat("[CONFIG]: Could not load any configuration");
                m_log.FatalFormat("[CONFIG]: Configuration exists, but there was an error loading it!");
                Environment.Exit(1);
            }

            // Make sure command line options take precedence
            m_config.Source.Merge(argvSource);


            IConfig enVars = m_config.Source.Configs["Environment"];

            if( enVars != null )
            {
                string[] env_keys = enVars.GetKeys();

                foreach ( string key in env_keys )
                {
                    envConfigSource.AddEnv(key, string.Empty);
                }

                envConfigSource.LoadEnv();
                m_config.Source.Merge(envConfigSource);
                m_config.Source.ExpandKeyValues();
            }


            ReadConfigSettings();

            return m_config;
        }
        /// <summary>
        /// Loads the region configuration
        /// </summary>
        /// <param name="argvSource">Parameters passed into the process when started</param>
        /// <param name="configSettings"></param>
        /// <param name="networkInfo"></param>
        /// <returns>A configuration that gets passed to modules</returns>
        public OpenSimConfigSource LoadConfigSettings(
                IConfigSource argvSource, EnvConfigSource envConfigSource, out ConfigSettings configSettings,
                out NetworkServersInfo networkInfo)
        {
            m_configSettings = configSettings = new ConfigSettings();
            m_networkServersInfo = networkInfo = new NetworkServersInfo();

            bool iniFileExists = false;

            IConfig startupConfig = argvSource.Configs["Startup"];

            List<string> sources = new List<string>();

            string masterFileName = startupConfig.GetString("inimaster", "OpenSimDefaults.ini");

            if (masterFileName == "none")
                masterFileName = String.Empty;

            if (IsUri(masterFileName))
            {
                if (!sources.Contains(masterFileName))
                    sources.Add(masterFileName);
            }
            else
            {
                string masterFilePath = Path.GetFullPath(
                        Path.Combine(Util.configDir(), masterFileName));

                if (masterFileName != String.Empty)
                {
                    if (File.Exists(masterFilePath))
                    {
                        if (!sources.Contains(masterFilePath))
                            sources.Add(masterFilePath);
                    }
                    else
                    {
                        m_log.ErrorFormat("Master ini file {0} not found", Path.GetFullPath(masterFilePath));
                        Environment.Exit(1);
                    }
                }
            }

            string iniFileName = startupConfig.GetString("inifile", "OpenSim.ini");

            if (IsUri(iniFileName))
            {
                if (!sources.Contains(iniFileName))
                    sources.Add(iniFileName);
                Application.iniFilePath = iniFileName;
            }
            else
            {
                Application.iniFilePath = Path.GetFullPath(
                    Path.Combine(Util.configDir(), iniFileName));

                if (!File.Exists(Application.iniFilePath))
                {
                    iniFileName = "OpenSim.xml";
                    Application.iniFilePath = Path.GetFullPath(Path.Combine(Util.configDir(), iniFileName));
                }

                if (File.Exists(Application.iniFilePath))
                {
                    if (!sources.Contains(Application.iniFilePath))
                        sources.Add(Application.iniFilePath);
                }
            }

            m_config = new OpenSimConfigSource();
            m_config.Source = new IniConfigSource();
            m_config.Source.Merge(DefaultConfig());

            m_log.Info("[CONFIG]: Reading configuration settings");

            for (int i = 0 ; i < sources.Count ; i++)
            {
                if (ReadConfig(m_config, sources[i]))
                {
                    iniFileExists = true;
                    AddIncludes(m_config, sources);
                }
            }

            // Override distro settings with contents of inidirectory
            string iniDirName = startupConfig.GetString("inidirectory", "config");
            string iniDirPath = Path.Combine(Util.configDir(), iniDirName);

            if (Directory.Exists(iniDirPath))
            {
                m_log.InfoFormat("[CONFIG]: Searching folder {0} for config ini files", iniDirPath);
                List<string> overrideSources = new List<string>();

                string[] fileEntries = Directory.GetFiles(iniDirName);
                foreach (string filePath in fileEntries)
                {
                    if (Path.GetExtension(filePath).ToLower() == ".ini")
                    {
                        if (!sources.Contains(Path.GetFullPath(filePath)))
                        {
                            overrideSources.Add(Path.GetFullPath(filePath));
                            // put it in sources too, to avoid circularity
                            sources.Add(Path.GetFullPath(filePath));
                        }
                    }
                }


                if (overrideSources.Count > 0)
                {
                    OpenSimConfigSource overrideConfig = new OpenSimConfigSource();
                    overrideConfig.Source = new IniConfigSource();

                    for (int i = 0 ; i < overrideSources.Count ; i++)
                    {
                        if (ReadConfig(overrideConfig, overrideSources[i]))
                        {
                            iniFileExists = true;
                            AddIncludes(overrideConfig, overrideSources);
                        } 
                    }
                    m_config.Source.Merge(overrideConfig.Source);
                }
            }

            if (sources.Count == 0)
            {
                m_log.FatalFormat("[CONFIG]: Could not load any configuration");
                Environment.Exit(1);
            } 
            else if (!iniFileExists)
            {
                m_log.FatalFormat("[CONFIG]: Could not load any configuration");
                m_log.FatalFormat("[CONFIG]: Configuration exists, but there was an error loading it!");
                Environment.Exit(1);
            }

            // Merge OpSys env vars
            m_log.Info("[CONFIG]: Loading environment variables for Config");
            Util.MergeEnvironmentToConfig(m_config.Source);

            // Make sure command line options take precedence
            m_config.Source.Merge(argvSource);

            m_config.Source.ReplaceKeyValues();

            ReadConfigSettings();

            return m_config;
        }
        public void IncludeTests()
        {
            const string mainIniFile = "OpenSimDefaults.ini";

            m_config = new IniConfigSource();

            // Create ini files in a directory structure
            IniConfigSource ini;
            IConfig         config;

            ini    = new IniConfigSource();
            config = ini.AddConfig("IncludeTest");
            config.Set("Include-absolute", "absolute/*/config/*.ini");
            config.Set("Include-relative", "../" + m_testSubdirectory + "/relative/*/config/*.ini");
            CreateIni(mainIniFile, ini);

            ini = new IniConfigSource();
            ini.AddConfig("Absolute1").Set("name1", "value1");
            CreateIni("absolute/one/config/setting.ini", ini);

            ini = new IniConfigSource();
            ini.AddConfig("Absolute2").Set("name2", 2.3);
            CreateIni("absolute/two/config/setting1.ini", ini);

            ini = new IniConfigSource();
            ini.AddConfig("Absolute2").Set("name3", "value3");
            CreateIni("absolute/two/config/setting2.ini", ini);

            ini = new IniConfigSource();
            ini.AddConfig("Relative1").Set("name4", "value4");
            CreateIni("relative/one/config/setting.ini", ini);

            ini = new IniConfigSource();
            ini.AddConfig("Relative2").Set("name5", true);
            CreateIni("relative/two/config/setting1.ini", ini);

            ini = new IniConfigSource();
            ini.AddConfig("Relative2").Set("name6", 6);
            CreateIni("relative/two/config/setting2.ini", ini);

            // Prepare call to ConfigurationLoader.LoadConfigSettings()
            ConfigurationLoader cl              = new ConfigurationLoader();
            IConfigSource       argvSource      = new IniConfigSource();
            EnvConfigSource     envConfigSource = new EnvConfigSource();

            argvSource.AddConfig("Startup").Set("inifile", mainIniFile);
            ConfigSettings     configSettings;
            NetworkServersInfo networkInfo;

            OpenSimConfigSource source = cl.LoadConfigSettings(argvSource, envConfigSource,
                                                               out configSettings, out networkInfo);

            // Remove default config
            config = source.Source.Configs["Startup"];
            source.Source.Configs.Remove(config);
            config = source.Source.Configs["Network"];
            source.Source.Configs.Remove(config);

            // Finally, we are able to check the result
            Assert.AreEqual(m_config.ToString(), source.Source.ToString(),
                            "Configuration with includes does not contain all settings.");
            // The following would be preferable but fails due to a type mismatch which I am not able to resolve
            //CollectionAssert.AreEquivalent(m_config.Configs, source.Source.Configs,
            //    String.Format("Configuration with includes does not contain all settings.\nAll settings:\n{0}\nSettings read:\n{1}", m_config, source.Source));
        }
        public void Works_fine_with_unset_values()
        {
            EnvConfigSource configSource = new EnvConfigSource();

            Assert.IsFalse(configSource.GetValue(typeof(int), "b", "a").IsSet);
        }
        /// <summary>
        /// Loads the region configuration
        /// </summary>
        /// <param name="argvSource">Parameters passed into the process when started</param>
        /// <param name="configSettings"></param>
        /// <param name="networkInfo"></param>
        /// <returns>A configuration that gets passed to modules</returns>
        public OpenSimConfigSource LoadConfigSettings(
            IConfigSource argvSource, EnvConfigSource envConfigSource, out ConfigSettings configSettings,
            out NetworkServersInfo networkInfo)
        {
            m_configSettings     = configSettings = new ConfigSettings();
            m_networkServersInfo = networkInfo = new NetworkServersInfo();

            bool iniFileExists = false;

            IConfig startupConfig = argvSource.Configs["Startup"];

            List <string> sources = new List <string>();

            string masterFileName = startupConfig.GetString("inimaster", "OpenSimDefaults.ini");

            if (masterFileName == "none")
            {
                masterFileName = String.Empty;
            }

            if (IsUri(masterFileName))
            {
                if (!sources.Contains(masterFileName))
                {
                    sources.Add(masterFileName);
                }
            }
            else
            {
                string masterFilePath = Path.GetFullPath(
                    Path.Combine(Util.configDir(), masterFileName));

                if (masterFileName != String.Empty)
                {
                    if (File.Exists(masterFilePath))
                    {
                        if (!sources.Contains(masterFilePath))
                        {
                            sources.Add(masterFilePath);
                        }
                    }
                    else
                    {
                        m_log.ErrorFormat("Master ini file {0} not found", Path.GetFullPath(masterFilePath));
                        Environment.Exit(1);
                    }
                }
            }

            string iniFileName = startupConfig.GetString("inifile", "OpenSim.ini");

            if (IsUri(iniFileName))
            {
                if (!sources.Contains(iniFileName))
                {
                    sources.Add(iniFileName);
                }
                Application.iniFilePath = iniFileName;
            }
            else
            {
                Application.iniFilePath = Path.GetFullPath(
                    Path.Combine(Util.configDir(), iniFileName));

                if (!File.Exists(Application.iniFilePath))
                {
                    iniFileName             = "OpenSim.xml";
                    Application.iniFilePath = Path.GetFullPath(Path.Combine(Util.configDir(), iniFileName));
                }

                if (File.Exists(Application.iniFilePath))
                {
                    if (!sources.Contains(Application.iniFilePath))
                    {
                        sources.Add(Application.iniFilePath);
                    }
                }
            }

            m_config        = new OpenSimConfigSource();
            m_config.Source = new IniConfigSource();
            m_config.Source.Merge(DefaultConfig());

            m_log.Info("[CONFIG]: Reading configuration settings");

            for (int i = 0; i < sources.Count; i++)
            {
                if (ReadConfig(m_config, sources[i]))
                {
                    iniFileExists = true;
                    AddIncludes(m_config, sources);
                }
            }

            // Override distro settings with contents of inidirectory
            string iniDirName = startupConfig.GetString("inidirectory", "config");
            string iniDirPath = Path.Combine(Util.configDir(), iniDirName);

            if (Directory.Exists(iniDirPath))
            {
                m_log.InfoFormat("[CONFIG]: Searching folder {0} for config ini files", iniDirPath);
                List <string> overrideSources = new List <string>();

                string[] fileEntries = Directory.GetFiles(iniDirName);
                foreach (string filePath in fileEntries)
                {
                    if (Path.GetExtension(filePath).ToLower() == ".ini")
                    {
                        if (!sources.Contains(Path.GetFullPath(filePath)))
                        {
                            overrideSources.Add(Path.GetFullPath(filePath));
                            // put it in sources too, to avoid circularity
                            sources.Add(Path.GetFullPath(filePath));
                        }
                    }
                }


                if (overrideSources.Count > 0)
                {
                    OpenSimConfigSource overrideConfig = new OpenSimConfigSource();
                    overrideConfig.Source = new IniConfigSource();

                    for (int i = 0; i < overrideSources.Count; i++)
                    {
                        if (ReadConfig(overrideConfig, overrideSources[i]))
                        {
                            iniFileExists = true;
                            AddIncludes(overrideConfig, overrideSources);
                        }
                    }
                    m_config.Source.Merge(overrideConfig.Source);
                }
            }

            if (sources.Count == 0)
            {
                m_log.FatalFormat("[CONFIG]: Could not load any configuration");
                Environment.Exit(1);
            }
            else if (!iniFileExists)
            {
                m_log.FatalFormat("[CONFIG]: Could not load any configuration");
                m_log.FatalFormat("[CONFIG]: Configuration exists, but there was an error loading it!");
                Environment.Exit(1);
            }

            // Merge OpSys env vars
            m_log.Info("[CONFIG]: Loading environment variables for Config");
            Util.MergeEnvironmentToConfig(m_config.Source);

            // Make sure command line options take precedence
            m_config.Source.Merge(argvSource);

            m_config.Source.ReplaceKeyValues();

            ReadConfigSettings();

            return(m_config);
        }
示例#7
0
        /// <summary>
        /// Loads the region configuration
        /// </summary>
        /// <param name="argvSource">Parameters passed into the process when started</param>
        /// <param name="configSettings"></param>
        /// <param name="networkInfo"></param>
        /// <returns>A configuration that gets passed to modules</returns>
        public OpenSimConfigSource LoadConfigSettings(
            IConfigSource argvSource, EnvConfigSource envConfigSource, out ConfigSettings configSettings,
            out NetworkServersInfo networkInfo)
        {
            m_configSettings     = configSettings = new ConfigSettings();
            m_networkServersInfo = networkInfo = new NetworkServersInfo();

            bool iniFileExists = false;

            IConfig startupConfig = argvSource.Configs["Startup"];

            List <string> sources = new List <string>();

            string masterFileName =
                startupConfig.GetString("inimaster", "OpenSimDefaults.ini");

            if (masterFileName == "none")
            {
                masterFileName = String.Empty;
            }

            if (IsUri(masterFileName))
            {
                if (!sources.Contains(masterFileName))
                {
                    sources.Add(masterFileName);
                }
            }
            else
            {
                string masterFilePath = Path.GetFullPath(
                    Path.Combine(Util.configDir(), masterFileName));

                if (masterFileName != String.Empty)
                {
                    if (File.Exists(masterFilePath))
                    {
                        if (!sources.Contains(masterFilePath))
                        {
                            sources.Add(masterFilePath);
                        }
                    }
                    else
                    {
                        m_log.ErrorFormat("Master ini file {0} not found", masterFilePath);
                        Environment.Exit(1);
                    }
                }
            }


            string iniFileName =
                startupConfig.GetString("inifile", "OpenSim.ini");

            if (IsUri(iniFileName))
            {
                if (!sources.Contains(iniFileName))
                {
                    sources.Add(iniFileName);
                }
                Application.iniFilePath = iniFileName;
            }
            else
            {
                Application.iniFilePath = Path.GetFullPath(
                    Path.Combine(Util.configDir(), iniFileName));

                if (!File.Exists(Application.iniFilePath))
                {
                    iniFileName             = "OpenSim.xml";
                    Application.iniFilePath = Path.GetFullPath(
                        Path.Combine(Util.configDir(), iniFileName));
                }

                if (File.Exists(Application.iniFilePath))
                {
                    if (!sources.Contains(Application.iniFilePath))
                    {
                        sources.Add(Application.iniFilePath);
                    }
                }
            }

            string iniDirName =
                startupConfig.GetString("inidirectory", "config");
            string iniDirPath =
                Path.Combine(Util.configDir(), iniDirName);

            if (Directory.Exists(iniDirPath))
            {
                m_log.InfoFormat("Searching folder {0} for config ini files",
                                 iniDirPath);

                string[] fileEntries = Directory.GetFiles(iniDirName);
                foreach (string filePath in fileEntries)
                {
                    if (Path.GetExtension(filePath).ToLower() == ".ini")
                    {
                        if (!sources.Contains(Path.GetFullPath(filePath)))
                        {
                            sources.Add(Path.GetFullPath(filePath));
                        }
                    }
                }
            }

            m_config        = new OpenSimConfigSource();
            m_config.Source = new IniConfigSource();
            m_config.Source.Merge(DefaultConfig());

            m_log.Info("[CONFIG]: Reading configuration settings");

            if (sources.Count == 0)
            {
                m_log.FatalFormat("[CONFIG]: Could not load any configuration");
                m_log.FatalFormat("[CONFIG]: Did you copy the OpenSimDefaults.ini.example file to OpenSimDefaults.ini?");
                Environment.Exit(1);
            }

            for (int i = 0; i < sources.Count; i++)
            {
                if (ReadConfig(sources[i]))
                {
                    iniFileExists = true;
                    AddIncludes(sources);
                }
            }

            if (!iniFileExists)
            {
                m_log.FatalFormat("[CONFIG]: Could not load any configuration");
                m_log.FatalFormat("[CONFIG]: Configuration exists, but there was an error loading it!");
                Environment.Exit(1);
            }

            // Make sure command line options take precedence
            m_config.Source.Merge(argvSource);


            IConfig enVars = m_config.Source.Configs["Environment"];

            if (enVars != null)
            {
                string[] env_keys = enVars.GetKeys();

                foreach (string key in env_keys)
                {
                    envConfigSource.AddEnv(key, string.Empty);
                }

                envConfigSource.LoadEnv();
                m_config.Source.Merge(envConfigSource);
                m_config.Source.ExpandKeyValues();
            }


            ReadConfigSettings();

            return(m_config);
        }
示例#8
0
 public EnvConfig(string name, IConfigSource source)
     :base(name, source)
 {
     parent = (EnvConfigSource)source;
 }