Path: redb-route/redb.Route.Xml/XmlContextLoader.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Xml/XmlContextLoader.cs
----
using System.Xml.Linq;
using Microsoft.Extensions.DependencyInjection;
using redb.Route.Abstractions;
using redb.Route.Components.Bean;
using redb.Route.Core;
using redb.Route.Definitions;
namespace redb.Route.Xml;
///
/// Loads a context.xml document: the context-level
/// sections a package carries besides its routes — an explicit <components> list,
/// context-wide <bean> declarations, and the <onInit> pipeline of
/// ordinary format steps, run ONCE at —
/// the engine's only fail-fast bootstrap hook, so a failed step keeps the context from
/// accepting traffic, exactly the module-init semantics the sketch asks for. Same eager
/// collect-all parsing with positions as the routes loader.
///
public sealed class XmlContextLoader
{
private readonly RouteContext _context;
private readonly ElementRegistry _registry;
private readonly bool _validate;
private readonly bool _hasExtensions;
/// Creates a loader for the given context.
public XmlContextLoader(RouteContext context, XmlRouteLoaderOptions? options = null)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_registry = ElementRegistry.CreateDefault(options?.Extensions);
_validate = options?.ValidateAgainstSchema ?? true;
_hasExtensions = options?.Extensions is { Count: > 0 };
}
/// Parses and applies every section of the document. See the class remarks.
public void Load(XDocument document, string? sourceName = null)
{
ArgumentNullException.ThrowIfNull(document);
sourceName ??= "";
var ctx = new XmlParseContext(_context, _registry, sourceName);
var root = document.Root;
if (root is null || root.Name.LocalName != "context")
{
throw new XmlRouteException(sourceName,
[$"the document root must be , found <{root?.Name.LocalName ?? "nothing"}>."]);
}
if (root.Name.NamespaceName != XmlRouteLoader.Namespace)
{
throw new XmlRouteException(sourceName,
[XmlRouteLoader.VersionError(root.Name.NamespaceName)
?? $"unexpected root namespace '{root.Name.NamespaceName}'; this loader reads '{XmlRouteLoader.Namespace}'."]);
}
ctx.CheckElementNamespaces(root);
// Components first, then beans (a bean may need a component's types loaded), then init.
foreach (var components in root.Elements().Where(e => e.Name.LocalName == "components"))
RegisterComponents(components, ctx);
foreach (var bean in root.Elements().Where(e => e.Name.LocalName == "bean"))
BeanSection.Declare(bean, ctx);
var onInits = root.Elements().Where(e => e.Name.LocalName == "onInit").ToList();
if (onInits.Count > 1)
ctx.AddError(onInits[1], " takes at most one .");
// Context-level contributions: a package element beside //
// (the redb bridge above all) applies to the context AFTER beans — a contribution may
// reference #names — and BEFORE parsing, whose steps may rely on what the
// contribution set up.
foreach (var element in root.Elements())
{
if (element.Name.LocalName is "components" or "bean" or "onInit" or XmlRouteLoader.RouteConfigurationElement)
continue;
if (ctx.Registry.Find(element.Name.LocalName) is IXmlContextContribution contextLevel
&& contextLevel.Kind == XmlElementKind.ContextLevel)
{
contextLevel.ApplyContext(element, _context, ctx);
continue;
}
var contributed = ctx.Registry.Contributions
.Where(c => c.Kind == XmlElementKind.ContextLevel)
.Select(c => "<" + c.Name + ">");
ctx.AddError(element, $"unknown section <{element.Name.LocalName}> " +
$"(valid: , , , {string.Concat(contributed.Select(n => ", " + n))}).");
}
// Route configurations: the context's shared error handling, for the routes of every file and builder. They
// live on a builder of their own, built here so every error surfaces at Load, added once the document is valid.
var configurationElements = root.Elements()
.Where(e => e.Name.LocalName == XmlRouteLoader.RouteConfigurationElement).ToList();
XmlRouteLoader.XmlRoutesBuilder? configurations = null;
if (configurationElements.Count > 0)
{
configurations = new XmlRouteLoader.XmlRoutesBuilder(sourceName, b =>
{
foreach (var element in configurationElements)
XmlRouteLoader.ParseRouteConfiguration(element, b, ctx);
});
configurations.BuildNow(_context);
}
RouteDefinition? init = null;
if (onInits.Count == 1)
{
// Ordinary steps of the format, parsed by the same registry; has no place in
// an init pipeline and is rejected as an unknown element naturally.
init = new RouteDefinition();
ctx.ParseSteps(onInits[0], init);
}
ctx.CheckSchemes();
if (_validate)
{
var schema = _hasExtensions ? XmlRouteSchema.For(_registry) : XmlRouteSchema.CoreSet;
foreach (var (line, column, message) in XmlRouteSchema.Validate(document, schema))
{
if (!ctx.ErrorLines.Contains(line))
ctx.AddRawError($"{sourceName}({line},{column}): [schema] {message}");
}
}
if (ctx.Errors.Count > 0)
throw new XmlRouteException(sourceName, ctx.Errors);
foreach (var work in ctx.ValidOnlyWork)
work();
if (configurations is not null)
_context.AddRoutes(configurations);
if (init is not null && init.Outputs.Count > 0)
_context.AddLifecycleListener(new XmlInitListener(init, sourceName));
}
private void RegisterComponents(XElement section, XmlParseContext ctx)
{
foreach (var child in section.Elements())
{
if (child.Name.LocalName != "component")
{
ctx.AddError(child, $" accepts only children, found <{child.Name.LocalName}>.");
continue;
}
var typeName = ctx.RequiredAttr(child, "type");
if (typeName is null)
continue;
var resolver = _context.GetService() ?? DefaultBeanTypeResolver.Instance;
var type = resolver.Resolve(typeName);
if (type is null)
{
ctx.AddError(child, $"component type '{typeName}' was not found in the loaded assemblies.");
continue;
}
if (!typeof(IComponent).IsAssignableFrom(type))
{
ctx.AddError(child, $"'{typeName}' does not implement IComponent.");
continue;
}
try
{
var provider = _context.GetServiceProvider();
var component = (IComponent)(provider is not null
? ActivatorUtilities.CreateInstance(CollectibleTypeServiceProvider.For(provider), type)
: Activator.CreateInstance(type)
?? throw new InvalidOperationException($"Activator returned null for {type}."));
_context.AddComponent(component);
}
catch (Exception ex) when (ex is MissingMethodException or MemberAccessException or InvalidOperationException)
{
ctx.AddError(child, $"component '{typeName}' could not be created: {ex.Message}");
}
}
}
}
///
/// Runs the parsed <onInit> pipeline once, in the fail-fast bootstrap phase. The
/// steps compile through the ordinary node pipeline (so telemetry decoration and disposal
/// registration apply) with the live context, and a step failure aborts Start().
///
internal sealed class XmlInitListener(RouteDefinition pipeline, string sourceName) : IRouteLifecycleListener
{
private bool _ran; // set only on success: a failed Start() must retry the init on the next Start()
public async Task OnContextStarting(IRouteContext context, CancellationToken ct)
{
if (_ran)
return;
var body = NodePipeline.Body(context, pipeline.Outputs);
var exchange = new Exchange(new Message((object?)null));
try
{
await body.Process(exchange, ct).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception thrown)
{
// A step may throw directly or record on the exchange — both name the source file.
throw new InvalidOperationException($" of '{sourceName}' failed: {thrown.Message}", thrown);
}
if (exchange.Exception is { } failure && !exchange.ExceptionHandled)
throw new InvalidOperationException($" of '{sourceName}' failed: {failure.Message}", failure);
_ran = true;
}
}