Files
DiunaBI/DiunaBI.Infrastructure/Services/PluginManager.cs

161 lines
5.2 KiB
C#
Raw Normal View History

2025-06-02 16:54:33 +02:00
using System.Reflection;
2025-11-05 20:50:25 +01:00
using DiunaBI.Infrastructure.Interfaces;
2025-06-02 16:54:33 +02:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
2025-11-05 20:50:25 +01:00
namespace DiunaBI.Infrastructure.Services;
2025-06-02 16:54:33 +02:00
public class PluginManager
{
private readonly ILogger<PluginManager> _logger;
private readonly IServiceProvider _serviceProvider;
private readonly List<Type> _processorTypes = new();
private readonly List<Type> _importerTypes = new();
2025-12-02 15:21:27 +01:00
private readonly List<Type> _exporterTypes = new();
2025-06-02 16:54:33 +02:00
private readonly List<IPlugin> _plugins = new();
public PluginManager(ILogger<PluginManager> logger, IServiceProvider serviceProvider)
{
_logger = logger;
_serviceProvider = serviceProvider;
}
public void LoadPluginsFromDirectory(string pluginsPath)
{
if (!Directory.Exists(pluginsPath))
{
_logger.LogWarning("Plugins directory not found: {Path}", pluginsPath);
return;
}
var dllFiles = Directory.GetFiles(pluginsPath, "*.dll", SearchOption.AllDirectories);
foreach (var dllFile in dllFiles)
{
try
{
LoadPluginFromAssembly(dllFile);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to load plugin from {File}", dllFile);
}
}
2025-12-02 15:21:27 +01:00
_logger.LogInformation("Loaded {ProcessorCount} processors, {ImporterCount} importers, and {ExporterCount} exporters from {AssemblyCount} assemblies",
2025-06-08 14:48:33 +02:00
_processorTypes.Count,
_importerTypes.Count,
2025-12-02 15:21:27 +01:00
_exporterTypes.Count,
dllFiles.Length);
2025-06-02 16:54:33 +02:00
}
private void LoadPluginFromAssembly(string assemblyPath)
{
_logger.LogDebug("Loading assembly from: {Path}", assemblyPath); // Information -> Debug
2025-06-08 14:48:33 +02:00
2025-06-02 16:54:33 +02:00
try
{
var assembly = Assembly.LoadFrom(assemblyPath);
_logger.LogDebug("Assembly loaded successfully: {Name}", assembly.FullName); // Information -> Debug
2025-06-08 14:48:33 +02:00
2025-06-02 16:54:33 +02:00
foreach (var type in assembly.GetTypes())
{
if (typeof(IDataProcessor).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
{
_processorTypes.Add(type);
_logger.LogDebug("Registered processor: {Type}", type.Name); // Information -> Debug
}
2025-06-08 14:48:33 +02:00
2025-06-02 16:54:33 +02:00
if (typeof(IDataImporter).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
{
_importerTypes.Add(type);
_logger.LogDebug("Registered importer: {Type}", type.Name); // Information -> Debug
}
2025-12-02 15:21:27 +01:00
if (typeof(IDataExporter).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
{
_exporterTypes.Add(type);
_logger.LogDebug("Registered exporter: {Type}", type.Name);
}
2025-06-02 16:54:33 +02:00
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to load assembly from {Path}", assemblyPath); // ZOSTAW jako Error
}
}
public IDataProcessor? GetProcessor(string processorType)
{
foreach (var type in _processorTypes)
{
try
{
2025-12-02 15:21:27 +01:00
var scope = _serviceProvider.CreateScope();
2025-06-02 16:54:33 +02:00
var instance = (IDataProcessor)ActivatorUtilities.CreateInstance(scope.ServiceProvider, type);
2025-06-08 14:48:33 +02:00
2025-06-02 16:54:33 +02:00
if (instance.CanProcess(processorType))
{
2025-12-02 15:21:27 +01:00
return instance;
2025-06-02 16:54:33 +02:00
}
2025-12-02 15:21:27 +01:00
scope.Dispose();
2025-06-02 16:54:33 +02:00
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to create processor instance of type {Type}", type.Name);
}
}
return null;
}
public IDataImporter? GetImporter(string importerType)
{
foreach (var type in _importerTypes)
{
try
{
2025-12-02 15:21:27 +01:00
var scope = _serviceProvider.CreateScope();
2025-06-02 16:54:33 +02:00
var instance = (IDataImporter)ActivatorUtilities.CreateInstance(scope.ServiceProvider, type);
2025-06-08 14:48:33 +02:00
2025-06-02 16:54:33 +02:00
if (instance.CanImport(importerType))
{
2025-12-02 15:21:27 +01:00
return instance;
2025-06-02 16:54:33 +02:00
}
2025-12-02 15:21:27 +01:00
scope.Dispose();
2025-06-02 16:54:33 +02:00
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to create importer instance of type {Type}", type.Name);
}
}
return null;
}
public IDataExporter? GetExporter(string exporterType)
{
2025-12-02 15:21:27 +01:00
foreach (var type in _exporterTypes)
{
try
{
var scope = _serviceProvider.CreateScope();
var instance = (IDataExporter)ActivatorUtilities.CreateInstance(scope.ServiceProvider, type);
if (instance.CanExport(exporterType))
{
return instance;
}
scope.Dispose();
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to create exporter instance of type {Type}", type.Name);
}
}
return null;
2025-06-02 16:54:33 +02:00
}
2025-12-02 15:21:27 +01:00
public int GetPluginsCount() => _processorTypes.Count + _importerTypes.Count + _exporterTypes.Count;
2025-06-02 16:54:33 +02:00
}