| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | #nullable enable |
| | 4 | |
|
| | 5 | | using System.Collections; |
| | 6 | | using System.Diagnostics; |
| | 7 | |
|
| | 8 | | namespace Ice; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Applications implement this interface to provide a plug-in factory |
| | 12 | | /// to the Ice run time. |
| | 13 | | /// </summary> |
| | 14 | | public interface PluginFactory |
| | 15 | | { |
| | 16 | | /// <summary>Gets the default and preferred name for plug-ins created by this factory.</summary> |
| | 17 | | string pluginName { get; } |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Called by the Ice run time to create a new plug-in. |
| | 21 | | /// </summary> |
| | 22 | | /// |
| | 23 | | /// <param name="communicator">The communicator that is in the process of being initialized.</param> |
| | 24 | | /// <param name="name">The name of the plug-in.</param> |
| | 25 | | /// <param name="args">The arguments that are specified in the plug-ins configuration.</param> |
| | 26 | | /// <returns>The plug-in that was created by this method.</returns> |
| | 27 | | Plugin create(Communicator communicator, string name, string[] args); |
| | 28 | | } |
| | 29 | |
|
| | 30 | | internal sealed class PluginManagerI : PluginManager |
| | 31 | | { |
| | 32 | | private const string _kindOfObject = "plugin"; |
| | 33 | |
|
| | 34 | | public void initializePlugins() |
| | 35 | | { |
| 1 | 36 | | if (_initialized) |
| | 37 | | { |
| 0 | 38 | | throw new InitializationException("Plug-ins already initialized."); |
| | 39 | | } |
| | 40 | |
|
| | 41 | | // |
| | 42 | | // Invoke initialize() on the plug-ins, in the order they were loaded. |
| | 43 | | // |
| 1 | 44 | | var initializedPlugins = new ArrayList(); |
| | 45 | | try |
| | 46 | | { |
| 1 | 47 | | foreach (PluginInfo p in _plugins) |
| | 48 | | { |
| | 49 | | try |
| | 50 | | { |
| 1 | 51 | | p.plugin.initialize(); |
| 1 | 52 | | } |
| 0 | 53 | | catch (PluginInitializationException) |
| | 54 | | { |
| 0 | 55 | | throw; |
| | 56 | | } |
| 1 | 57 | | catch (System.Exception ex) |
| | 58 | | { |
| 1 | 59 | | throw new PluginInitializationException($"Plugin '{p.name}' initialization failed.", ex); |
| | 60 | | } |
| 1 | 61 | | initializedPlugins.Add(p.plugin); |
| | 62 | | } |
| 1 | 63 | | } |
| 1 | 64 | | catch (System.Exception) |
| | 65 | | { |
| | 66 | | // |
| | 67 | | // Destroy the plug-ins that have been successfully initialized, in the |
| | 68 | | // reverse order. |
| | 69 | | // |
| 1 | 70 | | initializedPlugins.Reverse(); |
| 1 | 71 | | foreach (Plugin p in initializedPlugins) |
| | 72 | | { |
| | 73 | | try |
| | 74 | | { |
| 1 | 75 | | p.destroy(); |
| 1 | 76 | | } |
| 0 | 77 | | catch (System.Exception) |
| | 78 | | { |
| | 79 | | // Ignore. |
| 0 | 80 | | } |
| | 81 | | } |
| 1 | 82 | | throw; |
| | 83 | | } |
| | 84 | |
|
| 1 | 85 | | _initialized = true; |
| 1 | 86 | | } |
| | 87 | |
|
| | 88 | | public string[] getPlugins() |
| | 89 | | { |
| 0 | 90 | | lock (_mutex) |
| | 91 | | { |
| 0 | 92 | | var names = new ArrayList(); |
| 0 | 93 | | foreach (PluginInfo p in _plugins) |
| | 94 | | { |
| 0 | 95 | | names.Add(p.name); |
| | 96 | | } |
| 0 | 97 | | return (string[])names.ToArray(typeof(string)); |
| | 98 | | } |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public Plugin getPlugin(string name) |
| | 102 | | { |
| 1 | 103 | | lock (_mutex) |
| | 104 | | { |
| 1 | 105 | | if (_communicator is null) |
| | 106 | | { |
| 0 | 107 | | throw new CommunicatorDestroyedException(); |
| | 108 | | } |
| | 109 | |
|
| 1 | 110 | | Plugin? p = findPlugin(name); |
| 1 | 111 | | if (p is not null) |
| | 112 | | { |
| 1 | 113 | | return p; |
| | 114 | | } |
| | 115 | |
|
| 0 | 116 | | throw new NotRegisteredException(_kindOfObject, name); |
| | 117 | | } |
| 1 | 118 | | } |
| | 119 | |
|
| | 120 | | public void addPlugin(string name, Plugin plugin) |
| | 121 | | { |
| 1 | 122 | | lock (_mutex) |
| | 123 | | { |
| 1 | 124 | | if (_communicator is null) |
| | 125 | | { |
| 0 | 126 | | throw new CommunicatorDestroyedException(); |
| | 127 | | } |
| | 128 | |
|
| 1 | 129 | | if (findPlugin(name) is not null) |
| | 130 | | { |
| 0 | 131 | | throw new AlreadyRegisteredException(_kindOfObject, name); |
| | 132 | | } |
| | 133 | |
|
| 1 | 134 | | _plugins.Add(new PluginInfo(name, plugin)); |
| 1 | 135 | | } |
| 1 | 136 | | } |
| | 137 | |
|
| | 138 | | public void destroy() |
| | 139 | | { |
| 1 | 140 | | lock (_mutex) |
| | 141 | | { |
| 1 | 142 | | if (_communicator is not null) |
| | 143 | | { |
| 1 | 144 | | if (_initialized) |
| | 145 | | { |
| 1 | 146 | | var plugins = (ArrayList)_plugins.Clone(); |
| 1 | 147 | | plugins.Reverse(); |
| 1 | 148 | | foreach (PluginInfo p in plugins) |
| | 149 | | { |
| | 150 | | try |
| | 151 | | { |
| 1 | 152 | | p.plugin.destroy(); |
| 1 | 153 | | } |
| 0 | 154 | | catch (System.Exception ex) |
| | 155 | | { |
| 0 | 156 | | Util.getProcessLogger().warning("unexpected exception raised by plug-in `" + |
| 0 | 157 | | p.name + "' destruction:\n" + ex.ToString()); |
| 0 | 158 | | } |
| | 159 | | } |
| | 160 | | } |
| | 161 | |
|
| 1 | 162 | | _communicator = null; |
| | 163 | | } |
| 1 | 164 | | } |
| 1 | 165 | | } |
| | 166 | |
|
| 1 | 167 | | internal PluginManagerI(Communicator communicator) |
| | 168 | | { |
| 1 | 169 | | _communicator = communicator; |
| 1 | 170 | | _plugins = new ArrayList(); |
| 1 | 171 | | _initialized = false; |
| 1 | 172 | | } |
| | 173 | |
|
| | 174 | | internal void loadPlugins() |
| | 175 | | { |
| | 176 | | Debug.Assert(_communicator is not null); |
| 1 | 177 | | string prefix = "Ice.Plugin."; |
| 1 | 178 | | Properties properties = _communicator.getProperties(); |
| 1 | 179 | | Dictionary<string, string> plugins = properties.getPropertiesForPrefix(prefix); |
| | 180 | |
|
| | 181 | | // First, create plug-ins using the plug-in factories from initData, in order. |
| 1 | 182 | | foreach (PluginFactory pluginFactory in _communicator.instance.initializationData().pluginFactories) |
| | 183 | | { |
| 1 | 184 | | string name = pluginFactory.pluginName; |
| 1 | 185 | | string key = $"Ice.Plugin.{name}"; |
| 1 | 186 | | if (plugins.TryGetValue(key, out string? pluginSpec)) |
| | 187 | | { |
| 0 | 188 | | loadPlugin(pluginFactory, name, pluginSpec); |
| 0 | 189 | | plugins.Remove(key); |
| | 190 | | } |
| | 191 | | else |
| | 192 | | { |
| 1 | 193 | | loadPlugin(pluginFactory, name, ""); |
| | 194 | | } |
| | 195 | | } |
| | 196 | |
|
| | 197 | | // |
| | 198 | | // Load and initialize the plug-ins defined in the property set |
| | 199 | | // with the prefix "Ice.Plugin.". These properties should |
| | 200 | | // have the following format: |
| | 201 | | // |
| | 202 | | // Ice.Plugin.<name>=entry_point [args] |
| | 203 | | // |
| | 204 | | // The code below is different from the Java/C++ algorithm |
| | 205 | | // because C# must support full assembly names such as: |
| | 206 | | // |
| | 207 | | // Ice.Plugin.Logger=logger, Version=0.0.0.0, Culture=neutral:LoginPluginFactory |
| | 208 | | // |
| | 209 | | // If the Ice.PluginLoadOrder property is defined, load the |
| | 210 | | // specified plug-ins in the specified order, then load any |
| | 211 | | // remaining plug-ins. |
| | 212 | | // |
| | 213 | |
|
| 1 | 214 | | string[] loadOrder = properties.getIcePropertyAsList("Ice.PluginLoadOrder"); |
| 1 | 215 | | for (int i = 0; i < loadOrder.Length; ++i) |
| | 216 | | { |
| 1 | 217 | | if (loadOrder[i].Length == 0) |
| | 218 | | { |
| | 219 | | continue; |
| | 220 | | } |
| | 221 | |
|
| 1 | 222 | | if (findPlugin(loadOrder[i]) is not null) |
| | 223 | | { |
| 0 | 224 | | throw new PluginInitializationException($"Plug-in '{loadOrder[i]}' already loaded."); |
| | 225 | | } |
| | 226 | |
|
| 1 | 227 | | string key = $"Ice.Plugin.{loadOrder[i]}"; |
| 1 | 228 | | plugins.TryGetValue(key, out string? value); |
| 1 | 229 | | if (value is not null) |
| | 230 | | { |
| 1 | 231 | | loadPlugin(null, loadOrder[i], value); |
| 1 | 232 | | plugins.Remove(key); |
| | 233 | | } |
| | 234 | | else |
| | 235 | | { |
| 0 | 236 | | throw new PluginInitializationException($"Plug-in '{loadOrder[i]}' not defined."); |
| | 237 | | } |
| | 238 | | } |
| | 239 | |
|
| | 240 | | // |
| | 241 | | // Load any remaining plug-ins that weren't specified in PluginLoadOrder. |
| | 242 | | // |
| 1 | 243 | | foreach (KeyValuePair<string, string> entry in plugins) |
| | 244 | | { |
| 1 | 245 | | loadPlugin(null, entry.Key[prefix.Length..], entry.Value); |
| | 246 | | } |
| 1 | 247 | | } |
| | 248 | |
|
| | 249 | | private void loadPlugin(PluginFactory? pluginFactory, string name, string pluginSpec) |
| | 250 | | { |
| | 251 | | Debug.Assert(_communicator is not null); |
| | 252 | |
|
| 1 | 253 | | if (findPlugin(name) is not null) |
| | 254 | | { |
| 0 | 255 | | throw new AlreadyRegisteredException(_kindOfObject, name); |
| | 256 | | } |
| | 257 | |
|
| 1 | 258 | | string[] args = []; |
| 1 | 259 | | string entryPoint = ""; |
| 1 | 260 | | if (pluginSpec.Length > 0) |
| | 261 | | { |
| | 262 | | // |
| | 263 | | // Split the entire property value into arguments. An entry point containing spaces |
| | 264 | | // must be enclosed in quotes. |
| | 265 | | // |
| | 266 | | try |
| | 267 | | { |
| 1 | 268 | | args = Ice.UtilInternal.Options.split(pluginSpec); |
| 1 | 269 | | } |
| 0 | 270 | | catch (ParseException ex) |
| | 271 | | { |
| 0 | 272 | | throw new PluginInitializationException($"Invalid arguments for plug-in '{name}'.", ex); |
| | 273 | | } |
| | 274 | |
|
| | 275 | | Debug.Assert(args.Length > 0); |
| | 276 | |
|
| 1 | 277 | | entryPoint = args[0]; |
| | 278 | |
|
| | 279 | | // |
| | 280 | | // Shift the arguments. |
| | 281 | | // |
| 1 | 282 | | string[] tmp = new string[args.Length - 1]; |
| 1 | 283 | | Array.Copy(args, 1, tmp, 0, args.Length - 1); |
| 1 | 284 | | args = tmp; |
| | 285 | |
|
| | 286 | | // Convert command-line options into properties. |
| 1 | 287 | | Properties properties = _communicator.getProperties(); |
| 1 | 288 | | args = properties.parseCommandLineOptions(name, args); |
| | 289 | | } |
| | 290 | |
|
| 1 | 291 | | string err = "unable to load plug-in `" + entryPoint + "': "; |
| | 292 | |
|
| 1 | 293 | | if (pluginFactory is null) |
| | 294 | | { |
| | 295 | | // |
| | 296 | | // Extract the assembly name and the class name. |
| | 297 | | // |
| 1 | 298 | | int sepPos = entryPoint.IndexOf(':', StringComparison.Ordinal); |
| 1 | 299 | | if (sepPos != -1) |
| | 300 | | { |
| | 301 | | const string driveLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 1 | 302 | | if (entryPoint.Length > 3 && |
| 1 | 303 | | sepPos == 1 && |
| 1 | 304 | | driveLetters.Contains(entryPoint[0], StringComparison.Ordinal) && |
| 1 | 305 | | (entryPoint[2] == '\\' || entryPoint[2] == '/')) |
| | 306 | | { |
| 0 | 307 | | sepPos = entryPoint.IndexOf(':', 3); |
| | 308 | | } |
| | 309 | | } |
| 1 | 310 | | if (sepPos == -1) |
| | 311 | | { |
| 0 | 312 | | throw new PluginInitializationException($"{err}invalid entry point format"); |
| | 313 | | } |
| | 314 | |
|
| 1 | 315 | | string assemblyName = entryPoint[..sepPos]; |
| 1 | 316 | | string className = entryPoint[(sepPos + 1)..]; |
| | 317 | |
|
| | 318 | | System.Reflection.Assembly? pluginAssembly; |
| | 319 | | try |
| | 320 | | { |
| | 321 | | // |
| | 322 | | // First try to load the assembly using Assembly.Load, which will succeed |
| | 323 | | // if a fully-qualified name is provided or if a partial name has been qualified |
| | 324 | | // in configuration. If that fails, try Assembly.LoadFrom(), which will succeed |
| | 325 | | // if a file name is configured or a partial name is configured and DEVPATH is used. |
| | 326 | | // |
| | 327 | | // We catch System.Exception as this can fail with System.ArgumentNullException |
| | 328 | | // or System.IO.IOException depending of the .NET framework and platform. |
| | 329 | | // |
| | 330 | | try |
| | 331 | | { |
| 1 | 332 | | pluginAssembly = System.Reflection.Assembly.Load(assemblyName); |
| 1 | 333 | | } |
| 1 | 334 | | catch (System.Exception ex) |
| | 335 | | { |
| | 336 | | try |
| | 337 | | { |
| 1 | 338 | | pluginAssembly = System.Reflection.Assembly.LoadFrom(assemblyName); |
| 1 | 339 | | } |
| 0 | 340 | | catch (System.IO.IOException) |
| | 341 | | { |
| | 342 | | #pragma warning disable CA2200 // Rethrow to preserve stack details |
| 0 | 343 | | throw ex; |
| | 344 | | #pragma warning restore CA2200 // Rethrow to preserve stack details |
| | 345 | | } |
| 1 | 346 | | } |
| 1 | 347 | | } |
| 0 | 348 | | catch (System.Exception ex) |
| | 349 | | { |
| 0 | 350 | | throw new PluginInitializationException($"{err}unable to load assembly '{assemblyName}'.", ex); |
| | 351 | | } |
| | 352 | |
|
| | 353 | | // |
| | 354 | | // Instantiate the class. |
| | 355 | | // |
| | 356 | | Type? c; |
| | 357 | | try |
| | 358 | | { |
| 1 | 359 | | c = pluginAssembly.GetType(className, true); |
| 1 | 360 | | } |
| 0 | 361 | | catch (System.Exception ex) |
| | 362 | | { |
| 0 | 363 | | throw new PluginInitializationException($"{err}GetType failed for '{className}'.", ex); |
| | 364 | | } |
| | 365 | |
|
| | 366 | | try |
| | 367 | | { |
| 1 | 368 | | pluginFactory = (PluginFactory)Ice.Internal.AssemblyUtil.createInstance(c); |
| 1 | 369 | | if (pluginFactory is null) |
| | 370 | | { |
| 0 | 371 | | throw new PluginInitializationException($"{err}can't find constructor for '{className}'."); |
| | 372 | | } |
| 1 | 373 | | } |
| 0 | 374 | | catch (System.Exception ex) |
| | 375 | | { |
| 0 | 376 | | throw new PluginInitializationException($"{err}SystemException", ex); |
| | 377 | | } |
| | 378 | | } |
| | 379 | |
|
| | 380 | | Plugin? plugin; |
| | 381 | | try |
| | 382 | | { |
| 1 | 383 | | plugin = pluginFactory.create(_communicator, name, args); |
| 1 | 384 | | } |
| 1 | 385 | | catch (PluginInitializationException) |
| | 386 | | { |
| 1 | 387 | | throw; |
| | 388 | | } |
| 0 | 389 | | catch (System.Exception ex) |
| | 390 | | { |
| 0 | 391 | | throw new PluginInitializationException($"{err}System.Exception in factory.create", ex); |
| | 392 | | } |
| | 393 | |
|
| 1 | 394 | | if (plugin is null) |
| | 395 | | { |
| 0 | 396 | | throw new PluginInitializationException($"{err}factory.create returned null plug-in"); |
| | 397 | | } |
| | 398 | |
|
| 1 | 399 | | _plugins.Add(new PluginInfo(name, plugin)); |
| 1 | 400 | | } |
| | 401 | |
|
| | 402 | | private Plugin? findPlugin(string name) |
| | 403 | | { |
| 1 | 404 | | foreach (PluginInfo p in _plugins) |
| | 405 | | { |
| 1 | 406 | | if (name.Equals(p.name, StringComparison.Ordinal)) |
| | 407 | | { |
| 1 | 408 | | return p.plugin; |
| | 409 | | } |
| | 410 | | } |
| 1 | 411 | | return null; |
| 1 | 412 | | } |
| | 413 | |
|
| 1 | 414 | | internal record class PluginInfo(string name, Plugin plugin); |
| | 415 | |
|
| | 416 | | private Communicator? _communicator; |
| | 417 | | private readonly ArrayList _plugins; |
| | 418 | | private bool _initialized; |
| 1 | 419 | | private readonly object _mutex = new(); |
| | 420 | | } |