1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
#endregion
|
23
|
|
|
24
|
|
using System;
|
25
|
|
using System.Configuration;
|
26
|
|
using System.IO;
|
27
|
|
using System.Xml;
|
28
|
|
using ShowX.Session;
|
29
|
|
using log4net;
|
30
|
|
|
31
|
|
namespace ShowX.Config
|
32
|
|
{
|
33
|
|
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
|
|
38
|
|
public class Configuration
|
39
|
|
{
|
40
|
|
private static readonly ILog log = LogManager.GetLogger(
|
41
|
|
typeof(Configuration));
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
|
static private ISession session;
|
47
|
|
|
48
|
|
static private MappingModel mappingModel;
|
49
|
|
|
50
|
|
|
51
|
|
|
52
|
|
|
53
|
|
|
54
|
28
|
public static void createSession(ConfigurationSectionInfo settings)
|
55
|
|
{
|
56
|
28
|
switch(settings.MappingModel) {
|
57
|
|
|
58
|
|
case MappingModel.Attribute:
|
59
|
|
|
60
|
4
|
log.Debug("Creating attribute session.");
|
61
|
|
|
62
|
4
|
session = new AttributeSession();
|
63
|
4
|
mappingModel = MappingModel.Attribute;
|
64
|
4
|
break;
|
65
|
|
|
66
|
|
case MappingModel.Xml:
|
67
|
|
|
68
|
23
|
log.Debug("Creating xml mapping session.");
|
69
|
|
|
70
|
23
|
session = new XmlSession();
|
71
|
23
|
mappingModel = MappingModel.Xml;
|
72
|
23
|
(session as XmlSession).XmlMappingFiles = settings.XmlMappingFiles;
|
73
|
|
|
74
|
23
|
break;
|
75
|
|
}
|
76
|
|
}
|
77
|
|
|
78
|
|
|
79
|
|
|
80
|
|
|
81
|
|
|
82
|
10
|
public static void Configure()
|
83
|
|
{
|
84
|
10
|
try {
|
85
|
|
|
86
|
10
|
ConfigurationSectionInfo settings = ConfigurationSettings
|
87
|
|
.GetConfig(XmlConfigurationConst.rootTag) as ConfigurationSectionInfo;
|
88
|
|
|
89
|
10
|
createSession(settings);
|
90
|
|
}
|
91
|
|
catch (Exception e){
|
92
|
|
|
93
|
1
|
log.Fatal("Error configuring ShowX from .config section",e);
|
94
|
|
|
95
|
1
|
throw new ConfigurationException("Exception configuring ShowX "
|
96
|
|
+ " from .config section.",e);
|
97
|
|
}
|
98
|
|
}
|
99
|
|
|
100
|
|
|
101
|
|
|
102
|
|
|
103
|
|
|
104
|
7
|
public static void Configure(string configFileName)
|
105
|
|
{
|
106
|
7
|
try {
|
107
|
|
|
108
|
7
|
log.Info(string.Format("Configuring ShowX from '{0}'",configFileName));
|
109
|
|
|
110
|
7
|
XmlDocument config = new XmlDocument();
|
111
|
7
|
config.Load(configFileName);
|
112
|
|
|
113
|
5
|
XmlNode rootConfigTag = config
|
114
|
|
.SelectSingleNode("//" + XmlConfigurationConst.rootTag);
|
115
|
|
|
116
|
5
|
ConfigurationSectionInfo settings = XmlConfigurationReader
|
117
|
|
.Read(rootConfigTag);
|
118
|
|
|
119
|
3
|
createSession(settings);
|
120
|
|
}
|
121
|
|
catch(FileNotFoundException e) {
|
122
|
|
|
123
|
1
|
string errorMsg = string.Format(
|
124
|
|
"Exception configuring ShowX from file '{0}'.",configFileName);
|
125
|
|
|
126
|
1
|
log.Fatal(errorMsg,e);
|
127
|
|
|
128
|
1
|
throw new ConfigurationException(errorMsg,e);
|
129
|
|
}
|
130
|
|
catch (XmlException e) {
|
131
|
|
|
132
|
1
|
string errorMsg = string.Format(
|
133
|
|
"Exception loading or parsing configuration file '{0}'."
|
134
|
|
,configFileName);
|
135
|
|
|
136
|
1
|
log.Fatal(errorMsg,e);
|
137
|
|
|
138
|
1
|
throw new ConfigurationException(errorMsg,e);
|
139
|
|
}
|
140
|
|
catch (Exception e) {
|
141
|
|
|
142
|
2
|
log.Fatal("Unexpected configuration exception.",e);
|
143
|
2
|
throw new ConfigurationException("Configuration exception.",e);
|
144
|
|
}
|
145
|
|
}
|
146
|
|
|
147
|
|
|
148
|
|
|
149
|
|
|
150
|
|
|
151
|
|
|
152
|
16
|
public static void Abandon()
|
153
|
|
{
|
154
|
16
|
session = null;
|
155
|
|
}
|
156
|
|
|
157
|
|
|
158
|
|
|
159
|
|
|
160
|
|
|
161
|
|
|
162
|
|
|
163
|
|
|
164
|
|
|
165
|
|
|
166
|
|
|
167
|
|
|
168
|
|
|
169
|
|
|
170
|
31
|
public static void RegisterClass(Type t)
|
171
|
|
{
|
172
|
31
|
if (session is AttributeSession) return;
|
173
|
|
|
174
|
29
|
try
|
175
|
|
{
|
176
|
29
|
(session as XmlSession).RegisterClass(t);
|
177
|
|
}
|
178
|
|
catch (Exception e) {
|
179
|
|
|
180
|
1
|
throw new ConfigurationException(String.Format(
|
181
|
|
"Exception registering class '{0}'.",t.Name),e);
|
182
|
|
}
|
183
|
|
}
|
184
|
|
|
185
|
|
|
186
|
|
|
187
|
|
|
188
|
|
static public ISession Session
|
189
|
|
{
|
190
|
3059
|
get
|
191
|
|
{
|
192
|
3059
|
if (session == null)
|
193
|
1
|
throw new ConfigurationException("Session instance is null."
|
194
|
|
+ " You should initialize ShowX session first by using"
|
195
|
|
+ " 'Configuration.Configure' method.");
|
196
|
|
|
197
|
3058
|
return session;
|
198
|
|
}
|
199
|
|
}
|
200
|
|
|
201
|
|
|
202
|
|
|
203
|
|
|
204
|
|
public static MappingModel MappingModel
|
205
|
|
{
|
206
|
2
|
get { return mappingModel; }
|
207
|
|
}
|
208
|
|
}
|
209
|
|
}
|
210
|
|
|