Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkech committed Oct 9, 2023
1 parent 16f93c2 commit eb65593
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,14 @@ public class ShellConfiguration : IShellConfiguration
private UpdatableDataProvider _updatableData;

private readonly string _name;
private readonly Func<string, Task<IConfigurationBuilder>> _configBuilderFactory;
private readonly Func<string, Action<IConfigurationBuilder>, Task<IConfigurationRoot>> _factoryAsync;
private readonly SemaphoreSlim _semaphore = new(1);
private bool _released;

public ShellConfiguration()
{
}

public ShellConfiguration(IConfigurationBuilder builder)
{
_updatableData = new UpdatableDataProvider();
_configuration = builder.Add(_updatableData).Build();
}

public ShellConfiguration(IConfiguration configuration)
{
_updatableData = new UpdatableDataProvider();
Expand All @@ -44,10 +38,16 @@ public ShellConfiguration(IConfiguration configuration)
.Build();
}

public ShellConfiguration(string name, Func<string, Task<IConfigurationBuilder>> factory)
public ShellConfiguration(IConfigurationBuilder builder)
{
_updatableData = new UpdatableDataProvider();
_configuration = builder.Add(_updatableData).Build();
}

public ShellConfiguration(string name, Func<string, Action<IConfigurationBuilder>, Task<IConfigurationRoot>> factoryAsync)
{
_name = name;
_configBuilderFactory = factory;
_factoryAsync = factoryAsync;
}

public ShellConfiguration(ShellConfiguration configuration) : this(null, configuration)
Expand Down Expand Up @@ -75,7 +75,7 @@ public ShellConfiguration(string name, ShellConfiguration configuration)
return;
}

_configBuilderFactory = configuration._configBuilderFactory;
_factoryAsync = configuration._factoryAsync;
}

private void EnsureConfiguration()
Expand Down Expand Up @@ -103,12 +103,11 @@ internal async Task EnsureConfigurationAsync()
return;
}

var builder = _configBuilderFactory is not null ?
await _configBuilderFactory.Invoke(_name)
: new ConfigurationBuilder();

_updatableData = new UpdatableDataProvider();
_configuration = builder.Add(_updatableData).Build();

