Minor changes to improve testability of helpers
[castle.git] / MonoRail / Castle.MonoRail.Framework / Configuration / MonoRailConfiguration.cs
blob57dac4b3d49e1699fd68b431cd9bf69693b97263
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Configuration
17 using System;
18 using System.Configuration;
19 using System.Xml;
21 /// <summary>
22 /// Represents the MonoRail external configuration
23 /// </summary>
24 public class MonoRailConfiguration : ISerializedConfig
26 private static readonly String SectionName = "monorail";
27 private static readonly String AlternativeSectionName = "monoRail";
29 private bool checkClientIsConnected, useWindsorIntegration, matchHostNameAndPath, excludeAppPath;
30 private Type customFilterFactory;
31 private XmlNode configurationSection;
33 private SmtpConfig smtpConfig;
34 private ViewEngineConfig viewEngineConfig;
35 private ControllersConfig controllersConfig;
36 private ViewComponentsConfig viewComponentsConfig;
37 private ScaffoldConfig scaffoldConfig;
39 private RoutingRuleCollection routingRules;
40 private ExtensionEntryCollection extensions;
41 private ServiceEntryCollection services;
42 private DefaultUrlCollection defaultUrls;
44 /// <summary>
45 /// Initializes a new instance of the <see cref="MonoRailConfiguration"/> class.
46 /// </summary>
47 public MonoRailConfiguration()
49 smtpConfig = new SmtpConfig();
50 viewEngineConfig = new ViewEngineConfig();
51 controllersConfig = new ControllersConfig();
52 viewComponentsConfig = new ViewComponentsConfig();
53 scaffoldConfig = new ScaffoldConfig();
54 routingRules = new RoutingRuleCollection();
55 extensions = new ExtensionEntryCollection();
56 services = new ServiceEntryCollection();
57 defaultUrls = new DefaultUrlCollection();
59 checkClientIsConnected = false;
60 matchHostNameAndPath = false;
61 excludeAppPath = false;
64 /// <summary>
65 /// Pendent
66 /// </summary>
67 /// <param name="section"></param>
68 public MonoRailConfiguration(XmlNode section) : this()
70 configurationSection = section;
73 /// <summary>
74 /// Gets the config.
75 /// </summary>
76 /// <returns></returns>
77 public static MonoRailConfiguration GetConfig()
79 MonoRailConfiguration config =
80 ConfigurationManager.GetSection(SectionName) as MonoRailConfiguration;
82 if (config == null)
84 config =
85 ConfigurationManager.GetSection(AlternativeSectionName) as MonoRailConfiguration;
88 if (config == null)
90 throw new ApplicationException("You have to provide a small configuration to use " +
91 "MonoRail. Check the samples or the documentation");
94 return config;
97 #region ISerializedConfig implementation
99 /// <summary>
100 /// Deserializes the specified node.
101 /// </summary>
102 /// <param name="node">The node.</param>
103 public void Deserialize(XmlNode node)
105 viewEngineConfig.Deserialize(node);
106 smtpConfig.Deserialize(node);
107 controllersConfig.Deserialize(node);
108 viewComponentsConfig.Deserialize(node);
109 scaffoldConfig.Deserialize(node);
111 services.Deserialize(node);
112 extensions.Deserialize(node);
113 routingRules.Deserialize(node);
114 defaultUrls.Deserialize(node);
116 ProcessFilterFactoryNode(node.SelectSingleNode("customFilterFactory"));
117 ProcessMatchHostNameAndPath(node.SelectSingleNode("routing"));
118 ProcessExcludeAppPath(node.SelectSingleNode("routing"));
120 XmlAttribute checkClientIsConnectedAtt = node.Attributes["checkClientIsConnected"];
122 if (checkClientIsConnectedAtt != null && checkClientIsConnectedAtt.Value != String.Empty)
124 checkClientIsConnected = String.Compare(checkClientIsConnectedAtt.Value, "true", true) == 0;
127 XmlAttribute useWindsorAtt = node.Attributes["useWindsorIntegration"];
129 if (useWindsorAtt != null && useWindsorAtt.Value != String.Empty)
131 useWindsorIntegration = String.Compare(useWindsorAtt.Value, "true", true) == 0;
133 if (useWindsorIntegration)
135 ConfigureWindsorIntegration();
140 #endregion
142 /// <summary>
143 /// Gets the SMTP config.
144 /// </summary>
145 /// <value>The SMTP config.</value>
146 public SmtpConfig SmtpConfig
148 get { return smtpConfig; }
151 /// <summary>
152 /// Gets the view engine config.
153 /// </summary>
154 /// <value>The view engine config.</value>
155 public ViewEngineConfig ViewEngineConfig
157 get { return viewEngineConfig; }
160 /// <summary>
161 /// Gets the controllers config.
162 /// </summary>
163 /// <value>The controllers config.</value>
164 public ControllersConfig ControllersConfig
166 get { return controllersConfig; }
169 /// <summary>
170 /// Gets the view components config.
171 /// </summary>
172 /// <value>The view components config.</value>
173 public ViewComponentsConfig ViewComponentsConfig
175 get { return viewComponentsConfig; }
178 /// <summary>
179 /// Gets the routing rules.
180 /// </summary>
181 /// <value>The routing rules.</value>
182 public RoutingRuleCollection RoutingRules
184 get { return routingRules; }
187 /// <summary>
188 /// Gets the extension entries.
189 /// </summary>
190 /// <value>The extension entries.</value>
191 public ExtensionEntryCollection ExtensionEntries
193 get { return extensions; }
196 /// <summary>
197 /// Gets the service entries.
198 /// </summary>
199 /// <value>The service entries.</value>
200 public ServiceEntryCollection ServiceEntries
202 get { return services; }
205 /// <summary>
206 /// Gets the custom filter factory.
207 /// </summary>
208 /// <value>The custom filter factory.</value>
209 public Type CustomFilterFactory
211 get { return customFilterFactory; }
214 /// <summary>
215 /// Gets the scaffold config.
216 /// </summary>
217 /// <value>The scaffold config.</value>
218 public ScaffoldConfig ScaffoldConfig
220 get { return scaffoldConfig; }
223 /// <summary>
224 /// Gets a value indicating whether MR should check for client connection.
225 /// </summary>
226 /// <value>
227 /// <c>true</c> if it should check client is connected; otherwise, <c>false</c>.
228 /// </value>
229 public bool CheckClientIsConnected
231 get { return checkClientIsConnected; }
234 /// <summary>
235 /// Gets a value indicating whether to use windsor integration.
236 /// </summary>
237 /// <value>
238 /// <c>true</c> if it should use windsor integration; otherwise, <c>false</c>.
239 /// </value>
240 public bool UseWindsorIntegration
242 get { return useWindsorIntegration; }
245 /// <summary>
246 /// Gets a value indicating whether match host name and path should be used on
247 /// MonoRail routing.
248 /// </summary>
249 /// <value>
250 /// <c>true</c> if it should match host name and path; otherwise, <c>false</c>.
251 /// </value>
252 public bool MatchHostNameAndPath
254 get { return matchHostNameAndPath; }
257 /// <summary>
258 /// Gets a value indicating whether routing should exclude app path.
259 /// </summary>
260 /// <value><c>true</c> if exclude app path; otherwise, <c>false</c>.</value>
261 public bool ExcludeAppPath
263 get { return excludeAppPath; }
266 /// <summary>
267 /// Gets the configuration section.
268 /// </summary>
269 /// <value>The configuration section.</value>
270 public XmlNode ConfigurationSection
272 get { return configurationSection; }
275 /// <summary>
276 /// Gets the default urls.
277 /// </summary>
278 /// <value>The default urls.</value>
279 public DefaultUrlCollection DefaultUrls
281 get { return defaultUrls; }
284 private void ProcessFilterFactoryNode(XmlNode node)
286 if (node == null) return;
288 XmlAttribute type = node.Attributes["type"];
290 if (type == null)
292 String message = "The custom filter factory node must specify a 'type' attribute";
293 throw new ConfigurationErrorsException(message);
296 customFilterFactory = TypeLoadUtil.GetType(type.Value);
299 private void ProcessMatchHostNameAndPath(XmlNode node)
301 if (node == null) return;
303 XmlAttribute matchHostNameAndPathAtt = node.Attributes["matchHostNameAndPath"];
305 if (matchHostNameAndPathAtt != null && matchHostNameAndPathAtt.Value != String.Empty)
307 matchHostNameAndPath = String.Compare(matchHostNameAndPathAtt.Value, "true", true) == 0;
311 private void ProcessExcludeAppPath(XmlNode node)
313 if (node == null) return;
315 // maybe a check to make sure both matchHostNameAndPathAtt & includeAppPath
316 // are not both set as that wouldn't make sense?
317 XmlAttribute excludeAppPathAtt = node.Attributes["excludeAppPath"];
319 if (excludeAppPathAtt != null && excludeAppPathAtt.Value != String.Empty)
321 excludeAppPath = String.Compare(excludeAppPathAtt.Value, "true", true) == 0;
325 private void ConfigureWindsorIntegration()
327 const string windsorExtensionAssemblyName = "Castle.MonoRail.WindsorExtension";
329 services.RegisterService(ServiceIdentification.ControllerTree, TypeLoadUtil.GetType(
330 TypeLoadUtil.GetEffectiveTypeName(
331 "Castle.MonoRail.WindsorExtension.ControllerTreeAccessor, " +
332 windsorExtensionAssemblyName)));
334 controllersConfig.CustomControllerFactory = TypeLoadUtil.GetType(
335 TypeLoadUtil.GetEffectiveTypeName("Castle.MonoRail.WindsorExtension.WindsorControllerFactory, " +
336 windsorExtensionAssemblyName));
338 viewComponentsConfig.CustomViewComponentFactory = TypeLoadUtil.GetType(
339 TypeLoadUtil.GetEffectiveTypeName("Castle.MonoRail.WindsorExtension.WindsorViewComponentFactory, " +
340 windsorExtensionAssemblyName));
342 customFilterFactory = TypeLoadUtil.GetType(
343 TypeLoadUtil.GetEffectiveTypeName("Castle.MonoRail.WindsorExtension.WindsorFilterFactory, " +
344 windsorExtensionAssemblyName));