|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Reflection; |
| 9 | +using Microsoft.Azure.WebJobs.Host; |
| 10 | +using Microsoft.Azure.WebJobs.Host.Config; |
| 11 | +using Microsoft.Azure.WebJobs.Script.Config; |
| 12 | +using Microsoft.Azure.WebJobs.Script.Description; |
| 13 | +using Microsoft.Extensions.Logging; |
| 14 | + |
| 15 | +namespace Microsoft.Azure.WebJobs.Script.Binding |
| 16 | +{ |
| 17 | + internal class ExtensionLoader |
| 18 | + { |
| 19 | + private readonly TraceWriter _traceWriter; |
| 20 | + private readonly ILogger _startupLogger; |
| 21 | + private readonly ScriptHostConfiguration _config; |
| 22 | + private readonly bool _dynamicExtensionLoadingEnabled; |
| 23 | + |
| 24 | + // The set of "built in" binding types. These are types that are directly accesible without |
| 25 | + // needing an explicit load gesture. |
| 26 | + private static IReadOnlyDictionary<string, string> _builtinBindingTypes = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) |
| 27 | + { |
| 28 | + { "bot", "Microsoft.Azure.WebJobs.Extensions.BotFramework.Config.BotFrameworkConfiguration, Microsoft.Azure.WebJobs.Extensions.BotFramework" }, |
| 29 | + { "sendgrid", "Microsoft.Azure.WebJobs.Extensions.SendGrid.SendGridConfiguration, Microsoft.Azure.WebJobs.Extensions.SendGrid" }, |
| 30 | + { "eventGridTrigger", "Microsoft.Azure.WebJobs.Extensions.EventGrid.EventGridExtensionConfig, Microsoft.Azure.WebJobs.Extensions.EventGrid" } |
| 31 | + }; |
| 32 | + |
| 33 | + // The set of "built in" binding types that are built on the older/obsolete ScriptBindingProvider APIs |
| 34 | + // Over time these will migrate to the above set as they are reworked. |
| 35 | + private static IReadOnlyDictionary<string, string> _builtinScriptBindingTypes = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) |
| 36 | + { |
| 37 | + { "twilioSms", "Microsoft.Azure.WebJobs.Script.Binding.TwilioScriptBindingProvider" }, |
| 38 | + { "notificationHub", "Microsoft.Azure.WebJobs.Script.Binding.NotificationHubScriptBindingProvider" }, |
| 39 | + { "cosmosDBTrigger", "Microsoft.Azure.WebJobs.Script.Binding.DocumentDBScriptBindingProvider" }, |
| 40 | + { "documentDB", "Microsoft.Azure.WebJobs.Script.Binding.DocumentDBScriptBindingProvider" }, |
| 41 | + { "mobileTable", "Microsoft.Azure.WebJobs.Script.Binding.MobileAppsScriptBindingProvider" }, |
| 42 | + { "apiHubFileTrigger", "Microsoft.Azure.WebJobs.Script.Binding.ApiHubScriptBindingProvider" }, |
| 43 | + { "apiHubFile", "Microsoft.Azure.WebJobs.Script.Binding.ApiHubScriptBindingProvider" }, |
| 44 | + { "apiHubTable", "Microsoft.Azure.WebJobs.Script.Binding.ApiHubScriptBindingProvider" }, |
| 45 | + { "serviceBusTrigger", "Microsoft.Azure.WebJobs.Script.Binding.ServiceBusScriptBindingProvider" }, |
| 46 | + { "serviceBus", "Microsoft.Azure.WebJobs.Script.Binding.ServiceBusScriptBindingProvider" }, |
| 47 | + { "eventHubTrigger", "Microsoft.Azure.WebJobs.Script.Binding.ServiceBusScriptBindingProvider" }, |
| 48 | + { "eventHub", "Microsoft.Azure.WebJobs.Script.Binding.ServiceBusScriptBindingProvider" }, |
| 49 | + }; |
| 50 | + |
| 51 | + public ExtensionLoader(ScriptHostConfiguration config, TraceWriter traceWriter, ILogger startupLogger) |
| 52 | + { |
| 53 | + _config = config; |
| 54 | + _traceWriter = traceWriter; |
| 55 | + _startupLogger = startupLogger; |
| 56 | + _dynamicExtensionLoadingEnabled = FeatureFlags.IsEnabled(ScriptConstants.FeatureFlagsEnableDynamicExtensionLoading); |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Get the ScriptBindingProviderType for a given binding type. |
| 61 | + /// </summary> |
| 62 | + /// <param name="bindingTypeName">The Type name of the binding.</param> |
| 63 | + /// <returns>The binding provider Type, or null.</returns> |
| 64 | + public static Type GetScriptBindingProvider(string bindingTypeName) |
| 65 | + { |
| 66 | + string assemblyQualifiedTypeName; |
| 67 | + if (_builtinScriptBindingTypes.TryGetValue(bindingTypeName, out assemblyQualifiedTypeName)) |
| 68 | + { |
| 69 | + var type = Type.GetType(assemblyQualifiedTypeName); |
| 70 | + return type; |
| 71 | + } |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + public void LoadCustomExtensions() |
| 76 | + { |
| 77 | + string extensionsPath = _config.RootExtensionsPath; |
| 78 | + if (!string.IsNullOrWhiteSpace(extensionsPath)) |
| 79 | + { |
| 80 | + foreach (var dir in Directory.EnumerateDirectories(extensionsPath)) |
| 81 | + { |
| 82 | + LoadCustomExtensions(dir); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private void LoadCustomExtensions(string extensionsPath) |
| 88 | + { |
| 89 | + foreach (var path in Directory.EnumerateFiles(extensionsPath, "*.dll")) |
| 90 | + { |
| 91 | + // We don't want to load and reflect over every dll. |
| 92 | + // By convention, restrict to based on filenames. |
| 93 | + var filename = Path.GetFileName(path); |
| 94 | + if (!filename.ToLowerInvariant().Contains("extension")) |
| 95 | + { |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + try |
| 100 | + { |
| 101 | + // See GetNugetPackagesPath() for details |
| 102 | + // Script runtime is already setup with assembly resolution hooks, so use LoadFrom |
| 103 | + Assembly assembly = Assembly.LoadFrom(path); |
| 104 | + LoadExtensions(assembly, path); |
| 105 | + } |
| 106 | + catch (Exception e) |
| 107 | + { |
| 108 | + string msg = $"Failed to load custom extension from '{path}'."; |
| 109 | + _traceWriter.Error(msg, e); |
| 110 | + _startupLogger.LogError(0, e, msg); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // Load extensions that are directly referenced by the user types. |
| 116 | + public void LoadDirectlyReferencedExtensions(IEnumerable<Type> userTypes) |
| 117 | + { |
| 118 | + var possibleExtensionAssemblies = UserTypeScanner.GetPossibleExtensionAssemblies(userTypes); |
| 119 | + |
| 120 | + foreach (var kv in possibleExtensionAssemblies) |
| 121 | + { |
| 122 | + var assembly = kv.Key; |
| 123 | + var locationHint = kv.Value; |
| 124 | + LoadExtensions(assembly, locationHint); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private void LoadExtensions(Assembly assembly, string locationHint) |
| 129 | + { |
| 130 | + foreach (var type in assembly.ExportedTypes) |
| 131 | + { |
| 132 | + if (!typeof(IExtensionConfigProvider).IsAssignableFrom(type)) |
| 133 | + { |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + if (IsExtensionLoaded(type)) |
| 138 | + { |
| 139 | + continue; |
| 140 | + } |
| 141 | + |
| 142 | + var extensionConfigProvider = (IExtensionConfigProvider)Activator.CreateInstance(type); |
| 143 | + LoadExtension(extensionConfigProvider, locationHint); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private bool IsExtensionLoaded(Type type) |
| 148 | + { |
| 149 | + var registry = _config.HostConfig.GetService<IExtensionRegistry>(); |
| 150 | + var extensions = registry.GetExtensions<IExtensionConfigProvider>(); |
| 151 | + foreach (var extension in extensions) |
| 152 | + { |
| 153 | + var loadedExtentionType = extension.GetType(); |
| 154 | + if (loadedExtentionType == type) |
| 155 | + { |
| 156 | + return true; |
| 157 | + } |
| 158 | + } |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
| 162 | + private void LoadExtension(IExtensionConfigProvider extensionConfigProvider, string locationHint = null) |
| 163 | + { |
| 164 | + var extensionConfigProviderType = extensionConfigProvider.GetType(); |
| 165 | + string extensionName = extensionConfigProviderType.Name; |
| 166 | + |
| 167 | + string msg = null; |
| 168 | + if (!string.IsNullOrEmpty(locationHint)) |
| 169 | + { |
| 170 | + msg = $"Loaded binding extension '{extensionName}' from '{locationHint}'"; |
| 171 | + } |
| 172 | + else |
| 173 | + { |
| 174 | + msg = $"Loaded custom extension '{extensionName}'"; |
| 175 | + } |
| 176 | + |
| 177 | + _traceWriter.Info(msg); |
| 178 | + _startupLogger.LogInformation(msg); |
| 179 | + _config.HostConfig.AddExtension(extensionConfigProvider); |
| 180 | + } |
| 181 | + |
| 182 | + public void LoadBuiltinExtensions(IEnumerable<string> bindingTypes) |
| 183 | + { |
| 184 | + foreach (var bindingType in bindingTypes) |
| 185 | + { |
| 186 | + string assemblyQualifiedTypeName; |
| 187 | + if (_builtinBindingTypes.TryGetValue(bindingType, out assemblyQualifiedTypeName)) |
| 188 | + { |
| 189 | + Type typeExtension = Type.GetType(assemblyQualifiedTypeName); |
| 190 | + if (typeExtension == null) |
| 191 | + { |
| 192 | + string errorMsg = $"Can't find binding provider '{assemblyQualifiedTypeName}' for '{bindingType}'"; |
| 193 | + _traceWriter.Error(errorMsg); |
| 194 | + _startupLogger?.LogError(errorMsg); |
| 195 | + } |
| 196 | + else |
| 197 | + { |
| 198 | + IExtensionConfigProvider extension = (IExtensionConfigProvider)Activator.CreateInstance(typeExtension); |
| 199 | + LoadExtension(extension); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + public IEnumerable<string> DiscoverBindingTypes(IEnumerable<FunctionMetadata> functions) |
| 206 | + { |
| 207 | + if (_dynamicExtensionLoadingEnabled) |
| 208 | + { |
| 209 | + // only load binding extensions that are actually used |
| 210 | + var bindingTypes = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| 211 | + foreach (var function in functions) |
| 212 | + { |
| 213 | + foreach (var binding in function.InputBindings.Concat(function.OutputBindings)) |
| 214 | + { |
| 215 | + bindingTypes.Add(binding.Type); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + return bindingTypes; |
| 220 | + } |
| 221 | + else |
| 222 | + { |
| 223 | + // if not enabled, load all binding extensions |
| 224 | + return _builtinBindingTypes.Keys.Concat(_builtinScriptBindingTypes.Keys).ToArray(); |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | +} |
0 commit comments