_configuration = _factoryAsync is not null
? await _factoryAsync(_name, builder => builder.Add(_updatableData))
: new ConfigurationBuilder().Add(_updatableData).Build();
}
finally
{
Expand Down
94 changes: 55 additions & 39 deletions src/OrchardCore/OrchardCore/Shell/ShellSettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,33 @@

namespace OrchardCore.Environment.Shell
{
public class ShellSettingsManager : IShellSettingsManager
public class ShellSettingsManager : IShellSettingsManager, IDisposable
{
private readonly IConfiguration _applicationConfiguration;
private readonly IShellsConfigurationSources _tenantsConfigSources;
private readonly IShellConfigurationSources _tenantConfigSources;
private readonly IShellsSettingsSources _settingsSources;
private readonly IShellsSettingsSources _tenantsSettingsSources;

private IConfiguration _configuration;
private IConfigurationRoot _configurationRoot;
private IConfigurationRoot _tenantsSettingsRoot;
private IEnumerable<string> _configuredTenants;
private readonly SemaphoreSlim _semaphore = new(1);

private Func<string, Task<IConfigurationBuilder>> _tenantConfigBuilderFactory;
private Func<string, Action<IConfigurationBuilder>, Task<IConfigurationRoot>> _tenantConfigFactoryAsync;
private readonly SemaphoreSlim _tenantConfigSemaphore = new(1);
private bool _disposed;

public ShellSettingsManager(
IConfiguration applicationConfiguration,
IShellsConfigurationSources tenantsConfigSources,
IShellConfigurationSources tenantConfigSources,
IShellsSettingsSources settingsSources)
IShellsSettingsSources tenantsSettingsSources)
{
_applicationConfiguration = applicationConfiguration;
_tenantsConfigSources = tenantsConfigSources;
_tenantConfigSources = tenantConfigSources;
_settingsSources = settingsSources;
_tenantsSettingsSources = tenantsSettingsSources;
}

public ShellSettings CreateDefaultSettings()
Expand All @@ -54,26 +57,20 @@ public async Task<IEnumerable<ShellSettings>> LoadSettingsAsync()
{
await EnsureConfigurationAsync();

var tenantsSettings = (await new ConfigurationBuilder()
.AddSourcesAsync(_settingsSources))
.Build();

using var disposable = tenantsSettings as IDisposable;

var tenants = tenantsSettings.GetChildren().Select(section => section.Key);
var tenants = _tenantsSettingsRoot.GetChildren().Select(section => section.Key);
var allTenants = _configuredTenants.Concat(tenants).Distinct().ToArray();

var allSettings = new List<ShellSettings>();

foreach (var tenant in allTenants)
{
var tenantSettings = new ConfigurationBuilder()
var tenantSettingsBuilder = new ConfigurationBuilder()
.AddConfiguration(_configuration)
.AddConfiguration(_configuration.GetSection(tenant))
.AddConfiguration(tenantsSettings.GetSection(tenant));
.AddConfiguration(_tenantsSettingsRoot.GetSection(tenant));

var settings = new ShellConfiguration(tenantSettings);
var configuration = new ShellConfiguration(tenant, _tenantConfigBuilderFactory);
var settings = new ShellConfiguration(tenantSettingsBuilder);
var configuration = new ShellConfiguration(tenant, _tenantConfigFactoryAsync);

var shellSettings = new ShellSettings(settings, configuration)
{
Expand All @@ -98,13 +95,9 @@ public async Task<IEnumerable<string>> LoadSettingsNamesAsync()
{
await EnsureConfigurationAsync();

var tenantsSettings = (await new ConfigurationBuilder()
.AddSourcesAsync(_settingsSources))
.Build();

using var disposable = tenantsSettings as IDisposable;
_tenantsSettingsRoot.Reload();

var tenants = tenantsSettings.GetChildren().Select(section => section.Key);
var tenants = _tenantsSettingsRoot.GetChildren().Select(section => section.Key);
return _configuredTenants.Concat(tenants).Distinct().ToArray();
}
finally
Expand All @@ -120,19 +113,15 @@ public async Task<ShellSettings> LoadSettingsAsync(string tenant)
{
await EnsureConfigurationAsync();

var tenantsSettings = (await new ConfigurationBuilder()
.AddSourcesAsync(tenant, _settingsSources))
.Build();

using var disposable = tenantsSettings as IDisposable;
_tenantsSettingsRoot.Reload();

var tenantSettings = new ConfigurationBuilder()
var tenantSettingsBuilder = new ConfigurationBuilder()
.AddConfiguration(_configuration)
.AddConfiguration(_configuration.GetSection(tenant))
.AddConfiguration(tenantsSettings.GetSection(tenant));
.AddConfiguration(_tenantsSettingsRoot.GetSection(tenant));

var settings = new ShellConfiguration(tenantSettings);
var configuration = new ShellConfiguration(tenant, _tenantConfigBuilderFactory);
var settings = new ShellConfiguration(tenantSettingsBuilder);
var configuration = new ShellConfiguration(tenant, _tenantConfigFactoryAsync);

return new ShellSettings(settings, configuration)
{
Expand All @@ -152,7 +141,7 @@ public async Task SaveSettingsAsync(ShellSettings settings)
{
await EnsureConfigurationAsync();

if (settings == null)
if (settings is null)
{
throw new ArgumentNullException(nameof(settings));
}
Expand Down Expand Up @@ -189,7 +178,7 @@ public async Task SaveSettingsAsync(ShellSettings settings)

tenantSettings.Remove("Name");

await _settingsSources.SaveAsync(settings.Name, tenantSettings.ToObject<Dictionary<string, string>>());
await _tenantsSettingsSources.SaveAsync(settings.Name, tenantSettings.ToObject<Dictionary<string, string>>());

var tenantConfig = new JObject();

Expand Down Expand Up @@ -234,12 +223,12 @@ public async Task RemoveSettingsAsync(ShellSettings settings)
{
await EnsureConfigurationAsync();

if (settings == null)
if (settings is null)
{
throw new ArgumentNullException(nameof(settings));
}

await _settingsSources.RemoveAsync(settings.Name);
await _tenantsSettingsSources.RemoveAsync(settings.Name);

await _tenantConfigSemaphore.WaitAsync();
try
Expand All @@ -259,7 +248,7 @@ public async Task RemoveSettingsAsync(ShellSettings settings)

private async Task EnsureConfigurationAsync()
{
if (_configuration != null)
if (_configuration is not null)
{
return;
}
Expand All @@ -279,23 +268,32 @@ private async Task EnsureConfigurationAsync()
configurationBuilder.AddConfiguration(new ConfigurationRoot(lastProviders));
}

var configuration = configurationBuilder.Build().GetSection("OrchardCore");
_configurationRoot = configurationBuilder.Build();
var configuration = _configurationRoot.GetSection("OrchardCore");

_configuredTenants = configuration.GetChildren()
.Where(section => Enum.TryParse<TenantState>(section["State"], ignoreCase: true, out _))
.Select(section => section.Key)
.Distinct()
.ToArray();

_tenantConfigBuilderFactory = async (tenant) =>
_tenantsSettingsRoot = (await new ConfigurationBuilder()
.AddSourcesAsync(_tenantsSettingsSources))
.Build();

_tenantConfigFactoryAsync = async (tenant, configure) =>
{
await _tenantConfigSemaphore.WaitAsync();
try
{
return await new ConfigurationBuilder()
var builder = await new ConfigurationBuilder()
.AddConfiguration(_configuration)
.AddConfiguration(_configuration.GetSection(tenant))
.AddSourcesAsync(tenant, _tenantConfigSources);
configure(builder);
return builder.Build();
}
finally
{
Expand All @@ -305,5 +303,23 @@ private async Task EnsureConfigurationAsync()

_configuration = configuration;
}

public void Dispose()
{
if (_disposed)
{
return;
}

_disposed = true;

(_configurationRoot as IDisposable)?.Dispose();
(_tenantsSettingsRoot as IDisposable)?.Dispose();

_semaphore?.Dispose();
_tenantConfigSemaphore?.Dispose();

GC.SuppressFinalize(this);
}
}
}

0 comments on commit eb65593

Please sign in to comment.