mybean.console.pas 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. (*
  2. * Unit owner: D10.天地弦
  3. * blog: http://www.cnblogs.com/dksoft
  4. *
  5. * 2014-11-14 13:59:05
  6. * 添加GetBeanInfos
  7. *
  8. * v0.1.2 (2014-09-06 10:50:44)
  9. * 为了满足各种加载需求, 去掉自动加载方式,
  10. * 需要在工程开始的地方调用applicationContextInitialize进行初始化
  11. *
  12. * v0.1.1 (2014-09-03 23:46:16)
  13. * 添加 IApplicationContextEx01接口
  14. * 可以实现手动加载DLL和配置文件
  15. *
  16. *
  17. * v0.1.0(2014-08-29 13:00)
  18. * 修改加载方式(beanMananger.dll-改造)
  19. *
  20. * v0.0.1(2014-05-17)
  21. * + first release
  22. *
  23. *
  24. *)
  25. unit mybean.console;
  26. {$I 'MyBean.inc'}
  27. interface
  28. {$WARN UNIT_PLATFORM OFF}
  29. {$WARN SYMBOL_PLATFORM OFF}
  30. uses
  31. Classes, SysUtils, Windows, ShLwApi,
  32. mybean.core.intf,
  33. mybean.console.loader,
  34. mybean.console.loader.dll,
  35. mybean.strConsts,
  36. uKeyInterface, IniFiles,
  37. {$IFDEF CONSOLE}
  38. {$ELSE}
  39. // 引用Forms单元,避免在Application和Screen对象释放之后清理该单元
  40. {$if CompilerVersion < 23}
  41. Forms,
  42. {$else}
  43. Vcl.Forms,
  44. {$ifend}
  45. {$ENDIF}
  46. safeLogger;
  47. type
  48. TApplicationContext = class(TInterfacedObject
  49. , IApplicationContext
  50. , IApplicationContextEx01
  51. , IApplicationContextEx2
  52. , IApplicationContextEx3
  53. , IbeanFactoryRegister
  54. )
  55. private
  56. FINIFile:TIniFile;
  57. FTraceLoadFile: Boolean;
  58. /// <summary>
  59. /// 保存FactoryObject列表,LibFile -> FactoryObject
  60. /// </summary>
  61. FFactoryObjectList: TStrings;
  62. /// <summary>
  63. /// 保存beanID和FactoryObject的对应关系
  64. /// </summary>
  65. FBeanMapList: TStrings;
  66. procedure DoRegisterPluginIDS(pvPluginIDS: String; pvFactoryObject:
  67. TBaseFactoryObject);
  68. procedure DoRegisterPlugins(pvPlugins: TStrings; pvFactoryObject:
  69. TBaseFactoryObject);
  70. procedure checkCreateINIFile;
  71. function checkInitializeFactoryObject(pvFactoryObject:TBaseFactoryObject;
  72. pvRaiseException:Boolean): Boolean;
  73. procedure removeRegistedBeans(pvLibFile:string);
  74. protected
  75. /// <summary>
  76. /// 卸载掉指定的插件宿主文件(dll)
  77. /// 在卸载之前应该释放掉由所创建的对象实例,和分配的内存空间,
  78. /// 否则会在退出EXE的时候,出现内存访问违规错误
  79. /// 卸载如果出现问题,请查看日志文件
  80. /// *(谨慎使用)
  81. /// </summary>
  82. function unLoadLibraryFile(pvLibFile: PAnsiChar; pvRaiseException: Boolean =
  83. true): Boolean; stdcall;
  84. /// <summary>
  85. /// 判断BeanID是否存在
  86. /// </summary>
  87. function checkBeanExists(pvBeanID:PAnsiChar):Boolean; stdcall;
  88. /// <summary>
  89. /// 获取所有Bean信息
  90. /// result: 返回读取到的数据长度
  91. /// pvLength: 尝试读取的长度,传入的pvBeanInfo必须分配有足够的内存
  92. /// pvBeanInfo: 返回读取到的数据
  93. /// utf8 AnsiString
  94. /// [
  95. /// {"id":"beanid", "lib":"libfile"}
  96. /// ...
  97. /// ]
  98. /// </summary>
  99. function GetBeanInfos(pvBeanInfo:PAnsiChar; pvLength:Integer): Integer; stdcall;
  100. protected
  101. /// <summary>
  102. /// 加载库文件
  103. /// </summary>
  104. /// <returns>
  105. /// 加载成功返回true, 失败返回false, 可以用raiseLastOsError获取异常
  106. /// </returns>
  107. /// <param name="pvLibFile"> (PAnsiChar) </param>
  108. function checkLoadLibraryFile(pvLibFile:PAnsiChar): Boolean; stdcall;
  109. /// <summary>
  110. /// 加载配置文件
  111. /// </summary>
  112. /// <returns>
  113. /// 加载失败返回false<文件可能不存在>
  114. /// </returns>
  115. /// <param name="pvConfigFile"> (PAnsiChar) </param>
  116. function checkLoadBeanConfigFile(pvConfigFile:PAnsiChar): Boolean; stdcall;
  117. protected
  118. /// <summary>
  119. /// 直接从DLL和BPL文件中加载插件,在没有配置文件的情况下执行
  120. /// plug-ins\*.DLL, plug-ins\*.BPL, *.DLL
  121. /// </summary>
  122. procedure executeLoadLibrary; stdcall;
  123. /// <summary>
  124. /// 加载一个库文件, 获取其中插件,并进行注册
  125. /// </summary>
  126. procedure checkLoadALibFile(pvFile:string);
  127. /// <summary>
  128. /// 根据提供的Lib文件得到TLibFactoryObject对象,如果不列表中不存在则新增一个对象
  129. /// </summary>
  130. function checkCreateLibObject(pvFileName:string): TLibFactoryObject;
  131. /// <summary>
  132. /// 从FLibFactory中移除,加载失败时进行移除
  133. /// </summary>
  134. /// <returns>
  135. /// 如果移除返回true
  136. /// </returns>
  137. /// <param name="pvFileName"> 要移除的文件名(全路径) </param>
  138. function checkRemoveLibObjectFromList(pvFileName:String): Boolean;
  139. private
  140. /// <summary>
  141. /// Copy的目的文件
  142. /// </summary>
  143. FCopyDestPath: String;
  144. /// <summary>
  145. /// 应用程序根目录
  146. /// </summary>
  147. FRootPath:String;
  148. /// <summary>
  149. /// 从单个配置文件中配置插件, 返回成功处理的Bean配置数量
  150. /// 会整理配置中Bean对应libFile库对象(TLibFactoryObject)
  151. /// </summary>
  152. function executeLoadFromConfigFile(pvFileName: String): Integer;
  153. /// <summary>
  154. /// 从多个配置文件中读取配置插件, 返回成功处理的Bean配置数量
  155. /// </summary>
  156. function executeLoadFromConfigFiles(pvFiles: TStrings): Integer;
  157. /// <summary>
  158. /// 准备工作,读取配置文件
  159. /// </summary>
  160. procedure checkReady;
  161. /// <summary>
  162. /// 关联Bean和Lib对象(往FBeanMapList中注册关系)
  163. /// </summary>
  164. function checkRegisterBean(pvBeanID: string; pvFactoryObject:
  165. TBaseFactoryObject): Boolean;
  166. /// <summary>
  167. /// 从配置文件中加载, 返回成功处理的Bean配置数量
  168. /// </summary>
  169. function checkInitializeFromConfigFiles(pvConfigFiles: string): Integer;
  170. /// <summary>
  171. /// 初始化工厂对象
  172. /// </summary>
  173. procedure checkInitializeFactoryObjects;
  174. public
  175. constructor Create;
  176. procedure BeforeDestruction; override;
  177. destructor Destroy; override;
  178. /// <summary>
  179. /// 执行反初始化
  180. /// </summary>
  181. procedure checkFinalize; stdcall;
  182. /// <summary>
  183. /// 执行初始化
  184. /// </summary>
  185. procedure checkInitialize; stdcall;
  186. /// <summary>
  187. /// 获取根据BeanID获取一个对象
  188. /// </summary>
  189. function getBean(pvBeanID: PAnsiChar): IInterface; stdcall;
  190. /// <summary>
  191. /// 获取beanID对应的工厂接口
  192. /// </summary>
  193. function getBeanFactory(pvBeanID:PAnsiChar): IInterface; stdcall;
  194. protected
  195. /// <summary>
  196. /// 直接注册Bean工厂插件
  197. /// </summary>
  198. function registerBeanFactory(const pvFactory: IBeanFactory; const pvNameSapce:PAnsiChar):Integer;stdcall;
  199. public
  200. //1 根据基础路径和相对路径获取绝对路径(杨茂丰)
  201. class function getAbsolutePath(BasePath, RelativePath: string): string;
  202. class function getFileNameList(vFileList: TStrings; const aSearchPath: string):
  203. integer;
  204. class function instance: TApplicationContext;
  205. class function pathWithBackslash(const Path: string): String;
  206. class function pathWithoutBackslash(const Path: string): string;
  207. end;
  208. TKeyMapImpl = class(TInterfacedObject, IKeyMap)
  209. private
  210. FKeyIntface:TKeyInterface;
  211. protected
  212. function _AddRef: Integer; stdcall;
  213. function _Release: Integer; stdcall;
  214. protected
  215. /// <summary>
  216. /// 判断是否存在接口
  217. /// </summary>
  218. function existsObject(const pvKey:PAnsiChar):Boolean; stdcall;
  219. /// <summary>
  220. /// 根据key值获取接口
  221. /// </summary>
  222. function getObject(const pvKey:PAnsiChar):IInterface; stdcall;
  223. /// <summary>
  224. /// 赋值接口
  225. /// </summary>
  226. procedure setObject(const pvKey:PAnsiChar; const pvIntf: IInterface); stdcall;
  227. /// <summary>
  228. /// 移除接口
  229. /// </summary>
  230. procedure removeObject(const pvKey:PAnsiChar); stdcall;
  231. /// <summary>
  232. /// 清理对象
  233. /// </summary>
  234. procedure cleanupObjects; stdcall;
  235. public
  236. procedure AfterConstruction; override;
  237. destructor Destroy; override;
  238. end;
  239. /// <summary>
  240. /// 获取全局的appliationContext
  241. /// </summary>
  242. function appPluginContext: IApplicationContext; stdcall;
  243. /// <summary>
  244. /// 应用程序清理
  245. /// </summary>
  246. procedure appContextCleanup; stdcall;
  247. /// <summary>
  248. /// 注册beanFactory
  249. /// </summary>
  250. function registerFactoryObject(const pvBeanFactory:IBeanFactory; const
  251. pvNameSapce:PAnsiChar): Integer; stdcall;
  252. procedure executeKeyMapCleanup;
  253. /// <summary>
  254. /// 获取全局的KeyMap接口
  255. /// </summary>
  256. function applicationKeyMap: IKeyMap; stdcall;
  257. /// <summary>
  258. /// 加载文件
  259. /// executeLoadLibFiles('plugin\*.dll');
  260. /// </summary>
  261. procedure executeLoadLibFiles(const pvLibFiles: string);
  262. /// <summary>
  263. /// 执行初始化,如果已经初始化过,则会跳过
  264. /// 根据 ini的配置进行初始化,
  265. /// 如果没有配置文件,直接加载 *.dll和plug-ins\*.dll
  266. /// </summary>
  267. procedure applicationContextInitialize;
  268. /// <summary>
  269. /// 应用程序退出时可以手动调用该方法,
  270. /// 可以清理全局对象,卸载DLL
  271. /// </summary>
  272. procedure applicationContextFinalize;
  273. procedure logDebugInfo;
  274. /// <summary>
  275. /// 产生一个Hash值
  276. /// QDAC群-Hash函数
  277. /// </summary>
  278. function hashOf(const p:Pointer;l:Integer): Integer; overload;
  279. /// <summary>
  280. /// 产生一个Hash值
  281. /// </summary>
  282. function hashOf(const vStrData:String): Integer; overload;
  283. implementation
  284. uses
  285. superobject, uSOTools, Dialogs;
  286. var
  287. __instanceAppContext:TApplicationContext;
  288. __instanceAppContextAppContextIntf:IInterface;
  289. __instanceKeyMap:TKeyMapImpl;
  290. __instanceKeyMapKeyIntf:IInterface;
  291. __beanLogger:TSafeLogger;
  292. function appPluginContext: IApplicationContext;
  293. begin
  294. Result := TApplicationContext.instance;
  295. end;
  296. procedure appContextCleanup; stdcall;
  297. begin
  298. //清理KeyMap对象
  299. executeKeyMapCleanup;
  300. if __instanceAppContextAppContextIntf = nil then exit;
  301. try
  302. try
  303. __instanceAppContext.checkFinalize;
  304. except
  305. end;
  306. except
  307. end;
  308. end;
  309. function applicationKeyMap: IKeyMap;
  310. begin
  311. Result := __instanceKeyMap;
  312. end;
  313. procedure executeKeyMapCleanup;
  314. begin
  315. if __instanceKeyMapKeyIntf = nil then exit;
  316. try
  317. __instanceKeyMap.cleanupObjects;
  318. except
  319. end;
  320. end;
  321. function registerFactoryObject(const pvBeanFactory:IBeanFactory; const
  322. pvNameSapce:PAnsiChar): Integer;
  323. begin
  324. try
  325. Result := TApplicationContext.instance.registerBeanFactory(pvBeanFactory, pvNameSapce);
  326. except
  327. Result := -1;
  328. end;
  329. end;
  330. procedure logDebugInfo;
  331. begin
  332. if __instanceKeyMapKeyIntf = nil then exit;
  333. try
  334. if __instanceKeyMap.RefCount > 1 then
  335. begin
  336. __beanLogger.logMessage(sDebug_applicationKeyMapUnload,
  337. [__instanceKeyMap.RefCount-1], 'DEBUG_');
  338. end;
  339. except
  340. end;
  341. if __instanceAppContextAppContextIntf = nil then exit;
  342. try
  343. if __instanceAppContext.RefCount > 1 then
  344. begin
  345. __beanLogger.logMessage(sDebug_applicationContextUnload,
  346. [__instanceAppContext.RefCount-1], 'DEBUG_');
  347. end;
  348. except
  349. end;
  350. end;
  351. function hashOf(const p:Pointer;l:Integer): Integer;
  352. var
  353. ps:PInteger;
  354. lr:Integer;
  355. begin
  356. Result:=0;
  357. if l>0 then
  358. begin
  359. ps:=p;
  360. lr:=(l and $03);//检查长度是否为4的整数倍
  361. l:=(l and $FFFFFFFC);//整数长度
  362. while l>0 do
  363. begin
  364. Result:=((Result shl 5) or (Result shr 27)) xor ps^;
  365. Inc(ps);
  366. Dec(l,4);
  367. end;
  368. if lr<>0 then
  369. begin
  370. l:=0;
  371. Move(ps^,l,lr);
  372. Result:=((Result shl 5) or (Result shr 27)) xor l;
  373. end;
  374. end;
  375. end;
  376. function hashOf(const vStrData:String): Integer;
  377. var
  378. lvStr:AnsiString;
  379. begin
  380. lvStr := AnsiString(vStrData);
  381. Result := hashOf(PAnsiChar(lvStr), Length(lvStr));
  382. end;
  383. procedure executeLoadLibFiles(const pvLibFiles: string);
  384. begin
  385. TApplicationContext.instance.checkLoadLibraryFile(PAnsiChar(AnsiString(pvLibFiles)));
  386. end;
  387. procedure applicationContextInitialize;
  388. begin
  389. appPluginContext.checkInitialize;
  390. end;
  391. procedure applicationContextFinalize;
  392. begin
  393. mybean.core.intf.appPluginContext := nil;
  394. mybean.core.intf.applicationKeyMap := nil;
  395. executeKeyMapCleanup;
  396. appContextCleanup;
  397. end;
  398. procedure TApplicationContext.checkInitialize;
  399. var
  400. lvConfigFiles:String;
  401. begin
  402. if FFactoryObjectList.Count = 0 then
  403. begin
  404. // 先读取配置文件
  405. lvConfigFiles := FINIFile.ReadString('main', 'beanConfigFiles', '');
  406. if lvConfigFiles <> '' then
  407. begin
  408. if FTraceLoadFile then
  409. __beanLogger.logMessage(sDebug_loadFromConfigFile, 'LOAD_TRACE_');
  410. if checkInitializeFromConfigFiles(lvConfigFiles) > 0 then
  411. begin
  412. if FINIFile.ReadBool('main', 'loadOnStartup', False) then
  413. begin
  414. //加载DLL文件, 把DLL载入
  415. checkInitializeFactoryObjects;
  416. end;
  417. end else
  418. begin
  419. if FTraceLoadFile then
  420. __beanLogger.logMessage(sDebug_NoneConfigFile, 'LOAD_TRACE_');
  421. end;
  422. end else
  423. begin
  424. if FTraceLoadFile then
  425. __beanLogger.logMessage(sDebug_directlyLoadLibFile, 'LOAD_TRACE_');
  426. executeLoadLibrary;
  427. end;
  428. end;
  429. end;
  430. procedure TApplicationContext.checkReady;
  431. var
  432. lvTempPath:String;
  433. l:Integer;
  434. begin
  435. lvTempPath := FINIFile.ReadString('main', 'copyDest', 'plug-ins\');
  436. FTraceLoadFile := FINIFile.ReadBool('main','traceLoadLib', FTraceLoadFile);
  437. __beanLogger.Enable := FTraceLoadFile;
  438. FCopyDestPath := GetAbsolutePath(FRootPath, lvTempPath);
  439. l := Length(FCopyDestPath);
  440. if l = 0 then
  441. begin
  442. FCopyDestPath := FRootPath + 'plug-ins\';
  443. end else
  444. begin
  445. FCopyDestPath := PathWithBackslash(FCopyDestPath);
  446. end;
  447. // try
  448. // ForceDirectories(FCopyDestPath);
  449. // except
  450. // on E:Exception do
  451. // begin
  452. // __beanLogger.logMessage(
  453. // '创建Copy目标文件夹[%s]出现异常:%s', [FCopyDestPath, e.Message],
  454. // 'LOAD_ERROR_');
  455. // end;
  456. // end;
  457. end;
  458. function TApplicationContext.checkRegisterBean(pvBeanID: string;
  459. pvFactoryObject: TBaseFactoryObject): Boolean;
  460. var
  461. j:Integer;
  462. lvID:String;
  463. lvLibObject:TBaseFactoryObject;
  464. begin
  465. Result := false;
  466. lvID := trim(pvBeanID);
  467. if (lvID <> '') then
  468. begin
  469. j := FBeanMapList.IndexOf(lvID);
  470. if j <> -1 then
  471. begin
  472. lvLibObject := TBaseFactoryObject(FBeanMapList.Objects[j]);
  473. {$IFDEF LOG_ON}
  474. __beanLogger.logMessage(Format(sLoadTrace_BeanID_Repeat,
  475. [lvID,lvLibObject.namespace]), 'LOAD_TRACE_');
  476. {$ENDIF}
  477. end else
  478. begin
  479. FBeanMapList.AddObject(lvID, pvFactoryObject);
  480. Result := true;
  481. end;
  482. end;
  483. end;
  484. procedure TApplicationContext.BeforeDestruction;
  485. begin
  486. inherited;
  487. end;
  488. procedure TApplicationContext.checkFinalize;
  489. var
  490. lvLibObject:TBaseFactoryObject;
  491. i:Integer;
  492. begin
  493. ///清理掉applicationKeyMap中的全局资源
  494. applicationKeyMap.cleanupObjects;
  495. ///全部执行一次Finalize;
  496. for i := 0 to FFactoryObjectList.Count -1 do
  497. begin
  498. lvLibObject := TBaseFactoryObject(FFactoryObjectList.Objects[i]);
  499. lvLibObject.checkFinalize;
  500. end;
  501. ///卸载DLL
  502. for i := 0 to FFactoryObjectList.Count -1 do
  503. begin
  504. try
  505. lvLibObject := TBaseFactoryObject(FFactoryObjectList.Objects[i]);
  506. lvLibObject.cleanup;
  507. lvLibObject.Free;
  508. except
  509. on E: Exception do
  510. ShowMessage(E.Message);
  511. end;
  512. end;
  513. FFactoryObjectList.Clear;
  514. FBeanMapList.Clear;
  515. end;
  516. constructor TApplicationContext.Create;
  517. begin
  518. inherited Create;
  519. FFactoryObjectList := TStringList.Create();
  520. FBeanMapList := TStringList.Create;
  521. FRootPath := ExtractFilePath(ParamStr(0));
  522. checkCreateINIFile;
  523. checkReady;
  524. end;
  525. destructor TApplicationContext.Destroy;
  526. begin
  527. FINIFile.Free;
  528. checkFinalize;
  529. FBeanMapList.Free;
  530. FFactoryObjectList.Free;
  531. inherited Destroy;
  532. end;
  533. function TApplicationContext.checkBeanExists(pvBeanID: PAnsiChar): Boolean;
  534. var
  535. lvBeanID:String;
  536. begin
  537. lvBeanID := String(AnsiString(pvBeanID));
  538. Result := FBeanMapList.IndexOf(lvBeanID)<> -1;
  539. end;
  540. procedure TApplicationContext.checkCreateINIFile;
  541. var
  542. lvFile:String;
  543. begin
  544. lvFile := ExtractFilePath(ParamStr(0)) + 'appConfig.ini';
  545. // 不存在配置文件
  546. if not FileExists(lvFile) then
  547. begin
  548. {$IFDEF LOG_ON}
  549. FTraceLoadFile := true;
  550. {$ELSE}
  551. FTraceLoadFile := False;
  552. {$ENDIF}
  553. __beanLogger.Enable := FTraceLoadFile;
  554. end;
  555. FINIFile := TIniFile.Create(lvFile);
  556. end;
  557. function TApplicationContext.checkRemoveLibObjectFromList(pvFileName:String):
  558. Boolean;
  559. var
  560. lvNameSpace:String;
  561. i:Integer;
  562. begin
  563. Result := False;
  564. lvNameSpace :=ExtractFileName(pvFileName) + '_' + IntToStr(hashOf(pvFileName));
  565. if Length(lvNameSpace) = 0 then Exit;
  566. i := FFactoryObjectList.IndexOf(lvNameSpace);
  567. if i <> -1 then
  568. begin
  569. Result := true;
  570. FFactoryObjectList.Delete(i);
  571. end;
  572. end;
  573. function TApplicationContext.checkCreateLibObject(pvFileName:string):
  574. TLibFactoryObject;
  575. var
  576. lvNameSpace:String;
  577. i:Integer;
  578. begin
  579. Result := nil;
  580. lvNameSpace :=ExtractFileName(pvFileName) + '_' + IntToStr(hashOf(pvFileName));
  581. if Length(lvNameSpace) = 0 then Exit;
  582. i := FFactoryObjectList.IndexOf(lvNameSpace);
  583. if i = -1 then
  584. begin
  585. Result := TLibFactoryObject.Create;
  586. Result.LibFileName := pvFileName;
  587. FFactoryObjectList.AddObject(lvNameSpace, Result);
  588. end else
  589. begin
  590. Result := TLibFactoryObject(FFactoryObjectList.Objects[i]);
  591. end;
  592. end;
  593. function TApplicationContext.checkInitializeFactoryObject(
  594. pvFactoryObject:TBaseFactoryObject; pvRaiseException:Boolean): Boolean;
  595. begin
  596. try
  597. if pvFactoryObject.beanFactory = nil then
  598. begin
  599. if FTraceLoadFile then
  600. begin
  601. __beanLogger.logMessage(
  602. sLoadTrace_Lib_Initalize, [String(pvFactoryObject.namespace)], 'LOAD_TRACE_');
  603. end;
  604. if pvRaiseException then
  605. begin // 抛出异常的情况下, 直接进行初始化
  606. pvFactoryObject.checkInitialize;
  607. end else
  608. begin //
  609. if pvFactoryObject.checkIsValidLib(False) then
  610. begin
  611. pvFactoryObject.checkInitialize;
  612. end else
  613. begin
  614. {$IFDEF LOG_ON}
  615. __beanLogger.logMessage(
  616. Format(sLoadTrace_Lib_Invalidate, [String(pvFactoryObject.namespace)]),
  617. 'LOAD_TRACE_');
  618. {$ENDIF}
  619. end;
  620. end;
  621. end;
  622. Result := pvFactoryObject.beanFactory <> nil;
  623. except
  624. on E:Exception do
  625. begin
  626. Result := false;
  627. {$IFDEF LOG_ON}
  628. __beanLogger.logMessage(
  629. sLoadTrace_Lib_Error,
  630. [String(pvFactoryObject.namespace), e.Message],
  631. 'LOAD_TRACE_');
  632. {$ENDIF}
  633. if pvRaiseException then
  634. raise;
  635. end;
  636. end;
  637. end;
  638. function TApplicationContext.getBean(pvBeanID: PAnsiChar): IInterface;
  639. var
  640. j:Integer;
  641. lvLibObject:TBaseFactoryObject;
  642. lvBeanID:String;
  643. begin
  644. Result := nil;
  645. lvBeanID := string(AnsiString(pvBeanID));
  646. j := FBeanMapList.IndexOf(lvBeanID);
  647. if j <> -1 then
  648. begin
  649. lvLibObject := TBaseFactoryObject(FBeanMapList.Objects[j]);
  650. Result := lvLibObject.getBean(lvBeanID);
  651. end;
  652. end;
  653. procedure TApplicationContext.DoRegisterPluginIDS(pvPluginIDS: String;
  654. pvFactoryObject: TBaseFactoryObject);
  655. var
  656. lvStrings:TStrings;
  657. begin
  658. lvStrings := TStringList.Create;
  659. try
  660. lvStrings.Text := pvPluginIDS;
  661. DoRegisterPlugins(lvStrings, pvFactoryObject);
  662. finally
  663. lvStrings.Free;
  664. end;
  665. end;
  666. procedure TApplicationContext.DoRegisterPlugins(pvPlugins: TStrings;
  667. pvFactoryObject: TBaseFactoryObject);
  668. var
  669. i, j:Integer;
  670. lvID:String;
  671. lvLibObject:TBaseFactoryObject;
  672. begin
  673. for i := 0 to pvPlugins.Count - 1 do
  674. begin
  675. lvID := trim(pvPlugins[i]);
  676. if (lvID <> '') then
  677. begin
  678. j := FBeanMapList.IndexOf(lvID);
  679. if j <> -1 then
  680. begin
  681. lvLibObject := TBaseFactoryObject(FBeanMapList.Objects[j]);
  682. {$IFDEF LOG_ON}
  683. __beanLogger.logMessage(Format(sLoadTrace_BeanID_Repeat,
  684. [lvID,lvLibObject.namespace]));
  685. {$ENDIF}
  686. end else
  687. begin
  688. FBeanMapList.AddObject(lvID, pvFactoryObject);
  689. end;
  690. end;
  691. end;
  692. end;
  693. procedure TApplicationContext.checkInitializeFactoryObjects;
  694. var
  695. i: Integer;
  696. lvFactoryObject:TBaseFactoryObject;
  697. begin
  698. for i := 0 to FFactoryObjectList.Count -1 do
  699. begin
  700. lvFactoryObject := TBaseFactoryObject(FFactoryObjectList.Objects[i]);
  701. try
  702. if FTraceLoadFile then
  703. __beanLogger.logMessage(sLoadTrace_Factory_Initalize, [string(lvFactoryObject.namespace)],
  704. 'LOAD_TRACE_');
  705. lvFactoryObject.checkInitialize;
  706. except
  707. on E:Exception do
  708. begin
  709. __beanLogger.logMessage(
  710. sLoadTrace_Lib_Error, [lvFactoryObject.namespace,e.Message],
  711. 'LOAD_TRACE_');
  712. end;
  713. end;
  714. end;
  715. end;
  716. function TApplicationContext.checkInitializeFromConfigFiles(pvConfigFiles:
  717. string): Integer;
  718. var
  719. lvFilesList, lvStrings: TStrings;
  720. i: Integer;
  721. lvStr, lvFileName, lvPath:String;
  722. begin
  723. Result := 0;
  724. lvStrings := TStringList.Create;
  725. lvFilesList := TStringList.Create;
  726. try
  727. lvFilesList.Text := StringReplace(pvConfigFiles, ',', sLineBreak, [rfReplaceAll]);
  728. for i := 0 to lvFilesList.Count - 1 do
  729. begin
  730. lvStr := lvFilesList[i];
  731. lvFileName := ExtractFileName(lvStr);
  732. lvPath := ExtractFilePath(lvStr);
  733. lvPath := GetAbsolutePath(FRootPath, lvPath);
  734. lvFileName := lvPath + lvFileName;
  735. lvStrings.Clear;
  736. getFileNameList(lvStrings, lvFileName);
  737. Result := Result + executeLoadFromConfigFiles(lvStrings);
  738. end;
  739. finally
  740. lvFilesList.Free;
  741. lvStrings.Free;
  742. end;
  743. end;
  744. procedure TApplicationContext.checkLoadALibFile(pvFile: string);
  745. var
  746. lvFile: string;
  747. lvLib:TLibFactoryObject;
  748. lvIsOK:Boolean;
  749. lvBeanIDs:array[1..4096] of AnsiChar;
  750. begin
  751. if pvFile = '' then exit;
  752. lvFile := pvFile;
  753. lvLib := checkCreateLibObject(lvFile);
  754. lvIsOK := false;
  755. try
  756. if lvLib.Tag = 1 then
  757. begin //已经加载
  758. lvIsOK := true;
  759. end else
  760. begin
  761. if checkInitializeFactoryObject(TBaseFactoryObject(lvLib), False) then
  762. begin
  763. try
  764. ZeroMemory(@lvBeanIDs[1], 4096);
  765. lvLib.beanFactory.getBeanList(@lvBeanIDs[1], 4096);
  766. DoRegisterPluginIDS(String(AnsiString(PAnsiChar(@lvBeanIDs[1]))), TBaseFactoryObject(lvLib));
  767. lvIsOK := true;
  768. lvLib.Tag := 1;
  769. except
  770. on E:Exception do
  771. begin
  772. {$IFDEF LOG_ON}
  773. __beanLogger.logMessage(sLoadTrace_Lib_Error, [lvLib.LibFileName, e.Message],
  774. 'LOAD_TRACE_');
  775. {$ENDIF}
  776. end;
  777. end;
  778. end;
  779. end;
  780. finally
  781. if not lvIsOK then
  782. begin
  783. try
  784. checkRemoveLibObjectFromList(lvFile);
  785. lvLib.DoFreeLibrary;
  786. lvLib.Free;
  787. except
  788. end;
  789. end;
  790. end;
  791. end;
  792. function TApplicationContext.checkLoadBeanConfigFile(
  793. pvConfigFile: PAnsiChar): Boolean;
  794. begin
  795. Result := checkInitializeFromConfigFiles(String(AnsiString(pvConfigFile))) > 0;
  796. end;
  797. function TApplicationContext.checkLoadLibraryFile(
  798. pvLibFile: PAnsiChar): Boolean;
  799. var
  800. lvFilesList, lvStrings: TStrings;
  801. i, j: Integer;
  802. lvStr, lvFileName, lvPath:String;
  803. begin
  804. lvStrings := TStringList.Create;
  805. lvFilesList := TStringList.Create;
  806. try
  807. lvStr :=String(AnsiString(pvLibFile));
  808. {$IFDEF LOG_ON}
  809. __beanLogger.logMessage('加载插件宿主文件[%s]', [lvStr], 'LOAD_TRACE_');
  810. {$ENDIF}
  811. lvFilesList.Text := StringReplace(lvStr, ',', sLineBreak, [rfReplaceAll]);
  812. for i := 0 to lvFilesList.Count - 1 do
  813. begin
  814. lvStr := lvFilesList[i];
  815. lvFileName := ExtractFileName(lvStr);
  816. lvPath := ExtractFilePath(lvStr);
  817. lvPath := GetAbsolutePath(FRootPath, lvPath);
  818. lvFileName := lvPath + lvFileName;
  819. lvStrings.Clear;
  820. getFileNameList(lvStrings, lvFileName);
  821. for j := 0 to lvStrings.Count -1 do
  822. begin
  823. checkLoadALibFile(trim(lvStrings[j]));
  824. end;
  825. end;
  826. Result := true;
  827. finally
  828. lvFilesList.Free;
  829. lvStrings.Free;
  830. end;
  831. end;
  832. function TApplicationContext.executeLoadFromConfigFile(pvFileName: String):
  833. Integer;
  834. var
  835. lvConfig, lvPluginList, lvItem:ISuperObject;
  836. I: Integer;
  837. lvLibFile, lvID:String;
  838. lvLibObj:TBaseFactoryObject;
  839. begin
  840. Result := 0;
  841. lvConfig := TSOTools.JsnParseFromFile(pvFileName);
  842. if lvConfig = nil then Exit;
  843. if lvConfig.IsType(stArray) then lvPluginList := lvConfig
  844. else if lvConfig.O['list'] <> nil then lvPluginList := lvConfig.O['list']
  845. else if lvConfig.O['plugins'] <> nil then lvPluginList := lvConfig.O['plugins'];
  846. if (lvPluginList = nil) or (not lvPluginList.IsType(stArray)) then
  847. begin
  848. {$IFDEF LOG_ON}
  849. __beanLogger.logMessage(Format('配置文件[%s]非法', [pvFileName]), 'LOAD_TRACE_');
  850. {$ENDIF}
  851. Exit;
  852. end;
  853. for I := 0 to lvPluginList.AsArray.Length - 1 do
  854. begin
  855. lvItem := lvPluginList.AsArray.O[i];
  856. lvLibFile := FRootPath + lvItem.S['lib'];
  857. if not FileExists(lvLibFile) then
  858. begin
  859. {$IFDEF LOG_ON}
  860. __beanLogger.logMessage(Format('未找到配置文件[%s]中的Lib文件[%s]', [pvFileName, lvLibFile]),
  861. 'LOAD_TRACE_');
  862. {$ENDIF}
  863. end else
  864. begin
  865. lvLibObj := TBaseFactoryObject(checkCreateLibObject(lvLibFile));
  866. if lvLibObj = nil then
  867. begin
  868. {$IFDEF LOG_ON}
  869. __beanLogger.logMessage(Format('未找到Lib文件[%s]', [lvLibFile]), 'LOAD_TRACE_');
  870. {$ENDIF}
  871. end else
  872. begin
  873. try
  874. lvID := lvItem.S['id'];
  875. if lvID = '' then
  876. begin
  877. raise Exception.Create('非法的插件配置,没有指定beanID:' + sLineBreak + lvItem.AsJSon(true, false));
  878. end;
  879. if checkRegisterBean(lvID, lvLibObj) then
  880. begin
  881. //将配置放到对应的节点管理中
  882. lvLibObj.addBeanConfig(lvItem);
  883. end;
  884. Inc(result);
  885. except
  886. on E:Exception do
  887. begin
  888. {$IFDEF LOG_ON}
  889. __beanLogger.logMessage(
  890. sLoadTrace_Lib_Error,
  891. [String(lvLibObj.namespace), e.Message],
  892. 'LOAD_TRACE_');
  893. {$ENDIF}
  894. end;
  895. end;
  896. end;
  897. end;
  898. end;
  899. end;
  900. function TApplicationContext.executeLoadFromConfigFiles(pvFiles: TStrings):
  901. Integer;
  902. var
  903. i:Integer;
  904. lvFile:String;
  905. begin
  906. Result := 0;
  907. for i := 0 to pvFiles.Count - 1 do
  908. begin
  909. lvFile := pvFiles[i];
  910. Result := Result + executeLoadFromConfigFile(lvFile);
  911. end;
  912. end;
  913. procedure TApplicationContext.executeLoadLibrary;
  914. var
  915. lvStrings: TStrings;
  916. i: Integer;
  917. lvFile: string;
  918. begin
  919. lvStrings := TStringList.Create;
  920. try
  921. getFileNameList(lvStrings, ExtractFilePath(ParamStr(0)) + 'plug-ins\*.dll');
  922. getFileNameList(lvStrings, ExtractFilePath(ParamStr(0)) + 'plug-ins\*.bpl');
  923. getFileNameList(lvStrings, ExtractFilePath(ParamStr(0)) + '*.dll');
  924. for i := 0 to lvStrings.Count - 1 do
  925. begin
  926. lvFile := lvStrings[i];
  927. checkLoadALibFile(lvFile);
  928. end;
  929. finally
  930. lvStrings.Free;
  931. end;
  932. end;
  933. function TApplicationContext.getBeanFactory(pvBeanID:PAnsiChar): IInterface;
  934. var
  935. j:Integer;
  936. lvLibObject:TBaseFactoryObject;
  937. lvBeanID:AnsiString;
  938. begin
  939. Result := nil;
  940. lvBeanID := pvBeanID;
  941. try
  942. j := FBeanMapList.IndexOf(String(lvBeanID));
  943. if j <> -1 then
  944. begin
  945. lvLibObject := TBaseFactoryObject(FBeanMapList.Objects[j]);
  946. if lvLibObject.beanFactory = nil then
  947. begin
  948. if FTraceLoadFile then
  949. __beanLogger.logMessage(sLoadTrace_Factory_Init_BEGIN, [lvLibObject.namespace],
  950. 'LOAD_TRACE_');
  951. lvLibObject.checkInitialize;
  952. if FTraceLoadFile then
  953. __beanLogger.logMessage(sLoadTrace_Factory_Init_END, [lvLibObject.namespace],
  954. 'LOAD_TRACE_');
  955. end;
  956. Result := lvLibObject.beanFactory;
  957. end else
  958. begin
  959. {$IFDEF LOG_ON}
  960. __beanLogger.logMessage(
  961. Format('找不到对应的[%s]插件工厂', [lvBeanID]),
  962. 'LOAD_TRACE_');
  963. {$ENDIF}
  964. end;
  965. except
  966. on E:Exception do
  967. begin
  968. __beanLogger.logMessage(
  969. Format('获取插件工厂[%s]出现异常', [lvBeanID]) + e.Message,
  970. 'LOAD_TRACE_');
  971. end;
  972. end;
  973. end;
  974. class function TApplicationContext.instance: TApplicationContext;
  975. begin
  976. Result := __instanceAppContext;
  977. end;
  978. class function TApplicationContext.getAbsolutePath(BasePath, RelativePath:
  979. string): string;
  980. var
  981. Dest: array[0..MAX_PATH] of Char;
  982. begin
  983. FillChar(Dest, SizeOf(Dest), 0);
  984. PathCombine(Dest, PChar(BasePath), PChar(RelativePath));
  985. Result := string(Dest);
  986. end;
  987. function TApplicationContext.GetBeanInfos(pvBeanInfo:PAnsiChar;
  988. pvLength:Integer): Integer;
  989. var
  990. i:Integer;
  991. lvLibObject:TBaseFactoryObject;
  992. lvJSon, lvItem:ISuperObject;
  993. s :AnsiString;
  994. begin
  995. lvJSon := SO('[]');
  996. for i := 0 to FBeanMapList.Count - 1 do
  997. begin
  998. lvLibObject := TBaseFactoryObject(FBeanMapList.Objects[i]);
  999. lvItem := SO();
  1000. lvItem.S['id'] := FBeanMapList.Strings[i];
  1001. if lvLibObject is TLibFactoryObject then
  1002. begin
  1003. lvItem.s['lib'] := TLibFactoryObject(lvLibObject).libFileName;
  1004. end else
  1005. begin
  1006. lvItem.s['lib'] := lvLibObject.namespace;
  1007. end;
  1008. lvJSon.AsArray.Add(lvItem);
  1009. end;
  1010. s := UTF8Encode(lvJSon.AsJSon(True, False));
  1011. Result := Length(s);
  1012. if pvBeanInfo <> nil then
  1013. begin
  1014. if pvLength < Result then Result := pvLength;
  1015. Move(s[1], pvBeanInfo^, Result);
  1016. end;
  1017. end;
  1018. class function TApplicationContext.getFileNameList(vFileList: TStrings; const
  1019. aSearchPath: string): integer;
  1020. var dirinfo: TSearchRec;
  1021. dir, lCurrentDir: string;
  1022. begin
  1023. result := 0;
  1024. lCurrentDir := GetCurrentDir;
  1025. SetCurrentDir(ExtractFileDir(ParamStr(0)));
  1026. try
  1027. dir := ExtractFilePath(ExpandFileName(aSearchPath));
  1028. if (dir <> '') then
  1029. dir := IncludeTrailingPathDelimiter(dir);
  1030. if (FindFirst(aSearchPath, faArchive, dirinfo) = 0) then repeat
  1031. vFileList.Add(dir + dirinfo.Name);
  1032. Inc(result);
  1033. until (FindNext(dirinfo) <> 0);
  1034. SysUtils.FindClose(dirinfo);
  1035. finally
  1036. SetCurrentDir(lCurrentDir);
  1037. end;
  1038. end;
  1039. class function TApplicationContext.pathWithBackslash(const Path: string):
  1040. String;
  1041. var
  1042. ilen: Integer;
  1043. begin
  1044. Result := Path;
  1045. ilen := Length(Result);
  1046. if (ilen > 0) and
  1047. {$IFDEF UNICODE}
  1048. not CharInSet(Result[ilen], ['\', '/'])
  1049. {$ELSE}
  1050. not (Result[ilen] in ['\', '/'])
  1051. {$ENDIF}
  1052. then
  1053. Result := Result + '\';
  1054. end;
  1055. class function TApplicationContext.pathWithoutBackslash(const Path: string):
  1056. string;
  1057. var
  1058. I, ilen: Integer;
  1059. begin
  1060. Result := Path;
  1061. ilen := Length(Result);
  1062. for I := ilen downto 1 do
  1063. begin
  1064. {$IFDEF UNICODE}
  1065. if not CharInSet(Result[I], ['\', '/', ' ', #13]) then Break;
  1066. {$ELSE}
  1067. if not (Result[I] in ['\', '/', ' ', #13]) then Break;
  1068. {$ENDIF}
  1069. end;
  1070. if I <> ilen then
  1071. SetLength(Result, I);
  1072. end;
  1073. function TApplicationContext.registerBeanFactory(const pvFactory: IBeanFactory;
  1074. const pvNameSapce: PAnsiChar): Integer;
  1075. var
  1076. lvObj:TFactoryInstanceObject;
  1077. lvBeanIDs:array[1..4096] of AnsiChar;
  1078. begin
  1079. lvObj := TFactoryInstanceObject.Create;
  1080. try
  1081. lvObj.setFactoryObject(pvFactory);
  1082. lvObj.setNameSpace(String(AnsiString(pvNameSapce)));
  1083. ZeroMemory(@lvBeanIDs[1], 4096);
  1084. lvObj.beanFactory.getBeanList(@lvBeanIDs[1], 4096);
  1085. DoRegisterPluginIDS(String(AnsiString(PAnsiChar(@lvBeanIDs[1]))), lvObj);
  1086. FFactoryObjectList.AddObject(String(AnsiString(pvNameSapce)), lvObj);
  1087. Result := 0;
  1088. except
  1089. Result := -1;
  1090. end;
  1091. end;
  1092. procedure TApplicationContext.removeRegistedBeans(pvLibFile: string);
  1093. var
  1094. i:Integer;
  1095. // lvNameSpace:String;
  1096. lvObj:TBaseFactoryObject;
  1097. begin
  1098. // lvNameSpace :=ExtractFileName(pvLibFile) + '_' + IntToStr(hashOf(pvLibFile));
  1099. // if Length(lvNameSpace) = 0 then Exit;
  1100. for i := FBeanMapList.Count - 1 downto 0 do
  1101. begin
  1102. lvObj := TBaseFactoryObject(FBeanMapList.Objects[i]);
  1103. if lvObj.namespace = pvLibFile then
  1104. begin
  1105. FBeanMapList.Delete(i);
  1106. end;
  1107. end;
  1108. end;
  1109. function TApplicationContext.unLoadLibraryFile(pvLibFile: PAnsiChar;
  1110. pvRaiseException: Boolean = true): Boolean;
  1111. var
  1112. lvNameSpace:String;
  1113. i:Integer;
  1114. lvObj:TBaseFactoryObject;
  1115. begin
  1116. Result := true;
  1117. lvNameSpace :=ExtractFileName(String(AnsiString(pvLibFile))) + '_' +
  1118. IntToStr(hashOf(String(AnsiString(pvLibFile))));
  1119. if Length(lvNameSpace) = 0 then Exit;
  1120. i := FFactoryObjectList.IndexOf(lvNameSpace);
  1121. if i <> -1 then
  1122. begin
  1123. lvObj := TBaseFactoryObject(FFactoryObjectList.Objects[i]);
  1124. try
  1125. FFactoryObjectList.Delete(i);
  1126. removeRegistedBeans(String(AnsiString(pvLibFile)));
  1127. lvObj.checkFinalize;
  1128. lvObj.cleanup;
  1129. lvObj.Free;
  1130. except
  1131. on E:Exception do
  1132. begin
  1133. Result := false;
  1134. {$IFDEF LOG_ON}
  1135. __beanLogger.logMessage(
  1136. Format('卸载插件宿主文件时[%s]出现了异常' + sLineBreak + e.Message, [pvLibFile]),
  1137. 'LOAD_TRACE_');
  1138. {$ENDIF}
  1139. if pvRaiseException then
  1140. begin
  1141. raise;
  1142. end;
  1143. end;
  1144. end;
  1145. end;
  1146. end;
  1147. procedure TKeyMapImpl.AfterConstruction;
  1148. begin
  1149. inherited;
  1150. FKeyIntface := TKeyInterface.Create;
  1151. end;
  1152. procedure TKeyMapImpl.cleanupObjects;
  1153. begin
  1154. FKeyIntface.clear;
  1155. end;
  1156. destructor TKeyMapImpl.Destroy;
  1157. begin
  1158. cleanupObjects;
  1159. FKeyIntface.Free;
  1160. FKeyIntface := nil;
  1161. inherited Destroy;
  1162. end;
  1163. function TKeyMapImpl.existsObject(const pvKey: PAnsiChar): Boolean;
  1164. begin
  1165. Result := FKeyIntface.exists(string(AnsiString(pvKey)));
  1166. end;
  1167. function TKeyMapImpl.getObject(const pvKey: PAnsiChar): IInterface;
  1168. begin
  1169. Result := FKeyIntface.find(string(AnsiString(pvKey)));
  1170. end;
  1171. procedure TKeyMapImpl.removeObject(const pvKey: PAnsiChar);
  1172. begin
  1173. try
  1174. FKeyIntface.remove(string(AnsiString(pvKey)));
  1175. except
  1176. end;
  1177. end;
  1178. procedure TKeyMapImpl.setObject(const pvKey: PAnsiChar;
  1179. const pvIntf: IInterface);
  1180. begin
  1181. try
  1182. FKeyIntface.put(string(AnsiString(pvKey)), pvIntf);
  1183. except
  1184. end;
  1185. end;
  1186. function TKeyMapImpl._AddRef: Integer;
  1187. begin
  1188. Result := inherited _AddRef;
  1189. end;
  1190. function TKeyMapImpl._Release: Integer;
  1191. begin
  1192. Result := inherited _Release;
  1193. end;
  1194. initialization
  1195. __beanLogger := TSafeLogger.Create;
  1196. __beanLogger.setAppender(TLogFileAppender.Create(False));
  1197. __instanceKeyMap := TKeyMapImpl.Create;
  1198. __instanceKeyMapKeyIntf := __instanceKeyMap;
  1199. __instanceAppContext := TApplicationContext.Create;
  1200. __instanceAppContextAppContextIntf := __instanceAppContext;
  1201. mybean.core.intf.appPluginContext := __instanceAppContext;
  1202. mybean.core.intf.applicationKeyMap := __instanceKeyMap;
  1203. // // 初始化
  1204. // appPluginContext.checkInitialize;
  1205. finalization
  1206. applicationContextFinalize;
  1207. // 记录未释放的情况
  1208. {$IFDEF LOG_ON}
  1209. logDebugInfo;
  1210. {$ENDIF}
  1211. __instanceAppContextAppContextIntf := nil;
  1212. __instanceKeyMapKeyIntf := nil;
  1213. __beanLogger.Free;
  1214. __beanLogger := nil;
  1215. end.