-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvFactory.cs
More file actions
260 lines (228 loc) · 11.7 KB
/
EnvFactory.cs
File metadata and controls
260 lines (228 loc) · 11.7 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
/*************************************************************************************************
*
* THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*
*************************************************************************************************/
using System;
using System.Configuration;
using System.IO;
namespace Figaro.Configuration
{
#if CDS || TDS || HA
/// <summary>
/// Creates <see cref="FigaroEnv"/> instances using configuration.
/// </summary>
public class EnvFactory
{
/// <summary>
/// Create a new <see cref="FigaroEnv"/> instance using the first <see cref="FigaroEnvElement"/> object found in configuration.
/// </summary>
/// <returns>A new <see cref="FigaroEnv"/> instance, or <c>null</c> if no configuration instances exist.</returns>
public static FigaroEnv Create()
{
var section = (FigaroSection)ConfigurationManager.GetSection("Figaro");
if (section == null) throw new ConfigurationErrorsException("Figaro section could not be found.");
return Create(section.Environments[0]);
}
/// <summary>
/// Create a new <see cref="FigaroEnv"/> instance using the specified <see cref="FigaroEnvElement"/> object.
/// </summary>
/// <param name="element">The <see cref="FigaroEnvElement"/> to use.</param>
/// <returns>A new <see cref="FigaroEnv"/> instance.</returns>
public static FigaroEnv Create(FigaroEnvElement element)
{
var env = new FigaroEnv();
bool panic = false;
#region Tracing
if (element.Tracing != null)
{
if (!string.IsNullOrEmpty(element.Tracing.MessageFile))
{
var msgPath = Path.GetDirectoryName(PathUtility.ResolvePath(element.Tracing.MessageFile));
var msgFile = Path.GetFileName(element.Tracing.MessageFile);
if (!Directory.Exists(msgPath))
{
Directory.CreateDirectory(msgPath);
}
env.SetMessageFile(Path.Combine(msgPath,msgFile));
}
if (!string.IsNullOrEmpty(element.Tracing.Category))
LogConfiguration.SetCategory(
(LogConfigurationCategory)
Enum.Parse(typeof(LogConfigurationCategory), element.Tracing.Category.Replace(" ",",")), true);
if (!string.IsNullOrEmpty(element.Tracing.Level))
LogConfiguration.SetLogLevel((LogConfigurationLevel)Enum.Parse(typeof(LogConfigurationLevel), element.Tracing.Level.Replace(" ",",")), true);
if (!string.IsNullOrEmpty(element.Tracing.ErrorPrefix)) env.ErrorPrefix = element.Tracing.ErrorPrefix;
if (!string.IsNullOrEmpty(element.Tracing.ErrorFile))
{
var errPath = Path.GetDirectoryName(PathUtility.ResolvePath(element.Tracing.ErrorFile));
var errFile = Path.GetFileName(element.Tracing.ErrorFile);
if (!Directory.Exists(errPath))
{
Directory.CreateDirectory(errPath);
}
env.SetErrorFile(Path.Combine(errPath,errFile));
}
}
#endregion
#region DataDirectories
if (element.DataDirectories != null)
{
if (element.DataDirectories.Count > 0)
{
foreach (DataDirectoryElement directoryElement in element.DataDirectories)
{
if (directoryElement.Create && !Directory.Exists(PathUtility.ResolvePath(directoryElement.Path)))
Directory.CreateDirectory(PathUtility.ResolvePath(directoryElement.Path));
env.AddDataDirectory(PathUtility.ResolvePath(directoryElement.Path));
}
}
}
#endregion
#region Encryption
if (element.Encryption != null)
{
env.SetEncryption(element.Encryption.Password, element.Encryption.Enabled);
}
#endregion
#region EnvConfig
if (element.EnvConfig != null)
{
if (element.EnvConfig.Count > 0)
{
foreach (EnvConfigElement config in element.EnvConfig)
{
if (config.Setting.ToLower().Contains("panicenvironment"))
{
panic = true;
continue;
}
if (string.IsNullOrEmpty(config.Setting)) continue;
env.SetEnvironmentOption((EnvConfig) Enum.Parse(typeof (EnvConfig), config.Setting.Replace(" ",","), true),
config.Enabled);
}
}
}
#endregion
#region Cache & CacheMax
if (element.Cache != null) env.SetCacheSize(new EnvCacheSize(element.Cache.GBytes, element.Cache.Bytes), element.Cache.Regions);
if (element.CacheMax != null) env.SetCacheMax(new EnvCacheSize(element.CacheMax.GBytes, element.CacheMax.Bytes));
#endregion
#region Locking
if (element.Locking != null)
{
if (!string.IsNullOrEmpty(element.Locking.AutoDetect)) env.DeadlockDetectPolicy = (DeadlockDetectType)Enum.Parse(typeof(DeadlockDetectType), element.Locking.AutoDetect.Replace(" ",","));
if (element.Locking.LockPartitions > 0) env.SetLockPartitions(element.Locking.LockPartitions);
if (element.Locking.MaxLockers > 0) env.SetMaxLockers(element.Locking.MaxLockers);
if (element.Locking.MaxLockObjects > 0) env.SetMaxLockedObjects(element.Locking.MaxLockObjects);
if (element.Locking.MaxLocks > 0) env.SetMaxLocks(element.Locking.MaxLocks);
if (element.Locking.Timeout > 0) env.SetTimeout(element.Locking.Timeout, EnvironmentTimeoutType.Lock);
}
#endregion
#region Log
#if TDS || HA
if (element.Log != null)
{
if (element.Log.Create && !Directory.Exists(element.Log.Directory))
Directory.CreateDirectory(PathUtility.ResolvePath(element.Log.Directory));
if (element.Log.BufferSize >0) env.SetLogBufferSize(element.Log.BufferSize);
if (!string.IsNullOrEmpty(element.Log.Directory)) env.SetLogDirectory(PathUtility.ResolvePath(element.Log.Directory));
if (!string.IsNullOrEmpty(element.Log.LogOptions))
{
var logOptions =
(EnvLogOptions) Enum.Parse(typeof (EnvLogOptions), element.Log.LogOptions.Replace(" ",","), true);
env.SetLogOptions(logOptions, true);
}
if (element.Log.MaxFileSize > 0) env.MaxLogSize = element.Log.MaxFileSize;
if (element.Log.MaxRegionSize >0) env.SetMaxLogRegion(element.Log.MaxRegionSize);
}
#endif
#endregion
#region MemoryFileMap
if (element.MemoryFileMap != null)
{
if (element.MemoryFileMap.MaxFile >0) env.SetMaxFileDescriptors(element.MemoryFileMap.MaxFile);
if (element.MemoryFileMap.MaxFileMapSize > 0) env.MaxFileMapSize = element.MemoryFileMap.MaxFileMapSize;
if (element.MemoryFileMap.MaxWrite >0 && element.MemoryFileMap.WriteSleep >0) env.SetMaxSequentialWriteOperations(element.MemoryFileMap.MaxWrite,element.MemoryFileMap.WriteSleep);
}
#endregion
#region Mutex
if (element.Mutex != null)
{
if (element.Mutex.Align >0) env.SetMutexAlign(element.Mutex.Align);
if (element.Mutex.Increment >0) env.SetMutexIncrement(element.Mutex.Increment);
if (element.Mutex.TasSpinCount >0) env.SetTestAndSetSpinCount(element.Mutex.TasSpinCount);
}
#endregion
#region TempDirectory
if (element.TempDirectory != null)
{
if (!string.IsNullOrEmpty(element.TempDirectory.Path))
{
if (!Directory.Exists(PathUtility.ResolvePath(element.TempDirectory.Path)) && element.TempDirectory.Create)
Directory.CreateDirectory(PathUtility.ResolvePath(element.TempDirectory.Path));
env.SetTempDirectory(PathUtility.ResolvePath(element.TempDirectory.Path));
}
}
#endregion
#region ThreadCount
if (element.ThreadCount > 0)
env.SetThreadCount(element.ThreadCount);
#endregion
#region Transactions
#if TDS || HA
if (element.Transactions != null)
{
if (element.Transactions.MaxTransactions > 0)
env.SetMaxTransactions(element.Transactions.MaxTransactions);
if (element.Transactions.Timeout > 0)
env.SetTimeout(element.Transactions.Timeout, EnvironmentTimeoutType.Transaction);
}
#endif
#endregion
#region Open
if (element.Open != null)
{
EnvOpenOptions openOptions = EnvOpenOptions.None;
if (!string.IsNullOrEmpty(element.Open.Options))
openOptions = (EnvOpenOptions) Enum.Parse(typeof (EnvOpenOptions), element.Open.Options.Replace(" ",","), true);
if (openOptions == EnvOpenOptions.None && string.IsNullOrEmpty(element.Open.Home))
openOptions = EnvOpenOptions.UseEnvironmentRoot | EnvOpenOptions.Create;
if (element.Open.Create && !Directory.Exists(PathUtility.ResolvePath(element.Open.Home)))
Directory.CreateDirectory(PathUtility.ResolvePath(element.Open.Home));
env.Open(PathUtility.ResolvePath(element.Open.Home),openOptions);
}
#endregion
#region post-open
#if TDS || HA
if (panic)
{
env.SetEnvironmentOption(EnvConfig.PanicEnvironment, true);
}
#endif
#endregion
return env;
}
/// <summary>
/// Create a new <see cref="FigaroEnv"/> instance using the specified <see cref="FigaroEnvElement"/> object.
/// </summary>
/// <param name="envName">The <see cref="FigaroEnvElement"/> instance to use.</param>
/// <exception cref="ConfigurationErrorsException"></exception>
/// <returns>A new <see cref="FigaroEnv"/> instance, or <c>null</c> if the named instance does not exist.</returns>
public static FigaroEnv Create(string envName)
{
FigaroSection section = (FigaroSection)ConfigurationManager.GetSection("Figaro");
if (section == null) throw new ConfigurationErrorsException("Figaro section could not be found.");
foreach (FigaroEnvElement element in section.Environments)
{
if (element.Name.Equals(envName))
return Create(element);
}
throw new ConfigurationErrorsException(string.Format("FigaroEnv '{0}' could not be found.",envName));
}
}
#endif
}