diff options
| author | Stephen <snazy2000@btinternet.com> | 2014-06-30 06:20:24 +0100 |
|---|---|---|
| committer | Stephen <snazy2000@btinternet.com> | 2014-06-30 06:20:24 +0100 |
| commit | 2a9e4778124de3103bd4a50f806a3694b57d3c6a (patch) | |
| tree | 0275788f3b9706b26441edf48a0b4975cc02ee5d /MediaBrowser.Common.Implementations | |
| parent | 608ebf4829e7e394170bb2dec8a33c83600e9c08 (diff) | |
| parent | 62d98551edf421ba50f2eadf95d6c3d1fb1d20ae (diff) | |
Merge pull request #1 from MediaBrowser/master
update
Diffstat (limited to 'MediaBrowser.Common.Implementations')
3 files changed, 100 insertions, 16 deletions
diff --git a/MediaBrowser.Common.Implementations/BaseApplicationHost.cs b/MediaBrowser.Common.Implementations/BaseApplicationHost.cs index 7bd0ca748..ebd6c6b59 100644 --- a/MediaBrowser.Common.Implementations/BaseApplicationHost.cs +++ b/MediaBrowser.Common.Implementations/BaseApplicationHost.cs @@ -342,6 +342,7 @@ namespace MediaBrowser.Common.Implementations /// </summary> protected virtual void FindParts() { + ConfigurationManager.AddParts(GetExports<IConfigurationFactory>()); Plugins = GetExports<IPlugin>(); } diff --git a/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs b/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs index 8c4840ea7..60abc14f1 100644 --- a/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs +++ b/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs @@ -1,10 +1,13 @@ -using System.IO; -using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Events; using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.IO; +using System.Linq; using System.Threading; namespace MediaBrowser.Common.Implementations.Configuration @@ -26,6 +29,11 @@ namespace MediaBrowser.Common.Implementations.Configuration public event EventHandler<EventArgs> ConfigurationUpdated; /// <summary> + /// Occurs when [named configuration updated]. + /// </summary> + public event EventHandler<ConfigurationUpdateEventArgs> NamedConfigurationUpdated; + + /// <summary> /// Gets the logger. /// </summary> /// <value>The logger.</value> @@ -74,6 +82,9 @@ namespace MediaBrowser.Common.Implementations.Configuration } } + private ConfigurationStore[] _configurationStores = {}; + private IConfigurationFactory[] _configurationFactories; + /// <summary> /// Initializes a new instance of the <see cref="BaseConfigurationManager" /> class. /// </summary> @@ -89,10 +100,14 @@ namespace MediaBrowser.Common.Implementations.Configuration UpdateCachePath(); } - /// <summary> - /// The _save lock - /// </summary> - private readonly object _configurationSaveLock = new object(); + public void AddParts(IEnumerable<IConfigurationFactory> factories) + { + _configurationFactories = factories.ToArray(); + + _configurationStores = _configurationFactories + .SelectMany(i => i.GetConfigurations()) + .ToArray(); + } /// <summary> /// Saves the configuration. @@ -103,7 +118,7 @@ namespace MediaBrowser.Common.Implementations.Configuration Directory.CreateDirectory(Path.GetDirectoryName(path)); - lock (_configurationSaveLock) + lock (_configurationSyncLock) { XmlSerializer.SerializeToFile(CommonConfiguration, path); } @@ -144,8 +159,8 @@ namespace MediaBrowser.Common.Implementations.Configuration /// </summary> private void UpdateCachePath() { - ((BaseApplicationPaths)CommonApplicationPaths).CachePath = string.IsNullOrEmpty(CommonConfiguration.CachePath) ? - null : + ((BaseApplicationPaths)CommonApplicationPaths).CachePath = string.IsNullOrEmpty(CommonConfiguration.CachePath) ? + null : CommonConfiguration.CachePath; } @@ -168,5 +183,63 @@ namespace MediaBrowser.Common.Implementations.Configuration } } } + + private readonly ConcurrentDictionary<string, object> _configurations = new ConcurrentDictionary<string, object>(); + + private string GetConfigurationFile(string key) + { + return Path.Combine(CommonApplicationPaths.ConfigurationDirectoryPath, key.ToLower() + ".xml"); + } + + public object GetConfiguration(string key) + { + return _configurations.GetOrAdd(key, k => + { + var file = GetConfigurationFile(key); + + var configurationType = _configurationStores + .First(i => string.Equals(i.Key, key, StringComparison.OrdinalIgnoreCase)) + .ConfigurationType; + + lock (_configurationSyncLock) + { + return ConfigurationHelper.GetXmlConfiguration(configurationType, file, XmlSerializer); + } + }); + } + + public void SaveConfiguration(string key, object configuration) + { + var configurationType = GetConfigurationType(key); + + if (configuration.GetType() != configurationType) + { + throw new ArgumentException("Expected configuration type is " + configurationType.Name); + } + + _configurations.AddOrUpdate(key, configuration, (k, v) => configuration); + + var path = GetConfigurationFile(key); + Directory.CreateDirectory(Path.GetDirectoryName(path)); + + lock (_configurationSyncLock) + { + XmlSerializer.SerializeToFile(configuration, path); + } + + EventHelper.FireEventIfNotNull(NamedConfigurationUpdated, this, new ConfigurationUpdateEventArgs + { + Key = key, + NewConfiguration = configuration + + }, Logger); + } + + public Type GetConfigurationType(string key) + { + return _configurationStores + .First(i => string.Equals(i.Key, key, StringComparison.OrdinalIgnoreCase)) + .ConfigurationType; + } } } diff --git a/MediaBrowser.Common.Implementations/Updates/InstallationManager.cs b/MediaBrowser.Common.Implementations/Updates/InstallationManager.cs index 895c43076..751ff55a5 100644 --- a/MediaBrowser.Common.Implementations/Updates/InstallationManager.cs +++ b/MediaBrowser.Common.Implementations/Updates/InstallationManager.cs @@ -68,7 +68,7 @@ namespace MediaBrowser.Common.Implementations.Updates /// <param name="newVersion">The new version.</param> private void OnPluginUpdated(IPlugin plugin, PackageVersionInfo newVersion) { - _logger.Info("Plugin updated: {0} {1} {2}", newVersion.name, newVersion.version, newVersion.classification); + _logger.Info("Plugin updated: {0} {1} {2}", newVersion.name, newVersion.versionStr ?? string.Empty, newVersion.classification); EventHelper.FireEventIfNotNull(PluginUpdated, this, new GenericEventArgs<Tuple<IPlugin, PackageVersionInfo>> { Argument = new Tuple<IPlugin, PackageVersionInfo>(plugin, newVersion) }, _logger); @@ -87,7 +87,7 @@ namespace MediaBrowser.Common.Implementations.Updates /// <param name="package">The package.</param> private void OnPluginInstalled(PackageVersionInfo package) { - _logger.Info("New plugin installed: {0} {1} {2}", package.name, package.version, package.classification); + _logger.Info("New plugin installed: {0} {1} {2}", package.name, package.versionStr ?? string.Empty, package.classification); EventHelper.FireEventIfNotNull(PluginInstalled, this, new GenericEventArgs<PackageVersionInfo> { Argument = package }, _logger); @@ -133,6 +133,16 @@ namespace MediaBrowser.Common.Implementations.Updates _logger = logger; } + private Version GetPackageVersion(PackageVersionInfo version) + { + return new Version(ValueOrDefault(version.versionStr, "0.0.0.1")); + } + + private static string ValueOrDefault(string str, string def) + { + return string.IsNullOrEmpty(str) ? def : str; + } + /// <summary> /// Gets all available packages. /// </summary> @@ -197,7 +207,7 @@ namespace MediaBrowser.Common.Implementations.Updates foreach (var package in packages) { package.versions = package.versions.Where(v => !string.IsNullOrWhiteSpace(v.sourceUrl)) - .OrderByDescending(v => v.version).ToList(); + .OrderByDescending(GetPackageVersion).ToList(); } // Remove packages with no versions @@ -211,7 +221,7 @@ namespace MediaBrowser.Common.Implementations.Updates foreach (var package in packages) { package.versions = package.versions.Where(v => !string.IsNullOrWhiteSpace(v.sourceUrl)) - .OrderByDescending(v => v.version).ToList(); + .OrderByDescending(GetPackageVersion).ToList(); } if (packageType.HasValue) @@ -272,7 +282,7 @@ namespace MediaBrowser.Common.Implementations.Updates return null; } - return package.versions.FirstOrDefault(v => v.version.Equals(version) && v.classification == classification); + return package.versions.FirstOrDefault(v => GetPackageVersion(v).Equals(version) && v.classification == classification); } /// <summary> @@ -309,7 +319,7 @@ namespace MediaBrowser.Common.Implementations.Updates } return package.versions - .OrderByDescending(v => v.version) + .OrderByDescending(GetPackageVersion) .FirstOrDefault(v => v.classification <= classification && IsPackageVersionUpToDate(v, currentServerVersion)); } @@ -338,7 +348,7 @@ namespace MediaBrowser.Common.Implementations.Updates { var latestPluginInfo = GetLatestCompatibleVersion(catalog, p.Name, p.Id.ToString(), applicationVersion, _config.CommonConfiguration.SystemUpdateLevel); - return latestPluginInfo != null && latestPluginInfo.version != null && latestPluginInfo.version > p.Version ? latestPluginInfo : null; + return latestPluginInfo != null && GetPackageVersion(latestPluginInfo) > p.Version ? latestPluginInfo : null; }).Where(i => i != null).ToList(); |
