mybean.tools.beanFactory.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. (*
  2. * Unit owner: D10.天地弦
  3. * blog: http://www.cnblogs.com/dksoft
  4. *
  5. * v0.1.0(2014-08-29 13:00)
  6. * 修改加载方式(beanMananger.dll-改造)
  7. *
  8. * v0.0.1(2014-05-17)
  9. * + first release
  10. *
  11. *
  12. *)
  13. unit mybean.tools.beanFactory;
  14. interface
  15. uses
  16. mybean.core.intf, SysUtils;
  17. type
  18. TMyBeanFactoryTools = class(TObject)
  19. public
  20. /// <summary>
  21. /// 获取applicationContext接口
  22. /// </summary>
  23. class function applicationContext: IApplicationContext;
  24. class procedure checkRaiseErrorINfo(const pvIntf: IInterface);
  25. /// <summary>
  26. /// 根据beanID获取对应的插件接口,
  27. /// 如果beanID对应的配置为单实例模式,相应的对象只会创建一次
  28. /// </summary>
  29. class function getBean(pvBeanID: string; pvRaiseIfNil: Boolean = true): IInterface;
  30. /// <summary>
  31. /// 释放插件
  32. /// </summary>
  33. class procedure freeBeanInterface(const pvInterface:IInterface);
  34. /// <summary>
  35. /// 存放全局的对象
  36. /// </summary>
  37. class procedure setObject(const pvID: AnsiString; const pvObject: IInterface);
  38. /// <summary>
  39. /// 设置得到全局的接口对象
  40. /// </summary>
  41. class function getObject(const pvID:AnsiString):IInterface;
  42. /// <summary>
  43. /// 判断插件是否存在
  44. /// </summary>
  45. class function existsObject(pvID:String): Boolean;
  46. /// <summary>
  47. /// 移除全局的接口对象
  48. /// </summary>
  49. class procedure removeObject(pvID:AnsiString);
  50. end;
  51. implementation
  52. class function TMyBeanFactoryTools.applicationContext: IApplicationContext;
  53. begin
  54. Result := appPluginContext;
  55. end;
  56. class procedure TMyBeanFactoryTools.checkRaiseErrorINfo(const pvIntf: IInterface);
  57. var
  58. lvErr:IErrorINfo;
  59. lvErrCode:Integer;
  60. lvErrDesc:AnsiString;
  61. j:Integer;
  62. begin
  63. if pvIntf = nil then exit;
  64. if pvIntf.QueryInterface(IErrorINfo, lvErr) = S_OK then
  65. begin
  66. lvErrCode := lvErr.getErrorCode;
  67. if lvErrCode <> 0 then
  68. begin
  69. j:=lvErr.getErrorDesc(nil, 0);
  70. if j = 0 then
  71. begin
  72. lvErrDesc := '未知的错误信息';
  73. end else
  74. begin
  75. if j > 2048 then j := 2048;
  76. SetLength(lvErrDesc, j + 1);
  77. j := lvErr.getErrorDesc(PAnsiChar(lvErrDesc), j);
  78. lvErrDesc[j+1] := #0;
  79. end;
  80. if lvErrCode = -1 then
  81. begin
  82. raise Exception.Create(string(lvErrDesc));
  83. end else
  84. begin
  85. raise Exception.CreateFmt('错误信息:%s' + sLineBreak + '错误代码:%d', [lvErrDesc, lvErrCode]);
  86. end;
  87. end;
  88. end;
  89. end;
  90. class function TMyBeanFactoryTools.existsObject(pvID: String): Boolean;
  91. begin
  92. Result := applicationKeyMap.existsObject(PAnsiChar(AnsiString(pvID)));
  93. end;
  94. class procedure TMyBeanFactoryTools.freeBeanInterface(
  95. const pvInterface: IInterface);
  96. var
  97. lvFree:IFreeObject;
  98. begin
  99. if pvInterface.QueryInterface(IFreeObject, lvFree) = S_OK then
  100. begin
  101. lvFree.FreeObject;
  102. lvFree := nil;
  103. end;
  104. end;
  105. class function TMyBeanFactoryTools.getBean(pvBeanID: string; pvRaiseIfNil: Boolean
  106. = true): IInterface;
  107. var
  108. lvFactory:IBeanFactory;
  109. begin
  110. lvFactory := applicationContext.getBeanFactory(PAnsiChar(AnsiString(pvBeanID))) as IBeanFactory;
  111. if lvFactory = nil then
  112. begin
  113. if pvRaiseIfNil then
  114. raise Exception.CreateFmt('找不到插件[%s]对应的工厂', [pvBeanID]);
  115. end else
  116. begin
  117. result := lvFactory.getBean(PAnsiChar(AnsiString(pvBeanID)));
  118. if (Result = nil) and (pvRaiseIfNil) then
  119. begin
  120. checkRaiseErrorINfo(lvFactory);
  121. end;
  122. end;
  123. end;
  124. class function TMyBeanFactoryTools.getObject(const pvID: AnsiString): IInterface;
  125. begin
  126. Result := applicationKeyMap.getObject(PAnsiChar(pvID));
  127. end;
  128. class procedure TMyBeanFactoryTools.removeObject(pvID: AnsiString);
  129. begin
  130. if applicationKeyMap <> nil then
  131. applicationKeyMap.removeObject(PAnsiChar(pvID));
  132. end;
  133. class procedure TMyBeanFactoryTools.setObject(const pvID: AnsiString; const
  134. pvObject: IInterface);
  135. begin
  136. applicationKeyMap.setObject(PAnsiChar(pvID), pvObject);
  137. end;
  138. end.