-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load handlers list from configuration
- Loading branch information
Showing
19 changed files
with
290 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Reflection; | ||
using Microsoft.KernelMemory.Configuration; | ||
using Microsoft.KernelMemory.Pipeline; | ||
|
||
namespace Microsoft.KernelMemory.Handlers; | ||
|
||
internal static class HandlerTypeLoader | ||
{ | ||
internal static bool TryGetHandlerType(HandlerConfig config, [NotNullWhen(true)] out Type? handlerType) | ||
{ | ||
handlerType = null; | ||
|
||
// If part of the config is empty, the handler is disabled | ||
if (string.IsNullOrEmpty(config.Class) || string.IsNullOrEmpty(config.Assembly)) | ||
{ | ||
return false; | ||
} | ||
|
||
// Search the assembly in a few directories | ||
var path = string.Empty; | ||
var paths = new HashSet<string> | ||
{ | ||
config.Assembly, | ||
Path.Join(Environment.CurrentDirectory, config.Assembly), | ||
Path.Join(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), config.Assembly), | ||
}; | ||
|
||
foreach (var p in paths) | ||
{ | ||
if (!File.Exists(p)) { continue; } | ||
|
||
path = p; | ||
break; | ||
} | ||
|
||
// Check if the assembly exists | ||
if (string.IsNullOrEmpty(path)) | ||
{ | ||
throw new ConfigurationException($"Handler assembly not found: {config.Assembly}"); | ||
} | ||
|
||
Assembly assembly = Assembly.LoadFrom(path); | ||
|
||
// IPipelineStepHandler | ||
handlerType = assembly.GetType(config.Class); | ||
|
||
if (!typeof(IPipelineStepHandler).IsAssignableFrom(handlerType)) | ||
{ | ||
throw new ConfigurationException($"Handler assembly not found: {config.Class} doesn't implement interface {nameof(IPipelineStepHandler)}"); | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
namespace Microsoft.KernelMemory.Configuration; | ||
|
||
public class HandlerConfig | ||
{ | ||
/// <summary> | ||
/// .NET assembly containing the handler class | ||
/// </summary> | ||
public string Assembly { get; set; } | ||
|
||
/// <summary> | ||
/// .NET class in the assembly, containing the handler logic | ||
/// </summary> | ||
public string Class { get; set; } | ||
|
||
public HandlerConfig() | ||
{ | ||
this.Assembly = string.Empty; | ||
this.Class = string.Empty; | ||
} | ||
|
||
public HandlerConfig(string assembly, string className) | ||
{ | ||
this.Assembly = assembly; | ||
this.Class = className; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.