aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Plugins.Extensions.Loading/src/RoutingExtensions.cs
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2023-01-27 21:13:17 -0500
committerLibravatar vnugent <public@vaughnnugent.com>2023-01-27 21:13:17 -0500
commit282aad617b9c39a6f14c1cf527f6dd4523d0c54b (patch)
treed2942722aad3c2fb087f8639a5fc667f6767456c /lib/VNLib.Plugins.Extensions.Loading/src/RoutingExtensions.cs
parent94ee5cc9090b40fe042187362acd18c0995866f4 (diff)
Object cache overhaul and logger updates
Diffstat (limited to 'lib/VNLib.Plugins.Extensions.Loading/src/RoutingExtensions.cs')
-rw-r--r--lib/VNLib.Plugins.Extensions.Loading/src/RoutingExtensions.cs40
1 files changed, 26 insertions, 14 deletions
diff --git a/lib/VNLib.Plugins.Extensions.Loading/src/RoutingExtensions.cs b/lib/VNLib.Plugins.Extensions.Loading/src/RoutingExtensions.cs
index f27b3b3..c1c6bb6 100644
--- a/lib/VNLib.Plugins.Extensions.Loading/src/RoutingExtensions.cs
+++ b/lib/VNLib.Plugins.Extensions.Loading/src/RoutingExtensions.cs
@@ -30,6 +30,7 @@ using System.Collections.Generic;
using System.Runtime.CompilerServices;
using VNLib.Plugins.Extensions.Loading.Events;
+using System.Net;
namespace VNLib.Plugins.Extensions.Loading.Routing
{
@@ -50,42 +51,53 @@ namespace VNLib.Plugins.Extensions.Loading.Routing
public static T Route<T>(this PluginBase plugin, string? pluginConfigPathName) where T : IEndpoint
{
Type endpointType = typeof(T);
+
+ T endpoint;
+
//If the config attribute is not set, then ignore the config variables
if (string.IsNullOrWhiteSpace(pluginConfigPathName))
{
ConstructorInfo? constructor = endpointType.GetConstructor(new Type[] { typeof(PluginBase) });
+
_ = constructor ?? throw new EntryPointNotFoundException($"No constructor found for {endpointType.Name}");
+
//Create the new endpoint and pass the plugin instance
- T endpoint = (T)constructor.Invoke(new object[] { plugin });
+ endpoint = (T)constructor.Invoke(new object[] { plugin });
+
//Register event handlers for the endpoint
ScheduleIntervals(plugin, endpoint, endpointType, null);
- //Route the endpoint
- plugin.Route(endpoint);
-
- //Store ref to plugin for endpoint
- _pluginRefs.Add(endpoint, plugin);
-
- return endpoint;
}
else
{
ConstructorInfo? constructor = endpointType.GetConstructor(new Type[] { typeof(PluginBase), typeof(IReadOnlyDictionary<string, JsonElement>) });
+
//Make sure the constructor exists
_ = constructor ?? throw new EntryPointNotFoundException($"No constructor found for {endpointType.Name}");
+
//Get config variables for the endpoint
IReadOnlyDictionary<string, JsonElement> conf = plugin.GetConfig(pluginConfigPathName);
+
//Create the new endpoint and pass the plugin instance along with the configuration object
- T endpoint = (T)constructor.Invoke(new object[] { plugin, conf });
+ endpoint = (T)constructor.Invoke(new object[] { plugin, conf });
+
//Register event handlers for the endpoint
ScheduleIntervals(plugin, endpoint, endpointType, conf);
- //Route the endpoint
- plugin.Route(endpoint);
+ }
- //Store ref to plugin for endpoint
- _pluginRefs.Add(endpoint, plugin);
+ //Route the endpoint
+ plugin.Route(endpoint);
- return endpoint;
+ //Store ref to plugin for endpoint
+ _pluginRefs.Add(endpoint, plugin);
+
+ //See if the endpoint is disposable
+ if (endpoint is IDisposable dis)
+ {
+ //Register dispose for unload
+ _ = plugin.RegisterForUnload(dis.Dispose);
}
+
+ return endpoint;
}
/// <summary>