1
- using System . IO ;
2
- using MediaBrowser . Common . Configuration ;
1
+ using MediaBrowser . Common . Configuration ;
3
2
using MediaBrowser . Common . Events ;
4
3
using MediaBrowser . Model . Configuration ;
5
4
using MediaBrowser . Model . Logging ;
6
5
using MediaBrowser . Model . Serialization ;
7
6
using System ;
7
+ using System . Collections . Concurrent ;
8
+ using System . Collections . Generic ;
9
+ using System . IO ;
10
+ using System . Linq ;
8
11
using System . Threading ;
9
12
10
13
namespace MediaBrowser . Common . Implementations . Configuration
@@ -25,6 +28,11 @@ public abstract class BaseConfigurationManager : IConfigurationManager
25
28
/// </summary>
26
29
public event EventHandler < EventArgs > ConfigurationUpdated ;
27
30
31
+ /// <summary>
32
+ /// Occurs when [named configuration updated].
33
+ /// </summary>
34
+ public event EventHandler < ConfigurationUpdateEventArgs > NamedConfigurationUpdated ;
35
+
28
36
/// <summary>
29
37
/// Gets the logger.
30
38
/// </summary>
@@ -74,6 +82,9 @@ protected set
74
82
}
75
83
}
76
84
85
+ private ConfigurationStore [ ] _configurationStores = { } ;
86
+ private IConfigurationFactory [ ] _configurationFactories ;
87
+
77
88
/// <summary>
78
89
/// Initializes a new instance of the <see cref="BaseConfigurationManager" /> class.
79
90
/// </summary>
@@ -89,10 +100,14 @@ protected BaseConfigurationManager(IApplicationPaths applicationPaths, ILogManag
89
100
UpdateCachePath ( ) ;
90
101
}
91
102
92
- /// <summary>
93
- /// The _save lock
94
- /// </summary>
95
- private readonly object _configurationSaveLock = new object ( ) ;
103
+ public void AddParts ( IEnumerable < IConfigurationFactory > factories )
104
+ {
105
+ _configurationFactories = factories . ToArray ( ) ;
106
+
107
+ _configurationStores = _configurationFactories
108
+ . SelectMany ( i => i . GetConfigurations ( ) )
109
+ . ToArray ( ) ;
110
+ }
96
111
97
112
/// <summary>
98
113
/// Saves the configuration.
@@ -103,7 +118,7 @@ public void SaveConfiguration()
103
118
104
119
Directory . CreateDirectory ( Path . GetDirectoryName ( path ) ) ;
105
120
106
- lock ( _configurationSaveLock )
121
+ lock ( _configurationSyncLock )
107
122
{
108
123
XmlSerializer . SerializeToFile ( CommonConfiguration , path ) ;
109
124
}
@@ -144,8 +159,8 @@ public virtual void ReplaceConfiguration(BaseApplicationConfiguration newConfigu
144
159
/// </summary>
145
160
private void UpdateCachePath ( )
146
161
{
147
- ( ( BaseApplicationPaths ) CommonApplicationPaths ) . CachePath = string . IsNullOrEmpty ( CommonConfiguration . CachePath ) ?
148
- null :
162
+ ( ( BaseApplicationPaths ) CommonApplicationPaths ) . CachePath = string . IsNullOrEmpty ( CommonConfiguration . CachePath ) ?
163
+ null :
149
164
CommonConfiguration . CachePath ;
150
165
}
151
166
@@ -168,5 +183,63 @@ private void ValidateCachePath(BaseApplicationConfiguration newConfig)
168
183
}
169
184
}
170
185
}
186
+
187
+ private readonly ConcurrentDictionary < string , object > _configurations = new ConcurrentDictionary < string , object > ( ) ;
188
+
189
+ private string GetConfigurationFile ( string key )
190
+ {
191
+ return Path . Combine ( CommonApplicationPaths . ConfigurationDirectoryPath , key . ToLower ( ) + ".xml" ) ;
192
+ }
193
+
194
+ public object GetConfiguration ( string key )
195
+ {
196
+ return _configurations . GetOrAdd ( key , k =>
197
+ {
198
+ var file = GetConfigurationFile ( key ) ;
199
+
200
+ var configurationType = _configurationStores
201
+ . First ( i => string . Equals ( i . Key , key , StringComparison . OrdinalIgnoreCase ) )
202
+ . ConfigurationType ;
203
+
204
+ lock ( _configurationSyncLock )
205
+ {
206
+ return ConfigurationHelper . GetXmlConfiguration ( configurationType , file , XmlSerializer ) ;
207
+ }
208
+ } ) ;
209
+ }
210
+
211
+ public void SaveConfiguration ( string key , object configuration )
212
+ {
213
+ var configurationType = GetConfigurationType ( key ) ;
214
+
215
+ if ( configuration . GetType ( ) != configurationType )
216
+ {
217
+ throw new ArgumentException ( "Expected configuration type is " + configurationType . Name ) ;
218
+ }
219
+
220
+ _configurations . AddOrUpdate ( key , configuration , ( k , v ) => configuration ) ;
221
+
222
+ var path = GetConfigurationFile ( key ) ;
223
+ Directory . CreateDirectory ( Path . GetDirectoryName ( path ) ) ;
224
+
225
+ lock ( _configurationSyncLock )
226
+ {
227
+ XmlSerializer . SerializeToFile ( configuration , path ) ;
228
+ }
229
+
230
+ EventHelper . FireEventIfNotNull ( NamedConfigurationUpdated , this , new ConfigurationUpdateEventArgs
231
+ {
232
+ Key = key ,
233
+ NewConfiguration = configuration
234
+
235
+ } , Logger ) ;
236
+ }
237
+
238
+ public Type GetConfigurationType ( string key )
239
+ {
240
+ return _configurationStores
241
+ . First ( i => string . Equals ( i . Key , key , StringComparison . OrdinalIgnoreCase ) )
242
+ . ConfigurationType ;
243
+ }
171
244
}
172
245
}
0 commit comments