1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . IO ;
34using System . Linq ;
45using System . Threading . Tasks ;
6+ using Certify . Core . Management . Access ;
7+ using Certify . Datastore . SQLite ;
58using Certify . Models ;
69using Certify . Models . Config ;
710using Certify . Providers ;
@@ -13,6 +16,89 @@ public partial class CertifyManager
1316 {
1417 private object _dataStoreLocker = new object ( ) ;
1518
19+
20+ private async Task InitDataStore ( )
21+ {
22+ var enableExtendedDataStores = true ;
23+
24+ try
25+ {
26+ if ( enableExtendedDataStores )
27+ {
28+
29+ var defaultStoreId = CoreAppSettings . Current . ConfigDataStoreConnectionId ;
30+ var dataStoreInfo = await GetDataStore ( defaultStoreId ) ;
31+
32+ if ( string . IsNullOrEmpty ( defaultStoreId ) || defaultStoreId == "(default)" )
33+ {
34+ // default sqlite storage
35+ _itemManager = new SQLiteManagedItemStore ( "" , _serviceLog ) ;
36+ _credentialsManager = new SQLiteCredentialStore ( "" , _serviceLog ) ;
37+
38+ // config store is a generic store for settings etc
39+ _configStore = new SQLiteConfigurationStore ( "" , _serviceLog ) ;
40+ _accessControl = new AccessControl ( _serviceLog , _configStore ) ;
41+ }
42+ else
43+ {
44+ // select data store based on current default selection
45+ var managedItemStoreOK = await SelectManagedItemStore ( defaultStoreId ) ;
46+ if ( ! managedItemStoreOK )
47+ {
48+ var msg = $ "FATAL: Managed Item Store { defaultStoreId } could not connect or load. Service will not start.";
49+ _serviceLog . Error ( msg ) ;
50+ throw new Exception ( msg ) ;
51+ }
52+
53+ var credentialStoreOK = await SelectCredentialsStore ( defaultStoreId ) ;
54+
55+ if ( ! credentialStoreOK )
56+ {
57+ var msg = $ "FATAL: Credential Store { defaultStoreId } could not connect or load. Service will not start.";
58+ _serviceLog . Error ( msg ) ;
59+ throw new Exception ( msg ) ;
60+ }
61+
62+ _serviceLog . Information ( $ "Certify Manager is connected to data store { dataStoreInfo . Id } '{ dataStoreInfo . Title } ' [{ dataStoreInfo . TypeId } ]") ;
63+ }
64+ }
65+ else
66+ {
67+ _itemManager = new SQLiteManagedItemStore ( "" , _serviceLog ) ;
68+ _credentialsManager = new SQLiteCredentialStore ( "" , _serviceLog ) ;
69+
70+ _configStore = new SQLiteConfigurationStore ( "" , _serviceLog ) ;
71+ _accessControl = new AccessControl ( _serviceLog , _configStore ) ;
72+ }
73+
74+ // attempt to create and delete a test item
75+ try
76+ {
77+ var item = new ManagedCertificate { Id = $ "writecheck_{ Guid . NewGuid ( ) } " } ;
78+
79+ await _itemManager . Update ( item ) ;
80+
81+ await _itemManager . Delete ( item ) ;
82+ }
83+ catch ( Exception ex )
84+ {
85+ _serviceLog ? . Error ( ex , $ "Data store write failed. Check connection and data integrity. Ensure file based databases are not subject to locks via AV scanning etc as this can cause data corruption. { ex } ", ex . Message ) ;
86+ throw ;
87+ }
88+
89+ if ( ! _itemManager . IsInitialised ( ) . Result )
90+ {
91+ _serviceLog ? . Error ( $ "Item Manager failed to initialise properly. Check service logs for more information.") ;
92+ }
93+ }
94+ catch ( Exception exp )
95+ {
96+ var msg = $ "Failed to open or upgrade the managed items data store. :: { exp } ";
97+ _serviceLog ? . Error ( msg ) ;
98+ throw new Exception ( msg ) ;
99+ }
100+ }
101+
16102 private async Task < IManagedItemStore > GetManagedItemStoreProvider ( DataStoreConnection dataStore )
17103 {
18104
0 commit comments