unit ConfigAgent; interface uses System.IniFiles, SysUtils, ApplicationContext; const SELECT_SERVER_URL_PRODUCTIE = 'http://cache01/csp/admin1/WS.Prod.Select.CLS'; OPTI_SERVER_URL_PRODUCTIE = 'http://cache01/csp/admin1/WS.Prod.OptiBox.OptiServer.CLS'; USER_SERVER_URL_PRODUCTIE = 'http://cache01/csp/admin1/WS.Sys.Toegang.UserServer.cls'; SELECT_SERVER_URL_TEST = 'http://cacheaccept2010:57772/csp/dev1/WS.Prod.Select.CLS'; OPTI_SERVER_URL_TEST = 'http://cacheaccept2010:57772/csp/dev1/WS.Prod.OptiBox.OptiServer.CLS'; USER_SERVER_URL_TEST = 'http://cacheaccept2010:57772/csp/dev1/WS.Sys.Toegang.UserServer.cls'; type TConfigAgent = class private function HaalWaardeOp(Ini: TMemIniFile; Section: string; Identificatie: string; DefaultWaarde: string): string; overload; function HaalWaardeOp(Ini: TMemIniFile; Section: string; Identificatie: string; DefaultWaarde: boolean): boolean; overload; public procedure LaadConfigWaarden(ExeName: string; ConfigsContext: TConfigsContext); end; implementation // https://docwiki.embarcadero.com/RADStudio/Sydney/en/Using_TIniFile_and_TMemIniFile procedure TConfigAgent.LaadConfigWaarden(ExeName: string; ConfigsContext: TConfigsContext); var Ini: TMemIniFile; // In memory, sneller maar kans op data loss. Bij ons is dit geen ramp. begin Ini := TMemIniFile.Create(ChangeFileExt(ExeName, '.INI')); try // Controleer of alle configs aanwezig zijn. ConfigsContext.IsProductieOmgeving := HaalWaardeOp(Ini, 'ProductieStatus', 'IsProductieOmgeving', true); // Productie ConfigsContext.SelectServerUrl := HaalWaardeOp(Ini, 'Servers', 'Select', SELECT_SERVER_URL_PRODUCTIE); ConfigsContext.OptiServerUrl := HaalWaardeOp(Ini, 'Servers', 'Opti', OPTI_SERVER_URL_PRODUCTIE); ConfigsContext.UserServerUrl := HaalWaardeOp(Ini, 'Servers', 'User', USER_SERVER_URL_PRODUCTIE); // Test omgeving if ConfigsContext.IsProductieOmgeving then begin // Niet in test omgeving, maar velden wel genereren in config bestand HaalWaardeOp(Ini, 'Servers_TEST', 'Select', SELECT_SERVER_URL_TEST); HaalWaardeOp(Ini, 'Servers_TEST', 'Opti', OPTI_SERVER_URL_TEST); HaalWaardeOp(Ini, 'Servers_TEST', 'User', USER_SERVER_URL_TEST); end else begin // Overschrijven met test omgeving waarden ConfigsContext.SelectServerUrl := HaalWaardeOp(Ini, 'Servers_TEST', 'Select', SELECT_SERVER_URL_TEST); ConfigsContext.OptiServerUrl := HaalWaardeOp(Ini, 'Servers_TEST', 'Opti', OPTI_SERVER_URL_TEST); ConfigsContext.UserServerUrl := HaalWaardeOp(Ini, 'Servers_TEST', 'User', USER_SERVER_URL_TEST); end; Ini.UpdateFile; // Voer wijzigingen door finally Ini.Free; end; end; function TConfigAgent.HaalWaardeOp(Ini: TMemIniFile; Section: string; Identificatie: string; DefaultWaarde: boolean): boolean; begin if not Ini.ValueExists(Section, Identificatie) then // Ontbreekt, aanmaken Ini.WriteBool(Section, Identificatie, DefaultWaarde); // Uitlezen uit config Result := Ini.ReadBool(Section, Identificatie, DefaultWaarde); end; function TConfigAgent.HaalWaardeOp(Ini: TMemIniFile; Section: string; Identificatie: string; DefaultWaarde: string): string; begin if not Ini.ValueExists(Section, Identificatie) then // Ontbreekt, aanmaken Ini.writestring(Section, Identificatie, DefaultWaarde); // Uitlezen uit config Result := Ini.readstring(Section, Identificatie, DefaultWaarde); end; end.