DocSpace-buildtools/common/ASC.Core.Common/Configuration/Consumer.cs

432 lines
14 KiB
C#
Raw Normal View History

2019-05-15 14:56:09 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
using System;
using System.Collections;
using System.Collections.Generic;
2020-02-17 08:58:14 +00:00
using System.Linq;
2019-12-20 11:17:01 +00:00
2020-02-17 08:58:14 +00:00
using ASC.Common;
2019-05-15 14:56:09 +00:00
using ASC.Common.Caching;
2020-02-17 08:58:14 +00:00
using ASC.Core.Tenants;
using Autofac;
using Microsoft.Extensions.Configuration;
2019-05-15 14:56:09 +00:00
namespace ASC.Core.Common.Configuration
{
public class Consumer : IDictionary<string, string>
{
public bool CanSet { get; private set; }
public int Order { get; private set; }
public string Name { get; private set; }
protected readonly Dictionary<string, string> Props;
public IEnumerable<string> ManagedKeys
{
get { return Props.Select(r => r.Key).ToList(); }
}
protected readonly Dictionary<string, string> Additional;
public virtual IEnumerable<string> AdditionalKeys
{
get { return Additional.Select(r => r.Key).ToList(); }
}
public ICollection<string> Keys { get { return AllProps.Keys; } }
public ICollection<string> Values { get { return AllProps.Values; } }
private Dictionary<string, string> AllProps
{
get
{
var result = Props.ToDictionary(item => item.Key, item => item.Value);
foreach (var item in Additional.Where(item => !result.ContainsKey(item.Key)))
{
result.Add(item.Key, item.Value);
}
return result;
}
}
2019-09-27 11:51:09 +00:00
private readonly bool OnlyDefault;
2020-02-17 08:58:14 +00:00
public TenantManager TenantManager { get; set; }
public CoreBaseSettings CoreBaseSettings { get; set; }
public CoreSettings CoreSettings { get; set; }
2020-03-06 13:17:17 +00:00
public ConsumerFactory ConsumerFactory { get; set; }
2020-02-17 08:58:14 +00:00
public IConfiguration Configuration { get; }
public ICacheNotify<ConsumerCacheItem> Cache { get; }
2019-05-15 14:56:09 +00:00
public bool IsSet
{
get { return Props.Any() && !Props.All(r => string.IsNullOrEmpty(this[r.Key])); }
}
static Consumer()
2020-02-17 08:58:14 +00:00
{
2019-05-15 14:56:09 +00:00
}
public Consumer()
2020-02-17 08:58:14 +00:00
{
2019-11-12 08:32:24 +00:00
Props = new Dictionary<string, string>();
Additional = new Dictionary<string, string>();
2020-02-17 08:58:14 +00:00
}
public Consumer(
TenantManager tenantManager,
CoreBaseSettings coreBaseSettings,
CoreSettings coreSettings,
IConfiguration configuration,
2019-11-12 08:32:24 +00:00
ICacheNotify<ConsumerCacheItem> cache) : this()
2020-02-17 08:58:14 +00:00
{
TenantManager = tenantManager;
CoreBaseSettings = coreBaseSettings;
CoreSettings = coreSettings;
Configuration = configuration;
Cache = cache;
2019-09-27 11:51:09 +00:00
OnlyDefault = configuration["core:default-consumers"] == "true";
2019-05-15 14:56:09 +00:00
Name = "";
Order = int.MaxValue;
}
2020-02-17 08:58:14 +00:00
public Consumer(
TenantManager tenantManager,
CoreBaseSettings coreBaseSettings,
CoreSettings coreSettings,
IConfiguration configuration,
ICacheNotify<ConsumerCacheItem> cache,
string name, int order, Dictionary<string, string> additional)
2020-03-06 13:17:17 +00:00
: this(tenantManager, coreBaseSettings, coreSettings, configuration, cache)
2019-05-15 14:56:09 +00:00
{
Name = name;
Order = order;
Props = new Dictionary<string, string>();
Additional = additional;
}
2020-02-17 08:58:14 +00:00
public Consumer(
TenantManager tenantManager,
CoreBaseSettings coreBaseSettings,
CoreSettings coreSettings,
IConfiguration configuration,
ICacheNotify<ConsumerCacheItem> cache,
string name, int order, Dictionary<string, string> props, Dictionary<string, string> additional)
2020-03-06 13:17:17 +00:00
: this(tenantManager, coreBaseSettings, coreSettings, configuration, cache)
2019-05-15 14:56:09 +00:00
{
Name = name;
Order = order;
Props = props ?? new Dictionary<string, string>();
Additional = additional ?? new Dictionary<string, string>();
if (props != null && props.Any())
{
CanSet = props.All(r => string.IsNullOrEmpty(r.Value));
}
}
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
return AllProps.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(KeyValuePair<string, string> item)
{
}
public void Clear()
{
if (!CanSet)
{
throw new NotSupportedException("Key for read only.");
}
foreach (var providerProp in Props)
{
this[providerProp.Key] = null;
}
Cache.Publish(new ConsumerCacheItem() { Name = this.Name }, CacheNotifyAction.Remove);
2019-05-15 14:56:09 +00:00
}
public bool Contains(KeyValuePair<string, string> item)
{
return AllProps.Contains(item);
}
public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
{
}
public bool Remove(KeyValuePair<string, string> item)
{
return AllProps.Remove(item.Key);
}
public int Count { get { return AllProps.Count; } }
public bool IsReadOnly { get { return true; } }
public bool ContainsKey(string key)
{
return AllProps.ContainsKey(key);
}
public void Add(string key, string value)
{
}
public bool Remove(string key)
{
return false;
}
public bool TryGetValue(string key, out string value)
{
return AllProps.TryGetValue(key, out value);
}
public string this[string key]
{
get { return Get(key); }
set { Set(key, value); }
}
private string Get(string name)
{
string value = null;
if (!OnlyDefault && CanSet)
{
2019-09-18 15:19:30 +00:00
var tenant = CoreBaseSettings.Standalone
2019-05-15 14:56:09 +00:00
? Tenant.DEFAULT_TENANT
2019-09-18 13:33:01 +00:00
: TenantManager.GetCurrentTenant().TenantId;
2019-05-15 14:56:09 +00:00
2019-09-18 13:33:01 +00:00
value = CoreSettings.GetSetting(GetSettingsKey(name), tenant);
2019-05-15 14:56:09 +00:00
}
if (string.IsNullOrEmpty(value))
{
if (AllProps.ContainsKey(name))
{
value = AllProps[name];
}
}
return value;
}
private void Set(string name, string value)
{
if (!CanSet)
{
throw new NotSupportedException("Key for read only.");
}
if (!ManagedKeys.Contains(name))
{
if (Additional.ContainsKey(name))
{
Additional[name] = value;
}
else
{
Additional.Add(name, value);
}
return;
}
2019-09-18 15:19:30 +00:00
var tenant = CoreBaseSettings.Standalone
2019-05-15 14:56:09 +00:00
? Tenant.DEFAULT_TENANT
2019-09-18 13:33:01 +00:00
: TenantManager.GetCurrentTenant().TenantId;
CoreSettings.SaveSetting(GetSettingsKey(name), value, tenant);
2019-05-15 14:56:09 +00:00
}
protected virtual string GetSettingsKey(string name)
{
return "AuthKey_" + name;
}
}
public class DataStoreConsumer : Consumer, ICloneable
{
public Type HandlerType { get; private set; }
public DataStoreConsumer Cdn { get; private set; }
public const string HandlerTypeKey = "handlerType";
public const string CdnKey = "cdn";
2020-02-17 08:58:14 +00:00
public DataStoreConsumer() : base()
{
}
public DataStoreConsumer(
TenantManager tenantManager,
CoreBaseSettings coreBaseSettings,
CoreSettings coreSettings,
IConfiguration configuration,
ICacheNotify<ConsumerCacheItem> cache)
2020-03-06 13:17:17 +00:00
: base(tenantManager, coreBaseSettings, coreSettings, configuration, cache)
2020-02-17 08:58:14 +00:00
{
2019-05-15 14:56:09 +00:00
}
2020-02-17 08:58:14 +00:00
public DataStoreConsumer(
TenantManager tenantManager,
CoreBaseSettings coreBaseSettings,
CoreSettings coreSettings,
IConfiguration configuration,
ICacheNotify<ConsumerCacheItem> cache,
string name, int order, Dictionary<string, string> additional)
2020-03-06 13:17:17 +00:00
: base(tenantManager, coreBaseSettings, coreSettings, configuration, cache, name, order, additional)
2019-05-15 14:56:09 +00:00
{
Init(additional);
}
2020-02-17 08:58:14 +00:00
public DataStoreConsumer(
TenantManager tenantManager,
CoreBaseSettings coreBaseSettings,
CoreSettings coreSettings,
IConfiguration configuration,
ICacheNotify<ConsumerCacheItem> cache,
string name, int order, Dictionary<string, string> props, Dictionary<string, string> additional)
2020-03-06 13:17:17 +00:00
: base(tenantManager, coreBaseSettings, coreSettings, configuration, cache, name, order, props, additional)
2019-05-15 14:56:09 +00:00
{
Init(additional);
}
public override IEnumerable<string> AdditionalKeys
{
2019-08-15 12:04:42 +00:00
get { return base.AdditionalKeys.Where(r => r != HandlerTypeKey && r != "cdn").ToList(); }
2019-05-15 14:56:09 +00:00
}
protected override string GetSettingsKey(string name)
{
return base.GetSettingsKey(Name + name);
}
private void Init(IReadOnlyDictionary<string, string> additional)
{
if (additional == null || !additional.ContainsKey(HandlerTypeKey))
throw new ArgumentException(HandlerTypeKey);
HandlerType = Type.GetType(additional[HandlerTypeKey]);
if (additional.ContainsKey(CdnKey))
{
Cdn = GetCdn(additional[CdnKey]);
}
}
private DataStoreConsumer GetCdn(string cdn)
{
2019-12-20 11:17:01 +00:00
var fromConfig = ConsumerFactory.GetByKey<Consumer>(cdn);
2019-05-15 14:56:09 +00:00
var props = ManagedKeys.ToDictionary(prop => prop, prop => this[prop]);
var additional = fromConfig.AdditionalKeys.ToDictionary(prop => prop, prop => fromConfig[prop]);
additional.Add(HandlerTypeKey, HandlerType.AssemblyQualifiedName);
2020-03-06 13:17:17 +00:00
return new DataStoreConsumer(fromConfig.TenantManager, fromConfig.CoreBaseSettings, fromConfig.CoreSettings, fromConfig.Configuration, fromConfig.Cache, fromConfig.Name, fromConfig.Order, props, additional);
2019-05-15 14:56:09 +00:00
}
public object Clone()
{
2020-03-06 13:17:17 +00:00
return new DataStoreConsumer(TenantManager, CoreBaseSettings, CoreSettings, Configuration, Cache, Name, Order, Props.ToDictionary(r => r.Key, r => r.Value), Additional.ToDictionary(r => r.Key, r => r.Value));
2019-05-15 14:56:09 +00:00
}
}
2020-04-16 11:33:35 +00:00
public class ConsumerFactory : IDisposable
2019-05-15 14:56:09 +00:00
{
2020-02-17 08:58:14 +00:00
public ILifetimeScope Builder { get; set; }
2020-03-06 13:17:17 +00:00
2019-12-20 11:17:01 +00:00
public ConsumerFactory(IContainer builder)
2019-05-15 14:56:09 +00:00
{
2020-04-16 11:33:35 +00:00
Builder = builder.BeginLifetimeScope();
2019-05-15 14:56:09 +00:00
}
2019-12-23 12:57:09 +00:00
public Consumer GetByKey(string key)
2019-05-15 14:56:09 +00:00
{
2019-12-23 12:57:09 +00:00
if (Builder.TryResolveKeyed(key, typeof(Consumer), out var result))
2019-05-15 14:56:09 +00:00
{
return (Consumer)result;
}
return new Consumer();
}
2019-12-23 12:57:09 +00:00
public T GetByKey<T>(string key) where T : Consumer, new()
2019-05-15 14:56:09 +00:00
{
2019-12-23 12:57:09 +00:00
if (Builder.TryResolveKeyed(key, typeof(T), out var result))
2019-05-15 14:56:09 +00:00
{
return (T)result;
}
return new T();
}
2019-12-20 11:17:01 +00:00
public T Get<T>() where T : Consumer, new()
2019-05-15 14:56:09 +00:00
{
2019-08-15 13:05:50 +00:00
if (Builder.TryResolve(out T result))
2019-05-15 14:56:09 +00:00
{
return result;
}
return new T();
}
2019-12-20 11:17:01 +00:00
public IEnumerable<T> GetAll<T>() where T : Consumer, new()
2019-05-15 14:56:09 +00:00
{
return Builder.Resolve<IEnumerable<T>>();
2020-04-16 11:33:35 +00:00
}
public void Dispose()
{
Builder.Dispose();
}
2020-02-17 08:58:14 +00:00
}
public static class ConsumerFactoryExtension
{
public static DIHelper AddConsumerFactoryService(this DIHelper services)
{
services.TryAddScoped<ConsumerFactory>();
return services;
}
}
2019-05-15 14:56:09 +00:00
}