DocSpace-buildtools/common/services/ASC.Notify/NotifyService.cs

110 lines
4.1 KiB
C#
Raw Normal View History

2019-07-29 09:15:48 +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.Reflection;
2019-08-01 08:47:15 +00:00
using ASC.Common.Caching;
2019-07-29 09:15:48 +00:00
using ASC.Common.Logging;
using ASC.Core;
using ASC.Notify.Messages;
using ASC.Web.Core.WhiteLabel;
2019-09-13 11:18:27 +00:00
using Microsoft.Extensions.DependencyInjection;
2019-10-17 15:55:35 +00:00
using Microsoft.Extensions.Options;
2019-07-29 09:15:48 +00:00
namespace ASC.Notify
{
2019-08-01 08:47:15 +00:00
public class NotifyService : INotifyService, IDisposable
2019-07-29 09:15:48 +00:00
{
2019-10-17 15:55:35 +00:00
private readonly ILog log;
2019-07-29 09:15:48 +00:00
2019-08-01 08:47:15 +00:00
private readonly ICacheNotify<NotifyMessage> cacheNotify;
2019-07-29 09:15:48 +00:00
private readonly DbWorker db;
2019-09-13 11:18:27 +00:00
public IServiceProvider ServiceProvider { get; }
2019-10-17 15:55:35 +00:00
public NotifyService(DbWorker db, IServiceProvider serviceProvider, ICacheNotify<NotifyMessage> cacheNotify, IOptionsMonitor<LogNLog> options)
2019-07-29 09:15:48 +00:00
{
this.db = db;
2019-09-13 11:18:27 +00:00
ServiceProvider = serviceProvider;
2019-10-11 15:03:03 +00:00
this.cacheNotify = cacheNotify;
2019-10-17 15:55:35 +00:00
log = options.Get("ASC");
2019-08-01 08:47:15 +00:00
}
public void Start()
{
cacheNotify.Subscribe((n) => SendNotifyMessage(n), CacheNotifyAction.InsertOrUpdate);
}
public void Stop()
{
cacheNotify.Unsubscribe(CacheNotifyAction.InsertOrUpdate);
2019-07-29 09:15:48 +00:00
}
public void SendNotifyMessage(NotifyMessage notifyMessage)
{
try
{
db.SaveMessage(notifyMessage);
}
catch (Exception e)
{
log.Error(e);
}
}
public void InvokeSendMethod(string service, string method, int tenant, params object[] parameters)
{
var serviceType = Type.GetType(service, true);
var getinstance = serviceType.GetProperty("Instance", BindingFlags.Static | BindingFlags.Public);
var instance = getinstance.GetValue(serviceType, null);
if (instance == null)
{
throw new Exception("Service instance not found.");
}
var methodInfo = serviceType.GetMethod(method);
if (methodInfo == null)
{
throw new Exception("Method not found.");
}
2019-09-13 11:18:27 +00:00
using var scope = ServiceProvider.CreateScope();
var tenantManager = scope.ServiceProvider.GetService<TenantManager>();
var tenantWhiteLabelSettings = scope.ServiceProvider.GetService<TenantWhiteLabelSettings>();
tenantManager.SetCurrentTenant(tenant);
tenantWhiteLabelSettings.Apply(tenant);
2019-07-29 09:15:48 +00:00
methodInfo.Invoke(instance, parameters);
}
2019-08-01 08:47:15 +00:00
public void Dispose()
{
cacheNotify.Unsubscribe(CacheNotifyAction.InsertOrUpdate);
}
2019-07-29 09:15:48 +00:00
}
}