ServiceBase.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.IO;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using log4net;
  32. using Nini.Config;
  33. namespace OpenSim.Services.Base
  34. {
  35. public class ServiceBase
  36. {
  37. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  38. public T LoadPlugin<T>(string dllName) where T:class
  39. {
  40. return LoadPlugin<T>(dllName, new Object[0]);
  41. }
  42. public T LoadPlugin<T>(string dllName, Object[] args) where T:class
  43. {
  44. // The path:type separator : is unfortunate because it collides
  45. // with Windows paths like C:\...
  46. // When the path provided includes the drive, this fails.
  47. // Hence the root/noroot thing going on here.
  48. string pathRoot = Path.GetPathRoot(dllName);
  49. string noRoot = dllName.Substring(pathRoot.Length);
  50. string[] parts = noRoot.Split(new char[] {':'});
  51. dllName = pathRoot + parts[0];
  52. string className = String.Empty;
  53. if (parts.Length > 1)
  54. className = parts[1];
  55. return LoadPlugin<T>(dllName, className, args);
  56. }
  57. public T LoadPlugin<T>(string dllName, string className, Object[] args) where T:class
  58. {
  59. string interfaceName = typeof(T).ToString();
  60. try
  61. {
  62. Assembly pluginAssembly = Assembly.LoadFrom(dllName);
  63. //m_log.DebugFormat("[SERVICE BASE]: Found assembly {0}", dllName);
  64. foreach (Type pluginType in pluginAssembly.GetTypes())
  65. {
  66. //m_log.DebugFormat("[SERVICE BASE]: Found type {0}", pluginType);
  67. if (pluginType.IsPublic)
  68. {
  69. if (!string.IsNullOrEmpty(className) &&
  70. pluginType.ToString() != pluginType.Namespace + "." + className)
  71. continue;
  72. Type typeInterface = pluginType.GetInterface(interfaceName);
  73. if (typeInterface is not null)
  74. {
  75. return (T)Activator.CreateInstance(pluginType, args);
  76. }
  77. }
  78. }
  79. return null;
  80. }
  81. catch (Exception e)
  82. {
  83. List<string> strArgs = new List<string>();
  84. foreach (Object arg in args)
  85. strArgs.Add(arg.ToString());
  86. m_log.ErrorFormat(
  87. "[SERVICE BASE]: Failed to load plugin {0} from {1} with args {2}",
  88. interfaceName, dllName, string.Join(", ", strArgs.ToArray()), e);
  89. if (e.InnerException != null)
  90. m_log.Error($"[SERVICE BASE]: inner exception {e.InnerException.Message}");
  91. return null;
  92. }
  93. }
  94. public ServiceBase(IConfigSource config)
  95. {
  96. }
  97. }
  98. }