You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Config/Configuration.cs

409 lines
14 KiB

6 years ago
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using ZeroLevel.Services.Config;
using ZeroLevel.Services.Config.Implementation;
5 years ago
using ZeroLevel.Services.Reflection;
6 years ago
using ZeroLevel.Services.Serialization;
namespace ZeroLevel
{
public static class Configuration
{
/// <summary>
/// Application folder path
6 years ago
/// </summary>
5 years ago
public static readonly string BaseDirectory;
6 years ago
5 years ago
public static readonly string AppLocation;
6 years ago
public const string DEFAULT_SECTION_NAME = "_defaultsection";
#region Ctor
static Configuration()
{
_empty = new BaseConfiguration();
_emptySet = new BaseConfigurationSet();
_empty.Freeze(true);
_emptySet.FreezeConfiguration(true);
6 years ago
DefaultSet = Configuration.CreateSet();
5 years ago
var assembly = EntryAssemblyAttribute.GetEntryAssembly();
if (assembly != null)
{
BaseDirectory = Path.GetDirectoryName(assembly.Location);
AppLocation = assembly.Location;
}
5 years ago
else
{
BaseDirectory = Directory.GetCurrentDirectory();
}
6 years ago
}
#endregion Ctor
6 years ago
#region Cachee
6 years ago
private static readonly IConfiguration _empty;
private static readonly IConfigurationSet _emptySet;
private static readonly ConcurrentDictionary<string, IConfiguration> _cachee = new ConcurrentDictionary<string, IConfiguration>();
private static readonly ConcurrentDictionary<string, IConfigurationSet> _setCachee = new ConcurrentDictionary<string, IConfigurationSet>();
public static IConfiguration Empty { get { return _empty; } }
public static IConfigurationSet EmptySet { get { return _emptySet; } }
public static IConfiguration Default => DefaultSet?.Default;
6 years ago
public static IConfigurationSet DefaultSet { get; private set; }
public static void Save(string name, IConfiguration configuration)
{
_cachee.AddOrUpdate(name, configuration, (oldKey, oldValue) => configuration);
}
public static void Save(IConfiguration configuration)
{
if (DefaultSet == null)
6 years ago
{
DefaultSet = Configuration.CreateSet(configuration);
6 years ago
}
else
{
DefaultSet.Merge(Configuration.CreateSet(configuration));
6 years ago
}
}
public static void Save(string name, IConfigurationSet configurationSet)
{
_setCachee.AddOrUpdate(name, configurationSet, (oldKey, oldValue) => configurationSet);
}
public static void Save(IConfigurationSet configuration)
{
if (DefaultSet == null)
{
DefaultSet = configuration;
}
else
{
DefaultSet.Merge(configuration);
6 years ago
}
}
public static IConfiguration Get(string name)
{
IConfiguration result;
if (false == _cachee.TryGetValue(name, out result))
{
6 years ago
throw new KeyNotFoundException("Not found configuration '{name}'");
6 years ago
}
return result;
}
public static IConfigurationSet GetSet(string name)
{
IConfigurationSet result;
if (false == _setCachee.TryGetValue(name, out result))
{
6 years ago
throw new KeyNotFoundException("Not found configuration set '{name}'");
6 years ago
}
return result;
}
#endregion Cachee
6 years ago
#region Factory
6 years ago
public static IConfiguration Create()
{
return new BaseConfiguration();
}
public static IConfigurationSet CreateSet()
{
return new BaseConfigurationSet();
}
public static IConfigurationSet CreateSet(IConfiguration defaultConfiguration)
{
return new BaseConfigurationSet(defaultConfiguration);
}
#endregion Factory
6 years ago
#region Read configuration
/// <summary>
6 years ago
/// Creating a configuration from the AppSettings section of the app.config or web.config file
6 years ago
/// </summary>
6 years ago
/// <returns>Configuration</returns>
5 years ago
public static IConfiguration ReadFromApplicationConfig()
{
try
{
return new ApplicationConfigReader().ReadConfiguration();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadFromApplicationConfig] Can't read app.config file");
throw;
}
}
public static IConfiguration ReadOrEmptyFromApplicationConfig()
{
try
{
return new ApplicationConfigReader().ReadConfiguration();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadOrEmptyFromApplicationConfig] Can't read app.config file");
}
return _empty;
}
6 years ago
/// <summary>
6 years ago
/// Creating a configuration from the AppSettings section of the app.config file or web.config, is supplemented by the 'ConnectionStrings' section
6 years ago
/// </summary>
6 years ago
/// <returns>Configuration</returns>
5 years ago
public static IConfigurationSet ReadSetFromApplicationConfig()
{
try
{
return new ApplicationConfigReader().ReadConfigurationSet();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadSetFromApplicationConfig] Can't read app.config file");
throw;
}
}
public static IConfigurationSet ReadOrEmptySetFromApplicationConfig()
{
try
{
return new ApplicationConfigReader().ReadConfigurationSet();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadOrEmptySetFromApplicationConfig] Can't read app.config file");
}
return _emptySet;
}
6 years ago
/// <summary>
6 years ago
/// Creating a configuration from the AppSettings section of the app.config or web.config file
6 years ago
/// </summary>
6 years ago
/// <returns>Configuration</returns>
5 years ago
public static IConfiguration ReadFromApplicationConfig(string configFilePath)
{
try
{
return new ApplicationConfigReader(configFilePath).ReadConfiguration();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadFromApplicationConfig] Can't read config file '{configFilePath}'");
throw;
}
}
public static IConfiguration ReadOrEmptyFromApplicationConfig(string configFilePath)
{
try
{
return new ApplicationConfigReader(configFilePath).ReadConfiguration();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadOrEmptyFromApplicationConfig] Can't read config file '{configFilePath}'");
}
return _empty;
}
6 years ago
/// <summary>
6 years ago
/// Creating a configuration from the AppSettings section of the app.config file or web.config, is supplemented by the 'ConnectionStrings' section
6 years ago
/// </summary>
6 years ago
/// <returns>Configuration</returns>
5 years ago
public static IConfigurationSet ReadSetFromApplicationConfig(string configFilePath)
{
try
{
return new ApplicationConfigReader(configFilePath).ReadConfigurationSet();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadSetFromApplicationConfig] Can't read config file '{configFilePath}'");
throw;
}
}
public static IConfigurationSet ReadOrEmptySetFromApplicationConfig(string configFilePath)
{
try
{
return new ApplicationConfigReader(configFilePath).ReadConfigurationSet();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadOrEmptySetFromApplicationConfig] Can't read config file '{configFilePath}'");
}
return _emptySet;
}
6 years ago
/// <summary>
6 years ago
/// Create configuration from ini file
6 years ago
/// </summary>
6 years ago
/// <param name="path">Path to the ini file</param>
/// <returns>Configuration</returns>
5 years ago
public static IConfiguration ReadFromIniFile(string path)
{
try
{
return new IniFileReader(path).ReadConfiguration();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadFromIniFile] Can't read config file '{path}'");
throw;
}
}
public static IConfiguration ReadOrEmptyFromIniFile(string path)
{
try
{
return new IniFileReader(path).ReadConfiguration();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadOrEmptyFromIniFile] Can't read config file '{path}'");
}
return _empty;
}
6 years ago
/// <summary>
6 years ago
/// Creating a configuration from an ini file, including sections
6 years ago
/// </summary>
6 years ago
/// <param name="path">Path to the ini file</param>
/// <returns>Configuration</returns>
5 years ago
public static IConfigurationSet ReadSetFromIniFile(string path)
{
try
{
return new IniFileReader(path).ReadConfigurationSet();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadSetFromIniFile] Can't read config file '{path}'");
throw;
}
}
public static IConfigurationSet ReadOrEmptySetFromIniFile(string path)
{
try
{
return new IniFileReader(path).ReadConfigurationSet();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadOrEmptySetFromIniFile] Can't read config file '{path}'");
}
return _emptySet;
}
6 years ago
/// <summary>
6 years ago
/// Creating configuration from command line parameters
6 years ago
/// </summary>
6 years ago
/// <param name="args">Command line parameters</param>
/// <returns>Configuration</returns>
5 years ago
public static IConfiguration ReadFromCommandLine(string[] args)
{
try
{
return new CommandLineReader(args).ReadConfiguration();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadFromCommandLine] Can't read command line args");
throw;
}
}
public static IConfiguration ReadOrEmptyFromCommandLine(string[] args)
{
try
{
return new CommandLineReader(args).ReadConfiguration();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadOrEmptyFromCommandLine] Can't read command line args");
}
return _empty;
}
6 years ago
5 years ago
public static IConfiguration ReadFromBinaryReader(IBinaryReader reader)
{
5 years ago
try
{
return reader.Read<BaseConfiguration>();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadFromBinaryReader] Can't read config from binaryReader");
throw;
}
}
public static IConfiguration ReadOrEmptyFromBinaryReader(IBinaryReader reader)
{
try
{
return reader.Read<BaseConfiguration>();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadOrEmptyFromBinaryReader] Can't read config from binaryReader");
}
return _empty;
}
5 years ago
public static IConfigurationSet ReadSetFromBinaryReader(IBinaryReader reader)
{
try
{
return reader.Read<BaseConfigurationSet>();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadSetFromBinaryReader] Can't read config from binaryReader");
throw;
}
}
public static IConfigurationSet ReadSetOrEmptyFromBinaryReader(IBinaryReader reader)
{
try
{
return reader.Read<BaseConfigurationSet>();
}
catch (Exception ex)
{
Log.Error(ex, $"[Configuration.ReadSetOrEmptyFromBinaryReader] Can't read config from binaryReader");
}
return _emptySet;
}
#endregion Read configuration
6 years ago
#region Write configuration
6 years ago
/// <summary>
6 years ago
/// Write a simple configuration to the ini file
6 years ago
/// </summary>
6 years ago
/// <param name="configuration">Configuration</param>
/// <param name="path">Path to the ini file</param>
6 years ago
public static void WriteToIniFile(IConfiguration configuration, string path) { new IniFileWriter(path).WriteConfiguration(configuration); }
6 years ago
/// <summary>
6 years ago
/// Write the complete configuration to the ini-file
6 years ago
/// </summary>
6 years ago
/// <param name="configuration">Configuration</param>
/// <param name="path">Path to the ini file</param>
6 years ago
public static void WriteSetToIniFile(IConfigurationSet configuration, string path) { new IniFileWriter(path).WriteConfigurationSet(configuration); }
#endregion Write configuration
6 years ago
}
}

Powered by TurnKey Linux.