ServerConfigurationManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Implementations.Configuration;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Model.Configuration;
  6. using MediaBrowser.Model.Logging;
  7. using MediaBrowser.Model.Serialization;
  8. using System;
  9. using System.IO;
  10. namespace MediaBrowser.Server.Implementations.Configuration
  11. {
  12. /// <summary>
  13. /// Class ServerConfigurationManager
  14. /// </summary>
  15. public class ServerConfigurationManager : BaseConfigurationManager, IServerConfigurationManager
  16. {
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="ServerConfigurationManager" /> class.
  19. /// </summary>
  20. /// <param name="applicationPaths">The application paths.</param>
  21. /// <param name="logManager">The log manager.</param>
  22. /// <param name="xmlSerializer">The XML serializer.</param>
  23. public ServerConfigurationManager(IApplicationPaths applicationPaths, ILogManager logManager, IXmlSerializer xmlSerializer)
  24. : base(applicationPaths, logManager, xmlSerializer)
  25. {
  26. UpdateItemsByNamePath();
  27. }
  28. /// <summary>
  29. /// Gets the type of the configuration.
  30. /// </summary>
  31. /// <value>The type of the configuration.</value>
  32. protected override Type ConfigurationType
  33. {
  34. get { return typeof(ServerConfiguration); }
  35. }
  36. /// <summary>
  37. /// Gets the application paths.
  38. /// </summary>
  39. /// <value>The application paths.</value>
  40. public IServerApplicationPaths ApplicationPaths
  41. {
  42. get { return (IServerApplicationPaths)CommonApplicationPaths; }
  43. }
  44. /// <summary>
  45. /// Gets the configuration.
  46. /// </summary>
  47. /// <value>The configuration.</value>
  48. public ServerConfiguration Configuration
  49. {
  50. get { return (ServerConfiguration)CommonConfiguration; }
  51. }
  52. /// <summary>
  53. /// Called when [configuration updated].
  54. /// </summary>
  55. protected override void OnConfigurationUpdated()
  56. {
  57. UpdateItemsByNamePath();
  58. base.OnConfigurationUpdated();
  59. }
  60. /// <summary>
  61. /// Updates the items by name path.
  62. /// </summary>
  63. private void UpdateItemsByNamePath()
  64. {
  65. ((ServerApplicationPaths) ApplicationPaths).ItemsByNamePath = string.IsNullOrEmpty(Configuration.ItemsByNamePath) ?
  66. null :
  67. Configuration.ItemsByNamePath;
  68. }
  69. /// <summary>
  70. /// Replaces the configuration.
  71. /// </summary>
  72. /// <param name="newConfiguration">The new configuration.</param>
  73. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  74. public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration)
  75. {
  76. var newConfig = (ServerConfiguration) newConfiguration;
  77. ValidateItemByNamePath(newConfig);
  78. base.ReplaceConfiguration(newConfiguration);
  79. }
  80. /// <summary>
  81. /// Replaces the item by name path.
  82. /// </summary>
  83. /// <param name="newConfig">The new configuration.</param>
  84. /// <exception cref="System.IO.DirectoryNotFoundException"></exception>
  85. private void ValidateItemByNamePath(ServerConfiguration newConfig)
  86. {
  87. var newPath = newConfig.ItemsByNamePath;
  88. if (!string.IsNullOrWhiteSpace(newPath)
  89. && !string.Equals(Configuration.ItemsByNamePath ?? string.Empty, newPath))
  90. {
  91. // Validate
  92. if (!Directory.Exists(newPath))
  93. {
  94. throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
  95. }
  96. }
  97. }
  98. }
  99. }