|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.Web.Hosting; |
| 8 | +using SmartStore.Utilities; |
| 9 | +using SmartStore.Utilities.Threading; |
3 | 10 |
|
4 | 11 | namespace SmartStore.Core.Data |
5 | 12 | { |
6 | | - public partial class DataSettings |
| 13 | + |
| 14 | + public partial class DataSettings |
7 | 15 | { |
8 | | - public DataSettings() |
| 16 | + private static readonly ReaderWriterLockSlim s_rwLock = new ReaderWriterLockSlim(); |
| 17 | + private static DataSettings s_current = null; |
| 18 | + private static Func<DataSettings> s_settingsFactory = new Func<DataSettings>(() => new DataSettings()); |
| 19 | + |
| 20 | + protected const char SEPARATOR = ':'; |
| 21 | + protected const string FILENAME = "Settings.txt"; |
| 22 | + |
| 23 | + private DataSettings() |
9 | 24 | { |
10 | | - RawDataSettings=new Dictionary<string, string>(); |
| 25 | + RawDataSettings = new Dictionary<string, string>(); |
11 | 26 | } |
12 | | - public string DataProvider { get; set; } |
13 | | - public string DataConnectionString { get; set; } |
14 | | - public IDictionary<string, string> RawDataSettings { get; private set; } |
| 27 | + |
| 28 | + #region Static members |
| 29 | + |
| 30 | + public static void SetDefaultFactory(Func<DataSettings> factory) |
| 31 | + { |
| 32 | + Guard.ArgumentNotNull(() => factory); |
| 33 | + |
| 34 | + lock (s_rwLock.GetWriteLock()) |
| 35 | + { |
| 36 | + s_settingsFactory = factory; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public static DataSettings Current |
| 41 | + { |
| 42 | + get |
| 43 | + { |
| 44 | + using (s_rwLock.GetUpgradeableReadLock()) |
| 45 | + { |
| 46 | + if (s_current == null) |
| 47 | + { |
| 48 | + using (s_rwLock.GetWriteLock()) |
| 49 | + { |
| 50 | + if (s_current == null) |
| 51 | + { |
| 52 | + s_current = s_settingsFactory(); |
| 53 | + s_current.Load(); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return s_current; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static bool DatabaseIsInstalled() |
| 64 | + { |
| 65 | + return Current.IsValid(); |
| 66 | + } |
| 67 | + |
| 68 | + public static void Reload() |
| 69 | + { |
| 70 | + using (s_rwLock.GetWriteLock()) |
| 71 | + { |
| 72 | + s_current = null; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public static void Delete() |
| 77 | + { |
| 78 | + using (s_rwLock.GetWriteLock()) |
| 79 | + { |
| 80 | + string filePath = Path.Combine(CommonHelper.MapPath("~/App_Data/"), FILENAME); |
| 81 | + File.Delete(filePath); |
| 82 | + s_current = null; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + #endregion |
| 87 | + |
| 88 | + #region Instance members |
| 89 | + |
| 90 | + public string DataProvider |
| 91 | + { |
| 92 | + get; |
| 93 | + set; |
| 94 | + } |
| 95 | + |
| 96 | + public string DataConnectionString |
| 97 | + { |
| 98 | + get; |
| 99 | + set; |
| 100 | + } |
| 101 | + |
| 102 | + public IDictionary<string, string> RawDataSettings |
| 103 | + { |
| 104 | + get; |
| 105 | + private set; |
| 106 | + } |
15 | 107 |
|
16 | 108 | public bool IsValid() |
17 | 109 | { |
18 | | - return !String.IsNullOrEmpty(this.DataProvider) && !String.IsNullOrEmpty(this.DataConnectionString); |
19 | | - } |
20 | | - } |
| 110 | + return this.DataProvider.HasValue() && this.DataConnectionString.HasValue(); |
| 111 | + } |
| 112 | + |
| 113 | + public virtual bool Load() |
| 114 | + { |
| 115 | + using (s_rwLock.GetWriteLock()) |
| 116 | + { |
| 117 | + string filePath = Path.Combine(CommonHelper.MapPath("~/App_Data/"), FILENAME); |
| 118 | + |
| 119 | + this.Reset(); |
| 120 | + |
| 121 | + if (File.Exists(filePath)) |
| 122 | + { |
| 123 | + string text = File.ReadAllText(filePath); |
| 124 | + var settings = ParseSettings(text); |
| 125 | + if (settings.Any()) |
| 126 | + { |
| 127 | + this.RawDataSettings.AddRange(settings); |
| 128 | + if (settings.ContainsKey("DataProvider")) |
| 129 | + { |
| 130 | + this.DataProvider = settings["DataProvider"]; |
| 131 | + } |
| 132 | + if (settings.ContainsKey("DataConnectionString")) |
| 133 | + { |
| 134 | + this.DataConnectionString = settings["DataConnectionString"]; |
| 135 | + } |
| 136 | + |
| 137 | + return this.IsValid(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return false; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + public void Reset() |
| 146 | + { |
| 147 | + using (s_rwLock.GetWriteLock()) |
| 148 | + { |
| 149 | + this.RawDataSettings.Clear(); |
| 150 | + this.DataProvider = null; |
| 151 | + this.DataConnectionString = null; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + public virtual bool Save() |
| 156 | + { |
| 157 | + if (!this.IsValid()) |
| 158 | + return false; |
| 159 | + |
| 160 | + using (s_rwLock.GetWriteLock()) |
| 161 | + { |
| 162 | + string filePath = Path.Combine(CommonHelper.MapPath("~/App_Data/"), FILENAME); |
| 163 | + if (!File.Exists(filePath)) |
| 164 | + { |
| 165 | + using (File.Create(filePath)) |
| 166 | + { |
| 167 | + // we use 'using' to close the file after it's created |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + var text = SerializeSettings(); |
| 172 | + File.WriteAllText(filePath, text); |
| 173 | + |
| 174 | + return true; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + #endregion |
| 179 | + |
| 180 | + #region Instance helpers |
| 181 | + |
| 182 | + protected virtual IDictionary<string, string> ParseSettings(string text) |
| 183 | + { |
| 184 | + var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
| 185 | + |
| 186 | + if (text.IsEmpty()) |
| 187 | + return result; |
| 188 | + |
| 189 | + var settings = new List<string>(); |
| 190 | + using (var reader = new StringReader(text)) |
| 191 | + { |
| 192 | + string str; |
| 193 | + while ((str = reader.ReadLine()) != null) |
| 194 | + settings.Add(str); |
| 195 | + } |
| 196 | + |
| 197 | + foreach (var setting in settings) |
| 198 | + { |
| 199 | + var separatorIndex = setting.IndexOf(SEPARATOR); |
| 200 | + if (separatorIndex == -1) |
| 201 | + { |
| 202 | + continue; |
| 203 | + } |
| 204 | + string key = setting.Substring(0, separatorIndex).Trim(); |
| 205 | + string value = setting.Substring(separatorIndex + 1).Trim(); |
| 206 | + |
| 207 | + if (key.HasValue() && value.HasValue()) |
| 208 | + { |
| 209 | + result.Add(key, value); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + return result; |
| 214 | + } |
| 215 | + |
| 216 | + protected virtual string SerializeSettings() |
| 217 | + { |
| 218 | + return string.Format("DataProvider: {0}{2}DataConnectionString: {1}{2}", |
| 219 | + this.DataProvider, |
| 220 | + this.DataConnectionString, |
| 221 | + Environment.NewLine |
| 222 | + ); |
| 223 | + } |
| 224 | + |
| 225 | + #endregion |
| 226 | + |
| 227 | + } |
| 228 | + |
21 | 229 | } |
0 commit comments