ReflectCommandLoader.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. using SuperSocket.Common;
  7. using SuperSocket.SocketBase.Protocol;
  8. using SuperSocket.SocketBase.Config;
  9. namespace SuperSocket.SocketBase.Command
  10. {
  11. /// <summary>
  12. /// A command loader which loads commands from assembly by reflection
  13. /// </summary>
  14. public class ReflectCommandLoader<TCommand> : CommandLoaderBase<TCommand>
  15. where TCommand : class, ICommand
  16. {
  17. /// <summary>
  18. /// Initializes a new instance of the <see cref="ReflectCommandLoader{TCommand}"/> class.
  19. /// </summary>
  20. public ReflectCommandLoader()
  21. {
  22. }
  23. private IAppServer m_AppServer;
  24. /// <summary>
  25. /// Initializes the command loader by the root config and the server instance.
  26. /// </summary>
  27. /// <param name="rootConfig">The root config.</param>
  28. /// <param name="appServer">The app server.</param>
  29. /// <returns></returns>
  30. public override bool Initialize(IRootConfig rootConfig, IAppServer appServer)
  31. {
  32. m_AppServer = appServer;
  33. return true;
  34. }
  35. /// <summary>
  36. /// Tries to load commands.
  37. /// </summary>
  38. /// <param name="commands">The commands.</param>
  39. /// <returns></returns>
  40. public override bool TryLoadCommands(out IEnumerable<TCommand> commands)
  41. {
  42. commands = null;
  43. var commandAssemblies = new List<Assembly>();
  44. if (m_AppServer.GetType().Assembly != this.GetType().Assembly)
  45. commandAssemblies.Add(m_AppServer.GetType().Assembly);
  46. string commandAssembly = m_AppServer.Config.Options.GetValue("commandAssembly");
  47. if (!string.IsNullOrEmpty(commandAssembly))
  48. {
  49. OnError("The configuration attribute 'commandAssembly' is not in used, please try to use the child node 'commandAssemblies' instead!");
  50. return false;
  51. }
  52. if (m_AppServer.Config.CommandAssemblies != null && m_AppServer.Config.CommandAssemblies.Any())
  53. {
  54. try
  55. {
  56. var definedAssemblies = AssemblyUtil.GetAssembliesFromStrings(m_AppServer.Config.CommandAssemblies.Select(a => a.Assembly).ToArray());
  57. if (definedAssemblies.Any())
  58. commandAssemblies.AddRange(definedAssemblies);
  59. }
  60. catch (Exception e)
  61. {
  62. OnError(new Exception("Failed to load defined command assemblies!", e));
  63. return false;
  64. }
  65. }
  66. if (!commandAssemblies.Any())
  67. {
  68. commandAssemblies.Add(Assembly.GetEntryAssembly());
  69. }
  70. var outputCommands = new List<TCommand>();
  71. foreach (var assembly in commandAssemblies)
  72. {
  73. try
  74. {
  75. outputCommands.AddRange(assembly.GetImplementedObjectsByInterface<TCommand>());
  76. }
  77. catch (Exception exc)
  78. {
  79. OnError(new Exception(string.Format("Failed to get commands from the assembly {0}!", assembly.FullName), exc));
  80. return false;
  81. }
  82. }
  83. commands = outputCommands;
  84. return true;
  85. }
  86. }
  87. }