From 2e6e88cb9baf8ca8e09c1c1627261c12ed4c9e4b Mon Sep 17 00:00:00 2001 From: Mirco Bauer Date: Sat, 13 Dec 2014 13:13:37 +0100 Subject: [PATCH] Engine(-Tests), Server: implemented config overrides Added System.Core assembly reference to Server --- src/Engine-Tests/ConfigTests.cs | 59 ++++++++++++++++++++++++++++ src/Engine-Tests/Engine-Tests.csproj | 4 +- src/Engine/Config/Config.cs | 31 ++++++++++++++- src/Engine/Engine.cs | 28 +++++++++++++ src/Server/Main.cs | 20 +++++++++- src/Server/Makefile.am | 1 + src/Server/Server.cs | 4 +- src/Server/Server.csproj | 1 + 8 files changed, 142 insertions(+), 6 deletions(-) create mode 100644 src/Engine-Tests/ConfigTests.cs diff --git a/src/Engine-Tests/ConfigTests.cs b/src/Engine-Tests/ConfigTests.cs new file mode 100644 index 000000000..77e9c5372 --- /dev/null +++ b/src/Engine-Tests/ConfigTests.cs @@ -0,0 +1,59 @@ +// Smuxi - Smart MUltipleXed Irc +// +// Copyright (c) 2014 Mirco Bauer +// +// Full GPL License: +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +using System; +using System.Collections.Generic; +using NUnit.Framework; + +namespace Smuxi.Engine +{ + [TestFixture] + public class ConfigTests + { + [Test] + public void Indexer() + { + var config = new Config(); + config["Server/Port"] = 123; + Assert.AreEqual(123, (int) config["Server/Port"]); + } + + [Test] + public void ConfigOverridesIntValue() + { + var config = new Config(); + var overrides = config.Overrides; + overrides.Add("Server/Port", 7689); + + config["Server/Port"] = 123; + Assert.AreEqual(7689, (int) config["Server/Port"]); + } + + [Test] + public void ConfigOverridesRegexKey() + { + var config = new Config(); + var overrides = config.Overrides; + overrides.Add("/Engine/Users/.*/MessageBuffer/PersistencyType/", "PersistentSqlite"); + + config["Engine/Users/local/MessageBuffer/PersistencyType"] = "Volatile"; + Assert.AreEqual("PersistentSqlite", (string) config["Engine/Users/local/MessageBuffer/PersistencyType"]); + } + } +} diff --git a/src/Engine-Tests/Engine-Tests.csproj b/src/Engine-Tests/Engine-Tests.csproj index c3231b71f..4717a267c 100644 --- a/src/Engine-Tests/Engine-Tests.csproj +++ b/src/Engine-Tests/Engine-Tests.csproj @@ -47,6 +47,7 @@ + @@ -75,8 +76,5 @@ Newtonsoft.Json - - - diff --git a/src/Engine/Config/Config.cs b/src/Engine/Config/Config.cs index 77b608931..dc88b90f5 100644 --- a/src/Engine/Config/Config.cs +++ b/src/Engine/Config/Config.cs @@ -53,10 +53,22 @@ public class Config : PermanentRemoteObject protected Hashtable m_Preferences = Hashtable.Synchronized(new Hashtable()); public Version PreviousVersion { get; private set; } public Version CurrentVersion { get; private set; } + internal Dictionary Overrides { get; private set; } public event EventHandler Changed; public object this[string key] { get { + // config overrides + lock (Overrides) { + foreach (var @override in Overrides) { + var pattern = @override.Key; + var value = @override.Value; + if (Pattern.IsMatch(key, pattern)) { + return value; + } + } + } + return m_Preferences[key]; } set { @@ -66,6 +78,20 @@ public object this[string key] { #endif return; } + + // config overrides + lock (Overrides) { + foreach (var @override in Overrides) { + var pattern = @override.Key; + if (Pattern.IsMatch(key, pattern)) { +#if LOG4NET + _Logger.Debug("set[]: ignoring setting an overridden config key " + key + "."); +#endif + return; + } + } + } + var oldValue = m_Preferences[key]; m_Preferences[key] = value; @@ -86,6 +112,7 @@ public bool IsCleanConfig { public Config() { + Overrides = new Dictionary(); #if CONFIG_NINI m_ConfigPath = Path.Combine(Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData), "smuxi"); @@ -680,7 +707,9 @@ public IDictionary GetAll() lock (m_Preferences) { var dict = new Dictionary(m_Preferences.Count); foreach (DictionaryEntry entry in m_Preferences) { - dict.Add((string) entry.Key, entry.Value); + // honor config overrides + var value = this[(string) entry.Key]; + dict.Add((string) entry.Key, value); } return dict; } diff --git a/src/Engine/Engine.cs b/src/Engine/Engine.cs index ccc531349..e6cd9f1b6 100644 --- a/src/Engine/Engine.cs +++ b/src/Engine/Engine.cs @@ -24,6 +24,7 @@ using System.IO; using System.Text; using System.Reflection; +using System.Collections.Generic; using Smuxi.Common; namespace Smuxi.Engine @@ -38,6 +39,7 @@ public class Engine private static Config _Config; private static SessionManager _SessionManager; private static ProtocolManagerFactory _ProtocolManagerFactory; + public static Dictionary ConfigOverrides { get; set; } public static Version AssemblyVersion { get { @@ -176,6 +178,32 @@ public static void Init() _Config.Save(); + // config overrides + // we only scan all config keys so we can convert the override + // value into the right type + foreach (var kvp in _Config.GetAll()) { + var configKey = kvp.Key; + var configValue = kvp.Value; + + foreach (var @override in ConfigOverrides) { + var keyPattern = @override.Key; + + if (!Pattern.IsMatch(configKey, keyPattern)) { + continue; + } + + // convert string to type + var overridenValue = Convert.ChangeType(@override.Value, + configValue.GetType()); + // as the patttern could match many keys we only need one + // override with the right value type + if (_Config.Overrides.ContainsKey(keyPattern)) { + continue; + } + _Config.Overrides.Add(keyPattern, overridenValue); + } + } + string location = Path.GetDirectoryName(asm.Location); if (String.IsNullOrEmpty(location) && Environment.OSVersion.Platform == PlatformID.Unix) { diff --git a/src/Server/Main.cs b/src/Server/Main.cs index ef0b81e9a..6e8517030 100644 --- a/src/Server/Main.cs +++ b/src/Server/Main.cs @@ -23,7 +23,9 @@ using System; using System.IO; +using System.Linq; using System.Reflection; +using System.Collections.Generic; using NDesk.Options; using Smuxi.Common; using Smuxi.Engine; @@ -135,6 +137,22 @@ public static void Main(string[] args) } ); + var configOverrides = new Dictionary(); + parser.Add( + "override-config=", + _("Override config values"), + delegate (string val) { + if (String.IsNullOrEmpty(val) || !val.Contains("=")) { + return; + } + var overrideKeyPattern = val.Split('=')[0].Trim(); + var overrideValue = String.Join( + "=", val.Split('=').Skip(1).ToArray() + ).Trim(); + configOverrides.Add(overrideKeyPattern, overrideValue); + } + ); + parser.Add( "h|help", _("Show this help"), @@ -194,7 +212,7 @@ public static void Main(string[] args) } try { - Server.Init(args); + Server.Init(args, configOverrides); } catch (Exception e) { #if LOG4NET _Logger.Fatal(e); diff --git a/src/Server/Makefile.am b/src/Server/Makefile.am index 864d96609..4e7bd0871 100644 --- a/src/Server/Makefile.am +++ b/src/Server/Makefile.am @@ -88,6 +88,7 @@ EXTRAS = \ REFERENCES = \ System.Runtime.Remoting \ System \ + System.Core \ Mono.Posix DLL_REFERENCES = diff --git a/src/Server/Server.cs b/src/Server/Server.cs index ea9a8a760..9c7d78a23 100644 --- a/src/Server/Server.cs +++ b/src/Server/Server.cs @@ -29,6 +29,7 @@ using System; using System.Threading; using System.Collections; +using System.Collections.Generic; using System.Runtime.Remoting; using System.Runtime.Remoting.Lifetime; using System.Runtime.Remoting.Channels; @@ -51,8 +52,9 @@ public class Server private static readonly log4net.ILog _Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); #endif - public static void Init(string[] args) + public static void Init(string[] args, Dictionary configOverrides) { + Engine.Engine.ConfigOverrides = configOverrides; Engine.Engine.Init(); Engine.Engine.InitSessionManager(); string channel = (string)Engine.Engine.Config["Server/Channel"]; diff --git a/src/Server/Server.csproj b/src/Server/Server.csproj index 848cae15e..544c7f179 100644 --- a/src/Server/Server.csproj +++ b/src/Server/Server.csproj @@ -99,6 +99,7 @@ +