BaseConfigurationManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System.IO;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Common.Events;
  4. using MediaBrowser.Model.Configuration;
  5. using MediaBrowser.Model.Logging;
  6. using MediaBrowser.Model.Serialization;
  7. using System;
  8. using System.Threading;
  9. namespace MediaBrowser.Common.Implementations.Configuration
  10. {
  11. /// <summary>
  12. /// Class BaseConfigurationManager
  13. /// </summary>
  14. public abstract class BaseConfigurationManager : IConfigurationManager
  15. {
  16. /// <summary>
  17. /// Gets the type of the configuration.
  18. /// </summary>
  19. /// <value>The type of the configuration.</value>
  20. protected abstract Type ConfigurationType { get; }
  21. /// <summary>
  22. /// Occurs when [configuration updated].
  23. /// </summary>
  24. public event EventHandler<EventArgs> ConfigurationUpdated;
  25. /// <summary>
  26. /// Gets the logger.
  27. /// </summary>
  28. /// <value>The logger.</value>
  29. protected ILogger Logger { get; private set; }
  30. /// <summary>
  31. /// Gets the XML serializer.
  32. /// </summary>
  33. /// <value>The XML serializer.</value>
  34. protected IXmlSerializer XmlSerializer { get; private set; }
  35. /// <summary>
  36. /// Gets or sets the application paths.
  37. /// </summary>
  38. /// <value>The application paths.</value>
  39. public IApplicationPaths CommonApplicationPaths { get; private set; }
  40. /// <summary>
  41. /// The _configuration loaded
  42. /// </summary>
  43. private bool _configurationLoaded;
  44. /// <summary>
  45. /// The _configuration sync lock
  46. /// </summary>
  47. private object _configurationSyncLock = new object();
  48. /// <summary>
  49. /// The _configuration
  50. /// </summary>
  51. private BaseApplicationConfiguration _configuration;
  52. /// <summary>
  53. /// Gets the system configuration
  54. /// </summary>
  55. /// <value>The configuration.</value>
  56. public BaseApplicationConfiguration CommonConfiguration
  57. {
  58. get
  59. {
  60. // Lazy load
  61. LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationLoaded, ref _configurationSyncLock, () => (BaseApplicationConfiguration)ConfigurationHelper.GetXmlConfiguration(ConfigurationType, CommonApplicationPaths.SystemConfigurationFilePath, XmlSerializer));
  62. return _configuration;
  63. }
  64. protected set
  65. {
  66. _configuration = value;
  67. _configurationLoaded = value != null;
  68. }
  69. }
  70. /// <summary>
  71. /// Initializes a new instance of the <see cref="BaseConfigurationManager" /> class.
  72. /// </summary>
  73. /// <param name="applicationPaths">The application paths.</param>
  74. /// <param name="logManager">The log manager.</param>
  75. /// <param name="xmlSerializer">The XML serializer.</param>
  76. protected BaseConfigurationManager(IApplicationPaths applicationPaths, ILogManager logManager, IXmlSerializer xmlSerializer)
  77. {
  78. CommonApplicationPaths = applicationPaths;
  79. XmlSerializer = xmlSerializer;
  80. Logger = logManager.GetLogger(GetType().Name);
  81. UpdateCachePath();
  82. }
  83. /// <summary>
  84. /// The _save lock
  85. /// </summary>
  86. private readonly object _configurationSaveLock = new object();
  87. /// <summary>
  88. /// Saves the configuration.
  89. /// </summary>
  90. public void SaveConfiguration()
  91. {
  92. var path = CommonApplicationPaths.SystemConfigurationFilePath;
  93. Directory.CreateDirectory(Path.GetDirectoryName(path));
  94. lock (_configurationSaveLock)
  95. {
  96. XmlSerializer.SerializeToFile(CommonConfiguration, path);
  97. }
  98. OnConfigurationUpdated();
  99. }
  100. /// <summary>
  101. /// Called when [configuration updated].
  102. /// </summary>
  103. protected virtual void OnConfigurationUpdated()
  104. {
  105. UpdateCachePath();
  106. EventHelper.QueueEventIfNotNull(ConfigurationUpdated, this, EventArgs.Empty, Logger);
  107. }
  108. /// <summary>
  109. /// Replaces the configuration.
  110. /// </summary>
  111. /// <param name="newConfiguration">The new configuration.</param>
  112. /// <exception cref="System.ArgumentNullException">newConfiguration</exception>
  113. public virtual void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  114. {
  115. if (newConfiguration == null)
  116. {
  117. throw new ArgumentNullException("newConfiguration");
  118. }
  119. ValidateCachePath(newConfiguration);
  120. CommonConfiguration = newConfiguration;
  121. SaveConfiguration();
  122. }
  123. /// <summary>
  124. /// Updates the items by name path.
  125. /// </summary>
  126. private void UpdateCachePath()
  127. {
  128. ((BaseApplicationPaths)CommonApplicationPaths).CachePath = string.IsNullOrEmpty(CommonConfiguration.CachePath) ?
  129. null :
  130. CommonConfiguration.CachePath;
  131. }
  132. /// <summary>
  133. /// Replaces the cache path.
  134. /// </summary>
  135. /// <param name="newConfig">The new configuration.</param>
  136. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  137. private void ValidateCachePath(BaseApplicationConfiguration newConfig)
  138. {
  139. var newPath = newConfig.CachePath;
  140. if (!string.IsNullOrWhiteSpace(newPath)
  141. && !string.Equals(CommonConfiguration.CachePath ?? string.Empty, newPath))
  142. {
  143. // Validate
  144. if (!Directory.Exists(newPath))
  145. {
  146. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  147. }
  148. }
  149. }
  150. }
  151. }