up
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Concelier Attestation Tests / attestation-tests (push) Has been cancelled
Export Center CI / export-ci (push) Has been cancelled
Notify Smoke Test / Notify Unit Tests (push) Has been cancelled
Notify Smoke Test / Notifier Service Tests (push) Has been cancelled
Notify Smoke Test / Notification Smoke Test (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled
Scanner Analyzers / Discover Analyzers (push) Has been cancelled
Scanner Analyzers / Build Analyzers (push) Has been cancelled
Scanner Analyzers / Test Language Analyzers (push) Has been cancelled
Scanner Analyzers / Validate Test Fixtures (push) Has been cancelled
Scanner Analyzers / Verify Deterministic Output (push) Has been cancelled
Signals CI & Image / signals-ci (push) Has been cancelled
Signals Reachability Scoring & Events / reachability-smoke (push) Has been cancelled
Signals Reachability Scoring & Events / sign-and-upload (push) Has been cancelled
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
AOC Guard CI / aoc-guard (push) Has been cancelled
AOC Guard CI / aoc-verify (push) Has been cancelled
Concelier Attestation Tests / attestation-tests (push) Has been cancelled
Export Center CI / export-ci (push) Has been cancelled
Notify Smoke Test / Notify Unit Tests (push) Has been cancelled
Notify Smoke Test / Notifier Service Tests (push) Has been cancelled
Notify Smoke Test / Notification Smoke Test (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled
Scanner Analyzers / Discover Analyzers (push) Has been cancelled
Scanner Analyzers / Build Analyzers (push) Has been cancelled
Scanner Analyzers / Test Language Analyzers (push) Has been cancelled
Scanner Analyzers / Validate Test Fixtures (push) Has been cancelled
Scanner Analyzers / Verify Deterministic Output (push) Has been cancelled
Signals CI & Image / signals-ci (push) Has been cancelled
Signals Reachability Scoring & Events / reachability-smoke (push) Has been cancelled
Signals Reachability Scoring & Events / sign-and-upload (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
using System.Reflection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StellaOps.Plugin;
|
||||
using StellaOps.Plugin.Hosting;
|
||||
|
||||
namespace StellaOps.Messaging.Plugins;
|
||||
|
||||
/// <summary>
|
||||
/// Loads and registers messaging transport plugins.
|
||||
/// </summary>
|
||||
public sealed class MessagingPluginLoader
|
||||
{
|
||||
private readonly ILogger<MessagingPluginLoader>? _logger;
|
||||
|
||||
public MessagingPluginLoader(ILogger<MessagingPluginLoader>? logger = null)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Discovers and loads messaging transport plugins from the specified directory.
|
||||
/// </summary>
|
||||
public IReadOnlyList<IMessagingTransportPlugin> LoadFromDirectory(
|
||||
string pluginDirectory,
|
||||
string searchPattern = "StellaOps.Messaging.Transport.*.dll")
|
||||
{
|
||||
if (!Directory.Exists(pluginDirectory))
|
||||
{
|
||||
_logger?.LogWarning("Plugin directory does not exist: {Directory}", pluginDirectory);
|
||||
return [];
|
||||
}
|
||||
|
||||
var options = new PluginHostOptions
|
||||
{
|
||||
PluginsDirectory = pluginDirectory,
|
||||
EnsureDirectoryExists = false,
|
||||
RecursiveSearch = false
|
||||
};
|
||||
options.SearchPatterns.Add(searchPattern);
|
||||
|
||||
var result = PluginHost.LoadPlugins(options);
|
||||
var plugins = new List<IMessagingTransportPlugin>();
|
||||
|
||||
foreach (var pluginAssembly in result.Plugins)
|
||||
{
|
||||
var transportPlugins = PluginLoader.LoadPlugins<IMessagingTransportPlugin>(new[] { pluginAssembly.Assembly });
|
||||
plugins.AddRange(transportPlugins);
|
||||
|
||||
foreach (var plugin in transportPlugins)
|
||||
{
|
||||
_logger?.LogDebug("Loaded messaging transport plugin: {Name} from {Assembly}",
|
||||
plugin.Name, pluginAssembly.Assembly.GetName().Name);
|
||||
}
|
||||
}
|
||||
|
||||
return plugins;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads messaging transport plugins from the specified assemblies.
|
||||
/// </summary>
|
||||
public IReadOnlyList<IMessagingTransportPlugin> LoadFromAssemblies(IEnumerable<Assembly> assemblies)
|
||||
{
|
||||
return PluginLoader.LoadPlugins<IMessagingTransportPlugin>(assemblies);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds and registers the configured transport plugin.
|
||||
/// </summary>
|
||||
/// <param name="plugins">Available plugins.</param>
|
||||
/// <param name="services">Service collection.</param>
|
||||
/// <param name="configuration">Configuration.</param>
|
||||
/// <param name="configSectionPath">Configuration section path (default: "messaging").</param>
|
||||
/// <returns>True if a plugin was registered.</returns>
|
||||
public bool RegisterConfiguredTransport(
|
||||
IReadOnlyList<IMessagingTransportPlugin> plugins,
|
||||
IServiceCollection services,
|
||||
IConfiguration configuration,
|
||||
string configSectionPath = "messaging")
|
||||
{
|
||||
var messagingSection = configuration.GetSection(configSectionPath);
|
||||
var transportName = messagingSection.GetValue<string>("transport");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(transportName))
|
||||
{
|
||||
_logger?.LogWarning("No messaging transport configured at {Path}:transport", configSectionPath);
|
||||
return false;
|
||||
}
|
||||
|
||||
var plugin = plugins.FirstOrDefault(p =>
|
||||
string.Equals(p.Name, transportName, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (plugin is null)
|
||||
{
|
||||
_logger?.LogError("Messaging transport plugin '{Transport}' not found. Available: {Available}",
|
||||
transportName, string.Join(", ", plugins.Select(p => p.Name)));
|
||||
return false;
|
||||
}
|
||||
|
||||
var transportConfigSection = $"{configSectionPath}:{transportName}";
|
||||
var context = new MessagingTransportRegistrationContext(
|
||||
services,
|
||||
configuration,
|
||||
transportConfigSection);
|
||||
|
||||
plugin.Register(context);
|
||||
|
||||
_logger?.LogInformation("Registered messaging transport: {Transport}", transportName);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user