DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
ConfigurationExtensions.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Collections.Generic;
19 using System.Globalization;
20 using System.IO;
21 using System.Linq;
22 
23 namespace Deveel.Data.Configuration {
24  public static class ConfigurationExtensions {
25  #region Get Values
26 
27  public static IEnumerable<string> GetKeys(this IConfiguration config) {
28  return config.GetKeys(ConfigurationLevel.Current);
29  }
30 
31  public static IEnumerable<object> GetValues(this IConfiguration config, ConfigurationLevel level) {
32  var keys = config.GetKeys(level);
33  var values = keys.Select(x => config.GetValue(x))
34  .Where(value => value != null)
35  .ToList();
36 
37  return values.ToArray();
38  }
39 
40  #region GetValue(string)
41 
42  public static object GetValue(this IConfiguration config, string keyName) {
43  return GetValue(config, keyName, null);
44  }
45 
46  public static object GetValue(this IConfiguration config, string keyName, object defaultValue) {
47  var value = config.GetValue(keyName);
48  if (value == null)
49  return defaultValue;
50 
51  return value;
52  }
53 
54  public static T GetValue<T>(this IConfiguration config, string keyName) {
55  return GetValue<T>(config, keyName, default(T));
56  }
57 
58  private static T ToType<T>(object value) {
59  if (value == null)
60  return default(T);
61 
62  if (value is T)
63  return (T)value;
64 
65  if (typeof(T) == typeof(bool) &&
66  value is string)
67  return (T)ConvertToBoolean((string)value);
68 
69  if (typeof(T).IsEnum)
70  return ConvertToEnum<T>(value);
71 
72  if (!(value is IConvertible))
73  throw new InvalidCastException();
74 
75  return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
76  }
77 
78  private static T ConvertToEnum<T>(object value) {
79  if (value is int ||
80  value is short ||
81  value is long ||
82  value is byte)
83  return (T)value;
84 
85  if (value == null)
86  return default(T);
87 
88  var s = value.ToString();
89  return (T)Enum.Parse(typeof(T), s, true);
90  }
91 
92  private static object ConvertToBoolean(string value) {
93  if (String.Equals(value, "true", StringComparison.OrdinalIgnoreCase) ||
94  String.Equals(value, "enabled", StringComparison.OrdinalIgnoreCase) ||
95  String.Equals(value, "1") ||
96  String.Equals(value, "on", StringComparison.OrdinalIgnoreCase))
97  return true;
98  if (String.Equals(value, "false", StringComparison.OrdinalIgnoreCase) ||
99  String.Equals(value, "disabled", StringComparison.OrdinalIgnoreCase) ||
100  String.Equals(value, "0") ||
101  String.Equals(value, "off"))
102  return false;
103 
104  throw new InvalidCastException();
105  }
106 
107  public static T GetValue<T>(this IConfiguration config, string keyName, T defaultValue) {
108  var value = config.GetValue(keyName);
109  if (value == null)
110  return defaultValue;
111 
112  return ToType<T>(value);
113  }
114 
115  public static string GetString(this IConfiguration config, string propertyKey) {
116  return GetString(config, propertyKey, null);
117  }
118 
119  public static string GetString(this IConfiguration config, string propertyKey, string defaultValue) {
120  return config.GetValue(propertyKey, defaultValue);
121  }
122 
123  public static byte GetByte(this IConfiguration config, string propertyKey) {
124  return GetByte(config, propertyKey, 0);
125  }
126 
127  public static byte GetByte(this IConfiguration config, string propertyKey, byte defaultValue) {
128  return config.GetValue(propertyKey, defaultValue);
129  }
130 
131  [CLSCompliant(false)]
132  public static sbyte GetSByte(this IConfiguration config, string propertyKey, sbyte defaultValue) {
133  return config.GetValue(propertyKey, defaultValue);
134  }
135 
136  public static short GetInt16(this IConfiguration config, string propertyKey) {
137  return GetInt16(config, propertyKey, 0);
138  }
139 
140  public static short GetInt16(this IConfiguration config, string propertyKey, short defaultValue) {
141  return config.GetValue<short>(propertyKey, defaultValue);
142  }
143 
144  [CLSCompliant(false)]
145  public static ushort GetUInt16(this IConfiguration config, string propertyKey) {
146  return GetUInt16(config, propertyKey, 0);
147  }
148 
149  [CLSCompliant(false)]
150  public static ushort GetUInt16(this IConfiguration config, string propertyKey, ushort defaultValue) {
151  return config.GetValue(propertyKey, defaultValue);
152  }
153 
154  public static int GetInt32(this IConfiguration config, string propertyKey) {
155  return GetInt32(config, propertyKey, 0);
156  }
157 
158  public static int GetInt32(this IConfiguration config, string propertyKey, int defaultValue) {
159  return config.GetValue(propertyKey, defaultValue);
160  }
161 
162  [CLSCompliant(false)]
163  public static uint GetUInt32(this IConfiguration config, string propertyKey) {
164  return GetUInt32(config, propertyKey, 0);
165  }
166 
167  [CLSCompliant(false)]
168  public static uint GetUInt32(this IConfiguration config, string propertyKey, uint defaultValue) {
169  return config.GetValue(propertyKey, defaultValue);
170  }
171 
172  public static long GetInt64(this IConfiguration config, string propertyKey) {
173  return GetInt64(config, propertyKey, 0);
174  }
175 
176  public static long GetInt64(this IConfiguration config, string propertyKey, long defaultValue) {
177  return config.GetValue(propertyKey, defaultValue);
178  }
179 
180  [CLSCompliant(false)]
181  public static ulong GetUInt64(this IConfiguration config, string propertyKey) {
182  return GetUInt64(config, propertyKey, 0);
183  }
184 
185  [CLSCompliant(false)]
186  public static ulong GetUInt64(this IConfiguration config, string propertyKey, ulong defaultValue) {
187  return config.GetValue(propertyKey, defaultValue);
188  }
189 
190  public static bool GetBoolean(this IConfiguration config, string propertyKey) {
191  return GetBoolean(config, propertyKey, false);
192  }
193 
194  public static bool GetBoolean(this IConfiguration config, string propertyKey, bool defaultValue) {
195  return config.GetValue(propertyKey, defaultValue);
196  }
197 
198  public static float GetSingle(this IConfiguration config, string propertyKey) {
199  return GetSingle(config, propertyKey, 0);
200  }
201 
202  public static float GetSingle(this IConfiguration config, string propertyKey, float defaultValue) {
203  return config.GetValue(propertyKey, defaultValue);
204  }
205 
206  public static double GetDouble(this IConfiguration config, string propertyKey) {
207  return GetDouble(config, propertyKey, 0);
208  }
209 
210  public static double GetDouble(this IConfiguration config, string propertyKey, double defaultValue) {
211  return config.GetValue<double>(propertyKey, defaultValue);
212  }
213 
214  #endregion
215 
216  #endregion
217 
218  #region Load / Save
219 
220  public static void Load(this IConfiguration config, IConfigSource source) {
221  config.Load(source, new PropertiesConfigFormatter());
222  }
223 
224  public static void Load(this IConfiguration config, IConfigFormatter formatter) {
225  if (config.Source == null)
226  throw new InvalidOperationException("Source was not configured");
227 
228  config.Load(config.Source, formatter);
229  }
230 
231  public static void Load(this IConfiguration config, IConfigSource source, IConfigFormatter formatter) {
232  try {
233  if (source != null) {
234  using (var sourceStream = source.InputStream) {
235  if (!sourceStream.CanRead)
236  throw new ArgumentException("The input stream cannot be read.");
237 
238  sourceStream.Seek(0, SeekOrigin.Begin);
239  formatter.LoadInto(config, sourceStream);
240  }
241  }
242  } catch (Exception ex) {
243  throw new DatabaseConfigurationException(String.Format("Cannot load data from source"), ex);
244  }
245  }
246 
247 #if !PCL
248  public static void Load(this IConfiguration config, string fileName, IConfigFormatter formatter) {
249  config.Load(new FileConfigSource(fileName), formatter);
250  }
251 
252  public static void Load(this IConfiguration config, string fileName) {
253  config.Load(fileName, new PropertiesConfigFormatter());
254  }
255 #endif
256 
257  public static void Load(this IConfiguration config, Stream inputStream, IConfigFormatter formatter) {
258  config.Load(new StreamConfigSource(inputStream), formatter);
259  }
260 
261  public static void Load(this IConfiguration config, Stream inputStream) {
262  config.Load(inputStream, new PropertiesConfigFormatter());
263  }
264 
265  public static void Save(this IConfiguration config, IConfigSource source, IConfigFormatter formatter) {
266  Save(config, source, ConfigurationLevel.Current, formatter);
267  }
268 
269  public static void Save(this IConfiguration config, IConfigSource source, ConfigurationLevel level, IConfigFormatter formatter) {
270  try {
271  using (var outputStream = source.OutputStream) {
272  if (!outputStream.CanWrite)
273  throw new InvalidOperationException("The destination source cannot be written.");
274 
275  outputStream.Seek(0, SeekOrigin.Begin);
276  formatter.SaveFrom(config, level, outputStream);
277  outputStream.Flush();
278  }
279  } catch (Exception ex) {
280  throw new DatabaseConfigurationException("Cannot save the configuration.", ex);
281  }
282  }
283 
284  public static void Save(this IConfiguration config, IConfigFormatter formatter) {
285  Save(config, ConfigurationLevel.Current, formatter);
286  }
287 
288  public static void Save(this IConfiguration config, ConfigurationLevel level, IConfigFormatter formatter) {
289  if (config.Source == null)
290  throw new DatabaseConfigurationException("The source was not configured in the configuration.");
291 
292  config.Save(config.Source, level, formatter);
293  }
294 
295  public static void Save(this IConfiguration config) {
296  Save(config, ConfigurationLevel.Current);
297  }
298 
299  public static void Save(this IConfiguration config, ConfigurationLevel level) {
300  Save(config, level, new PropertiesConfigFormatter());
301  }
302 
303 #if !PCL
304  public static void Save(this IConfiguration config, string fileName) {
305  Save(config, ConfigurationLevel.Current, fileName);
306  }
307 
308  public static void Save(this IConfiguration config, ConfigurationLevel level, string fileName) {
309  Save(config, level, fileName, new PropertiesConfigFormatter());
310  }
311 
312  public static void Save(this IConfiguration config, string fileName, IConfigFormatter formatter) {
313  Save(config, ConfigurationLevel.Current, fileName, formatter);
314  }
315  public static void Save(this IConfiguration config, ConfigurationLevel level, string fileName, IConfigFormatter formatter) {
316  config.Save(new FileConfigSource(fileName), level, formatter);
317  }
318 #endif
319 
320  public static void Save(this IConfiguration config, Stream outputStream) {
321  Save(config, ConfigurationLevel.Current, outputStream);
322  }
323 
324  public static void Save(this IConfiguration config, ConfigurationLevel level, Stream outputStream) {
325  Save(config, level, outputStream, new PropertiesConfigFormatter());
326  }
327 
328  public static void Save(this IConfiguration config, Stream outputStream, IConfigFormatter formatter) {
329  Save(config, ConfigurationLevel.Current, outputStream, formatter);
330  }
331 
332  public static void Save(this IConfiguration config, ConfigurationLevel level, Stream outputStream, IConfigFormatter formatter) {
333  config.Save(new StreamConfigSource(outputStream), level, formatter);
334  }
335 
336  #endregion
337 
338  public static IConfiguration MergeWith(this IConfiguration configuration, IConfiguration other) {
339  var newConfig = new Configuration(configuration);
340  foreach (var pair in other) {
341  newConfig.SetValue(pair.Key, pair.Value);
342  }
343 
344  return newConfig;
345  }
346  }
347 }
static short GetInt16(this IConfiguration config, string propertyKey)
static void Load(this IConfiguration config, string fileName)
object GetValue(string key)
Gets a configuration setting for the given key.
static int GetInt32(this IConfiguration config, string propertyKey)
A source for stored configurations or destination to configurations to store.
static void Load(this IConfiguration config, IConfigSource source, IConfigFormatter formatter)
static short GetInt16(this IConfiguration config, string propertyKey, short defaultValue)
static void Load(this IConfiguration config, string fileName, IConfigFormatter formatter)
void LoadInto(IConfiguration config, Stream inputStream)
Loads a stored configuration from the given stream into the configuration argument.
static IEnumerable< string > GetKeys(this IConfiguration config)
ConfigurationLevel
Defines the level of configuration settings to save or read
Stream InputStream
Gets a Stream that is used to load the configurations.
static byte GetByte(this IConfiguration config, string propertyKey, byte defaultValue)
An implementation of IConfigSource that handles a single Stream as source and destination of the conf...
static bool GetBoolean(this IConfiguration config, string propertyKey, bool defaultValue)
static void Save(this IConfiguration config, ConfigurationLevel level, Stream outputStream, IConfigFormatter formatter)
static float GetSingle(this IConfiguration config, string propertyKey)
static void Load(this IConfiguration config, Stream inputStream, IConfigFormatter formatter)
static ushort GetUInt16(this IConfiguration config, string propertyKey, ushort defaultValue)
static void Save(this IConfiguration config, Stream outputStream, IConfigFormatter formatter)
static void Save(this IConfiguration config, ConfigurationLevel level, string fileName, IConfigFormatter formatter)
static uint GetUInt32(this IConfiguration config, string propertyKey)
static long GetInt64(this IConfiguration config, string propertyKey, long defaultValue)
IConfigSource Source
Gets or sets an optional source of the configuration object
static string GetString(this IConfiguration config, string propertyKey)
static void Save(this IConfiguration config, ConfigurationLevel level)
static void Load(this IConfiguration config, IConfigSource source)
static void Save(this IConfiguration config, Stream outputStream)
static void Save(this IConfiguration config, string fileName, IConfigFormatter formatter)
static int GetInt32(this IConfiguration config, string propertyKey, int defaultValue)
static void Save(this IConfiguration config, ConfigurationLevel level, string fileName)
void SaveFrom(IConfiguration config, ConfigurationLevel level, Stream outputStream)
Stores the given level of configurations into the output stream provided, in the format handled by th...
static void Save(this IConfiguration config, IConfigSource source, ConfigurationLevel level, IConfigFormatter formatter)
static long GetInt64(this IConfiguration config, string propertyKey)
static byte GetByte(this IConfiguration config, string propertyKey)
static void Save(this IConfiguration config, ConfigurationLevel level, IConfigFormatter formatter)
Stream OutputStream
Gets a Stream that can be writtern with the configurations from a IConfiguration. ...
static ulong GetUInt64(this IConfiguration config, string propertyKey)
static void Load(this IConfiguration config, IConfigFormatter formatter)
static uint GetUInt32(this IConfiguration config, string propertyKey, uint defaultValue)
static double GetDouble(this IConfiguration config, string propertyKey)
static void Load(this IConfiguration config, Stream inputStream)
Defines the contract for the configuration node of a component within the system or of the system its...
static IConfiguration MergeWith(this IConfiguration configuration, IConfiguration other)
static ulong GetUInt64(this IConfiguration config, string propertyKey, ulong defaultValue)
Provides the format to load and store configurations in and from a stream.
A channel used to read from and write to a given file in the underlying file-system.
static sbyte GetSByte(this IConfiguration config, string propertyKey, sbyte defaultValue)
static string GetString(this IConfiguration config, string propertyKey, string defaultValue)
static void Save(this IConfiguration config, ConfigurationLevel level, Stream outputStream)
static double GetDouble(this IConfiguration config, string propertyKey, double defaultValue)
static IEnumerable< object > GetValues(this IConfiguration config, ConfigurationLevel level)
static object GetValue(this IConfiguration config, string keyName)
static object GetValue(this IConfiguration config, string keyName, object defaultValue)
static void Save(this IConfiguration config, IConfigFormatter formatter)
static void Save(this IConfiguration config, string fileName)
static void Save(this IConfiguration config)
static void Save(this IConfiguration config, IConfigSource source, IConfigFormatter formatter)
static ushort GetUInt16(this IConfiguration config, string propertyKey)
IEnumerable< string > GetKeys(ConfigurationLevel level)
Enumerates the keys that can be obtained by the object, at the given ConfigurationLevel.
static bool GetBoolean(this IConfiguration config, string propertyKey)
static float GetSingle(this IConfiguration config, string propertyKey, float defaultValue)