mybean.console.loader.dll.pas 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. (*
  2. * Unit owner: D10.天地弦
  3. * blog: http://www.cnblogs.com/dksoft
  4. *
  5. * v0.1.1(2014-11-06 21:27:40)
  6. * 修正checkIsValidLib- bug, 释放时判断是否BPL,bpl按照BPL释放的方式
  7. *
  8. * v0.1.0(2014-08-29 13:00)
  9. * 修改加载方式(beanMananger.dll-改造)
  10. *
  11. * v0.0.1(2014-05-17)
  12. * + first release
  13. *
  14. *
  15. *)
  16. unit mybean.console.loader.dll;
  17. interface
  18. uses
  19. Windows, SysUtils, Classes, mybean.core.intf, superobject, uSOTools,
  20. mybean.console.loader;
  21. type
  22. /// <summary>
  23. /// DLL文件管理
  24. /// </summary>
  25. TLibFactoryObject = class(TBaseFactoryObject)
  26. private
  27. FLibHandle:THandle;
  28. FlibFileName: String;
  29. private
  30. procedure doInitalizeBeanFactory;
  31. procedure doCreatePluginFactory;
  32. procedure doInitialize;
  33. procedure SetlibFileName(const Value: String);
  34. public
  35. procedure checkInitialize; override;
  36. procedure cleanup;override;
  37. /// <summary>
  38. /// 判断指定的Lib文件是否是MyBean的插件文件
  39. /// </summary>
  40. function checkIsValidLib(pvUnLoadIfSucc: Boolean = false): Boolean; override;
  41. /// <summary>
  42. /// 根据beanID获取插件
  43. /// </summary>
  44. function getBean(pvBeanID:string): IInterface; override;
  45. /// <summary>
  46. /// 释放Dll句柄
  47. /// </summary>
  48. procedure doFreeLibrary;
  49. /// <summary>
  50. /// 加载dll文件
  51. /// </summary>
  52. function checkLoadLibrary(pvRaiseIfNil: Boolean = true): Boolean;
  53. /// <summary>
  54. /// DLL文件
  55. /// </summary>
  56. property libFileName: String read FlibFileName write SetlibFileName;
  57. end;
  58. implementation
  59. uses
  60. LoggerImport;
  61. procedure TLibFactoryObject.doCreatePluginFactory;
  62. var
  63. lvFunc:function:IBeanFactory; stdcall;
  64. begin
  65. @lvFunc := GetProcAddress(FLibHandle, PChar('getBeanFactory'));
  66. if (@lvFunc = nil) then
  67. begin
  68. raise Exception.CreateFmt('非法的Plugin模块文件(%s),找不到入口函数(getBeanFactory)', [self.FlibFileName]);
  69. end;
  70. FBeanFactory := lvFunc;
  71. end;
  72. procedure TLibFactoryObject.doFreeLibrary;
  73. begin
  74. FBeanFactory := nil;
  75. if FLibHandle <> 0 then
  76. begin
  77. if LowerCase(ExtractFileExt(FlibFileName)) = '.bpl' then
  78. begin
  79. UnloadPackage(FLibHandle);
  80. end else
  81. begin
  82. FreeLibrary(FLibHandle);
  83. end;
  84. end;
  85. end;
  86. procedure TLibFactoryObject.doInitalizeBeanFactory;
  87. var
  88. lvFunc:procedure(appContext: IApplicationContext; appKeyMap: IKeyMap); stdcall;
  89. begin
  90. @lvFunc := GetProcAddress(FLibHandle, PChar('initializeBeanFactory'));
  91. if (@lvFunc = nil) then
  92. begin
  93. raise Exception.CreateFmt(
  94. '非法的Plugin模块文件(%s),找不到入口函数(initializeBeanFactory)',
  95. [self.FlibFileName]);
  96. end;
  97. lvFunc(appPluginContext, applicationKeyMap);
  98. end;
  99. procedure TLibFactoryObject.doInitialize;
  100. begin
  101. doInitalizeBeanFactory;
  102. doCreatePluginFactory;
  103. FbeanFactory.checkInitalize;
  104. end;
  105. procedure TLibFactoryObject.checkInitialize;
  106. var
  107. lvConfigStr, lvBeanID:AnsiString;
  108. lvBeanConfig:ISuperObject;
  109. i: Integer;
  110. begin
  111. if FbeanFactory = nil then
  112. begin
  113. checkLoadLibrary;
  114. //将配置传入到beanFactory中
  115. for i := 0 to FConfig.A['list'].Length-1 do
  116. begin
  117. lvBeanConfig := FConfig.A['list'].O[i];
  118. lvBeanID := AnsiString(lvBeanConfig.S['id']);
  119. lvConfigStr := AnsiString(lvBeanConfig.AsJSon(false, false));
  120. //配置单个bean
  121. FbeanFactory.configBean(PAnsiChar(lvBeanID), PAnsiChar(lvConfigStr));
  122. end;
  123. end;
  124. //避免提前释放
  125. lvConfigStr := '';
  126. lvBeanID:= '';
  127. end;
  128. function TLibFactoryObject.checkIsValidLib(pvUnLoadIfSucc: Boolean = false): Boolean;
  129. var
  130. lvFunc:procedure(appContext: IApplicationContext; appKeyMap: IKeyMap); stdcall;
  131. lvLibHandle:THandle;
  132. lvIsBpl:Boolean;
  133. begin
  134. Result := False;
  135. if FLibHandle = 0 then
  136. begin
  137. lvIsBpl :=LowerCase(ExtractFileExt(FlibFileName)) = '.bpl';
  138. if lvIsBpl then
  139. begin
  140. lvLibHandle := LoadPackage(FlibFileName);
  141. end else
  142. begin
  143. lvLibHandle := LoadLibrary(PChar(FlibFileName));
  144. end;
  145. if lvLibHandle <> 0 then
  146. begin
  147. try
  148. @lvFunc := GetProcAddress(lvLibHandle, PChar('initializeBeanFactory'));
  149. result := (@lvFunc <> nil);
  150. finally
  151. if Result then
  152. begin
  153. if pvUnLoadIfSucc then
  154. begin
  155. if lvIsBpl then
  156. begin
  157. UnloadPackage(lvLibHandle);
  158. end else
  159. begin
  160. FreeLibrary(lvLibHandle);
  161. end;
  162. end else
  163. begin
  164. FLibHandle := lvLibHandle;
  165. end;
  166. end;
  167. end;
  168. end else
  169. begin
  170. Result := False;
  171. end;
  172. end else
  173. begin // 已经成功加载
  174. Result := true;
  175. end;
  176. end;
  177. function TLibFactoryObject.checkLoadLibrary(pvRaiseIfNil: Boolean = true): Boolean;
  178. begin
  179. if FLibHandle = 0 then
  180. begin
  181. if not FileExists(FlibFileName) then
  182. begin
  183. if pvRaiseIfNil then
  184. begin
  185. raise Exception.Create('文件[' + FlibFileName + ']未找到!');
  186. end;
  187. Result := false;
  188. end else
  189. begin
  190. if LowerCase(ExtractFileExt(FlibFileName)) = '.bpl' then
  191. begin
  192. FLibHandle := LoadPackage(FlibFileName);
  193. end else
  194. begin
  195. FLibHandle := LoadLibrary(PChar(FlibFileName));
  196. end;
  197. end;
  198. end;
  199. Result := FLibHandle <> 0;
  200. if not Result then RaiseLastOSError;
  201. if Result then DoInitialize;
  202. end;
  203. procedure TLibFactoryObject.cleanup;
  204. begin
  205. try
  206. doFreeLibrary;
  207. except
  208. on E: Exception do
  209. Debug(E.Message, 'TLibFactoryObject.cleanup-' + Self.FlibFileName);
  210. end;
  211. end;
  212. function TLibFactoryObject.getBean(pvBeanID:string): IInterface;
  213. begin
  214. result := inherited getBean(pvBeanID);
  215. end;
  216. procedure TLibFactoryObject.SetlibFileName(const Value: String);
  217. begin
  218. FlibFileName := Value;
  219. Fnamespace := FlibFileName;
  220. end;
  221. end.