-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathHibernateConfiguration.cs
161 lines (147 loc) · 4.53 KB
/
HibernateConfiguration.cs
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
using System;
using System.Xml;
using System.Xml.XPath;
namespace NHibernate.Cfg.ConfigurationSchema
{
/// <summary>
/// Values for bytecode-provider system property.
/// </summary>
public enum BytecodeProviderType
{
/// <summary>Xml value: codedom</summary>
Codedom,
/// <summary>Xml value: lcg</summary>
Lcg,
/// <summary>Xml value: null</summary>
Null
}
/// <summary>
/// Configuration parsed values for hibernate-configuration section.
/// </summary>
public class HibernateConfiguration : IHibernateConfiguration
{
private static readonly IInternalLogger log = LoggerProvider.LoggerFor(typeof(HibernateConfiguration));
/// <summary>
/// Initializes a new instance of the <see cref="HibernateConfiguration"/> class.
/// </summary>
/// <param name="hbConfigurationReader">The XML reader to parse.</param>
/// <remarks>
/// The nhibernate-configuration.xsd is applied to the XML.
/// </remarks>
/// <exception cref="HibernateConfigException">When nhibernate-configuration.xsd can't be applied.</exception>
public HibernateConfiguration(XmlReader hbConfigurationReader)
: this(hbConfigurationReader, false)
{
}
private HibernateConfiguration(XmlReader hbConfigurationReader, bool fromAppSetting)
{
XPathNavigator nav;
try
{
nav = new XPathDocument(XmlReader.Create(hbConfigurationReader, GetSettings())).CreateNavigator();
}
catch (HibernateConfigException)
{
throw;
}
catch (Exception e)
{
// Encapsulate and reThrow
throw new HibernateConfigException(e);
}
Parse(nav, fromAppSetting);
}
internal static HibernateConfiguration FromAppConfig(XmlNode node)
{
XmlTextReader reader = new XmlTextReader(node.OuterXml, XmlNodeType.Document, null);
return new HibernateConfiguration(reader, true);
}
private XmlReaderSettings GetSettings()
{
XmlReaderSettings xmlrs = (new XmlSchemas()).CreateConfigReaderSettings();
return xmlrs;
}
private void Parse(XPathNavigator navigator, bool fromAppConfig)
{
ParseByteCodeProvider(navigator, fromAppConfig);
ParseReflectionOptimizer(navigator, fromAppConfig);
XPathNavigator xpn = navigator.SelectSingleNode(CfgXmlHelper.SessionFactoryExpression);
if (xpn != null)
{
sessionFactory = new SessionFactoryConfiguration(navigator);
}
else
{
if (!fromAppConfig)
{
throw new HibernateConfigException("<session-factory xmlns='" + CfgXmlHelper.CfgSchemaXMLNS +
"'> element was not found in the configuration file.");
}
}
}
private void ParseByteCodeProvider(XPathNavigator navigator, bool fromAppConfig)
{
XPathNavigator xpn = navigator.SelectSingleNode(CfgXmlHelper.ByteCodeProviderExpression);
if (xpn != null)
{
if (fromAppConfig)
{
xpn.MoveToFirstAttribute();
byteCodeProviderType = xpn.Value;
}
else
{
LogWarnIgnoredProperty("bytecode-provider");
}
}
}
private static void LogWarnIgnoredProperty(string propName)
{
if (log.IsWarnEnabled)
log.Warn(string.Format("{0} property is ignored out of application configuration file.", propName));
}
private void ParseReflectionOptimizer(XPathNavigator navigator, bool fromAppConfig)
{
XPathNavigator xpn = navigator.SelectSingleNode(CfgXmlHelper.ReflectionOptimizerExpression);
if (xpn != null)
{
if (fromAppConfig)
{
xpn.MoveToFirstAttribute();
useReflectionOptimizer = xpn.ValueAsBoolean;
}
else
{
LogWarnIgnoredProperty("reflection-optimizer");
}
}
}
private string byteCodeProviderType = BytecodeProviderType.Lcg.ToConfigurationString();
/// <summary>
/// Value for bytecode-provider system property.
/// </summary>
/// <remarks>Default value <see cref="BytecodeProviderType.Lcg"/>.</remarks>
public string ByteCodeProviderType
{
get { return byteCodeProviderType; }
}
private bool useReflectionOptimizer = true;
/// <summary>
/// Value for reflection-optimizer system property.
/// </summary>
/// <remarks>Default value true.</remarks>
public bool UseReflectionOptimizer
{
get { return useReflectionOptimizer; }
}
private SessionFactoryConfiguration sessionFactory;
/// <summary>
/// The <see cref="SessionFactoryConfiguration"/> if the session-factory exists in hibernate-configuration;
/// Otherwise null.
/// </summary>
public ISessionFactoryConfiguration SessionFactory
{
get { return sessionFactory; }
}
}
}