forked from smartstore/SmartStoreNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSettings.cs
More file actions
290 lines (241 loc) · 5.74 KB
/
Copy pathDataSettings.cs
File metadata and controls
290 lines (241 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Hosting;
using SmartStore.Utilities;
using SmartStore.Utilities.Threading;
namespace SmartStore.Core.Data
{
public partial class DataSettings
{
private static readonly ReaderWriterLockSlim s_rwLock = new ReaderWriterLockSlim();
private static DataSettings s_current = null;
private static Func<DataSettings> s_settingsFactory = new Func<DataSettings>(() => new DataSettings());
private static bool? s_installed = null;
private static bool s_TestMode = false;
protected const char SEPARATOR = ':';
protected const string FILENAME = "Settings.txt";
private DataSettings()
{
RawDataSettings = new Dictionary<string, string>();
}
#region Static members
public static void SetDefaultFactory(Func<DataSettings> factory)
{
Guard.ArgumentNotNull(() => factory);
lock (s_rwLock.GetWriteLock())
{
s_settingsFactory = factory;
}
}
public static DataSettings Current
{
get
{
using (s_rwLock.GetUpgradeableReadLock())
{
if (s_current == null)
{
using (s_rwLock.GetWriteLock())
{
if (s_current == null)
{
s_current = s_settingsFactory();
s_current.Load();
}
}
}
}
return s_current;
}
}
public static bool DatabaseIsInstalled()
{
if (s_TestMode)
return false;
if (!s_installed.HasValue)
{
s_installed = Current.IsValid();
}
return s_installed.Value;
}
internal static void SetTestMode(bool isTestMode)
{
s_TestMode = isTestMode;
}
public static void Reload()
{
using (s_rwLock.GetWriteLock())
{
s_current = null;
s_installed = null;
}
}
public static void Delete()
{
using (s_rwLock.GetWriteLock())
{
string filePath = Path.Combine(CommonHelper.MapPath("~/App_Data/"), FILENAME);
File.Delete(filePath);
s_current = null;
s_installed = null;
}
}
#endregion
#region Instance members
public Version AppVersion
{
get;
set;
}
public string DataProvider
{
get;
set;
}
public string ProviderInvariantName
{
get
{
if (this.DataProvider.HasValue() && this.DataProvider.IsCaseInsensitiveEqual("sqlserver"))
return "System.Data.SqlClient";
// SqlCe should always be the default provider
return "System.Data.SqlServerCe.4.0";
}
}
public string ProviderFriendlyName
{
get
{
if (this.DataProvider.HasValue() && this.DataProvider.IsCaseInsensitiveEqual("sqlserver"))
return "SQL Server";
// SqlCe should always be the default provider
return "SQL Server Compact (SQL CE)";
}
}
public bool IsSqlServer
{
get
{
return this.DataProvider.HasValue() && this.DataProvider.IsCaseInsensitiveEqual("sqlserver");
}
}
public string DataConnectionString
{
get;
set;
}
public IDictionary<string, string> RawDataSettings
{
get;
private set;
}
public bool IsValid()
{
return this.DataProvider.HasValue() && this.DataConnectionString.HasValue();
}
public virtual bool Load()
{
using (s_rwLock.GetWriteLock())
{
string filePath = Path.Combine(CommonHelper.MapPath("~/App_Data/"), FILENAME);
this.Reset();
if (File.Exists(filePath))
{
string text = File.ReadAllText(filePath);
var settings = ParseSettings(text);
if (settings.Any())
{
this.RawDataSettings.AddRange(settings);
if (settings.ContainsKey("AppVersion"))
{
this.AppVersion = new Version(settings["AppVersion"]);
}
if (settings.ContainsKey("DataProvider"))
{
this.DataProvider = settings["DataProvider"];
}
if (settings.ContainsKey("DataConnectionString"))
{
this.DataConnectionString = settings["DataConnectionString"];
}
return this.IsValid();
}
}
return false;
}
}
public void Reset()
{
using (s_rwLock.GetWriteLock())
{
this.RawDataSettings.Clear();
this.AppVersion = null;
this.DataProvider = null;
this.DataConnectionString = null;
s_installed = null;
}
}
public virtual bool Save()
{
if (!this.IsValid())
return false;
using (s_rwLock.GetWriteLock())
{
string filePath = Path.Combine(CommonHelper.MapPath("~/App_Data/"), FILENAME);
if (!File.Exists(filePath))
{
using (File.Create(filePath))
{
// we use 'using' to close the file after it's created
}
}
var text = SerializeSettings();
File.WriteAllText(filePath, text);
return true;
}
}
#endregion
#region Instance helpers
protected virtual IDictionary<string, string> ParseSettings(string text)
{
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
if (text.IsEmpty())
return result;
var settings = new List<string>();
using (var reader = new StringReader(text))
{
string str;
while ((str = reader.ReadLine()) != null)
settings.Add(str);
}
foreach (var setting in settings)
{
var separatorIndex = setting.IndexOf(SEPARATOR);
if (separatorIndex == -1)
{
continue;
}
string key = setting.Substring(0, separatorIndex).Trim();
string value = setting.Substring(separatorIndex + 1).Trim();
if (key.HasValue() && value.HasValue())
{
result.Add(key, value);
}
}
return result;
}
protected virtual string SerializeSettings()
{
return string.Format("AppVersion: {0}{3}DataProvider: {1}{3}DataConnectionString: {2}{3}",
this.AppVersion.ToString(),
this.DataProvider,
this.DataConnectionString,
Environment.NewLine);
}
#endregion
}
}